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
use crate::{
environment::EnvironmentError,
eval::{
bc::frame::alloca_frame,
compiler::{
add_span_to_expr_error, expr_throw,
scope::{CstLoad, CstStmt, ScopeId, Slot},
Compiler, EvalException,
},
runtime::call_stack::FrozenFileSpan,
},
syntax::ast::StmtP,
values::Value,
};
impl<'v> Compiler<'v, '_, '_> {
fn eval_load(&mut self, load: CstLoad) -> Result<(), EvalException> {
let name = load.node.module.node;
let span = FrozenFileSpan {
file: self.codemap,
span: load.span,
};
let loadenv = match self.eval.loader.as_ref() {
None => {
return Err(add_span_to_expr_error(
EnvironmentError::NoImportsAvailable(name).into(),
span,
self.eval,
));
}
Some(loader) => expr_throw(loader.load(&name), span, self.eval)?,
};
for (our_name, their_name) in load.node.args {
let (slot, _captured) = self.scope_data.get_assign_ident_slot(&our_name);
let slot = match slot {
Slot::Local(..) => unreachable!("symbol need to be resolved to module"),
Slot::Module(slot) => slot,
};
let value = expr_throw(
self.eval.module_env.load_symbol(&loadenv, &their_name.node),
FrozenFileSpan {
file: self.codemap,
span: our_name.span.merge(their_name.span),
},
self.eval,
)?;
self.eval.set_slot_module(slot, value)
}
Ok(())
}
fn eval_top_level_stmt(
&mut self,
stmt: CstStmt,
local_count: u32,
) -> Result<Value<'v>, EvalException> {
match stmt.node {
StmtP::Statements(stmts) => {
let mut last = Value::new_none();
for stmt in stmts {
last = self.eval_top_level_stmt(stmt, local_count)?;
}
Ok(last)
}
StmtP::Load(load) => {
self.eval_load(load)?;
Ok(Value::new_none())
}
_ => {
let stmt = self.module_top_level_stmt(stmt);
let bc = stmt.as_bc(
&self.compile_context(),
local_count,
self.eval.module_env.frozen_heap(),
);
alloca_frame(self.eval, local_count, bc.max_stack_size, |eval| {
bc.run(eval)
})
}
}
}
pub(crate) fn eval_module(
&mut self,
stmt: CstStmt,
local_count: u32,
) -> Result<Value<'v>, EvalException> {
self.enter_scope(ScopeId::module());
let value = self.eval_top_level_stmt(stmt, local_count)?;
self.exit_scope();
assert!(self.locals.is_empty());
Ok(value)
}
}