[fine] The VM lives! We can test it a little!

This commit is contained in:
John Doty 2024-01-14 16:06:14 -08:00
parent 866830b485
commit 1eb7da77fc
8 changed files with 117 additions and 15 deletions

View file

@ -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 {

View file

@ -1,6 +1,6 @@
use std::rc::Rc;
use crate::compiler::{Function, Instruction, Module};
use crate::compiler::{Export, Function, Instruction, Module};
use crate::semantics::Type;
use thiserror::Error;
@ -175,6 +175,13 @@ impl Context {
Context { module, globals }
}
pub fn init(&mut self) -> std::result::Result<(), VMError> {
let init_index = self.module.init;
let init_fn = self.module.functions[init_index].clone();
eval(self, init_fn, vec![])?;
Ok(())
}
fn get_global(&self, i: usize) -> Result<StackValue> {
self.globals
.get(i)
@ -321,8 +328,10 @@ fn eval_one(
Instruction::Return => match stack.pop() {
Some(mut frame) => {
// The return value is at the top of the stack already.
let retval = f.pop_value()?;
std::mem::swap(&mut frame, f);
*index = f.pc;
f.push_value(retval);
}
None => return Ok(Flow::Break),
},
@ -343,6 +352,18 @@ pub fn eval(
loop {
let instructions = f.func.instructions();
let instruction = instructions[index];
// {
// eprint!("{index}: {instruction:?} [");
// for val in f.stack.iter().rev().take(3) {
// eprint!("{val:?} ");
// }
// if f.stack.len() > 3 {
// eprint!("...");
// }
// eprintln!("]");
// }
index += 1;
match eval_one(instruction, &mut index, c, &mut f, &mut stack) {
@ -369,3 +390,19 @@ pub fn eval(
};
}
}
pub fn eval_export_fn(
c: &mut Context,
name: &str,
args: &[StackValue],
) -> std::result::Result<StackValue, VMError> {
let export = match c.module.exports.get(name) {
Some(Export::Function(id)) => id,
Some(_) => todo!(),
None => todo!(),
};
let function = c.module.functions[*export].clone();
let args = args.iter().map(|a| a.clone()).collect();
eval(c, function, args)
}