diff options
| author | Gustav Sörnäs <gustav@sornas.net> | 2021-01-29 21:01:29 +0100 |
|---|---|---|
| committer | Gustav Sörnäs <gustav@sornas.net> | 2021-01-29 21:01:29 +0100 |
| commit | 674695d7e28f03218aa5a3facd933b87d508ea7d (patch) | |
| tree | 582118c95770f2834b802f58bb3c834d7fd0f2e9 | |
| parent | 1d4916ed80ceac20a7dac4e500a200e0e03574e7 (diff) | |
| download | sylt-674695d7e28f03218aa5a3facd933b87d508ea7d.tar.gz | |
??
| -rw-r--r-- | src/compiler.rs | 4 | ||||
| -rw-r--r-- | src/lib.rs | 8 | ||||
| -rw-r--r-- | src/vm.rs | 9 |
3 files changed, 13 insertions, 8 deletions
diff --git a/src/compiler.rs b/src/compiler.rs index d31ee0d..866e0a3 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -50,10 +50,11 @@ nextable_enum!(Prec { }); -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct Prog { pub blocks: Vec<Rc<RefCell<Block>>>, pub blobs: Vec<Rc<Blob>>, + pub functions: Vec<RustFunction>, } #[derive(Debug, Clone)] @@ -1077,6 +1078,7 @@ impl Compiler { Ok(Prog { blocks: self.blocks.clone(), blobs: self.blobs.iter().map(|x| Rc::new(x.clone())).collect(), + functions: functions.iter().map(|(_, f)| *f).collect(), }) } else { Err(self.errors.clone()) @@ -20,10 +20,10 @@ pub fn run_string(s: &str, print: bool, functions: Vec<(String, RustFunction)>) pub fn run(tokens: TokenStream, path: &Path, print: bool, functions: Vec<(String, RustFunction)>) -> Result<(), Vec<Error>> { match compiler::compile("main", path, tokens, &functions) { - Ok(blocks) => { - let mut vm = vm::VM::new(&functions).print_blocks(print).print_ops(print); - vm.typecheck(&blocks)?; - if let Err(e) = vm.run(&blocks) { + Ok(prog) => { + let mut vm = vm::VM::new().print_blocks(print).print_ops(print); + vm.typecheck(&prog)?; + if let Err(e) = vm.run(&prog) { Err(vec![e]) } else { Ok(()) @@ -295,6 +295,7 @@ pub struct VM { print_ops: bool, extern_functions: Vec<RustFunction>, + } enum OpResult { @@ -303,7 +304,7 @@ enum OpResult { } impl VM { - pub fn new(functions: &[(String, RustFunction)]) -> Self { + pub fn new() -> Self { Self { upvalues: HashMap::new(), @@ -313,7 +314,7 @@ impl VM { print_blocks: false, print_ops: false, - extern_functions: functions.iter().map(|(_, f)| *f).collect() + extern_functions: Vec::new() } } @@ -627,7 +628,7 @@ impl VM { return Ok(OpResult::Continue); } Value::ExternFunction(slot) => { - let extern_func = self.extern_functions[*slot]; + let extern_func = self.extern_functions[slot]; let res = extern_func(&self.stack[new_base+1..]); self.stack.truncate(new_base); self.stack.push(res); @@ -682,6 +683,7 @@ impl VM { pub fn run(&mut self, prog: &Prog) -> Result<(), Error>{ let block = Rc::clone(&prog.blocks[0]); self.blobs = prog.blobs.clone(); + self.extern_functions = prog.functions.clone(); self.stack.clear(); self.frames.clear(); @@ -941,6 +943,7 @@ impl VM { let mut errors = Vec::new(); self.blobs = prog.blobs.clone(); + self.extern_functions = prog.functions.clone(); for block in prog.blocks.iter() { errors.append(&mut self.typecheck_block(Rc::clone(block))); } |
