Else if chains

This commit is contained in:
allexanderbergmns
2026-08-24 15:44:27 +02:00
parent 55a3ee0b18
commit fc0ff47459
6 changed files with 257 additions and 10 deletions
+34
View File
@@ -190,6 +190,40 @@ fn (mut g Gen) gen_stmt(st Stmt) ! {
}
g.emit_label(end_l)
}
.match_stmt {
// match x { v1 {..} v2 {..} else {..} } → subject := x; a chain of
// equality tests jumping to the matching arm; else falls through.
subj_idx := g.new_local()
end_l := g.new_label()
g.gen_expr(st.expr)!
g.emit_store(subj_idx)
for i, arm in st.arms {
next_l := g.new_label()
g.emit_load(subj_idx)
g.gen_expr(arm.val)!
g.code << op_eq
g.code << op_jz
g.code << obj.encode_i64(0)
g.fixups << Fixup{ name: next_l, off: u32(g.code.len) - 8 }
for s in arm.body {
g.gen_stmt(s)!
}
g.code << op_jmp
g.code << obj.encode_i64(0)
g.fixups << Fixup{ name: end_l, off: u32(g.code.len) - 8 }
g.emit_label(next_l)
if i == st.arms.len - 1 && !st.has_else {
// no else: fall through to the end label
g.emit_label(end_l)
}
}
if st.has_else {
for s in st.els_body {
g.gen_stmt(s)!
}
g.emit_label(end_l)
}
}
.while_stmt {
loop_l := g.new_label()
end_l := g.new_label()
+2
View File
@@ -34,6 +34,7 @@ pub enum TokKind {
kw_while
kw_for
kw_in
kw_match
kw_break
kw_continue
kw_return
@@ -253,6 +254,7 @@ fn (mut l Lexer) lex_ident(line int) Tok {
'while' { TokKind.kw_while }
'for' { TokKind.kw_for }
'in' { TokKind.kw_in }
'match' { TokKind.kw_match }
'break' { TokKind.kw_break }
'continue' { TokKind.kw_continue }
'return' { TokKind.kw_return }
+55 -8
View File
@@ -7,7 +7,8 @@
// stmt := 'let' IDENT '=' expr
// | IDENT '=' expr
// | postfix '=' expr (a[i] = v)
// | 'if' cond block ['else' block]
// | 'if' cond block ['else' ('if' ... | block)]
// | 'match' expr '{' (expr block | 'else' block)* '}'
// | 'while' cond block
// | 'for' IDENT 'in' range block (range := expr '..' expr | expr '...' expr)
// | 'for' IDENT 'in' expr block (iterate an array)
@@ -58,6 +59,7 @@ pub enum StmtKind {
assign_stmt
index_assign
if_stmt
match_stmt
while_stmt
for_range_stmt
for_in_stmt
@@ -67,6 +69,13 @@ pub enum StmtKind {
assert_stmt
}
// MatchArm is a single `value { body }` arm of a match statement.
pub struct MatchArm {
pub mut:
val Expr
body []Stmt
}
pub struct Stmt {
pub mut:
kind StmtKind
@@ -77,6 +86,9 @@ pub mut:
idx Expr // index_assign: the index expression
body []Stmt
els []Stmt
arms []MatchArm // match_stmt: the arms (val + body)
has_else bool // match_stmt: a trailing else arm exists
els_body []Stmt // match_stmt: body of the else arm
has_val bool
inclusive bool // for_range_stmt: `..` (false) vs `...` (true)
line int
@@ -194,15 +206,31 @@ fn (mut p Parser) parse_stmt() !Stmt {
e := p.parse_expr()!
return Stmt{ kind: .let_stmt, target: name.lit, expr: e, line: t.line }
} .kw_if {
return p.parse_if(t)!
}
.kw_match {
p.advance()
cond := p.parse_cond()!
body := p.parse_block()!
mut els := []Stmt{}
if p.cur().kind == .kw_else {
p.advance()
els = p.parse_block()!
subject := p.parse_expr()!
p.expect(.lbrace, "'{'")!
mut arms := []MatchArm{}
mut has_else := false
mut els_body := []Stmt{}
for p.cur().kind != .rbrace {
if p.cur().kind == .eof {
return error('unexpected end of file inside match (missing "}")')
}
if p.cur().kind == .kw_else {
p.advance()
els_body = p.parse_block()!
has_else = true
continue
}
val := p.parse_expr()!
body := p.parse_block()!
arms << MatchArm{ val: val, body: body }
}
return Stmt{ kind: .if_stmt, cond: cond, body: body, els: els, line: t.line }
p.expect(.rbrace, "'}'")!
return Stmt{ kind: .match_stmt, expr: subject, arms: arms, has_else: has_else, els_body: els_body, line: t.line }
}
.kw_while {
p.advance()
@@ -284,6 +312,25 @@ fn (mut p Parser) parse_stmt() !Stmt {
}
}
// parse_if parses `if cond block ['else' ('if' ... | block)]`. An `else if`
// chain is represented by putting the nested if-statement in the else list,
// so codegen needs no special casing.
fn (mut p Parser) parse_if(t Tok) !Stmt {
p.advance()
cond := p.parse_cond()!
body := p.parse_block()!
mut els := []Stmt{}
if p.cur().kind == .kw_else {
p.advance()
if p.cur().kind == .kw_if {
els << p.parse_if(p.cur())!
} else {
els = p.parse_block()!
}
}
return Stmt{ kind: .if_stmt, cond: cond, body: body, els: els, line: t.line }
}
// bin_node allocates a binary-operator node. It takes copies of the operands
// so that `&l`/`&r` target fresh heap objects (taking the address of a local
// that is later reassigned would create a self-referential node).