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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use std::any::Any;
use std::borrow::Cow;
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::{Expr, Literal, SpannedExpr, TypedIdent};
use base::metadata::Metadata;
use base::pos;
use base::symbol::Symbol;
use vm::macros::{Macro, MacroExpander, Error as MacroError};
use vm::thread::{Thread, ThreadInternal};
use vm::internal::Value;
use super::{filename_to_module, Compiler};
use base::fnv::FnvMap;
quick_error! {
#[derive(Debug)]
pub enum Error {
CyclicDependency(module: String) {
description("Cyclic dependency")
display("Module '{}' occurs in a cyclic dependency", module)
}
String(message: String) {
description(message)
display("{}", message)
}
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")))),*]
}
}
static STD_LIBS: [(&'static str, &'static str); 7] = std_libs!(
"prelude",
"types",
"map",
"string",
"state",
"test",
"writer"
);
pub trait Importer: Any + Clone + Sync + Send {
fn import(
&self,
compiler: &mut Compiler,
vm: &Thread,
modulename: &str,
input: &str,
expr: SpannedExpr<Symbol>,
) -> Result<(), MacroError>;
}
#[derive(Clone)]
pub struct DefaultImporter;
impl Importer for DefaultImporter {
fn import(
&self,
compiler: &mut Compiler,
vm: &Thread,
modulename: &str,
input: &str,
expr: SpannedExpr<Symbol>,
) -> Result<(), MacroError> {
use compiler_pipeline::*;
MacroValue { expr: expr }
.load_script(compiler, vm, modulename, input, None)
.sync_or_error()?;
Ok(())
}
}
#[derive(Clone, Default)]
pub struct CheckImporter(pub Arc<Mutex<FnvMap<String, SpannedExpr<Symbol>>>>);
impl CheckImporter {
pub fn new() -> CheckImporter {
CheckImporter::default()
}
}
impl Importer for CheckImporter {
fn import(
&self,
compiler: &mut Compiler,
vm: &Thread,
module_name: &str,
input: &str,
expr: SpannedExpr<Symbol>,
) -> Result<(), MacroError> {
use compiler_pipeline::*;
let macro_value = MacroValue { expr: expr };
let TypecheckValue { expr, typ } = macro_value.typecheck(compiler, vm, module_name, input)?;
self.0.lock().unwrap().insert(module_name.into(), expr);
let metadata = Metadata::default();
vm.global_env()
.set_global(Symbol::from(module_name), typ, metadata, Value::Int(0))?;
Ok(())
}
}
pub struct Import<I = DefaultImporter> {
pub paths: RwLock<Vec<PathBuf>>,
pub importer: I,
}
impl<I> Import<I> {
pub fn new(importer: I) -> Import<I> {
Import {
paths: RwLock::new(vec![PathBuf::from(".")]),
importer: importer,
}
}
pub fn add_path<P: Into<PathBuf>>(&self, path: P) {
self.paths.write().unwrap().push(path.into());
}
pub fn read_file<P>(&self, filename: P) -> Result<Cow<'static, str>, MacroError>
where
P: AsRef<Path>,
{
self.read_file_(filename.as_ref())
}
fn read_file_(&self, filename: &Path) -> Result<Cow<'static, str>, MacroError> {
let mut buffer = String::new();
let std_file = filename
.to_str()
.and_then(|filename| STD_LIBS.iter().find(|tup| tup.0 == filename));
Ok(match std_file {
Some(tup) => Cow::Borrowed(tup.1),
None => {
let file = self.paths
.read()
.unwrap()
.iter()
.filter_map(|p| {
let base = p.join(filename);
match File::open(&base) {
Ok(file) => Some(file),
Err(_) => None,
}
})
.next();
let mut file = file.ok_or_else(|| {
Error::String(format!("Could not find file '{}'", filename.display()))
})?;
file.read_to_string(&mut buffer)?;
Cow::Owned(buffer)
}
})
}
}
fn get_state<'m>(macros: &'m mut MacroExpander) -> &'m mut State {
macros
.state
.entry(String::from("import"))
.or_insert_with(|| Box::new(State { visited: Vec::new() }))
.downcast_mut::<State>()
.unwrap()
}
struct State {
visited: Vec<String>,
}
impl<I> Macro for Import<I>
where
I: Importer,
{
fn expand(
&self,
macros: &mut MacroExpander,
args: &mut [SpannedExpr<Symbol>],
) -> Result<SpannedExpr<Symbol>, MacroError> {
use compiler_pipeline::*;
if args.len() != 1 {
return Err(
Error::String("Expected import to get 1 argument".into()).into(),
);
}
match args[0].value {
Expr::Literal(Literal::String(ref filename)) => {
let vm = macros.vm;
let modulename = filename_to_module(filename);
let name = Symbol::from(&*modulename);
debug!("Import '{}' {:?}", modulename, get_state(macros).visited);
if !vm.global_env().global_exists(&modulename) {
{
let state = get_state(macros);
if state.visited.iter().any(|m| **m == **filename) {
return Err(Error::CyclicDependency(filename.clone()).into());
}
state.visited.push(filename.clone());
}
let file_contents = self.read_file(filename)?;
let mut compiler = Compiler::new().implicit_prelude(modulename != "std.types");
let errors = macros.errors.len();
let macro_result = file_contents
.expand_macro_with(&mut compiler, macros, &modulename)?;
if errors != macros.errors.len() {
if let Some(err) = macros.errors.pop() {
return Err(err);
}
}
get_state(macros).visited.pop();
self.importer.import(
&mut compiler,
vm,
&modulename,
&file_contents,
macro_result.expr,
)?;
}
Ok(pos::spanned(
args[0].span,
Expr::Ident(TypedIdent::new(name)),
))
}
_ => Err(
Error::String("Expected a string literal to import".into()).into(),
),
}
}
}