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.expr(it.cond)
g.writeln(';') // $it.blocks.len')
for branch in it.branches {
g.write('if ')
for i, expr in branch.exprs {
g.write('$tmp == ')
g.expr(expr)
if i < branch.exprs.len - 1 {
g.write(' || ')
}
for j, branch in it.branches {
if j == it.branches.len - 1 {
// last block is an `else{}`
g.writeln('else {')
}
else {
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.writeln('}')
}

View File

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

View File

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