cgen: fix `_ := []byte{init:1}`

pull/4989/head^2
Delyan Angelov 2020-05-22 18:17:13 +03:00
parent b5af6d44c2
commit aef751861d
1 changed files with 18 additions and 19 deletions

View File

@ -969,6 +969,9 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
if is_call { if is_call {
g.expr(val) g.expr(val)
} else { } else {
if val is ast.ArrayInit {
g.gen_default_init_value( val as ast.ArrayInit )
}
g.write('{$styp _ = ') g.write('{$styp _ = ')
g.expr(val) g.expr(val)
g.writeln(';}') g.writeln(';}')
@ -977,26 +980,8 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
right_sym := g.table.get_type_symbol(assign_stmt.right_types[i]) right_sym := g.table.get_type_symbol(assign_stmt.right_types[i])
mut is_fixed_array_init := false mut is_fixed_array_init := false
mut has_val := false mut has_val := false
/*
if val is ast.ArrayInit { if val is ast.ArrayInit {
is_fixed_array_init = val.is_fixed is_fixed_array_init, has_val = g.gen_default_init_value( val as ast.ArrayInit )
has_val = val.has_val
}
*/
match val {
ast.ArrayInit {
is_fixed_array_init = it.is_fixed
has_val = it.has_val
elem_type_str := g.typ(it.elem_type)
if it.has_default {
g.write('$elem_type_str _val_$it.pos.pos = ')
g.expr(it.default_expr)
g.writeln(';')
} else if it.has_len && it.elem_type == table.string_type {
g.writeln('$elem_type_str _val_$it.pos.pos = tos_lit("");')
}
}
else {}
} }
is_inside_ternary := g.inside_ternary != 0 is_inside_ternary := g.inside_ternary != 0
cur_line := if is_inside_ternary { cur_line := if is_inside_ternary {
@ -1062,6 +1047,20 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
} }
} }
fn (mut g Gen) gen_default_init_value(val ast.ArrayInit) (bool, bool) {
is_fixed_array_init := val.is_fixed
has_val := val.has_val
elem_type_str := g.typ(val.elem_type)
if val.has_default {
g.write('$elem_type_str _val_$val.pos.pos = ')
g.expr(val.default_expr)
g.writeln(';')
} else if val.has_len && val.elem_type == table.string_type {
g.writeln('$elem_type_str _val_$val.pos.pos = tos_lit("");')
}
return is_fixed_array_init, has_val
}
fn (mut g Gen) register_ternary_name(name string) { fn (mut g Gen) register_ternary_name(name string) {
level_key := g.inside_ternary.str() level_key := g.inside_ternary.str()
if level_key !in g.ternary_level_names { if level_key !in g.ternary_level_names {