v2: tidy checker remove old comments & code
parent
d501ea0afb
commit
ed763df42e
|
@ -23,9 +23,6 @@ mut:
|
||||||
errors []string
|
errors []string
|
||||||
expected_type table.Type
|
expected_type table.Type
|
||||||
fn_return_type table.Type // current function's return type
|
fn_return_type table.Type // current function's return type
|
||||||
// TODO: remove once all exprs/stmts are handled
|
|
||||||
unhandled_exprs []string
|
|
||||||
unhandled_stmts []string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new_checker(table &table.Table) Checker {
|
pub fn new_checker(table &table.Table) Checker {
|
||||||
|
@ -72,7 +69,6 @@ pub fn (c mut Checker) check_files(ast_files []ast.File) {
|
||||||
for file in ast_files {
|
for file in ast_files {
|
||||||
c.check(file)
|
c.check(file)
|
||||||
}
|
}
|
||||||
c.print_unhandled_nodes()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (c mut Checker) check_struct_init(struct_init ast.StructInit) table.Type {
|
pub fn (c mut Checker) check_struct_init(struct_init ast.StructInit) table.Type {
|
||||||
|
@ -141,9 +137,6 @@ pub fn (c mut Checker) infix_expr(infix_expr ast.InfixExpr) table.Type {
|
||||||
if right.kind in [.array, .map] && infix_expr.op == .key_in {
|
if right.kind in [.array, .map] && infix_expr.op == .key_in {
|
||||||
return table.bool_type
|
return table.bool_type
|
||||||
}
|
}
|
||||||
// if !c.table.check(&infix_expr.right_type, &infix_expr.right_type) {
|
|
||||||
// c.error('infix expr: cannot use `$infix_expr.right_type.name` as `$infix_expr.left_type.name`', infix_expr.pos)
|
|
||||||
// ltyp := typeof(infix_expr.left)
|
|
||||||
c.error('infix expr: cannot use `$right.name` (right) as `$left.name`', infix_expr.pos)
|
c.error('infix expr: cannot use `$right.name` (right) as `$left.name`', infix_expr.pos)
|
||||||
}
|
}
|
||||||
if infix_expr.op.is_relational() {
|
if infix_expr.op.is_relational() {
|
||||||
|
@ -424,10 +417,8 @@ fn (c mut Checker) stmt(node ast.Stmt) {
|
||||||
mut field := it.fields[i]
|
mut field := it.fields[i]
|
||||||
typ := c.expr(expr)
|
typ := c.expr(expr)
|
||||||
mut xconst := c.table.consts[field.name]
|
mut xconst := c.table.consts[field.name]
|
||||||
// if xconst.typ == 0 {
|
|
||||||
xconst.typ = typ
|
xconst.typ = typ
|
||||||
c.table.consts[field.name] = xconst
|
c.table.consts[field.name] = xconst
|
||||||
// }
|
|
||||||
field.typ = typ
|
field.typ = typ
|
||||||
it.fields[i] = field
|
it.fields[i] = field
|
||||||
}
|
}
|
||||||
|
@ -443,8 +434,6 @@ fn (c mut Checker) stmt(node ast.Stmt) {
|
||||||
}
|
}
|
||||||
ast.ForStmt {
|
ast.ForStmt {
|
||||||
typ := c.expr(it.cond)
|
typ := c.expr(it.cond)
|
||||||
// typ_sym := c.table.get_type_symbol(typ)
|
|
||||||
// if typ_sym.kind != .bool {
|
|
||||||
if !it.is_inf && table.type_idx(typ) != table.bool_type_idx {
|
if !it.is_inf && table.type_idx(typ) != table.bool_type_idx {
|
||||||
c.error('non-bool used as for condition', it.pos)
|
c.error('non-bool used as for condition', it.pos)
|
||||||
}
|
}
|
||||||
|
@ -470,23 +459,12 @@ fn (c mut Checker) stmt(node ast.Stmt) {
|
||||||
// ast.StructDecl {}
|
// ast.StructDecl {}
|
||||||
ast.VarDecl {
|
ast.VarDecl {
|
||||||
typ := c.expr(it.expr)
|
typ := c.expr(it.expr)
|
||||||
// typ_sym := c.table.get_type_symbol(typ)
|
|
||||||
// println('var $it.name - $typ - $it.typ - $typ_sym.name')
|
|
||||||
// if it.typ == 0 {
|
|
||||||
// it.typ = typ
|
|
||||||
// }
|
|
||||||
it.typ = typ
|
it.typ = typ
|
||||||
}
|
}
|
||||||
else {}
|
else {
|
||||||
// println('checker.stmt(): unhandled node')
|
// println('checker.stmt(): unhandled node')
|
||||||
/*
|
// println('checker.stmt(): unhandled node (${typeof(node)})')
|
||||||
println('1')
|
|
||||||
node_name := typeof(node)
|
|
||||||
println('2')
|
|
||||||
if !(node_name in c.unhandled_stmts) {
|
|
||||||
c.unhandled_stmts << node_name
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -594,10 +572,11 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
else {}
|
else {
|
||||||
|
// println('checker.expr(): unhandled node')
|
||||||
// TODO: find nil string bug triggered with typeof
|
// TODO: find nil string bug triggered with typeof
|
||||||
// node_name := typeof(node)
|
// println('checker.expr(): unhandled node (${typeof(node)})')
|
||||||
// if !(node_name) in c.unhandled_exprs { c.unhandled_exprs << node_name }
|
}
|
||||||
}
|
}
|
||||||
return table.void_type
|
return table.void_type
|
||||||
}
|
}
|
||||||
|
@ -739,13 +718,9 @@ pub fn (c mut Checker) if_expr(node mut ast.IfExpr) table.Type {
|
||||||
ast.ExprStmt {
|
ast.ExprStmt {
|
||||||
// type_sym := p.table.get_type_symbol(it.typ)
|
// type_sym := p.table.get_type_symbol(it.typ)
|
||||||
// p.warn('if expr ret $type_sym.name')
|
// p.warn('if expr ret $type_sym.name')
|
||||||
// typ = it.typ
|
|
||||||
// return it.typ
|
|
||||||
t := c.expr(it.expr)
|
t := c.expr(it.expr)
|
||||||
node.typ = t
|
node.typ = t
|
||||||
return t
|
return t
|
||||||
// return node,it.ti
|
|
||||||
// left =
|
|
||||||
}
|
}
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
|
@ -774,14 +749,6 @@ pub fn (c mut Checker) postfix_expr(node ast.PostfixExpr) table.Type {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn (c mut Checker) index_expr(node ast.IndexExpr) table.Type {
|
pub fn (c mut Checker) index_expr(node ast.IndexExpr) table.Type {
|
||||||
/*
|
|
||||||
mut typ := left_type
|
|
||||||
left_type_sym := p.table.get_type_symbol(left_type)
|
|
||||||
if left_type_sym.kind == .array {
|
|
||||||
info := left_type_sym.info as table.Array
|
|
||||||
typ = info.elem_type
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
typ := c.expr(node.left)
|
typ := c.expr(node.left)
|
||||||
mut is_range := false // TODO is_range := node.index is ast.RangeExpr
|
mut is_range := false // TODO is_range := node.index is ast.RangeExpr
|
||||||
match node.index {
|
match node.index {
|
||||||
|
@ -879,16 +846,6 @@ pub fn (c mut Checker) map_init(node mut ast.MapInit) table.Type {
|
||||||
return map_type
|
return map_type
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: remove once all exprs/stmts are handled
|
|
||||||
pub fn (c &Checker) print_unhandled_nodes() {
|
|
||||||
if c.unhandled_exprs.len > 0 {
|
|
||||||
eprintln(' # unhandled Expr nodes:\n\t * ' + c.unhandled_exprs.join(', ') + '\n')
|
|
||||||
}
|
|
||||||
if c.unhandled_stmts.len > 0 {
|
|
||||||
eprintln(' # unhandled Stmt nodes:\n\t * ' + c.unhandled_stmts.join(', ') + '\n')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn (c mut Checker) error(s string, pos token.Position) {
|
pub fn (c mut Checker) error(s string, pos token.Position) {
|
||||||
c.nr_errors++
|
c.nr_errors++
|
||||||
print_backtrace()
|
print_backtrace()
|
||||||
|
@ -911,7 +868,6 @@ pub fn (c mut Checker) error(s string, pos token.Position) {
|
||||||
|
|
||||||
println('\n\n')
|
println('\n\n')
|
||||||
if c.nr_errors >= max_nr_errors {
|
if c.nr_errors >= max_nr_errors {
|
||||||
c.print_unhandled_nodes()
|
|
||||||
exit(1)
|
exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue