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

@ -8,11 +8,11 @@ import (
v.table v.table
) )
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral |
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr
// | IncDecStmt k // | IncDecStmt k
// Stand-alone expression in a statement list. // Stand-alone expression in a statement list.
@ -246,9 +246,10 @@ pub:
pub struct IndexExpr { pub struct IndexExpr {
pub: pub:
// op token.Kind // op token.Kind
pos token.Position
left Expr left Expr
index Expr // [0], [start..end] etc index Expr // [0], [start..end] etc
typ table.Type typ table.Type
} }
pub struct IfExpr { pub struct IfExpr {

View File

@ -284,9 +284,22 @@ pub fn (c &Checker) expr(node ast.Expr) table.Type {
return c.selector_expr(it) return c.selector_expr(it)
} }
ast.IndexExpr { 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) // c.expr(it.index)
return it.typ //return it.typ
} }
ast.IfExpr { ast.IfExpr {
typ := c.expr(it.cond) typ := c.expr(it.cond)

View File

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

View File

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

View File

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

View File

@ -399,7 +399,7 @@ pub fn (p mut Parser) name_expr() (ast.Expr,table.Type) {
typ: typ typ: typ
name: ident.name name: ident.name
// expr: p.expr(0)// var.expr // expr: p.expr(0)// var.expr
} }
// ident.ti = ti // ident.ti = ti
node = ident node = ident
@ -738,7 +738,7 @@ fn (p mut Parser) if_expr() (ast.Expr,table.Type) {
ti: ti ti: ti
pos: p.tok.position() pos: p.tok.position()
// left: left // left: left
} }
return node,ti return node,ti
} }
@ -901,7 +901,7 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
fields << table.Field{ fields << table.Field{
name: field_name name: field_name
// type_idx: ti.idx // type_idx: ti.idx
ti: ti ti: ti
} }
} }
@ -974,7 +974,7 @@ fn (p mut Parser) var_decl() ast.VarDecl {
name: name name: name
is_mut: is_mut is_mut: is_mut
// expr: expr // expr: expr
typ: typ typ: typ
}) })
p.warn('var decl name=$name typ=$typ.name') p.warn('var decl name=$name typ=$typ.name')
@ -982,7 +982,7 @@ fn (p mut Parser) var_decl() ast.VarDecl {
node := ast.VarDecl{ node := ast.VarDecl{
name: name name: name
expr: expr // p.expr(token.lowest_prec) expr: expr // p.expr(token.lowest_prec)
is_mut: is_mut is_mut: is_mut
typ: typ typ: typ
pos: p.tok.position() pos: p.tok.position()