cgen: add `expr_string() and remove redundant codes (#9188)

pull/9193/head
yuyi 2021-03-08 18:46:39 +08:00 committed by GitHub
parent f2e570d63c
commit 568faeed77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 28 deletions

View File

@ -557,6 +557,14 @@ fn (mut g Gen) base_type(t table.Type) string {
return styp return styp
} }
fn (mut g Gen) expr_string(expr ast.Expr) string {
pos := g.out.len
g.expr(expr)
expr_str := g.out.after(pos)
g.out.go_back(expr_str.len)
return expr_str.trim_space()
}
// TODO this really shouldnt be seperate from typ // TODO this really shouldnt be seperate from typ
// but I(emily) would rather have this generation // but I(emily) would rather have this generation
// all unified in one place so that it doesnt break // all unified in one place so that it doesnt break
@ -1065,10 +1073,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
g.enum_typedefs.write_string('\t${enum_name}_$field.name') g.enum_typedefs.write_string('\t${enum_name}_$field.name')
if field.has_expr { if field.has_expr {
g.enum_typedefs.write_string(' = ') g.enum_typedefs.write_string(' = ')
pos := g.out.len expr_str := g.expr_string(field.expr)
g.expr(field.expr)
expr_str := g.out.after(pos)
g.out.go_back(expr_str.len)
g.enum_typedefs.write_string(expr_str) g.enum_typedefs.write_string(expr_str)
cur_enum_expr = expr_str cur_enum_expr = expr_str
cur_enum_offset = 0 cur_enum_offset = 0
@ -1375,11 +1380,7 @@ fn (mut g Gen) for_in_stmt(node ast.ForInStmt) {
val_sym := g.table.get_type_symbol(node.val_type) val_sym := g.table.get_type_symbol(node.val_type)
mut cond_var := '' mut cond_var := ''
if node.cond is ast.Ident || node.cond is ast.SelectorExpr { if node.cond is ast.Ident || node.cond is ast.SelectorExpr {
pos := g.out.len cond_var = g.expr_string(node.cond)
g.expr(node.cond)
cond_var = g.out.after(pos)
g.out.go_back(cond_var.len)
cond_var = cond_var.trim_space()
} else { } else {
cond_var = g.new_tmp_var() cond_var = g.new_tmp_var()
g.write(g.typ(node.cond_type)) g.write(g.typ(node.cond_type))
@ -1429,11 +1430,7 @@ fn (mut g Gen) for_in_stmt(node ast.ForInStmt) {
g.expr(node.cond) g.expr(node.cond)
g.writeln(');') g.writeln(');')
} else { } else {
pos := g.out.len cond_var = g.expr_string(node.cond)
g.expr(node.cond)
cond_var = g.out.after(pos)
g.out.go_back(cond_var.len)
cond_var = cond_var.trim_space()
} }
idx := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var } idx := if node.key_var in ['', '_'] { g.new_tmp_var() } else { node.key_var }
cond_sym := g.table.get_type_symbol(node.cond_type) cond_sym := g.table.get_type_symbol(node.cond_type)
@ -1469,11 +1466,7 @@ fn (mut g Gen) for_in_stmt(node ast.ForInStmt) {
g.writeln('// FOR IN map') g.writeln('// FOR IN map')
mut cond_var := '' mut cond_var := ''
if node.cond is ast.Ident { if node.cond is ast.Ident {
pos := g.out.len cond_var = g.expr_string(node.cond)
g.expr(node.cond)
cond_var = g.out.after(pos)
g.out.go_back(cond_var.len)
cond_var = cond_var.trim_space()
} else { } else {
cond_var = g.new_tmp_var() cond_var = g.new_tmp_var()
g.write(g.typ(node.cond_type)) g.write(g.typ(node.cond_type))
@ -3611,11 +3604,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) {
} }
if node.cond is ast.Ident || node.cond is ast.SelectorExpr || node.cond is ast.IntegerLiteral if node.cond is ast.Ident || node.cond is ast.SelectorExpr || node.cond is ast.IntegerLiteral
|| node.cond is ast.StringLiteral || node.cond is ast.FloatLiteral { || node.cond is ast.StringLiteral || node.cond is ast.FloatLiteral {
pos := g.out.len cond_var = g.expr_string(node.cond)
g.expr(node.cond)
cond_var = g.out.after(pos)
g.out.go_back(cond_var.len)
cond_var = cond_var.trim_space()
} else { } else {
line := if is_expr { line := if is_expr {
g.empty_line = true g.empty_line = true
@ -5392,10 +5381,7 @@ fn (mut g Gen) type_default(typ_ table.Type) string {
field_sym := g.table.get_type_symbol(field.typ) field_sym := g.table.get_type_symbol(field.typ)
if field_sym.kind in [.array, .map] || field.has_default_expr { if field_sym.kind in [.array, .map] || field.has_default_expr {
if field.has_default_expr { if field.has_default_expr {
pos := g.out.len expr_str := g.expr_string(ast.fe2ex(field.default_expr))
g.expr(ast.fe2ex(field.default_expr))
expr_str := g.out.after(pos)
g.out.go_back(expr_str.len)
init_str += '.$field.name = $expr_str,' init_str += '.$field.name = $expr_str,'
} else { } else {
init_str += '.$field.name = ${g.type_default(field.typ)},' init_str += '.$field.name = ${g.type_default(field.typ)},'