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

View File

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

View File

@ -284,15 +284,28 @@ fn (g mut Gen) expr(node ast.Expr) {
} }
ast.CallExpr { ast.CallExpr {
g.write('${it.name}(') g.write('${it.name}(')
g.call_args(it.args)
g.write(')')
/*
for i, expr in it.args { for i, expr in it.args {
g.expr(expr) g.expr(expr)
if i != it.args.len - 1 { if i != it.args.len - 1 {
g.write(', ') 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(')') g.write(')')
} }
ast.MethodCallExpr {}
ast.Ident { ast.Ident {
g.write('$it.name') 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) { fn verror(s string) {
println(s) println(s)
exit(1) exit(1)

View File

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

View File

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

View File

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