cgen: fix swap assign of prefix and postfix expr

pull/5086/head
yuyi 2020-05-28 07:22:09 +08:00 committed by GitHub
parent cecb7d29c7
commit 9cbd9db4e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 17 deletions

View File

@ -1078,6 +1078,14 @@ fn (mut g Gen) gen_cross_tmp_variable(idents []ast.Ident, val ast.Expr) {
g.write(it.op.str())
g.gen_cross_tmp_variable(idents, it.right)
}
ast.PrefixExpr {
g.write(it.op.str())
g.gen_cross_tmp_variable(idents, it.right)
}
ast.PostfixExpr {
g.gen_cross_tmp_variable(idents, it.expr)
g.write(it.op.str())
}
else {
g.expr(val)
}

View File

@ -35,25 +35,18 @@ fn (mut p Parser) check_cross_variables(idents []ast.Ident, expr ast.Expr) bool
match expr {
ast.Ident {
for ident in idents {
if ident.name == it.name {
return true
}
if ident.name == it.name { return true }
}
}
ast.InfixExpr {
if p.check_cross_variables(idents, it.left) {
return true
}
if p.check_cross_variables(idents, it.right) {
return true
}
if p.check_cross_variables(idents, it.left) { return true }
if p.check_cross_variables(idents, it.right) { return true }
}
ast.StringInterLiteral {
for expr_ in it.exprs {
if p.check_cross_variables(idents, expr_) {
return true
}
}
ast.PrefixExpr {
if p.check_cross_variables(idents, it.right) { return true }
}
ast.PostfixExpr {
if p.check_cross_variables(idents, it.expr) { return true }
}
else {}
}

View File

@ -34,3 +34,33 @@ fn test_multiple_assign_infix_expr() {
assert b == 22
assert c == 22
}
fn test_multiple_assign_prefix_expr() {
mut a := 11
mut b := 22
mut c := 33
a, b, c = -b, -c, -a
assert a == -22
assert b == -33
assert c == -11
}
fn test_multiple_assign_postfix_expr() {
mut a := 11
mut b := 22
mut c := 33
a, b, c = b++, c++, a--
assert a == 22
assert b == 33
assert c == 11
}
fn test_multiple_assign_complex_expr() {
mut a := 11
mut b := 22
mut c := 33
a, b, c = -b + 1, -c * 2, a++
assert a == -21
assert b == -66
assert c == 11
}