gen: move helper fns to ast

pull/4053/head
Joe Conigliaro 2020-03-18 11:19:23 +11:00
parent 6ca47aeb4b
commit 3fecf154aa
4 changed files with 33 additions and 38 deletions

View File

@ -640,3 +640,30 @@ enum BinaryOp {
or_bool or_bool
} }
*/ */
[inline]
pub fn expr_is_blank_ident(expr Expr) bool {
match expr {
Ident {
return it.kind == .blank_ident
}
else {
return false
}
}
}
[inline]
pub fn expr_is_call(expr Expr) bool {
return match expr {
CallExpr {
true
}
MethodCallExpr {
true
}
else {
false
}
}
}

View File

@ -154,14 +154,9 @@ pub fn (c mut Checker) infix_expr(infix_expr mut ast.InfixExpr) table.Type {
} }
fn (c mut Checker) assign_expr(assign_expr mut ast.AssignExpr) { fn (c mut Checker) assign_expr(assign_expr mut ast.AssignExpr) {
match assign_expr.left { if ast.expr_is_blank_ident(assign_expr.left) {
ast.Ident {
if it.kind == .blank_ident {
return return
} }
}
else {}
}
left_type := c.expr(assign_expr.left) left_type := c.expr(assign_expr.left)
c.expected_type = left_type c.expected_type = left_type
assign_expr.left_type = left_type assign_expr.left_type = left_type

View File

@ -401,7 +401,7 @@ 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 {
if is_call(val) { if ast.expr_is_call(val) {
g.expr(val) g.expr(val)
} }
else { else {
@ -562,8 +562,8 @@ fn (g mut Gen) expr(node ast.Expr) {
g.write('/* as */') g.write('/* as */')
} }
ast.AssignExpr { ast.AssignExpr {
if is_blank_ident(it.left) { if ast.expr_is_blank_ident(it.left) {
if is_call(it.val) { if ast.expr_is_call(it.val) {
g.expr(it.val) g.expr(it.val)
} }
else { else {
@ -1335,33 +1335,6 @@ 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)

View File

@ -406,7 +406,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
} }
} }
// TODO: merge wtih AssignStmt & VarDecl // TODO: is it possible to merge with AssignStmt?
pub fn (p mut Parser) assign_expr(left ast.Expr) ast.AssignExpr { pub fn (p mut Parser) assign_expr(left ast.Expr) ast.AssignExpr {
op := p.tok.kind op := p.tok.kind
p.next() p.next()