From d04554f0b7792be162613b5aa8c92a4096d630e0 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 19 Jun 2020 11:46:08 +0200 Subject: [PATCH] all: more match updates --- vlib/v/ast/ast.v | 88 +++++++++++++-------------- vlib/v/fmt/fmt.v | 6 ++ vlib/v/gen/cgen.v | 135 ++++++++++++++++++++--------------------- vlib/v/gen/js/js.v | 14 +---- vlib/v/gen/x64/gen.v | 36 +++++------ vlib/v/parser/parser.v | 3 + 6 files changed, 138 insertions(+), 144 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 2db63dd1e3..01625bcfce 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -369,9 +369,9 @@ pub mut: } pub fn (i &Ident) var_info() IdentVar { - match i.info { + match i.info as info { IdentVar { - return it + return info } else { // return IdentVar{} @@ -826,7 +826,7 @@ pub struct SqlInsertExpr { [inline] pub fn (expr Expr) is_blank_ident() bool { match expr { - Ident { return it.kind == .blank_ident } + Ident { return expr.kind == .blank_ident } else { return false } } } @@ -835,91 +835,91 @@ pub fn (expr Expr) position() token.Position { // all uncommented have to be implemented match mut expr { ArrayInit { - return it.pos + return expr.pos } AsCast { - return it.pos + return expr.pos } // ast.Ident { } CastExpr { - return it.pos + return expr.pos } Assoc { - return it.pos + return expr.pos } BoolLiteral { - return it.pos + return expr.pos } CallExpr { - return it.pos + return expr.pos } CharLiteral { - return it.pos + return expr.pos } EnumVal { - return it.pos + return expr.pos } FloatLiteral { - return it.pos + return expr.pos } Ident { - return it.pos + return expr.pos } IfExpr { - return it.pos + return expr.pos } // ast.IfGuardExpr { } IndexExpr { - return it.pos + return expr.pos } InfixExpr { - left_pos := it.left.position() - right_pos := it.right.position() + left_pos := expr.left.position() + right_pos := expr.right.position() if left_pos.pos == 0 || right_pos.pos == 0 { - return it.pos + return expr.pos } return token.Position{ - line_nr: it.pos.line_nr + line_nr: expr.pos.line_nr pos: left_pos.pos len: right_pos.pos - left_pos.pos + right_pos.len } } IntegerLiteral { - return it.pos + return expr.pos } MapInit { - return it.pos + return expr.pos } MatchExpr { - return it.pos + return expr.pos } None { - return it.pos + return expr.pos } PostfixExpr { - return it.pos + return expr.pos } // ast.None { } PrefixExpr { - return it.pos + return expr.pos } // ast.ParExpr { } SelectorExpr { - return it.pos + return expr.pos } // ast.SizeOf { } StringLiteral { - return it.pos + return expr.pos } StringInterLiteral { - return it.pos + return expr.pos } // ast.Type { } StructInit { - return it.pos + return expr.pos } Likely { - return it.pos + return expr.pos } // ast.TypeOf { } else { @@ -930,8 +930,8 @@ pub fn (expr Expr) position() token.Position { pub fn (stmt Stmt) position() token.Position { match stmt { - AssertStmt { return it.pos } - AssignStmt { return it.pos } + AssertStmt { return stmt.pos } + AssignStmt { return stmt.pos } /* // Attr { // } @@ -940,19 +940,19 @@ pub fn (stmt Stmt) position() token.Position { // BranchStmt { // } */ - Comment { return it.pos } - CompIf { return it.pos } - ConstDecl { return it.pos } + Comment { return stmt.pos } + CompIf { return stmt.pos } + ConstDecl { return stmt.pos } /* // DeferStmt { // } */ - EnumDecl { return it.pos } - ExprStmt { return it.pos } - FnDecl { return it.pos } - ForCStmt { return it.pos } - ForInStmt { return it.pos } - ForStmt { return it.pos } + EnumDecl { return stmt.pos } + ExprStmt { return stmt.pos } + FnDecl { return stmt.pos } + ForCStmt { return stmt.pos } + ForInStmt { return stmt.pos } + ForStmt { return stmt.pos } /* // GlobalDecl { // } @@ -965,15 +965,15 @@ pub fn (stmt Stmt) position() token.Position { // HashStmt { // } */ - Import { return it.pos } + Import { return stmt.pos } /* // InterfaceDecl { // } // Module { // } */ - Return { return it.pos } - StructDecl { return it.pos } + Return { return stmt.pos } + StructDecl { return stmt.pos } /* // TypeDecl { // } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index c6f302ed1f..c20c11e27e 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1012,6 +1012,12 @@ pub fn (mut f Fmt) match_expr(it ast.MatchExpr) { if it.cond is ast.Ident { ident := it.cond as ast.Ident f.it_name = ident.name + } else if it.cond is ast.SelectorExpr { + // `x.y as z` + // if ident.name != it.var_name && it.var_name != '' { + if it.var_name != '' { + f.write(' as $it.var_name') + } } f.writeln(' {') f.indent++ diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 4a4af861d0..74d7208d7d 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -3676,77 +3676,72 @@ fn (g Gen) is_importing_os() bool { fn (mut g Gen) go_stmt(node ast.GoStmt) { tmp := g.new_tmp_var() - // x := node.call_expr as ast.CallEpxr // TODO - match node.call_expr { - ast.CallExpr { - mut name := it.name // .replace('.', '__') - if it.is_method { - receiver_sym := g.table.get_type_symbol(it.receiver_type) - name = receiver_sym.name + '_' + name - } - name = name.replace('.', '__') - g.writeln('// go') - wrapper_struct_name := 'thread_arg_' + name - wrapper_fn_name := name + '_thread_wrapper' - arg_tmp_var := 'arg_' + tmp - g.writeln('$wrapper_struct_name *$arg_tmp_var = malloc(sizeof(thread_arg_$name));') - if it.is_method { - g.write('$arg_tmp_var->arg0 = ') - g.expr(it.left) - g.writeln(';') - } - for i, arg in it.args { - g.write('$arg_tmp_var->arg${i+1} = ') - g.expr(arg.expr) - g.writeln(';') - } - if g.pref.os == .windows { - g.writeln('CreateThread(0,0, (LPTHREAD_START_ROUTINE)$wrapper_fn_name, $arg_tmp_var, 0,0);') - } else { - g.writeln('pthread_t thread_$tmp;') - g.writeln('pthread_create(&thread_$tmp, NULL, (void*)$wrapper_fn_name, $arg_tmp_var);') - } - g.writeln('// endgo\n') - // Register the wrapper type and function - if name in g.threaded_fns { - return - } - g.type_definitions.writeln('\ntypedef struct $wrapper_struct_name {') - if it.is_method { - styp := g.typ(it.receiver_type) - g.type_definitions.writeln('\t$styp arg0;') - } - if it.args.len == 0 { - g.type_definitions.writeln('EMPTY_STRUCT_DECLARATION;') - } else { - for i, arg in it.args { - styp := g.typ(arg.typ) - g.type_definitions.writeln('\t$styp arg${i+1};') - } - } - g.type_definitions.writeln('} $wrapper_struct_name;') - g.type_definitions.writeln('void* ${wrapper_fn_name}($wrapper_struct_name *arg);') - g.gowrappers.writeln('void* ${wrapper_fn_name}($wrapper_struct_name *arg) {') - g.gowrappers.write('\t${name}(') - if it.is_method { - g.gowrappers.write('arg->arg0') - if it.args.len > 0 { - g.gowrappers.write(', ') - } - } - for i in 0 .. it.args.len { - g.gowrappers.write('arg->arg${i+1}') - if i < it.args.len - 1 { - g.gowrappers.write(', ') - } - } - g.gowrappers.writeln(');') - g.gowrappers.writeln('\treturn 0;') - g.gowrappers.writeln('}') - g.threaded_fns << name - } - else {} + expr := node.call_expr as ast.CallExpr + mut name := expr.name // .replace('.', '__') + if expr.is_method { + receiver_sym := g.table.get_type_symbol(expr.receiver_type) + name = receiver_sym.name + '_' + name } + name = name.replace('.', '__') + g.writeln('// go') + wrapper_struct_name := 'thread_arg_' + name + wrapper_fn_name := name + '_thread_wrapper' + arg_tmp_var := 'arg_' + tmp + g.writeln('$wrapper_struct_name *$arg_tmp_var = malloc(sizeof(thread_arg_$name));') + if expr.is_method { + g.write('$arg_tmp_var->arg0 = ') + g.expr(expr.left) + g.writeln(';') + } + for i, arg in expr.args { + g.write('$arg_tmp_var->arg${i+1} = ') + g.expr(arg.expr) + g.writeln(';') + } + if g.pref.os == .windows { + g.writeln('CreateThread(0,0, (LPTHREAD_START_ROUTINE)$wrapper_fn_name, $arg_tmp_var, 0,0);') + } else { + g.writeln('pthread_t thread_$tmp;') + g.writeln('pthread_create(&thread_$tmp, NULL, (void*)$wrapper_fn_name, $arg_tmp_var);') + } + g.writeln('// endgo\n') + // Register the wrapper type and function + if name in g.threaded_fns { + return + } + g.type_definitions.writeln('\ntypedef struct $wrapper_struct_name {') + if expr.is_method { + styp := g.typ(expr.receiver_type) + g.type_definitions.writeln('\t$styp arg0;') + } + if expr.args.len == 0 { + g.type_definitions.writeln('EMPTY_STRUCT_DECLARATION;') + } else { + for i, arg in expr.args { + styp := g.typ(arg.typ) + g.type_definitions.writeln('\t$styp arg${i+1};') + } + } + g.type_definitions.writeln('} $wrapper_struct_name;') + g.type_definitions.writeln('void* ${wrapper_fn_name}($wrapper_struct_name *arg);') + g.gowrappers.writeln('void* ${wrapper_fn_name}($wrapper_struct_name *arg) {') + g.gowrappers.write('\t${name}(') + if expr.is_method { + g.gowrappers.write('arg->arg0') + if expr.args.len > 0 { + g.gowrappers.write(', ') + } + } + for i in 0 .. expr.args.len { + g.gowrappers.write('arg->arg${i+1}') + if i < expr.args.len - 1 { + g.gowrappers.write(', ') + } + } + g.gowrappers.writeln(');') + g.gowrappers.writeln('\treturn 0;') + g.gowrappers.writeln('}') + g.threaded_fns << name } fn (mut g Gen) as_cast(node ast.AsCast) { diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index 669d2eb879..f36c7368de 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -277,11 +277,11 @@ pub fn (mut g JsGen) typ(t table.Type) string { // TODO: Implement interfaces styp = 'interface' } + } /* else { println('jsgen.typ: Unhandled type $t') styp = sym.name } */ - } if styp.starts_with('JS.') { return styp[3..] } return styp } @@ -369,7 +369,7 @@ pub fn (mut g JsGen) write(s string) { pub fn (mut g JsGen) writeln(s string) { g.gen_indent() g.out.writeln(s) - g.empty_line = true +g.empty_line = true } pub fn (mut g JsGen) new_tmp_var() string { @@ -510,11 +510,6 @@ fn (mut g JsGen) stmt(node ast.Stmt) { ast.UnsafeStmt { g.stmts(it.stmts) } - /* - else { - verror('jsgen.stmt(): bad node ${typeof(node)}') - } - */ } } @@ -639,11 +634,6 @@ fn (mut g JsGen) expr(node ast.Expr) { ast.ComptimeCall { // TODO } - /* - else { - println(term.red('jsgen.expr(): unhandled node "${typeof(node)}"')) - } - */ } } diff --git a/vlib/v/gen/x64/gen.v b/vlib/v/gen/x64/gen.v index e1e60a83d6..e4b0d7eb3a 100644 --- a/vlib/v/gen/x64/gen.v +++ b/vlib/v/gen/x64/gen.v @@ -463,9 +463,9 @@ pub fn (mut g Gen) gen_print_from_expr(expr ast.Expr, newline bool) { match expr { ast.StringLiteral { if newline { - g.gen_print(it.val + '\n') + g.gen_print(expr.val + '\n') } else { - g.gen_print(it.val) + g.gen_print(expr.val) } } else {} @@ -559,11 +559,11 @@ pub fn (mut g Gen) call_fn(node ast.CallExpr) { match expr { ast.IntegerLiteral { // `foo(2)` => `mov edi,0x2` - g.mov(fn_arg_registers[i], it.val.int()) + g.mov(fn_arg_registers[i], expr.val.int()) } ast.Ident { // `foo(x)` => `mov edi,DWORD PTR [rbp-0x8]` - var_offset := g.get_var_offset(it.name) + var_offset := g.get_var_offset(expr.name) println('i=$i fn name= $name offset=$var_offset') println(int(fn_arg_registers[i])) g.mov_var_to_reg(fn_arg_registers[i], var_offset) @@ -584,20 +584,20 @@ pub fn (mut g Gen) call_fn(node ast.CallExpr) { fn (mut g Gen) stmt(node ast.Stmt) { match node { ast.AssignStmt { - g.assign_stmt(it) + g.assign_stmt(node) } ast.ConstDecl {} ast.ExprStmt { - g.expr(it.expr) + g.expr(node.expr) } ast.FnDecl { - g.fn_decl(it) + g.fn_decl(node) } ast.ForStmt { - g.for_stmt(it) + g.for_stmt(node) } ast.HashStmt { - words := it.val.split(' ') + words := node.val.split(' ') for word in words { if word.len != 2 { verror('opcodes format: xx xx xx xx') @@ -615,7 +615,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { } ast.StructDecl {} ast.UnsafeStmt { - g.stmts(it.stmts) + g.stmts(node.stmts) } else { println('x64.stmt(): bad node: ' + typeof(node)) @@ -631,22 +631,22 @@ fn (mut g Gen) expr(node ast.Expr) { ast.ArrayInit {} ast.BoolLiteral {} ast.CallExpr { - if it.name in ['println', 'print', 'eprintln', 'eprint'] { - expr := it.args[0].expr - g.gen_print_from_expr(expr, it.name in ['println', 'eprintln']) + if node.name in ['println', 'print', 'eprintln', 'eprint'] { + expr := node.args[0].expr + g.gen_print_from_expr(expr, node.name in ['println', 'eprintln']) return } - g.call_fn(it) + g.call_fn(node) } ast.FloatLiteral {} ast.Ident {} ast.IfExpr { - g.if_expr(it) + g.if_expr(node) } ast.InfixExpr {} ast.IntegerLiteral {} ast.PostfixExpr { - g.postfix_expr(it) + g.postfix_expr(node) } ast.StringLiteral {} ast.StructInit {} @@ -700,10 +700,10 @@ fn (mut g Gen) assign_stmt(node ast.AssignStmt) { // ident := left as ast.Ident match right { ast.IntegerLiteral { - g.allocate_var(name, 4, it.val.int()) + g.allocate_var(name, 4, right.val.int()) } ast.InfixExpr { - g.infix_expr(it) + g.infix_expr(right) g.allocate_var(name, 4, 0) // `mov DWORD PTR [rbp-0x8],eax` offset := g.get_var_offset(name) diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index a032d7834f..5696b41a6c 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -793,6 +793,9 @@ pub fn (mut p Parser) parse_ident(language table.Language) ast.Ident { } } } + if p.inside_match_body && name == 'it' { + // p.warn('it') + } if p.expr_mod.len > 0 { name = '${p.expr_mod}.$name' }