[fine] Compile iteratively instead of recursively
This commit is contained in:
parent
7fb88ef199
commit
01798236ec
6 changed files with 68 additions and 52 deletions
|
|
@ -120,19 +120,17 @@ impl std::fmt::Debug for Function {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Eq, PartialEq, Hash, Clone)]
|
||||
struct FunctionKey {
|
||||
tree: TreeRef,
|
||||
}
|
||||
|
||||
struct Compiler<'a> {
|
||||
semantics: &'a Semantics<'a>,
|
||||
syntax: &'a SyntaxTree<'a>,
|
||||
|
||||
// TODO: generic functions will actually be keyed by treeref and concrete
|
||||
// types
|
||||
function_bindings: HashMap<TreeRef, usize>,
|
||||
|
||||
// We need to hold a space in the function array while we're compiling
|
||||
// the function, but the Module functions are not Option<>. Here we just
|
||||
// make a space that *is* Option<> so that we have a place to hold things
|
||||
// while we compile. This will get spilled into module.functions at the
|
||||
// end.
|
||||
function_bindings: HashMap<FunctionKey, usize>,
|
||||
pending_functions: Vec<(FunctionKey, usize, Function)>,
|
||||
temp_functions: Vec<Option<Rc<Function>>>,
|
||||
|
||||
module: Module,
|
||||
|
|
@ -223,25 +221,34 @@ pub fn compile(semantics: &Semantics) -> Rc<Module> {
|
|||
semantics,
|
||||
syntax: semantics.tree(),
|
||||
function_bindings: HashMap::new(),
|
||||
pending_functions: Vec::new(),
|
||||
temp_functions: Vec::new(),
|
||||
|
||||
module: Module::new(),
|
||||
function: Function::new("<< module >>", 0),
|
||||
};
|
||||
|
||||
if let Some(t) = semantics.tree().root() {
|
||||
compiler.temp_functions.push(None);
|
||||
file(&mut compiler, t);
|
||||
compiler.temp_functions[0] = Some(Rc::new(compiler.function));
|
||||
compiler.module.init = 0;
|
||||
}
|
||||
|
||||
while let Some((fk, idx, func)) = compiler.pending_functions.pop() {
|
||||
if idx >= compiler.temp_functions.len() {
|
||||
compiler.temp_functions.resize(idx + 1, None);
|
||||
}
|
||||
compiler.function = func;
|
||||
compile_function(&mut compiler, fk.tree);
|
||||
compiler.temp_functions[idx] = Some(Rc::new(compiler.function));
|
||||
}
|
||||
|
||||
let mut module = compiler.module;
|
||||
|
||||
for f in compiler.temp_functions {
|
||||
module.functions.push(f.unwrap());
|
||||
}
|
||||
|
||||
let index = module.functions.len();
|
||||
module.functions.push(Rc::new(compiler.function));
|
||||
module.init = index;
|
||||
|
||||
Rc::new(module)
|
||||
}
|
||||
|
||||
|
|
@ -465,14 +472,15 @@ fn compile_identifier_expression(c: &mut Compiler, t: TreeRef, tree: &Tree) -> O
|
|||
}
|
||||
}
|
||||
Declaration::Function { declaration, .. } => {
|
||||
let index = match c.function_bindings.get(declaration) {
|
||||
let key = FunctionKey { tree: *declaration };
|
||||
let index = match c.function_bindings.get(&key) {
|
||||
Some(index) => *index,
|
||||
None => {
|
||||
let tree = &c.syntax[*declaration];
|
||||
compiler_assert_eq!(c, t, tree.kind, TreeKind::FunctionDecl);
|
||||
compile_function_declaration(c, t, tree, false)?;
|
||||
*c.function_bindings
|
||||
.get(declaration)
|
||||
.get(&key)
|
||||
.expect("did not compile the function!")
|
||||
}
|
||||
};
|
||||
|
|
@ -608,27 +616,25 @@ fn compile_let_statement(c: &mut Compiler, t: TreeRef, tree: &Tree, gen_value: b
|
|||
|
||||
fn compile_function_declaration(c: &mut Compiler, t: TreeRef, tree: &Tree, gen_value: bool) -> CR {
|
||||
// Only compile a given function once.
|
||||
// TODO: This should actually be compiled on access! How is this going to work??
|
||||
if !c.function_bindings.contains_key(&t) {
|
||||
//
|
||||
// TODO: When it's time for generics, this should only actually compile
|
||||
// if we have no unbound type variables.
|
||||
let fk = FunctionKey { tree: t };
|
||||
if !c.function_bindings.contains_key(&fk) {
|
||||
let name = tree.nth_token(1)?;
|
||||
|
||||
let block = tree.child_of_kind(c.syntax, TreeKind::Block)?;
|
||||
let param_list = tree.child_tree_of_kind(c.syntax, TreeKind::ParamList)?;
|
||||
let param_count = param_list.children.len() - 2;
|
||||
|
||||
let function_index = c.temp_functions.len();
|
||||
c.temp_functions.push(None);
|
||||
c.function_bindings.insert(t, function_index);
|
||||
|
||||
// Now compile the function.
|
||||
let mut prev = Function::new(name.as_str(), param_count);
|
||||
std::mem::swap(&mut c.function, &mut prev);
|
||||
|
||||
compile_expression(c, block);
|
||||
c.push(Instruction::Return);
|
||||
|
||||
std::mem::swap(&mut c.function, &mut prev);
|
||||
c.temp_functions[function_index] = Some(Rc::new(prev));
|
||||
c.pending_functions.push((
|
||||
fk.clone(),
|
||||
function_index,
|
||||
Function::new(name.as_str(), param_count),
|
||||
));
|
||||
c.function_bindings.insert(fk, function_index);
|
||||
c.module
|
||||
.exports
|
||||
.insert(name.to_string(), Export::Function(function_index));
|
||||
|
|
@ -641,6 +647,16 @@ fn compile_function_declaration(c: &mut Compiler, t: TreeRef, tree: &Tree, gen_v
|
|||
OK
|
||||
}
|
||||
|
||||
fn compile_function(c: &mut Compiler, t: TreeRef) -> CR {
|
||||
let tree = &c.syntax[t];
|
||||
let block = tree.child_of_kind(c.syntax, TreeKind::Block)?;
|
||||
|
||||
compile_expression(c, block);
|
||||
c.push(Instruction::Return);
|
||||
|
||||
OK
|
||||
}
|
||||
|
||||
fn compile_block_statement(c: &mut Compiler, t: TreeRef, gen_value: bool) -> CR {
|
||||
compile_expression(c, t);
|
||||
if !gen_value {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue