From 7a499b3cd364e358dc93faee72ae010219d34983 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 1 Mar 2020 13:07:51 +0100 Subject: [PATCH] checker: fix first() and last(); call_args; method cgen --- vlib/v/ast/ast.v | 31 ++++++++++++++++--------------- vlib/v/checker/checker.v | 37 +++++++++++++++++++++---------------- vlib/v/gen/cgen.v | 24 +++++++++++++++++++++++- vlib/v/gen/tests/1.c | 1 + vlib/v/gen/tests/1.vv | 1 + vlib/v/gen/tests/4.c | 4 ++-- 6 files changed, 64 insertions(+), 34 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index f95441675f..822b0cea1f 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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 { diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index a21effe139..608c0ed414 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 7409d6a326..c62c96c2d1 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -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) diff --git a/vlib/v/gen/tests/1.c b/vlib/v/gen/tests/1.c index 2b03992a2f..3a9880ff16 100644 --- a/vlib/v/gen/tests/1.c +++ b/vlib/v/gen/tests/1.c @@ -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) { diff --git a/vlib/v/gen/tests/1.vv b/vlib/v/gen/tests/1.vv index 2d6924a7aa..d1a0b50772 100644 --- a/vlib/v/gen/tests/1.vv +++ b/vlib/v/gen/tests/1.vv @@ -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) diff --git a/vlib/v/gen/tests/4.c b/vlib/v/gen/tests/4.c index a5d471a509..9e876de5df 100644 --- a/vlib/v/gen/tests/4.c +++ b/vlib/v/gen/tests/4.c @@ -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; }