[fine] Fix block compilation

This commit is contained in:
John Doty 2024-01-14 19:18:54 -08:00
parent 1eb7da77fc
commit 40d611ae37
3 changed files with 83 additions and 30 deletions

View file

@ -477,6 +477,11 @@ fn compile_call_expression(c: &mut Compiler, tree: &Tree) -> CR {
} }
fn compile_block_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_is_brace = tree.nth_token(tree.children.len() - 1).is_some();
let last_index = tree.children.len() - if last_is_brace { 2 } else { 1 }; 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::LetStatement => compile_let_statement(c, t, tree, gen_value),
TreeKind::ExpressionStatement => compile_expression_statement(c, tree, gen_value), TreeKind::ExpressionStatement => compile_expression_statement(c, tree, gen_value),
TreeKind::IfStatement => compile_if_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), _ => ice!(c, t, "unsupported tree kind {:?}", tree.kind),
}; };
if matches!(cr, None) { if matches!(cr, None) {
@ -607,3 +613,12 @@ fn compile_function_declaration(c: &mut Compiler, t: TreeRef, tree: &Tree, gen_v
OK 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
}

View file

@ -1,32 +1,43 @@
1 * 2 + -3 * 4; fun test() {
1 * 2 + -3 * 4
}
// @no-errors // @no-errors
// @type: 6 f64 // @eval: Float(-10.0)
// @type: 23 f64
// @concrete: // @concrete:
// | File // | File
// | ExpressionStatement // | FunctionDecl
// | BinaryExpression // | Fun:'"fun"'
// | BinaryExpression // | Identifier:'"test"'
// | LiteralExpression // | ParamList
// | Number:'"1"' // | LeftParen:'"("'
// | Star:'"*"' // | RightParen:'")"'
// | LiteralExpression // | Block
// | Number:'"2"' // | LeftBrace:'"{"'
// | Plus:'"+"' // | ExpressionStatement
// | BinaryExpression // | BinaryExpression
// | UnaryExpression // | BinaryExpression
// | Minus:'"-"' // | LiteralExpression
// | LiteralExpression // | Number:'"1"'
// | Number:'"3"' // | Star:'"*"'
// | Star:'"*"' // | LiteralExpression
// | LiteralExpression // | Number:'"2"'
// | Number:'"4"' // | Plus:'"+"'
// | Semicolon:'";"' // | BinaryExpression
// | UnaryExpression
// | Minus:'"-"'
// | LiteralExpression
// | Number:'"3"'
// | Star:'"*"'
// | LiteralExpression
// | Number:'"4"'
// | RightBrace:'"}"'
// | // |
// @compiles-to: // @compiles-to:
// | function << module >> (0 args, 0 locals): // | function test (0 args, 0 locals):
// | strings (0): // | strings (0):
// | code (12): // | code (10):
// | 0: PushFloat(1.0) // | 0: PushFloat(1.0)
// | 1: PushFloat(2.0) // | 1: PushFloat(2.0)
// | 2: FloatMultiply // | 2: FloatMultiply
@ -36,7 +47,10 @@
// | 6: PushFloat(4.0) // | 6: PushFloat(4.0)
// | 7: FloatMultiply // | 7: FloatMultiply
// | 8: FloatAdd // | 8: FloatAdd
// | 9: Discard // | 9: Return
// | 10: PushNothing // | function << module >> (0 args, 0 locals):
// | 11: Return // | strings (0):
// | code (2):
// | 0: PushNothing
// | 1: Return
// | // |

View file

@ -1,10 +1,34 @@
{} fun test() {
{}
}
// @no-errors // @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: // @concrete:
// | File // | File
// | Block // | FunctionDecl
// | LeftBrace:'"{"' // | Fun:'"fun"'
// | RightBrace:'"}"' // | Identifier:'"test"'
// | ParamList
// | LeftParen:'"("'
// | RightParen:'")"'
// | Block
// | LeftBrace:'"{"'
// | Block
// | LeftBrace:'"{"'
// | RightBrace:'"}"'
// | RightBrace:'"}"'
// | // |