[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
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
// |
|
||||
|
|
|
|||
|
|
@ -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
|
||||
// |
|
||||
|
|
|
|||
|
|
@ -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
|
||||
// |
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
// @compiles-to:
|
||||
// | function << module >> (0 args, 0 locals):
|
||||
// | strings (0):
|
||||
// | code (1):
|
||||
// | code (2):
|
||||
// | 0: PushNothing
|
||||
// | 1: Return
|
||||
// |
|
||||
|
|
|
|||
|
|
@ -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
|
||||
// |
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue