[fine] Fix block compilation
This commit is contained in:
parent
1eb7da77fc
commit
40d611ae37
3 changed files with 83 additions and 30 deletions
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,20 @@
|
||||||
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
|
||||||
|
// | FunctionDecl
|
||||||
|
// | Fun:'"fun"'
|
||||||
|
// | Identifier:'"test"'
|
||||||
|
// | ParamList
|
||||||
|
// | LeftParen:'"("'
|
||||||
|
// | RightParen:'")"'
|
||||||
|
// | Block
|
||||||
|
// | LeftBrace:'"{"'
|
||||||
// | ExpressionStatement
|
// | ExpressionStatement
|
||||||
// | BinaryExpression
|
// | BinaryExpression
|
||||||
// | BinaryExpression
|
// | BinaryExpression
|
||||||
|
|
@ -21,12 +32,12 @@
|
||||||
// | Star:'"*"'
|
// | Star:'"*"'
|
||||||
// | LiteralExpression
|
// | LiteralExpression
|
||||||
// | Number:'"4"'
|
// | Number:'"4"'
|
||||||
// | Semicolon:'";"'
|
// | 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
|
||||||
// |
|
// |
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
// | FunctionDecl
|
||||||
|
// | Fun:'"fun"'
|
||||||
|
// | Identifier:'"test"'
|
||||||
|
// | ParamList
|
||||||
|
// | LeftParen:'"("'
|
||||||
|
// | RightParen:'")"'
|
||||||
|
// | Block
|
||||||
|
// | LeftBrace:'"{"'
|
||||||
// | Block
|
// | Block
|
||||||
// | LeftBrace:'"{"'
|
// | LeftBrace:'"{"'
|
||||||
// | RightBrace:'"}"'
|
// | RightBrace:'"}"'
|
||||||
|
// | RightBrace:'"}"'
|
||||||
// |
|
// |
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue