1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix match

This commit is contained in:
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,8 +513,16 @@ 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 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)
@ -522,7 +530,8 @@ fn (g mut Gen) expr(node ast.Expr) {
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 {