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
}
*/
[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,13 +154,8 @@ 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) {
match assign_expr.left {
ast.Ident {
if it.kind == .blank_ident {
return
}
}
else {}
if ast.expr_is_blank_ident(assign_expr.left) {
return
}
left_type := c.expr(assign_expr.left)
c.expected_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()
styp := g.typ(ident_var_info.typ)
if ident.kind == .blank_ident {
if is_call(val) {
if ast.expr_is_call(val) {
g.expr(val)
}
else {
@ -562,8 +562,8 @@ fn (g mut Gen) expr(node ast.Expr) {
g.write('/* as */')
}
ast.AssignExpr {
if is_blank_ident(it.left) {
if is_call(it.val) {
if ast.expr_is_blank_ident(it.left) {
if ast.expr_is_call(it.val) {
g.expr(it.val)
}
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) {
println('cgen error: $s')
// 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 {
op := p.tok.kind
p.next()