[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 {
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
}

View file

@ -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
// |

View file

@ -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:'"}"'
// |