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:
pos token.Position
exprs []Expr
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) {
match node {
ast.FnDecl {
@ -215,9 +231,7 @@ fn (c &Checker) expr(node ast.Expr) {
c.check_method_call_expr(it)
}
ast.ArrayInit {
for expr in it.exprs {
c.expr(expr)
}
c.check_array_init(it)
}
// ast.Ident {}
// ast.BoolLiteral {}

View File

@ -107,9 +107,6 @@ fn (g mut Gen) stmt(node ast.Stmt) {
mut ti := it.ti
if ti.kind == .unresolved {
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.expr(it.expr)

View File

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