checker: fix first() and last(); call_args; method cgen

pull/3893/head
Alexander Medvednikov 2020-03-01 13:07:51 +01:00
parent 9978fb3e2c
commit 7a499b3cd3
6 changed files with 64 additions and 34 deletions

View File

@ -8,14 +8,14 @@ import (
v.table
)
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral |
FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr |
AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr |
CastExpr | EnumVal | Assoc | SizeOf | None | MapInit | IfGuardExpr | ParExpr | OrExpr
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt |
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt |
LineComment | MultiLineComment | AssertStmt | UnsafeStmt
pub type Type = StructType | ArrayType
@ -147,13 +147,13 @@ pub:
pub struct CallExpr {
pub:
// tok token.Token
pos token.Position
pos token.Position
mut:
// func Expr
name string
args []Expr
is_c bool
muts []bool
name string
args []Expr
is_c bool
muts []bool
or_block OrExpr
}
@ -166,6 +166,7 @@ pub:
args []Expr
muts []bool
or_block OrExpr
typ table.Type
}
pub struct Return {
@ -513,10 +514,10 @@ pub:
pub struct Assoc {
pub:
var_name string
fields []string
exprs []Expr
pos token.Position
var_name string
fields []string
exprs []Expr
pos token.Position
}
pub struct SizeOf {

View File

@ -12,17 +12,17 @@ import (
)
const (
max_nr_errors = 50
max_nr_errors = 150
)
pub struct Checker {
table &table.Table
table &table.Table
mut:
file ast.File
nr_errors int
errors []string
expected_type table.Type
fn_return_type table.Type // current function's return type
file ast.File
nr_errors int
errors []string
expected_type table.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
@ -45,6 +45,7 @@ pub fn (c mut Checker) check(ast_file ast.File) {
println(t.name + ' - ' + t.kind.str())
}
*/
}
pub fn (c mut Checker) check2(ast_file ast.File) []string {
@ -71,7 +72,6 @@ pub fn (c mut Checker) check_files(ast_files []ast.File) {
for file in ast_files {
c.check(file)
}
c.print_unhandled_nodes()
}
@ -232,8 +232,11 @@ pub fn (c mut Checker) check_method_call_expr(method_call_expr ast.MethodCallExp
return method.return_type
}
if typ_sym.kind == .array && name in ['filter', 'clone'] {
// info := typ_sym.info as table.Array
return typ // info.elem_type
return typ
}
if typ_sym.kind == .array && name in ['first', 'last'] {
info := typ_sym.info as table.Array
return info.elem_type
}
// check parent
if typ_sym.parent_idx != 0 {
@ -436,7 +439,9 @@ fn (c mut Checker) stmt(node ast.Stmt) {
}
else {
node_name := typeof(node)
if !(node_name) in c.unhandled_stmts { c.unhandled_stmts << node_name }
if !(node_name) in c.unhandled_stmts {
c.unhandled_stmts << node_name
}
}
}
}
@ -538,11 +543,11 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
c.expr(it.left)
}
*/
else {
// TODO: find nil string bug triggered with typeof
// node_name := typeof(node)
// if !(node_name) in c.unhandled_exprs { c.unhandled_exprs << node_name }
}
else {}
// TODO: find nil string bug triggered with typeof
// node_name := typeof(node)
// if !(node_name) in c.unhandled_exprs { c.unhandled_exprs << node_name }
}
return table.void_type
}

View File

@ -284,15 +284,28 @@ fn (g mut Gen) expr(node ast.Expr) {
}
ast.CallExpr {
g.write('${it.name}(')
g.call_args(it.args)
g.write(')')
/*
for i, expr in it.args {
g.expr(expr)
if i != it.args.len - 1 {
g.write(', ')
}
}
*/
}
ast.MethodCallExpr {
typ := 'TODO'
g.write('${typ}_${it.name}(')
g.expr(it.expr)
if it.args.len > 0 {
g.write(', ')
}
g.call_args(it.args)
g.write(')')
}
ast.MethodCallExpr {}
ast.Ident {
g.write('$it.name')
}
@ -382,6 +395,15 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
}
}
fn (g mut Gen) call_args(args []ast.Expr) {
for i, expr in args {
g.expr(expr)
if i != args.len - 1 {
g.write(', ')
}
}
}
fn verror(s string) {
println(s)
exit(1)

View File

@ -66,6 +66,7 @@ i < 10; i++;
bool q = true || false;
bool b2 = bools[0] || true;
bool b3 = get_bool() || true;
int f = TODO_first(nums);
}
int get_int(string a) {

View File

@ -72,6 +72,7 @@ fn foo(a int) {
q := true || false
b2 := bools[0] || true
b3 := get_bool() || true
f := nums.first()
//cloned = nums.clone()
//cloned1 := cloned[0]
//println(cloned1 == 1)

View File

@ -17,7 +17,7 @@ int main() {
a.a = tos3("da");
a.b.a = 111;
string a1 = a.a;
int a2 = ;
int a2 = TODO_testa(b);
int c = testa();
c = 1;
string d = testb(1);
@ -56,7 +56,7 @@ int testc(int a) {
}
int testa() {
int a = ;
int a = TODO_testb(f);
a = 1;
return 4;
}