v2: move array_init to checker

pull/3504/head
joe-conigliaro 2020-01-19 23:52:34 +11:00 committed by Alexander Medvednikov
parent cf1fd6e950
commit 3a8b437b8d
4 changed files with 21 additions and 14 deletions

View File

@ -285,6 +285,7 @@ pub:
pub struct ArrayInit { pub struct ArrayInit {
pub: pub:
pos token.Position
exprs []Expr exprs []Expr
ti types.TypeIdent ti types.TypeIdent
} }

View File

@ -150,6 +150,22 @@ pub fn (c &Checker) check_return_stmt(return_stmt ast.Return) {
} }
} }
pub fn (c &Checker) check_array_init(array_init ast.ArrayInit) {
mut val_ti := types.void_ti
for i, expr in array_init.exprs {
c.expr(expr)
ti := c.table.get_expr_ti(expr)
// The first element's type
if i == 0 {
val_ti = ti
continue
}
if !c.table.check(val_ti, ti) {
c.error('expected array element with type `$val_ti.name`', array_init.pos)
}
}
}
fn (c &Checker) stmt(node ast.Stmt) { fn (c &Checker) stmt(node ast.Stmt) {
match node { match node {
ast.FnDecl { ast.FnDecl {
@ -215,9 +231,7 @@ fn (c &Checker) expr(node ast.Expr) {
c.check_method_call_expr(it) c.check_method_call_expr(it)
} }
ast.ArrayInit { ast.ArrayInit {
for expr in it.exprs { c.check_array_init(it)
c.expr(expr)
}
} }
// ast.Ident {} // ast.Ident {}
// ast.BoolLiteral {} // ast.BoolLiteral {}

View File

@ -107,9 +107,6 @@ fn (g mut Gen) stmt(node ast.Stmt) {
mut ti := it.ti mut ti := it.ti
if ti.kind == .unresolved { if ti.kind == .unresolved {
ti = g.table.get_expr_ti(it.expr) ti = g.table.get_expr_ti(it.expr)
// println('A $it.ti.name')
// println('B $ti.name')
// panic("############# UNRESOLVED")
} }
g.write('$ti.name $it.name = ') g.write('$ti.name $it.name = ')
g.expr(it.expr) g.expr(it.expr)

View File

@ -661,18 +661,12 @@ fn (p mut Parser) array_init() (ast.Expr,types.TypeIdent) {
p.check(.lsbr) p.check(.lsbr)
mut val_ti := types.void_ti mut val_ti := types.void_ti
mut exprs := []ast.Expr mut exprs := []ast.Expr
mut i := 0 for i:=0; p.tok.kind != .rsbr; i++ {
for p.tok.kind != .rsbr {
expr,ti := p.expr(0) expr,ti := p.expr(0)
// The first element's type exprs << expr
if i == 0 { if i == 0 {
val_ti = ti val_ti = ti
} }
else if !p.table.check(val_ti, ti) {
p.error('expected array element with type `$val_ti.name`')
}
exprs << expr
i++
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.check(.comma)
} }
@ -683,6 +677,7 @@ fn (p mut Parser) array_init() (ast.Expr,types.TypeIdent) {
node = ast.ArrayInit{ node = ast.ArrayInit{
ti: array_ti ti: array_ti
exprs: exprs exprs: exprs
pos: p.tok.position()
} }
p.check(.rsbr) p.check(.rsbr)
return node,array_ti return node,array_ti