cgen: remove redundant code for array.map/filter(anon_fn)

pull/5338/head
yuyi 2020-06-10 23:25:54 +08:00 committed by GitHub
parent 1254d7a302
commit b6e2495aa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 27 deletions

View File

@ -1357,18 +1357,22 @@ fn (g &Gen) autofree_var_call(free_fn_name string, v ast.Var) string {
} }
} }
fn (mut g Gen) gen_anon_fn_decl(it ast.AnonFn) {
pos := g.out.len
def_pos := g.definitions.len
g.stmt(it.decl)
fn_body := g.out.after(pos)
g.out.go_back(fn_body.len)
g.definitions.go_back(g.definitions.len - def_pos)
g.definitions.write(fn_body)
}
fn (mut g Gen) expr(node ast.Expr) { fn (mut g Gen) expr(node ast.Expr) {
// println('cgen expr() line_nr=$node.pos.line_nr') // println('cgen expr() line_nr=$node.pos.line_nr')
match node { match node {
ast.AnonFn { ast.AnonFn {
// TODO: dont fiddle with buffers // TODO: dont fiddle with buffers
pos := g.out.len g.gen_anon_fn_decl(it)
def_pos := g.definitions.len
g.stmt(it.decl)
fn_body := g.out.after(pos)
g.out.go_back(fn_body.len)
g.definitions.go_back(g.definitions.len - def_pos)
g.definitions.write(fn_body)
fsym := g.table.get_type_symbol(it.typ) fsym := g.table.get_type_symbol(it.typ)
g.write('&${fsym.name}') g.write('&${fsym.name}')
} }
@ -3189,6 +3193,10 @@ fn (mut g Gen) gen_map(node ast.CallExpr) {
g.writeln('.data)[$i];') g.writeln('.data)[$i];')
g.write('\t$ret_elem_type ti = ') g.write('\t$ret_elem_type ti = ')
match node.args[0].expr { match node.args[0].expr {
ast.AnonFn {
g.gen_anon_fn_decl(it)
g.write('${it.decl.name}(it)')
}
ast.Ident { ast.Ident {
if it.kind == .function { if it.kind == .function {
g.write('${it.name}(it)') g.write('${it.name}(it)')
@ -3204,16 +3212,6 @@ fn (mut g Gen) gen_map(node ast.CallExpr) {
g.expr(node.args[0].expr) g.expr(node.args[0].expr)
} }
} }
ast.AnonFn {
pos := g.out.len
def_pos := g.definitions.len
g.stmt(it.decl)
fn_body := g.out.after(pos)
g.out.go_back(fn_body.len)
g.definitions.go_back(g.definitions.len - def_pos)
g.definitions.write(fn_body)
g.write('${it.decl.name}(it)')
}
else { else {
g.expr(node.args[0].expr) g.expr(node.args[0].expr)
} }
@ -3247,6 +3245,10 @@ fn (mut g Gen) gen_filter(node ast.CallExpr) {
g.writeln('.data)[i];') g.writeln('.data)[i];')
g.write('if (') g.write('if (')
match node.args[0].expr { match node.args[0].expr {
ast.AnonFn {
g.gen_anon_fn_decl(it)
g.write('${it.decl.name}(it)')
}
ast.Ident { ast.Ident {
if it.kind == .function { if it.kind == .function {
g.write('${it.name}(it)') g.write('${it.name}(it)')
@ -3262,16 +3264,6 @@ fn (mut g Gen) gen_filter(node ast.CallExpr) {
g.expr(node.args[0].expr) g.expr(node.args[0].expr)
} }
} }
ast.AnonFn {
pos := g.out.len
def_pos := g.definitions.len
g.stmt(it.decl)
fn_body := g.out.after(pos)
g.out.go_back(fn_body.len)
g.definitions.go_back(g.definitions.len - def_pos)
g.definitions.write(fn_body)
g.write('${it.decl.name}(it)')
}
else { else {
g.expr(node.args[0].expr) g.expr(node.args[0].expr)
} }