checker: fix Assoc

pull/3884/head
Alexander Medvednikov 2020-02-29 18:07:29 +01:00
parent 85f67a3f73
commit 27ce38937c
5 changed files with 26 additions and 16 deletions

View File

@ -513,9 +513,10 @@ pub:
pub struct Assoc { pub struct Assoc {
pub: pub:
name string var_name string
fields []string fields []string
exprs []Expr exprs []Expr
pos token.Position
} }
pub struct SizeOf { pub struct SizeOf {

View File

@ -431,6 +431,15 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
ast.AssignExpr { ast.AssignExpr {
c.check_assign_expr(it) c.check_assign_expr(it)
} }
ast.Assoc {
scope := c.file.scope.innermost(it.pos.pos) or {
c.file.scope
}
var := scope.find_var(it.var_name) or {
panic(err)
}
return var.typ
}
ast.EnumVal { ast.EnumVal {
return c.enum_val(it) return c.enum_val(it)
} }
@ -733,7 +742,6 @@ pub fn (c mut Checker) enum_val(node ast.EnumVal) table.Type {
typ_idx := if node.enum_name == '' { c.expected_type } else { // typ_idx := if node.enum_name == '' { c.expected_type } else { //
c.table.find_type_idx(node.enum_name) } c.table.find_type_idx(node.enum_name) }
typ := c.table.get_type_symbol(table.Type(typ_idx)) typ := c.table.get_type_symbol(table.Type(typ_idx))
// info := typ.info as table.Enum // info := typ.info as table.Enum
info := typ.enum_info() info := typ.enum_info()
// rintln('checker: x = $info.x enum val $c.expected_type $typ.name') // rintln('checker: x = $info.x enum val $c.expected_type $typ.name')

View File

@ -310,7 +310,7 @@ fn (f mut Fmt) expr(node ast.Expr) {
ast.Assoc { ast.Assoc {
f.writeln('{') f.writeln('{')
// f.indent++ // f.indent++
f.writeln('\t$it.name |') f.writeln('\t$it.var_name |')
// TODO StructInit copy pasta // TODO StructInit copy pasta
for i, field in it.fields { for i, field in it.fields {
f.write('\t$field: ') f.write('\t$field: ')

View File

@ -793,9 +793,10 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
} }
} }
node = ast.Assoc{ node = ast.Assoc{
name: name var_name: name
fields: fields fields: fields
exprs: vals exprs: vals
pos:p.tok.position()
} }
} }
p.check(.rcbr) p.check(.rcbr)

View File

@ -76,7 +76,7 @@ x := 10
8+4 8+4
' '
table := &table.Table{} table := &table.Table{}
prog := parse_file(s, table) prog := parse_file(s, table, .skip_comments)
mut checker := checker.new_checker(table) mut checker := checker.new_checker(table)
checker.check(prog) checker.check(prog)
res := gen.cgen([prog], table) res := gen.cgen([prog], table)