v2: move index expr type check

pull/3635/head
Alexander Medvednikov 2020-02-03 07:44:52 +01:00
parent 2d5c70832c
commit d918903252
6 changed files with 43 additions and 12 deletions

View File

@ -246,6 +246,7 @@ pub:
pub struct IndexExpr {
pub:
// op token.Kind
pos token.Position
left Expr
index Expr // [0], [start..end] etc
typ table.Type

View File

@ -284,9 +284,22 @@ pub fn (c &Checker) expr(node ast.Expr) table.Type {
return c.selector_expr(it)
}
ast.IndexExpr {
// c.expr(it.left)
mut typ := c.expr(it.left)
if typ.name.starts_with('array_') {
elm_typ := typ.name[6..]
// TODO `typ = ... or ...`
x := c.table.find_type(elm_typ) or {
c.error(elm_typ, it.pos)
exit(0)
}
typ = x
}
else {
typ = table.int_type
}
return typ
// c.expr(it.index)
return it.typ
//return it.typ
}
ast.IfExpr {
typ := c.expr(it.cond)

View File

@ -1,5 +1,6 @@
void foo(int a);
int get_int(string a);
bool get_bool();
int get_int2();
void myuser();
multi_return_int_string multi_return();
@ -51,12 +52,17 @@ i < 10; i++;
int n = get_int2();
bool q = true || false;
bool b2 = bools[0] || true;
bool b3 = get_bool() || true;
}
int get_int(string a) {
return 10;
}
bool get_bool() {
return true;
}
int get_int2() {
string a = tos3("hello");
return get_int(a);

View File

@ -52,12 +52,17 @@ fn foo(a int) {
n := get_int2()
q := true || false
b2 := bools[0] || true
b3 := get_bool() || true
}
fn get_int(a string) int {
return 10
}
fn get_bool() bool {
return true
}
fn get_int2() int {
a := 'hello'
//return get_int('sdf')

View File

@ -77,9 +77,15 @@ fn (p mut Parser) fn_decl(/*high bool*/) ast.FnDecl {
}
mut name := ''
if p.tok.kind == .name {
// TODO
// TODO high
name = p.check_name()
}
// <T>
if p.tok.kind == .lt {
p.next()
p.next()
p.check(.gt)
}
// println('fn decl $name')
p.check(.lpar)
// Args