2020-01-23 21:04:46 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2020-01-18 23:26:14 +01:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module checker
|
|
|
|
|
|
|
|
import (
|
|
|
|
v.ast
|
|
|
|
v.table
|
|
|
|
v.token
|
2020-02-07 07:34:18 +01:00
|
|
|
os
|
2020-01-18 23:26:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
pub struct Checker {
|
2020-02-07 18:46:42 +01:00
|
|
|
table &table.Table
|
2020-01-18 23:26:14 +01:00
|
|
|
mut:
|
2020-02-07 18:46:42 +01:00
|
|
|
file_name string
|
|
|
|
resolved []table.TypeRef
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_checker(table &table.Table) Checker {
|
|
|
|
return Checker{
|
|
|
|
table: table
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 12:03:12 +01:00
|
|
|
pub fn (c mut Checker) check(ast_file ast.File) {
|
|
|
|
c.file_name = ast_file.path
|
2020-02-07 18:46:42 +01:00
|
|
|
// if ast_file.unresolved.len != c.resolved.len {
|
|
|
|
// c.resolve_exprs(file)
|
|
|
|
// }
|
2020-02-08 09:50:12 +01:00
|
|
|
c.complete_types()
|
2020-01-18 23:26:14 +01:00
|
|
|
for stmt in ast_file.stmts {
|
|
|
|
c.stmt(stmt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 07:31:54 +01:00
|
|
|
pub fn (c mut Checker) check_files(ast_files []ast.File) {
|
2020-02-07 18:46:42 +01:00
|
|
|
// this cant be moved to check() for multiple
|
|
|
|
// files this muse be done first. TODO: optimize
|
|
|
|
for file in ast_files {
|
|
|
|
c.file_name = file.path
|
2020-02-08 09:50:12 +01:00
|
|
|
c.resolve_expr_types(file.unresolved)
|
2020-02-07 18:46:42 +01:00
|
|
|
}
|
2020-02-03 07:31:54 +01:00
|
|
|
for file in ast_files {
|
2020-01-18 23:26:14 +01:00
|
|
|
c.check(file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-07 18:46:42 +01:00
|
|
|
// resolve type of unresolved expressions
|
2020-02-08 09:50:12 +01:00
|
|
|
fn (c mut Checker) resolve_expr_types(exprs []ast.Expr) {
|
|
|
|
for x in exprs {
|
2020-02-04 12:03:12 +01:00
|
|
|
c.resolved << c.expr(x)
|
|
|
|
}
|
2020-02-07 18:46:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// update any types chich contain unresolved sub types
|
2020-02-08 09:50:12 +01:00
|
|
|
fn (c &Checker) complete_types() {
|
2020-02-06 13:57:35 +01:00
|
|
|
for idx, t in c.table.types {
|
2020-02-07 18:46:42 +01:00
|
|
|
// println('Resolve type: $t.name')
|
2020-02-06 13:57:35 +01:00
|
|
|
if t.kind == .array {
|
2020-02-06 17:38:02 +01:00
|
|
|
mut info := t.array_info()
|
2020-02-06 13:57:35 +01:00
|
|
|
if info.elem_type.typ.kind == .unresolved {
|
|
|
|
info.elem_type = c.resolved[info.elem_type.idx]
|
|
|
|
mut t1 := &c.table.types[idx]
|
|
|
|
t1.name = table.array_name(&info.elem_type, info.nr_dims)
|
|
|
|
t1.info = info
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if t.kind == .map {
|
2020-02-06 17:38:02 +01:00
|
|
|
mut info := t.map_info()
|
2020-02-06 13:57:35 +01:00
|
|
|
mut updated := false
|
|
|
|
if info.key_type.typ.kind == .unresolved {
|
|
|
|
info.key_type = c.resolved[info.key_type.idx]
|
|
|
|
updated = true
|
|
|
|
}
|
|
|
|
if info.value_type.typ.kind == .unresolved {
|
|
|
|
info.value_type = c.resolved[info.value_type.idx]
|
|
|
|
updated = true
|
|
|
|
}
|
|
|
|
if updated {
|
|
|
|
mut t1 := &c.table.types[idx]
|
|
|
|
t1.name = table.map_name(&info.key_type, &info.value_type)
|
|
|
|
t1.info = info
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 17:38:02 +01:00
|
|
|
else if t.kind == .multi_return {
|
|
|
|
mut info := t.mr_info()
|
|
|
|
mut types := info.types
|
|
|
|
mut updated := false
|
|
|
|
for i, ut in types {
|
|
|
|
if ut.typ.kind == .unresolved {
|
|
|
|
types[i] = c.resolved[ut.idx]
|
|
|
|
updated = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if updated {
|
|
|
|
mut t1 := &c.table.types[idx]
|
|
|
|
info.types = types
|
|
|
|
t1.info = info
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
}
|
2020-02-04 12:03:12 +01:00
|
|
|
}
|
|
|
|
|
2020-02-06 13:57:35 +01:00
|
|
|
pub fn (c &Checker) check_struct_init(struct_init ast.StructInit) table.TypeRef {
|
|
|
|
typ := c.table.find_type(struct_init.typ.typ.name) or {
|
|
|
|
c.error('unknown struct: $struct_init.typ.typ.name', struct_init.pos)
|
2020-01-18 23:26:14 +01:00
|
|
|
panic('')
|
|
|
|
}
|
|
|
|
match typ.kind {
|
|
|
|
.placeholder {
|
2020-02-06 13:57:35 +01:00
|
|
|
c.error('unknown struct: $struct_init.typ.typ.name', struct_init.pos)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
.struct_ {
|
2020-01-22 21:34:38 +01:00
|
|
|
info := typ.info as table.Struct
|
2020-01-18 23:26:14 +01:00
|
|
|
for i, expr in struct_init.exprs {
|
|
|
|
field := info.fields[i]
|
2020-01-22 21:34:38 +01:00
|
|
|
field_type := c.expr(expr)
|
2020-02-06 13:57:35 +01:00
|
|
|
if !c.table.check(field_type, field.typ) {
|
|
|
|
c.error('cannot assign $field_type.typ.name as $field.typ.typ.name for field $field.name', struct_init.pos)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
return struct_init.typ
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
|
2020-02-06 13:57:35 +01:00
|
|
|
pub fn (c &Checker) infix_expr(infix_expr ast.InfixExpr) table.TypeRef {
|
|
|
|
left_type := c.expr(infix_expr.left)
|
|
|
|
right_type := c.expr(infix_expr.right)
|
|
|
|
if !c.table.check(&right_type, &left_type) {
|
2020-01-22 21:34:38 +01:00
|
|
|
// 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)
|
2020-02-06 13:57:35 +01:00
|
|
|
c.error('infix expr: cannot use `$left_type.typ.name` as `$right_type.typ.name`', infix_expr.pos)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
if infix_expr.op.is_relational() {
|
2020-02-06 13:57:35 +01:00
|
|
|
return c.table.type_ref(table.bool_type_idx)
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
return left_type
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (c &Checker) check_assign_expr(assign_expr ast.AssignExpr) {
|
2020-02-06 13:57:35 +01:00
|
|
|
left_type := c.expr(assign_expr.left)
|
|
|
|
right_type := c.expr(assign_expr.val)
|
|
|
|
if !c.table.check(right_type, left_type) {
|
|
|
|
c.error('cannot assign $right_type.typ.name to $left_type.typ.name', assign_expr.pos)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:57:35 +01:00
|
|
|
pub fn (c &Checker) call_expr(call_expr ast.CallExpr) table.TypeRef {
|
2020-01-18 23:26:14 +01:00
|
|
|
fn_name := call_expr.name
|
|
|
|
if f := c.table.find_fn(fn_name) {
|
|
|
|
// return_ti := f.return_ti
|
2020-02-07 07:34:18 +01:00
|
|
|
if !f.is_c {
|
|
|
|
if call_expr.args.len < f.args.len {
|
|
|
|
c.error('too few arguments in call to `$fn_name`', call_expr.pos)
|
|
|
|
}
|
|
|
|
else if call_expr.args.len > f.args.len {
|
2020-02-07 21:29:28 +01:00
|
|
|
c.error('too many arguments in call to `$fn_name` ($call_expr.args.len instead of $f.args.len)', call_expr.pos)
|
2020-02-07 07:34:18 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-08 09:50:12 +01:00
|
|
|
// for debugging
|
|
|
|
if f.name == 'backtrace_symbols_fd' {
|
|
|
|
println('ARGS FOR: backtrace_symbols_fd:')
|
|
|
|
for i, arg_expr in call_expr.args {
|
|
|
|
typ := c.expr(arg_expr)
|
|
|
|
println(' -- $i - $typ.typ.name')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// TODO: variadic
|
|
|
|
if fn_name != 'printf' && f.args.len > 0 {
|
2020-01-18 23:26:14 +01:00
|
|
|
for i, arg in f.args {
|
|
|
|
arg_expr := call_expr.args[i]
|
2020-02-06 13:57:35 +01:00
|
|
|
typ := c.expr(arg_expr)
|
|
|
|
if !c.table.check(&typ, &arg.typ) {
|
|
|
|
c.error('!cannot use type `$typ.typ.name` as type `$arg.typ.typ.name` in argument to `$fn_name`', call_expr.pos)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-08 09:50:12 +01:00
|
|
|
}
|
2020-01-22 21:34:38 +01:00
|
|
|
return f.return_type
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-04 12:03:12 +01:00
|
|
|
c.error('unknown fn: $fn_name', call_expr.pos)
|
|
|
|
exit(1)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
|
2020-02-06 13:57:35 +01:00
|
|
|
pub fn (c &Checker) check_method_call_expr(method_call_expr ast.MethodCallExpr) table.TypeRef {
|
|
|
|
typ := c.expr(method_call_expr.expr)
|
|
|
|
if method := typ.typ.find_method(method_call_expr.name) {
|
2020-02-04 12:03:12 +01:00
|
|
|
return method.return_type
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-08 09:50:12 +01:00
|
|
|
// check parent
|
|
|
|
if !isnil(typ.typ.parent) {
|
|
|
|
if method := typ.typ.parent.find_method(method_call_expr.name) {
|
2020-02-07 09:19:45 +01:00
|
|
|
return method.return_type
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
c.error('type `$typ.typ.name` has no method `$method_call_expr.name`', method_call_expr.pos)
|
2020-02-04 12:03:12 +01:00
|
|
|
exit(1)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
|
2020-02-06 13:57:35 +01:00
|
|
|
pub fn (c &Checker) selector_expr(selector_expr ast.SelectorExpr) table.TypeRef {
|
|
|
|
typ := c.expr(selector_expr.expr)
|
2020-01-18 23:26:14 +01:00
|
|
|
field_name := selector_expr.field
|
2020-02-06 13:57:35 +01:00
|
|
|
match typ.typ.kind {
|
2020-01-18 23:26:14 +01:00
|
|
|
.struct_ {
|
2020-02-06 13:57:35 +01:00
|
|
|
field := c.table.struct_find_field(typ.typ, field_name) or {
|
|
|
|
c.error('unknown field `${typ.typ.name}.$field_name`', selector_expr.pos)
|
2020-01-22 21:34:38 +01:00
|
|
|
exit(0)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
return field.typ
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-02-08 09:50:12 +01:00
|
|
|
// types with parent struct (array/maps) handled here
|
|
|
|
if !isnil(typ.typ.parent) && typ.typ.parent.kind == .struct_ {
|
|
|
|
parent := typ.typ.parent
|
|
|
|
if field := c.table.struct_find_field(parent, field_name) {
|
|
|
|
if field.typ.typ.kind == .unresolved {
|
|
|
|
return c.resolved[field.typ.idx]
|
|
|
|
}
|
|
|
|
return field.typ
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
c.error('`$typ.typ.name` is not a struct', selector_expr.pos)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
return c.table.type_ref(table.void_type_idx)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: non deferred
|
2020-01-22 21:34:38 +01:00
|
|
|
pub fn (c &Checker) return_stmt(return_stmt ast.Return) {
|
2020-02-06 13:57:35 +01:00
|
|
|
mut got_types := []table.TypeRef
|
2020-01-22 21:34:38 +01:00
|
|
|
if return_stmt.exprs.len == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
for expr in return_stmt.exprs {
|
2020-02-06 13:57:35 +01:00
|
|
|
typ := c.expr(expr)
|
|
|
|
got_types << typ
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
expected_type := return_stmt.expected_type
|
|
|
|
mut expected_types := [expected_type]
|
|
|
|
if expected_type.typ.kind == .multi_return {
|
|
|
|
mr_info := expected_type.typ.info as table.MultiReturn
|
|
|
|
expected_types = mr_info.types
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
if expected_types.len > 0 && expected_types.len != got_types.len {
|
|
|
|
c.error('wrong number of return arguments:\n\texpected: $expected_types.str()\n\tgot: $got_types.str()', return_stmt.pos)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
for i, exp_typ in expected_types {
|
|
|
|
got_typ := got_types[i]
|
|
|
|
if !c.table.check(got_typ, exp_typ) {
|
|
|
|
c.error('cannot use `$got_typ.typ.name` as type `$exp_typ.typ.name` in return argument', return_stmt.pos)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 17:38:02 +01:00
|
|
|
pub fn (c &Checker) assign_stmt(assign_stmt ast.AssignStmt) {}
|
|
|
|
|
2020-02-06 13:57:35 +01:00
|
|
|
pub fn (c &Checker) array_init(array_init ast.ArrayInit) table.TypeRef {
|
|
|
|
mut elem_type := c.table.type_ref(table.void_type_idx)
|
2020-01-19 13:52:34 +01:00
|
|
|
for i, expr in array_init.exprs {
|
|
|
|
c.expr(expr)
|
2020-02-06 13:57:35 +01:00
|
|
|
typ := c.expr(expr)
|
2020-01-19 13:52:34 +01:00
|
|
|
// The first element's type
|
|
|
|
if i == 0 {
|
2020-02-06 13:57:35 +01:00
|
|
|
elem_type = typ
|
2020-01-19 13:52:34 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
if !c.table.check(elem_type, typ) {
|
|
|
|
c.error('expected array element with type `$elem_type.typ.name`', array_init.pos)
|
2020-01-19 13:52:34 +01:00
|
|
|
}
|
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
return array_init.typ
|
2020-01-19 13:52:34 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 23:26:14 +01:00
|
|
|
fn (c &Checker) stmt(node ast.Stmt) {
|
2020-02-03 10:27:06 +01:00
|
|
|
match mut node {
|
2020-01-18 23:26:14 +01:00
|
|
|
ast.FnDecl {
|
|
|
|
for stmt in it.stmts {
|
|
|
|
c.stmt(stmt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ast.Return {
|
2020-01-22 21:34:38 +01:00
|
|
|
c.return_stmt(it)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-06 17:38:02 +01:00
|
|
|
ast.AssignStmt {
|
|
|
|
c.assign_stmt(it)
|
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
ast.VarDecl {
|
2020-01-22 21:34:38 +01:00
|
|
|
typ := c.expr(it.expr)
|
2020-02-03 09:11:10 +01:00
|
|
|
// println('checker: var decl $typ.name it.typ=$it.typ.name $it.pos.line_nr')
|
2020-02-06 13:57:35 +01:00
|
|
|
if typ.typ.kind != .void {
|
2020-02-03 09:11:10 +01:00
|
|
|
it.typ = typ
|
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
ast.ForStmt {
|
|
|
|
typ := c.expr(it.cond)
|
2020-02-06 13:57:35 +01:00
|
|
|
if typ.typ.kind != .bool {
|
2020-02-03 07:02:54 +01:00
|
|
|
c.error('non-bool used as for condition', it.pos)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
for stmt in it.stmts {
|
|
|
|
c.stmt(stmt)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
ast.ForCStmt {
|
|
|
|
c.stmt(it.init)
|
|
|
|
c.expr(it.cond)
|
|
|
|
c.stmt(it.inc)
|
|
|
|
for stmt in it.stmts {
|
|
|
|
c.stmt(stmt)
|
2020-02-02 14:31:54 +01:00
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
// ast.StructDecl {}
|
|
|
|
ast.ExprStmt {
|
|
|
|
c.expr(it.expr)
|
|
|
|
}
|
|
|
|
else {}
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
|
2020-02-06 13:57:35 +01:00
|
|
|
pub fn (c &Checker) expr(node ast.Expr) table.TypeRef {
|
|
|
|
match mut node {
|
2020-02-03 07:02:54 +01:00
|
|
|
ast.AssignExpr {
|
|
|
|
c.check_assign_expr(it)
|
|
|
|
}
|
|
|
|
ast.IntegerLiteral {
|
2020-02-06 13:57:35 +01:00
|
|
|
return c.table.type_ref(table.int_type_idx)
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
// ast.FloatLiteral {}
|
|
|
|
ast.PostfixExpr {
|
2020-02-04 07:37:38 +01:00
|
|
|
return c.postfix_expr(it)
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
/*
|
2020-01-18 23:26:14 +01:00
|
|
|
ast.UnaryExpr {
|
|
|
|
c.expr(it.left)
|
|
|
|
}
|
2020-01-22 21:34:38 +01:00
|
|
|
*/
|
|
|
|
|
2020-02-03 07:02:54 +01:00
|
|
|
ast.StringLiteral {
|
2020-02-06 13:57:35 +01:00
|
|
|
return c.table.type_ref(table.string_type_idx)
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
ast.PrefixExpr {
|
|
|
|
return c.expr(it.right)
|
|
|
|
}
|
|
|
|
ast.InfixExpr {
|
|
|
|
return c.infix_expr(it)
|
|
|
|
}
|
|
|
|
ast.StructInit {
|
|
|
|
return c.check_struct_init(it)
|
|
|
|
}
|
|
|
|
ast.CallExpr {
|
|
|
|
return c.call_expr(it)
|
|
|
|
}
|
|
|
|
ast.MethodCallExpr {
|
|
|
|
return c.check_method_call_expr(it)
|
|
|
|
}
|
|
|
|
ast.ArrayInit {
|
|
|
|
return c.array_init(it)
|
|
|
|
}
|
|
|
|
ast.Ident {
|
|
|
|
if it.kind == .variable {
|
2020-02-06 13:57:35 +01:00
|
|
|
mut info := it.info as ast.IdentVar
|
|
|
|
if info.typ.typ.kind == .unresolved {
|
|
|
|
typ := c.resolved[info.typ.idx]
|
|
|
|
info.typ = typ
|
|
|
|
it.info = info
|
|
|
|
return typ
|
2020-01-22 21:34:38 +01:00
|
|
|
}
|
2020-02-04 12:03:12 +01:00
|
|
|
return info.typ
|
2020-01-22 21:34:38 +01:00
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
return c.table.type_ref(table.void_type_idx)
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
ast.BoolLiteral {
|
2020-02-06 13:57:35 +01:00
|
|
|
return c.table.type_ref(table.bool_type_idx)
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
ast.SelectorExpr {
|
|
|
|
return c.selector_expr(it)
|
|
|
|
}
|
|
|
|
ast.IndexExpr {
|
2020-02-03 09:11:10 +01:00
|
|
|
return c.index_expr(it)
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
ast.IfExpr {
|
|
|
|
typ := c.expr(it.cond)
|
2020-02-06 13:57:35 +01:00
|
|
|
if typ.typ.kind != .bool {
|
|
|
|
c.error('non-bool (`$typ.typ.name`) used as if condition', it.pos)
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
for i, stmt in it.stmts {
|
|
|
|
c.stmt(stmt)
|
2020-02-02 14:31:54 +01:00
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
if it.else_stmts.len > 0 {
|
|
|
|
for stmt in it.else_stmts {
|
2020-01-18 23:26:14 +01:00
|
|
|
c.stmt(stmt)
|
|
|
|
}
|
|
|
|
}
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
|
|
|
else {}
|
2020-01-18 23:26:14 +01:00
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
return c.table.type_ref(table.void_type_idx)
|
2020-02-03 07:02:54 +01:00
|
|
|
}
|
2020-01-18 23:26:14 +01:00
|
|
|
|
2020-02-06 13:57:35 +01:00
|
|
|
pub fn (c &Checker) postfix_expr(node ast.PostfixExpr) table.TypeRef {
|
2020-02-04 07:37:38 +01:00
|
|
|
/*
|
|
|
|
match node.expr {
|
|
|
|
ast.IdentVar {
|
|
|
|
println('postfix identvar')
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
typ := c.expr(node.expr)
|
2020-02-06 13:57:35 +01:00
|
|
|
if typ.typ.kind != .int {
|
|
|
|
c.error('invalid operation: $node.op.str() (non-numeric type `$typ.typ.name`)', node.pos)
|
2020-02-04 07:37:38 +01:00
|
|
|
}
|
|
|
|
return typ
|
|
|
|
}
|
|
|
|
|
2020-02-06 13:57:35 +01:00
|
|
|
pub fn (c &Checker) index_expr(node ast.IndexExpr) table.TypeRef {
|
2020-02-03 09:11:10 +01:00
|
|
|
mut typ := c.expr(node.left)
|
2020-02-03 11:29:50 +01:00
|
|
|
mut is_range := false // TODO is_range := node.index is ast.RangeExpr
|
|
|
|
match node.index {
|
|
|
|
ast.RangeExpr {
|
|
|
|
is_range = true
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
if typ.typ.kind == .array {
|
2020-02-03 11:29:50 +01:00
|
|
|
if is_range {} // `x[start..end]` has the same type as `x`
|
|
|
|
else {
|
|
|
|
// Check index type
|
|
|
|
index_type := c.expr(node.index)
|
2020-02-06 13:57:35 +01:00
|
|
|
if index_type.typ.kind != .int {
|
|
|
|
c.error('non-integer index (type `$index_type.typ.name`)', node.pos)
|
2020-02-03 11:29:50 +01:00
|
|
|
}
|
2020-02-06 13:57:35 +01:00
|
|
|
info := typ.typ.info as table.Array
|
|
|
|
return info.elem_type
|
2020-02-03 09:11:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2020-02-06 13:57:35 +01:00
|
|
|
typ = c.table.type_ref(table.int_type_idx)
|
2020-02-03 09:11:10 +01:00
|
|
|
}
|
|
|
|
return typ
|
|
|
|
// c.expr(it.index)
|
|
|
|
// return it.typ
|
|
|
|
}
|
|
|
|
|
2020-02-03 07:02:54 +01:00
|
|
|
pub fn (c &Checker) error(s string, pos token.Position) {
|
|
|
|
print_backtrace()
|
2020-02-07 07:34:18 +01:00
|
|
|
mut path := c.file_name
|
|
|
|
// Get relative path
|
|
|
|
workdir := os.getwd() + os.path_separator
|
|
|
|
if path.starts_with(workdir) {
|
|
|
|
path = path.replace(workdir, '')
|
|
|
|
}
|
|
|
|
final_msg_line := '$path:$pos.line_nr: checker error: $s'
|
2020-02-08 09:50:12 +01:00
|
|
|
// sometimes eprintln wasnt working?
|
|
|
|
println(final_msg_line)
|
2020-02-03 07:02:54 +01:00
|
|
|
eprintln(final_msg_line)
|
|
|
|
/*
|
2020-01-18 23:26:14 +01:00
|
|
|
if colored_output {
|
|
|
|
eprintln(term.bold(term.red(final_msg_line)))
|
|
|
|
}else{
|
|
|
|
eprintln(final_msg_line)
|
|
|
|
}
|
|
|
|
*/
|
2020-01-22 21:34:38 +01:00
|
|
|
|
2020-02-03 07:02:54 +01:00
|
|
|
exit(1)
|
|
|
|
}
|