cgen: fix match

pull/3947/head
Alexander Medvednikov 2020-03-07 00:47:49 +01:00
parent 9846fecd84
commit 968e310793
3 changed files with 23 additions and 11 deletions

View File

@ -513,16 +513,25 @@ fn (g mut Gen) expr(node ast.Expr) {
g.write('$type_sym.name $tmp = ') g.write('$type_sym.name $tmp = ')
g.expr(it.cond) g.expr(it.cond)
g.writeln(';') // $it.blocks.len') g.writeln(';') // $it.blocks.len')
for branch in it.branches { for j, branch in it.branches {
g.write('if ') if j == it.branches.len - 1 {
for i, expr in branch.exprs { // last block is an `else{}`
g.write('$tmp == ') g.writeln('else {')
g.expr(expr) }
if i < branch.exprs.len - 1 { else {
g.write(' || ') if j > 0 {
} g.write('else ')
}
g.write('if (')
for i, expr in branch.exprs {
g.write('$tmp == ')
g.expr(expr)
if i < branch.exprs.len - 1 {
g.write(' || ')
}
}
g.writeln(') {')
} }
g.writeln('{')
g.stmts(branch.stmts) g.stmts(branch.stmts)
g.writeln('}') g.writeln('}')
} }

View File

@ -148,13 +148,15 @@ void println(string s) {
void matches() { void matches() {
int a = 100; int a = 100;
int tmp1 = a; int tmp1 = a;
if tmp1 == 10{ if (tmp1 == 10) {
println(tos3("10")); println(tos3("10"));
} }
if tmp1 == 20{ else if (tmp1 == 20) {
int k = a + 1; int k = a + 1;
}
else {
} }
; ;
} }

View File

@ -147,6 +147,7 @@ fn matches() {
20 { 20 {
k := a + 1 k := a + 1
} }
else{}
} }
/* /*
n := match a { n := match a {