From 6ca47aeb4b160052b92097a7228e40de948d5a63 Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Wed, 18 Mar 2020 10:41:04 +1100 Subject: [PATCH] gen: blank identifier fixes --- vlib/v/gen/cgen.v | 99 ++++++++++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 35 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 6e9b0c4d9b..c525eb00a5 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -401,23 +401,13 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { ident_var_info := ident.var_info() styp := g.typ(ident_var_info.typ) if ident.kind == .blank_ident { - is_call := match val { - ast.CallExpr{ - true - } - ast.MethodCallExpr{ - true - } - else { - false} - } - if is_call { + if is_call(val) { g.expr(val) } else { g.write('{$styp _ = ') g.expr(val) - g.write('}') + g.writeln('}') } } else { @@ -572,30 +562,42 @@ fn (g mut Gen) expr(node ast.Expr) { g.write('/* as */') } ast.AssignExpr { - 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)` + if is_blank_ident(it.left) { + if is_call(it.val) { + g.expr(it.val) + } + 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.write(' = string_add(') - str_add = true - } - g.expr(it.left) - // arr[i] = val => `array_set(arr, i, val)`, not `array_get(arr, i) = val` - if !g.is_array_set && !str_add { - g.write(' $it.op.str() ') - } - else if str_add { - g.write(', ') - } - g.is_assign_expr = false - g.expr_with_cast(it.right_type, it.left_type, it.val) - if g.is_array_set { - g.write(' })') - g.is_array_set = false - } - else if str_add { - g.write(')') + // arr[i] = val => `array_set(arr, i, val)`, not `array_get(arr, i) = val` + if !g.is_array_set && !str_add { + g.write(' $it.op.str() ') + } + else if str_add { + g.write(', ') + } + g.is_assign_expr = false + g.expr_with_cast(it.right_type, it.left_type, it.val) + if g.is_array_set { + g.write(' })') + g.is_array_set = false + } + else if str_add { + g.write(')') + } } } 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) { println('cgen error: $s') // exit(1)