From 1eb7da77fca52c9c6d6ac1044758221dd378dcb1 Mon Sep 17 00:00:00 2001 From: John Doty Date: Sun, 14 Jan 2024 16:06:14 -0800 Subject: [PATCH] [fine] The VM lives! We can test it a little! --- fine/src/compiler.rs | 28 ++++++++++++++-- fine/src/vm.rs | 39 +++++++++++++++++++++- fine/tests/example_tests.rs | 16 ++++++--- fine/tests/expression/argument.fine | 37 ++++++++++++++++++-- fine/tests/expression/arithmetic.fine | 3 +- fine/tests/expression/conditional.fine | 3 +- fine/tests/expression/empty_statement.fine | 3 +- fine/tests/expression/variable.fine | 3 +- 8 files changed, 117 insertions(+), 15 deletions(-) diff --git a/fine/src/compiler.rs b/fine/src/compiler.rs index 09518819..b0647559 100644 --- a/fine/src/compiler.rs +++ b/fine/src/compiler.rs @@ -123,6 +123,14 @@ struct Compiler<'a> { // TODO: generic functions will actually be keyed by treeref and concrete // types function_bindings: HashMap, + + // 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>>, + module: Module, function: Function, } @@ -211,6 +219,7 @@ pub fn compile(semantics: &Semantics) -> Rc { 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 { } 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 { diff --git a/fine/src/vm.rs b/fine/src/vm.rs index eb9b6c81..b63eeadf 100644 --- a/fine/src/vm.rs +++ b/fine/src/vm.rs @@ -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 { 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 { + 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) +} diff --git a/fine/tests/example_tests.rs b/fine/tests/example_tests.rs index fa974f48..c544f262 100644 --- a/fine/tests/example_tests.rs +++ b/fine/tests/example_tests.rs @@ -2,7 +2,7 @@ use fine::compiler::{compile, Function, Module}; use fine::parser::SyntaxTree; use fine::semantics::{check, Error, Semantics, Type}; use fine::tokens::Lines; -use fine::vm::{eval, Context}; +use fine::vm::{eval_export_fn, Context}; use pretty_assertions::assert_eq; use std::fmt::Write as _; @@ -264,13 +264,19 @@ fn assert_eval_ok(tree: &SyntaxTree, lines: &Lines, expected: &str) { let semantics = Semantics::new(tree, lines); let module = compile(&semantics); - let main_function = module.functions[module.init].clone(); + let mut context = Context::new(module); + context.init().expect("Unable to initialize module"); - let mut context = Context::new(module.clone()); - match eval(&mut context, main_function, vec![]) { + match eval_export_fn(&mut context, "test", &[]) { Ok(v) => { let actual = format!("{:?}", v); - semantic_assert_eq!(&semantics, None, expected, &actual, "module evaluated"); + semantic_assert_eq!( + &semantics, + None, + expected, + &actual, + "wrong return from test function" + ); } Err(e) => { semantic_panic!(&semantics, None, "error occurred while running: {:?}", e); diff --git a/fine/tests/expression/argument.fine b/fine/tests/expression/argument.fine index c927f7cd..2b6d9fd2 100644 --- a/fine/tests/expression/argument.fine +++ b/fine/tests/expression/argument.fine @@ -2,7 +2,12 @@ fun foo(x: f64) { x + 7 } +fun test() { + foo(1) +} + // @no-errors +// @eval: Float(8.0) // @type: 20 f64 // @concrete: // | File @@ -27,16 +32,44 @@ fun foo(x: f64) { // | LiteralExpression // | Number:'"7"' // | RightBrace:'"}"' +// | FunctionDecl +// | Fun:'"fun"' +// | Identifier:'"test"' +// | ParamList +// | LeftParen:'"("' +// | RightParen:'")"' +// | Block +// | LeftBrace:'"{"' +// | ExpressionStatement +// | CallExpression +// | Identifier +// | Identifier:'"foo"' +// | ArgumentList +// | LeftParen:'"("' +// | Argument +// | LiteralExpression +// | Number:'"1"' +// | RightParen:'")"' +// | RightBrace:'"}"' // | // @compiles-to: // | function foo (1 args, 0 locals): // | strings (0): -// | code (3): +// | code (4): // | 0: LoadArgument(0) // | 1: PushFloat(7.0) // | 2: FloatAdd +// | 3: Return +// | function test (0 args, 0 locals): +// | strings (0): +// | code (4): +// | 0: PushFloat(1.0) +// | 1: LoadFunction(0) +// | 2: Call(1) +// | 3: Return // | function << module >> (0 args, 0 locals): // | strings (0): -// | code (1): +// | code (2): // | 0: PushNothing +// | 1: Return // | diff --git a/fine/tests/expression/arithmetic.fine b/fine/tests/expression/arithmetic.fine index 9dfa9dd0..70d1a3d9 100644 --- a/fine/tests/expression/arithmetic.fine +++ b/fine/tests/expression/arithmetic.fine @@ -26,7 +26,7 @@ // @compiles-to: // | function << module >> (0 args, 0 locals): // | strings (0): -// | code (11): +// | code (12): // | 0: PushFloat(1.0) // | 1: PushFloat(2.0) // | 2: FloatMultiply @@ -38,4 +38,5 @@ // | 8: FloatAdd // | 9: Discard // | 10: PushNothing +// | 11: Return // | diff --git a/fine/tests/expression/conditional.fine b/fine/tests/expression/conditional.fine index b4e3bd22..9f948fe7 100644 --- a/fine/tests/expression/conditional.fine +++ b/fine/tests/expression/conditional.fine @@ -48,7 +48,7 @@ if true { "discarded"; 23 } else { 45 } // | function << module >> (0 args, 0 locals): // | strings (1): // | 0: "discarded" -// | code (7): +// | code (8): // | 0: PushTrue // | 1: JumpFalse(6) // | 2: PushString(0) @@ -56,4 +56,5 @@ if true { "discarded"; 23 } else { 45 } // | 4: PushFloat(23.0) // | 5: Jump(7) // | 6: PushFloat(45.0) +// | 7: Return // | diff --git a/fine/tests/expression/empty_statement.fine b/fine/tests/expression/empty_statement.fine index afa4fbc2..9a841984 100644 --- a/fine/tests/expression/empty_statement.fine +++ b/fine/tests/expression/empty_statement.fine @@ -9,6 +9,7 @@ // @compiles-to: // | function << module >> (0 args, 0 locals): // | strings (0): -// | code (1): +// | code (2): // | 0: PushNothing +// | 1: Return // | diff --git a/fine/tests/expression/variable.fine b/fine/tests/expression/variable.fine index b7d07538..2cbe7eaf 100644 --- a/fine/tests/expression/variable.fine +++ b/fine/tests/expression/variable.fine @@ -47,7 +47,7 @@ z; // @compiles-to: // | function << module >> (0 args, 0 locals): // | strings (0): -// | code (13): +// | code (14): // | 0: PushFloat(23.0) // | 1: StoreModule(0) // | 2: LoadModule(0) @@ -61,4 +61,5 @@ z; // | 10: LoadModule(2) // | 11: Discard // | 12: PushNothing +// | 13: Return // |