From 40d611ae37bcc68a103d0c83ee94c69b3e1fd3b5 Mon Sep 17 00:00:00 2001 From: John Doty Date: Sun, 14 Jan 2024 19:18:54 -0800 Subject: [PATCH] [fine] Fix block compilation --- fine/src/compiler.rs | 15 +++++++ fine/tests/expression/arithmetic.fine | 64 ++++++++++++++++----------- fine/tests/expression/block.fine | 34 +++++++++++--- 3 files changed, 83 insertions(+), 30 deletions(-) diff --git a/fine/src/compiler.rs b/fine/src/compiler.rs index b0647559..b97fc539 100644 --- a/fine/src/compiler.rs +++ b/fine/src/compiler.rs @@ -477,6 +477,11 @@ fn compile_call_expression(c: &mut Compiler, tree: &Tree) -> CR { } fn compile_block_expression(c: &mut Compiler, tree: &Tree) -> CR { + if tree.children.len() == 2 { + c.push(Instruction::PushNothing); + return OK; + } + let last_is_brace = tree.nth_token(tree.children.len() - 1).is_some(); let last_index = tree.children.len() - if last_is_brace { 2 } else { 1 }; @@ -499,6 +504,7 @@ fn compile_statement(c: &mut Compiler, t: TreeRef, gen_value: bool) { TreeKind::LetStatement => compile_let_statement(c, t, tree, gen_value), TreeKind::ExpressionStatement => compile_expression_statement(c, tree, gen_value), TreeKind::IfStatement => compile_if_statement(c, tree, gen_value), + TreeKind::Block => compile_block_statement(c, t, gen_value), _ => ice!(c, t, "unsupported tree kind {:?}", tree.kind), }; if matches!(cr, None) { @@ -607,3 +613,12 @@ fn compile_function_declaration(c: &mut Compiler, t: TreeRef, tree: &Tree, gen_v OK } + +fn compile_block_statement(c: &mut Compiler, t: TreeRef, gen_value: bool) -> CR { + compile_expression(c, t); + if !gen_value { + c.push(Instruction::Discard); + } + + OK +} diff --git a/fine/tests/expression/arithmetic.fine b/fine/tests/expression/arithmetic.fine index 70d1a3d9..bbe46cbf 100644 --- a/fine/tests/expression/arithmetic.fine +++ b/fine/tests/expression/arithmetic.fine @@ -1,32 +1,43 @@ -1 * 2 + -3 * 4; +fun test() { + 1 * 2 + -3 * 4 +} // @no-errors -// @type: 6 f64 +// @eval: Float(-10.0) +// @type: 23 f64 // @concrete: // | File -// | ExpressionStatement -// | BinaryExpression -// | BinaryExpression -// | LiteralExpression -// | Number:'"1"' -// | Star:'"*"' -// | LiteralExpression -// | Number:'"2"' -// | Plus:'"+"' -// | BinaryExpression -// | UnaryExpression -// | Minus:'"-"' -// | LiteralExpression -// | Number:'"3"' -// | Star:'"*"' -// | LiteralExpression -// | Number:'"4"' -// | Semicolon:'";"' +// | FunctionDecl +// | Fun:'"fun"' +// | Identifier:'"test"' +// | ParamList +// | LeftParen:'"("' +// | RightParen:'")"' +// | Block +// | LeftBrace:'"{"' +// | ExpressionStatement +// | BinaryExpression +// | BinaryExpression +// | LiteralExpression +// | Number:'"1"' +// | Star:'"*"' +// | LiteralExpression +// | Number:'"2"' +// | Plus:'"+"' +// | BinaryExpression +// | UnaryExpression +// | Minus:'"-"' +// | LiteralExpression +// | Number:'"3"' +// | Star:'"*"' +// | LiteralExpression +// | Number:'"4"' +// | RightBrace:'"}"' // | // @compiles-to: -// | function << module >> (0 args, 0 locals): +// | function test (0 args, 0 locals): // | strings (0): -// | code (12): +// | code (10): // | 0: PushFloat(1.0) // | 1: PushFloat(2.0) // | 2: FloatMultiply @@ -36,7 +47,10 @@ // | 6: PushFloat(4.0) // | 7: FloatMultiply // | 8: FloatAdd -// | 9: Discard -// | 10: PushNothing -// | 11: Return +// | 9: Return +// | function << module >> (0 args, 0 locals): +// | strings (0): +// | code (2): +// | 0: PushNothing +// | 1: Return // | diff --git a/fine/tests/expression/block.fine b/fine/tests/expression/block.fine index 20fd57a6..1d6b0c14 100644 --- a/fine/tests/expression/block.fine +++ b/fine/tests/expression/block.fine @@ -1,10 +1,34 @@ -{} +fun test() { + {} +} // @no-errors -// @type: 0 () +// @compiles-to: +// | function test (0 args, 0 locals): +// | strings (0): +// | code (2): +// | 0: PushNothing +// | 1: Return +// | function << module >> (0 args, 0 locals): +// | strings (0): +// | code (2): +// | 0: PushNothing +// | 1: Return +// | +// @eval: Nothing +// @type: 15 () // @concrete: // | File -// | Block -// | LeftBrace:'"{"' -// | RightBrace:'"}"' +// | FunctionDecl +// | Fun:'"fun"' +// | Identifier:'"test"' +// | ParamList +// | LeftParen:'"("' +// | RightParen:'")"' +// | Block +// | LeftBrace:'"{"' +// | Block +// | LeftBrace:'"{"' +// | RightBrace:'"}"' +// | RightBrace:'"}"' // |