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
)
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral |
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral |
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
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
// | IncDecStmt k
// Stand-alone expression in a statement list.
@ -246,9 +246,10 @@ pub:
pub struct IndexExpr {
pub:
// op token.Kind
pos token.Position
left Expr
index Expr // [0], [start..end] etc
typ table.Type
typ table.Type
}
pub struct IfExpr {

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

View File

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