[fine] The VM lives! We can test it a little!
This commit is contained in:
parent
866830b485
commit
1eb7da77fc
8 changed files with 117 additions and 15 deletions
|
|
@ -123,6 +123,14 @@ struct Compiler<'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.
|
||||
temp_functions: Vec<Option<Rc<Function>>>,
|
||||
|
||||
module: Module,
|
||||
function: Function,
|
||||
}
|
||||
|
|
@ -211,6 +219,7 @@ pub fn compile(semantics: &Semantics) -> Rc<Module> {
|
|||
semantics,
|
||||
syntax: semantics.tree(),
|
||||
function_bindings: HashMap::new(),
|
||||
temp_functions: Vec::new(),
|
||||
module: Module::new(),
|
||||
function: Function::new("<< module >>", 0),
|
||||
};
|
||||
|
|
@ -220,6 +229,11 @@ pub fn compile(semantics: &Semantics) -> Rc<Module> {
|
|||
}
|
||||
|
||||
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;
|
||||
|
|
@ -240,6 +254,7 @@ fn file(c: &mut Compiler, t: TreeRef) {
|
|||
}
|
||||
compile_statement(c, *children.last().unwrap(), true);
|
||||
}
|
||||
c.push(Instruction::Return);
|
||||
}
|
||||
|
||||
type CR = Option<()>;
|
||||
|
|
@ -568,15 +583,22 @@ fn compile_function_declaration(c: &mut Compiler, t: TreeRef, tree: &Tree, gen_v
|
|||
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);
|
||||
|
||||
c.function_bindings.insert(t, c.module.functions.len());
|
||||
|
||||
compile_expression(c, block);
|
||||
c.push(Instruction::Return);
|
||||
|
||||
std::mem::swap(&mut c.function, &mut prev);
|
||||
c.module.functions.push(Rc::new(prev));
|
||||
c.temp_functions[function_index] = Some(Rc::new(prev));
|
||||
c.module
|
||||
.exports
|
||||
.insert(name.to_string(), Export::Function(function_index));
|
||||
}
|
||||
|
||||
if gen_value {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue