gen: blank identifier fixes

pull/4053/head
Joe Conigliaro 2020-03-18 10:41:04 +11:00
parent 330745da30
commit 6ca47aeb4b
1 changed files with 64 additions and 35 deletions

View File

@ -401,23 +401,13 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
ident_var_info := ident.var_info() ident_var_info := ident.var_info()
styp := g.typ(ident_var_info.typ) styp := g.typ(ident_var_info.typ)
if ident.kind == .blank_ident { if ident.kind == .blank_ident {
is_call := match val { if is_call(val) {
ast.CallExpr{
true
}
ast.MethodCallExpr{
true
}
else {
false}
}
if is_call {
g.expr(val) g.expr(val)
} }
else { else {
g.write('{$styp _ = ') g.write('{$styp _ = ')
g.expr(val) g.expr(val)
g.write('}') g.writeln('}')
} }
} }
else { else {
@ -572,30 +562,42 @@ fn (g mut Gen) expr(node ast.Expr) {
g.write('/* as */') g.write('/* as */')
} }
ast.AssignExpr { ast.AssignExpr {
g.is_assign_expr = true if is_blank_ident(it.left) {
mut str_add := false if is_call(it.val) {
if it.left_type == table.string_type_idx && it.op == .plus_assign { g.expr(it.val)
// str += str2 => `str = string_add(str, str2)` }
else {
g.write('{${g.typ(it.left_type)} _ = ')
g.expr(it.val)
g.writeln('}')
}
}
else {
g.is_assign_expr = true
mut str_add := false
if it.left_type == table.string_type_idx && it.op == .plus_assign {
// str += str2 => `str = string_add(str, str2)`
g.expr(it.left)
g.write(' = string_add(')
str_add = true
}
g.expr(it.left) g.expr(it.left)
g.write(' = string_add(') // arr[i] = val => `array_set(arr, i, val)`, not `array_get(arr, i) = val`
str_add = true if !g.is_array_set && !str_add {
} g.write(' $it.op.str() ')
g.expr(it.left) }
// arr[i] = val => `array_set(arr, i, val)`, not `array_get(arr, i) = val` else if str_add {
if !g.is_array_set && !str_add { g.write(', ')
g.write(' $it.op.str() ') }
} g.is_assign_expr = false
else if str_add { g.expr_with_cast(it.right_type, it.left_type, it.val)
g.write(', ') if g.is_array_set {
} g.write(' })')
g.is_assign_expr = false g.is_array_set = false
g.expr_with_cast(it.right_type, it.left_type, it.val) }
if g.is_array_set { else if str_add {
g.write(' })') g.write(')')
g.is_array_set = false }
}
else if str_add {
g.write(')')
} }
} }
ast.Assoc { ast.Assoc {
@ -1333,6 +1335,33 @@ fn (g mut Gen) ref_or_deref_arg(arg ast.CallArg) {
} }
} }
[inline]
fn is_blank_ident(expr ast.Expr) bool {
match expr {
ast.Ident {
return it.kind == .blank_ident
}
else {
return false
}
}
}
[inline]
fn is_call(expr ast.Expr) bool {
return match expr {
ast.CallExpr {
true
}
ast.MethodCallExpr {
true
}
else {
false
}
}
}
fn verror(s string) { fn verror(s string) {
println('cgen error: $s') println('cgen error: $s')
// exit(1) // exit(1)