forked from gluon-lang/gluon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimport.rs
182 lines (170 loc) · 7 KB
/
import.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! Implementation of the `import` macro.
use std::any::Any;
use std::sync::{Arc, RwLock, Mutex};
use std::fs::File;
use std::io;
use std::io::Read;
use std::path::{Path, PathBuf};
use base::ast::{self, Expr, LiteralEnum, SpannedExpr};
use base::metadata::Metadata;
use base::pos;
use base::symbol::Symbol;
use vm::macros::{Macro, Error as MacroError};
use vm::thread::{Thread, ThreadInternal};
use vm::internal::Value;
use super::{filename_to_module, Compiler};
use base::types::TcIdent;
use base::fnv::FnvMap;
quick_error! {
/// Error type for the import macro
#[derive(Debug)]
pub enum Error {
/// The importer found a cyclic dependency when loading files
CyclicDependency(module: String) {
description("Cyclic dependency")
display("Module '{}' occurs in a cyclic dependency", module)
}
/// Generic message error
String(message: String) {
description(message)
display("{}", message)
}
/// The importer could not load the imported file
IO(err: io::Error) {
description(err.description())
display("{}", err)
from()
}
}
}
macro_rules! std_libs {
($($file: expr),*) => {
[$((concat!("std/", $file, ".glu"), include_str!(concat!("../std/", $file, ".glu")))),*]
}
}
// Include the standard library distribution in the binary
static STD_LIBS: [(&'static str, &'static str); 8] = std_libs!("prelude",
"types",
"map",
"repl",
"string",
"state",
"test",
"writer");
pub trait Importer: Any + Clone + Sync + Send {
fn import(&self, vm: &Thread, modulename: &str, input: &str) -> Result<(), MacroError>;
}
#[derive(Clone)]
pub struct DefaultImporter;
impl Importer for DefaultImporter {
fn import(&self, vm: &Thread, modulename: &str, input: &str) -> Result<(), MacroError> {
let mut compiler = Compiler::new().implicit_prelude(modulename != "std.types");
try!(compiler.load_script(vm, &modulename, input));
Ok(())
}
}
#[derive(Clone)]
pub struct CheckImporter(pub Arc<Mutex<FnvMap<String, SpannedExpr<ast::TcIdent<Symbol>>>>>);
impl CheckImporter {
pub fn new() -> CheckImporter {
CheckImporter(Arc::new(Mutex::new(FnvMap::default())))
}
}
impl Importer for CheckImporter {
fn import(&self, vm: &Thread, modulename: &str, input: &str) -> Result<(), MacroError> {
use compiler_pipeline::*;
let mut compiler = Compiler::new().implicit_prelude(modulename != "std.types");
let TypecheckValue(expr, typ) = try!(input.typecheck(&mut compiler, vm, modulename, input));
self.0.lock().unwrap().insert(modulename.into(), expr);
let metadata = Metadata::default();
// Insert a global to ensure the globals type can be looked up
try!(vm.global_env().set_global(Symbol::new(modulename), typ, metadata, Value::Int(0)));
Ok(())
}
}
/// Macro which rewrites occurances of `import "filename"` to a load of that file if it is not
/// already loaded and then a global access to the loaded module
pub struct Import<I = DefaultImporter> {
visited: RwLock<Vec<String>>,
pub paths: RwLock<Vec<PathBuf>>,
pub importer: I,
}
impl<I> Import<I> {
/// Creates a new import macro
pub fn new(importer: I) -> Import<I> {
Import {
visited: RwLock::new(Vec::new()),
paths: RwLock::new(vec![PathBuf::from(".")]),
importer: importer,
}
}
/// Adds a path to the list of paths which the importer uses to find files
pub fn add_path<P: Into<PathBuf>>(&self, path: P) {
self.paths.write().unwrap().push(path.into());
}
}
impl<I> Macro for Import<I>
where I: Importer
{
fn expand(&self,
vm: &Thread,
arguments: &mut [SpannedExpr<TcIdent>])
-> Result<SpannedExpr<TcIdent>, MacroError> {
if arguments.len() != 1 {
return Err(Error::String("Expected import to get 1 argument".into()).into());
}
match arguments[0].value {
Expr::Literal(LiteralEnum::String(ref filename)) => {
let modulename = filename_to_module(filename);
let path = Path::new(&filename[..]);
// Only load the script if it is not already loaded
let name = Symbol::new(&*modulename);
debug!("Import '{}' {:?}", modulename, self.visited);
if !vm.global_env().global_exists(&modulename) {
if self.visited.read().unwrap().iter().any(|m| **m == **filename) {
return Err(Error::CyclicDependency(filename.clone()).into());
}
self.visited.write().unwrap().push(filename.clone());
let mut buffer = String::new();
let file_contents = match STD_LIBS.iter().find(|tup| tup.0 == filename) {
Some(tup) => tup.1,
None => {
let file = self.paths
.read()
.unwrap()
.iter()
.filter_map(|p| {
let mut base = p.clone();
base.push(path);
match File::open(&base) {
Ok(file) => Some(file),
Err(_) => None,
}
})
.next();
let mut file = try!(file.ok_or_else(|| {
Error::String(format!("Could not find file '{}'", filename))
}));
try!(file.read_to_string(&mut buffer));
&*buffer
}
};
// FIXME Remove this hack
self.visited.write().unwrap().pop();
try!(self.importer.import(vm, &modulename, file_contents));
}
// FIXME Does not handle shadowing
Ok(pos::spanned(arguments[0].span,
Expr::Identifier(TcIdent::new(name))))
}
_ => return Err(Error::String("Expected a string literal to import".into()).into()),
}
}
fn clone(&self) -> Box<Macro> {
Box::new(Import {
visited: RwLock::new(Vec::new()),
paths: RwLock::new(self.paths.read().unwrap().clone()),
importer: self.importer.clone(),
})
}
}