Add block expressions and if expressions

If and block statements are now expressions. The last expression, if
any, is used as the return value of this expression.

Also fixed a (major) bug in the if statement generation that was causing
the wrong jump address to be generated.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2024-10-21 15:39:48 -07:00
parent 7bce6f8e23
commit 46c38de20c
5 changed files with 295 additions and 194 deletions

View File

@@ -250,6 +250,24 @@ GENERATE = [
.add_struct("List", ["lbracket: Token", "exprs: Vec<ExprP>", "rbracket: Token"])
.add_struct(
"Map", ["lbracket: Token", "pairs: Vec<(ExprP, ExprP)>", "rbracket: Token"]
)
.add_struct(
"If",
[
"if_kw: Token",
"condition: ExprP",
"then_branch: BlockExpr",
"else_branch: Option<BlockExpr>",
],
)
.add_struct(
"Block",
[
"lbrace: Token",
"stmts: Vec<StmtP>",
"return_expr: Option<ExprP>",
"rbrace: Token",
],
),
# Stmt
GenerateGroup("Stmt")
@@ -260,17 +278,7 @@ GENERATE = [
"IndexAssign", ["expr: ExprP", "index: ExprP", "op: Token", "rhs: ExprP"]
)
.add_struct("Set", ["expr: ExprP", "name: Token", "op: Token", "rhs: ExprP"])
.add_struct("Block", ["lbrace: Token", "stmts: Vec<StmtP>", "rbrace: Token"])
.add_struct("Return", ["return_kw: Token", "expr: Option<ExprP>"])
.add_struct(
"If",
[
"if_kw: Token",
"condition: ExprP",
"then_branch: BlockStmt",
"else_branch: Vec<StmtP>",
],
),
.add_struct("Return", ["return_kw: Token", "expr: Option<ExprP>"]),
]