From 78b32892acf714d955ea5de8b2e40eae0733000c Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 18 Jun 2020 20:21:08 +0200 Subject: [PATCH] cgen: use the new match syntax --- vlib/v/gen/cgen.v | 466 ++++++++++++++++++++--------------------- vlib/v/parser/fn.v | 14 +- vlib/v/parser/parser.v | 38 ++-- vlib/v/parser/pratt.v | 3 +- 4 files changed, 254 insertions(+), 267 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index e909fd8823..4a4af861d0 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -41,8 +41,10 @@ const ( 'class', 'typename' ] - cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le'] // same order as in token.Kind - cmp_rev = ['eq', 'ne', 'le', 'ge', 'lt', 'gt'] // when operands are switched + // same order as in token.Kind + cmp_str = ['eq', 'ne', 'gt', 'lt', 'ge', 'le'] + // when operands are switched + cmp_rev = ['eq', 'ne', 'le', 'ge', 'lt', 'gt'] ) struct Gen { @@ -269,7 +271,7 @@ pub fn (mut g Gen) init() { g.comptime_defines.writeln('// All custom defines : ' + g.pref.compile_defines_all.join(',')) g.comptime_defines.writeln('// Turned ON custom defines: ' + g.pref.compile_defines.join(',')) for cdefine in g.pref.compile_defines { - g.comptime_defines.writeln('#define CUSTOM_DEFINE_${cdefine}') + g.comptime_defines.writeln('#define CUSTOM_DEFINE_$cdefine') } g.comptime_defines.writeln('') } @@ -310,14 +312,14 @@ pub fn (mut g Gen) write_typeof_functions() { if typ.kind == .sum_type { sum_info := typ.info as table.SumType tidx := g.table.find_type_idx(typ.name) - g.writeln('char * v_typeof_sumtype_${tidx}(int sidx) { /* ${typ.name} */ ') + g.writeln('char * v_typeof_sumtype_${tidx}(int sidx) { /* $typ.name */ ') g.writeln(' switch(sidx) {') g.writeln(' case $tidx: return "$typ.name";') for v in sum_info.variants { subtype := g.table.get_type_symbol(v) g.writeln(' case $v: return "$subtype.name";') } - g.writeln(' default: return "unknown ${typ.name}";') + g.writeln(' default: return "unknown $typ.name";') g.writeln(' }') g.writeln('}') } @@ -496,7 +498,7 @@ pub fn (mut g Gen) write_multi_return_types() { // for field in struct_info.fields { for i, mr_typ in info.types { type_name := g.typ(mr_typ) - g.type_definitions.writeln('\t$type_name arg${i};') + g.type_definitions.writeln('\t$type_name arg$i;') } g.type_definitions.writeln('} $name;\n') // g.typedefs.writeln('typedef struct $name $name;') @@ -596,45 +598,45 @@ fn (mut g Gen) stmt(node ast.Stmt) { // g.writeln('//// stmt start') match node { ast.AssertStmt { - g.gen_assert_stmt(it) + g.gen_assert_stmt(node) } ast.AssignStmt { g.gen_assign_stmt(node) } ast.Attr { g.attrs << node.name - g.writeln('// Attr: [$it.name]') + g.writeln('// Attr: [$node.name]') } ast.Block { g.writeln('{') - g.stmts(it.stmts) + g.stmts(node.stmts) g.writeln('}') } ast.BranchStmt { // continue or break - g.write(it.tok.kind.str()) + g.write(node.tok.kind.str()) g.writeln(';') } ast.ConstDecl { // if g.pref.build_mode != .build_module { - g.const_decl(it) + g.const_decl(node) // } } ast.CompIf { - g.comp_if(it) + g.comp_if(node) } ast.DeferStmt { - mut defer_stmt := *it + mut defer_stmt := *node defer_stmt.ifdef = g.defer_ifdef g.defer_stmts << defer_stmt } ast.EnumDecl { - enum_name := it.name.replace('.', '__') + enum_name := node.name.replace('.', '__') g.enum_typedefs.writeln('typedef enum {') mut cur_enum_expr := '' mut cur_enum_offset := 0 - for field in it.fields { - g.enum_typedefs.write('\t${enum_name}_${field.name}') + for field in node.fields { + g.enum_typedefs.write('\t${enum_name}_$field.name') if field.has_expr { g.enum_typedefs.write(' = ') pos := g.out.len @@ -645,15 +647,15 @@ fn (mut g Gen) stmt(node ast.Stmt) { cur_enum_expr = expr_str cur_enum_offset = 0 } - cur_value := if cur_enum_offset > 0 { '${cur_enum_expr}+${cur_enum_offset}' } else { cur_enum_expr } - g.enum_typedefs.writeln(', // ${cur_value}') + cur_value := if cur_enum_offset > 0 { '$cur_enum_expr+$cur_enum_offset' } else { cur_enum_expr } + g.enum_typedefs.writeln(', // $cur_value') cur_enum_offset++ } - g.enum_typedefs.writeln('} ${enum_name};\n') + g.enum_typedefs.writeln('} $enum_name;\n') } ast.ExprStmt { - g.expr(it.expr) - if g.inside_ternary == 0 && !it.is_expr && !(it.expr is ast.IfExpr) { + g.expr(node.expr) + if g.inside_ternary == 0 && !node.is_expr && !(node.expr is ast.IfExpr) { g.writeln(';') } } @@ -661,7 +663,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { mut skip := false pos := g.out.buf.len if g.pref.build_mode == .build_module { - if !it.name.starts_with(g.module_built + '.') { + if !node.name.starts_with(g.module_built + '.') { // Skip functions that don't have to be generated // for this module. skip = true @@ -670,20 +672,22 @@ fn (mut g Gen) stmt(node ast.Stmt) { skip = false } if !skip { - println('build module `$g.module_built` fn `$it.name`') + println('build module `$g.module_built` fn `$node.name`') } } keep_fn_decl := g.fn_decl - g.fn_decl = it // &it - if it.name == 'main' { + g.fn_decl = node // &it + if node.name == 'main' { // just remember `it`; main code will be generated in finish() - g.fn_main = it + g.fn_main = node } else { - if it.name == 'backtrace' || it.name == 'backtrace_symbols' || it.name == 'backtrace_symbols_fd' { + if node.name == 'backtrace' || node.name == 'backtrace_symbols' || node.name == + 'backtrace_symbols_fd' { g.write('\n#ifndef __cplusplus\n') } - g.gen_fn_decl(it) - if it.name == 'backtrace' || it.name == 'backtrace_symbols' || it.name == 'backtrace_symbols_fd' { + g.gen_fn_decl(node) + if node.name == 'backtrace' || node.name == 'backtrace_symbols' || node.name == + 'backtrace_symbols_fd' { g.write('\n#endif\n') } } @@ -691,65 +695,64 @@ fn (mut g Gen) stmt(node ast.Stmt) { if skip { g.out.go_back_to(pos) } - if it.language != .c { + if node.language != .c { g.writeln('') } } ast.ForCStmt { g.write('for (') - if !it.has_init { + if !node.has_init { g.write('; ') } else { - g.stmt(it.init) + g.stmt(node.init) } - if it.has_cond { - g.expr(it.cond) + if node.has_cond { + g.expr(node.cond) } g.write('; ') - if it.has_inc { - - g.stmt(it.inc) + if node.has_inc { + g.stmt(node.inc) } g.writeln(') {') - g.stmts(it.stmts) + g.stmts(node.stmts) g.writeln('}') } ast.ForInStmt { - g.for_in(it) + g.for_in(node) } ast.ForStmt { g.write('while (') - if it.is_inf { + if node.is_inf { g.write('1') } else { - g.expr(it.cond) + g.expr(node.cond) } g.writeln(') {') - g.stmts(it.stmts) + g.stmts(node.stmts) g.writeln('}') } ast.GlobalDecl { - styp := g.typ(it.typ) - g.definitions.writeln('$styp $it.name; // global') + styp := g.typ(node.typ) + g.definitions.writeln('$styp $node.name; // global') } ast.GoStmt { - g.go_stmt(it) + g.go_stmt(node) } ast.GotoLabel { - g.writeln('$it.name: {}') + g.writeln('$node.name: {}') } ast.GotoStmt { - g.writeln('goto $it.name;') + g.writeln('goto $node.name;') } ast.HashStmt { // #include etc - typ := it.val.all_before(' ') + typ := node.val.all_before(' ') if typ == 'include' { - g.includes.writeln('// added by module `$it.mod`:') - g.includes.writeln('#$it.val') + g.includes.writeln('// added by module `$node.mod`:') + g.includes.writeln('#$node.val') } if typ == 'define' { - g.includes.writeln('#$it.val') + g.includes.writeln('#$node.val') } } ast.Import {} @@ -757,25 +760,25 @@ fn (mut g Gen) stmt(node ast.Stmt) { // definitions are sorted and added in write_types } ast.Module { - g.is_builtin_mod = it.name == 'builtin' + g.is_builtin_mod = node.name == 'builtin' } ast.Return { g.write_defer_stmts_when_needed() - g.write_autofree_stmts_when_needed(it) - g.return_statement(it) + g.write_autofree_stmts_when_needed(node) + g.return_statement(node) } ast.StructDecl { - name := if it.language == .c { it.name.replace('.', '__') } else { c_name(it.name) } + name := if node.language == .c { node.name.replace('.', '__') } else { c_name(node.name) } // g.writeln('typedef struct {') // for field in it.fields { // field_type_sym := g.table.get_type_symbol(field.typ) // g.writeln('\t$field_type_sym.name $field.name;') // } // g.writeln('} $name;') - if it.language == .c { + if node.language == .c { return } - if it.is_union { + if node.is_union { g.typedefs.writeln('typedef union $name $name;') } else { g.typedefs.writeln('typedef struct $name $name;') @@ -785,7 +788,7 @@ fn (mut g Gen) stmt(node ast.Stmt) { g.writeln('// TypeDecl') } ast.UnsafeStmt { - g.stmts(it.stmts) + g.stmts(node.stmts) } else { verror('cgen.stmt(): unhandled node ' + typeof(node)) @@ -826,14 +829,14 @@ fn (mut g Gen) for_in(it ast.ForInStmt) { cond_type_is_ptr := it.cond_type.is_ptr() atmp := g.new_tmp_var() atmp_type := if cond_type_is_ptr { 'array *' } else { 'array' } - g.write('${atmp_type} ${atmp} = ') + g.write('$atmp_type $atmp = ') g.expr(it.cond) g.writeln(';') i := if it.key_var in ['', '_'] { g.new_tmp_var() } else { it.key_var } op_field := if cond_type_is_ptr { '->' } else { '.' } - g.writeln('for (int $i = 0; $i < ${atmp}${op_field}len; $i++) {') + g.writeln('for (int $i = 0; $i < $atmp${op_field}len; $i++) {') if it.val_var != '_' { - g.writeln('\t$styp ${c_name(it.val_var)} = (($styp*)${atmp}${op_field}data)[$i];') + g.writeln('\t$styp ${c_name(it.val_var)} = (($styp*)$atmp${op_field}data)[$i];') } g.stmts(it.stmts) g.writeln('}') @@ -897,7 +900,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type, expected_type table.Type) got_styp := g.typ(got_type) exp_styp := g.typ(expected_type) got_idx := got_type.idx() - g.write('/* sum type cast */ ($exp_styp) {.obj = memdup(&(${got_styp}[]) {') + g.write('/* sum type cast */ ($exp_styp) {.obj = memdup(&($got_styp[]) {') g.expr(expr) g.write('}, sizeof($got_styp)), .typ = $got_idx /* $got_sym.name */}') return @@ -945,11 +948,11 @@ fn (mut g Gen) gen_assert_stmt(a ast.AssertStmt) { g.writeln('{') g.writeln(' g_test_oks++;') metaname_ok := g.gen_assert_metainfo(a) - g.writeln(' cb_assertion_ok(&${metaname_ok});') + g.writeln(' cb_assertion_ok(&$metaname_ok);') g.writeln('} else {') g.writeln(' g_test_fails++;') metaname_fail := g.gen_assert_metainfo(a) - g.writeln(' cb_assertion_failed(&${metaname_fail});') + g.writeln(' cb_assertion_failed(&$metaname_fail);') g.writeln(' longjmp(g_jump_buffer, 1);') g.writeln(' // TODO') g.writeln(' // Maybe print all vars in a test function if it fails?') @@ -958,7 +961,7 @@ fn (mut g Gen) gen_assert_stmt(a ast.AssertStmt) { } g.writeln('{} else {') metaname_panic := g.gen_assert_metainfo(a) - g.writeln(' __print_assert_failure(&${metaname_panic});') + g.writeln(' __print_assert_failure(&$metaname_panic);') g.writeln(' v_panic(tos_lit("Assertion failed..."));') g.writeln(' exit(1);') g.writeln('}') @@ -969,11 +972,11 @@ fn (mut g Gen) gen_assert_metainfo(a ast.AssertStmt) string { fn_name := g.fn_decl.name line_nr := a.pos.line_nr src := cestring(a.expr.str()) - metaname := 'v_assert_meta_info_${g.new_tmp_var()}' + metaname := 'v_assert_meta_info_$g.new_tmp_var()' g.writeln(' VAssertMetaInfo $metaname;') g.writeln(' memset(&$metaname, 0, sizeof(VAssertMetaInfo));') g.writeln(' ${metaname}.fpath = ${ctoslit(mod_path)};') - g.writeln(' ${metaname}.line_nr = ${line_nr};') + g.writeln(' ${metaname}.line_nr = $line_nr;') g.writeln(' ${metaname}.fn_name = ${ctoslit(fn_name)};') g.writeln(' ${metaname}.src = ${ctoslit(src)};') match a.expr { @@ -1040,7 +1043,9 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { for i, lx in assign_stmt.left { match lx { ast.Ident { - if it.kind == .blank_ident { continue } + if lx.kind == .blank_ident { + continue + } } else {} } @@ -1051,7 +1056,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { g.expr(lx) if is_optional { mr_base_styp := g.base_type(return_type) - g.writeln(' = (*(${mr_base_styp}*)${mr_var_name}.data).arg$i;') + g.writeln(' = (*($mr_base_styp*)${mr_var_name}.data).arg$i;') } else { g.writeln(' = ${mr_var_name}.arg$i;') } @@ -1061,11 +1066,11 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } // TODO: non idents on left (exprs) if assign_stmt.has_cross_var { - for i,left in assign_stmt.left { + for i, left in assign_stmt.left { match left { ast.Ident { styp := g.typ(assign_stmt.left_types[i]) - g.writeln('$styp _var_${it.pos.pos} = $it.name;') + g.writeln('$styp _var_$left.pos.pos = $left.name;') } else {} } @@ -1073,7 +1078,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } // `a := 1` | `a,b := 1,2` for i, left in assign_stmt.left { - mut var_type := assign_stmt.left_types[i] + mut var_type := assign_stmt.left_types[i] val_type := assign_stmt.right_types[i] val := assign_stmt.right[i] mut is_call := false @@ -1090,25 +1095,25 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { mut has_val := false match val { ast.ArrayInit { - is_fixed_array_init = it.is_fixed - has_val = it.has_val + is_fixed_array_init = val.is_fixed + has_val = val.has_val } ast.CallExpr { is_call = true - return_type = it.return_type + return_type = val.return_type } // TODO: no buffer fiddling ast.AnonFn { if blank_assign { g.write('{') } - ret_styp := g.typ(it.decl.return_type) + ret_styp := g.typ(val.decl.return_type) g.write('$ret_styp (*$ident.name) (') def_pos := g.definitions.len - g.fn_args(it.decl.args, it.decl.is_variadic) + g.fn_args(val.decl.args, val.decl.is_variadic) g.definitions.go_back(g.definitions.len - def_pos) g.write(') = ') - g.expr(*it) + g.expr(*val) g.writeln(';') if blank_assign { g.write('}') @@ -1130,8 +1135,7 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { g.expr(val) g.writeln(';}') } - } - else if right_sym.kind == .array_fixed && assign_stmt.op == .assign { + } else if right_sym.kind == .array_fixed && assign_stmt.op == .assign { right := val as ast.ArrayInit for j, expr in right.exprs { g.expr(left) @@ -1156,9 +1160,8 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { g.write(' = /*f*/string_add(') str_add = true } - if right_sym.kind == .function && is_decl { - if is_inside_ternary && is_decl { + if is_inside_ternary && is_decl { g.out.write(tabs[g.indent - g.inside_ternary]) } func := right_sym.info as table.FnType @@ -1184,7 +1187,6 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } g.is_assign_lhs = false g.is_assign_rhs = true - if !g.is_array_set && !str_add { g.write(' $op ') } else if str_add { @@ -1197,11 +1199,16 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } } unwrap_optional := !var_type.has_flag(.optional) && val_type.has_flag(.optional) - if unwrap_optional { g.write('*($styp*)') } + if unwrap_optional { + g.write('*($styp*)') + } if !cloned { if is_decl { - if is_fixed_array_init && !has_val { g.write('{0}') } - else { g.expr(val) } + if is_fixed_array_init && !has_val { + g.write('{0}') + } else { + g.expr(val) + } } else { if assign_stmt.has_cross_var { g.gen_cross_tmp_variable(assign_stmt.left, val) @@ -1210,7 +1217,9 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } } } - if unwrap_optional { g.write('.data') } + if unwrap_optional { + g.write('.data') + } if g.is_array_set { g.write(' })') g.is_array_set = false @@ -1227,38 +1236,39 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { } fn (mut g Gen) gen_cross_tmp_variable(left []ast.Expr, val ast.Expr) { - match val as val_expr { + val_ := val + match val { ast.Ident { mut has_var := false for lx in left { if lx is ast.Ident { ident := lx as ast.Ident - if val_expr.name == ident.name { - g.write('_var_${ident.pos.pos}') + if val.name == ident.name { + g.write('_var_$ident.pos.pos') has_var = true break } } } if !has_var { - g.expr(val) + g.expr(val_) } } ast.InfixExpr { - g.gen_cross_tmp_variable(left, val_expr.left) - g.write(val_expr.op.str()) - g.gen_cross_tmp_variable(left, val_expr.right) + g.gen_cross_tmp_variable(left, val.left) + g.write(val.op.str()) + g.gen_cross_tmp_variable(left, val.right) } ast.PrefixExpr { - g.write(val_expr.op.str()) - g.gen_cross_tmp_variable(left, val_expr.right) + g.write(val.op.str()) + g.gen_cross_tmp_variable(left, val.right) } ast.PostfixExpr { - g.gen_cross_tmp_variable(left, val_expr.expr) - g.write(val_expr.op.str()) + g.gen_cross_tmp_variable(left, val.expr) + g.write(val.op.str()) } else { - g.expr(val) + g.expr(val_) } } } @@ -1321,7 +1331,7 @@ fn (mut g Gen) autofree_scope_vars(pos int) string { // // TODO why 0? // continue // } - v := *it + v := *obj is_optional := v.typ.has_flag(.optional) if is_optional { // TODO: free optionals @@ -1399,24 +1409,24 @@ fn (mut g Gen) expr(node ast.Expr) { match node { ast.AnonFn { // TODO: dont fiddle with buffers - g.gen_anon_fn_decl(it) - fsym := g.table.get_type_symbol(it.typ) + g.gen_anon_fn_decl(node) + fsym := g.table.get_type_symbol(node.typ) g.write(fsym.name) } ast.ArrayInit { - g.array_init(it) + g.array_init(node) } ast.AsCast { - g.as_cast(it) + g.as_cast(node) } ast.Assoc { - g.assoc(it) + g.assoc(node) } ast.BoolLiteral { - g.write(it.val.str()) + g.write(node.val.str()) } ast.CallExpr { - g.call_expr(it) + g.call_expr(node) } ast.CastExpr { // g.write('/*cast*/') @@ -1424,97 +1434,97 @@ fn (mut g Gen) expr(node ast.Expr) { // &Foo(0) => ((Foo*)0) g.out.go_back(1) } - sym := g.table.get_type_symbol(it.typ) - if sym.kind == .string && !it.typ.is_ptr() { + sym := g.table.get_type_symbol(node.typ) + if sym.kind == .string && !node.typ.is_ptr() { // `string(x)` needs `tos()`, but not `&string(x) // `tos(str, len)`, `tos2(str)` - if it.has_arg { + if node.has_arg { g.write('tos((byteptr)') } else { g.write('tos2((byteptr)') } - g.expr(it.expr) - expr_sym := g.table.get_type_symbol(it.expr_type) + g.expr(node.expr) + expr_sym := g.table.get_type_symbol(node.expr_type) if expr_sym.kind == .array { // if we are casting an array, we need to add `.data` g.write('.data') } - if it.has_arg { + if node.has_arg { // len argument g.write(', ') - g.expr(it.arg) + g.expr(node.arg) } g.write(')') } else if sym.kind == .sum_type { - g.expr_with_cast(it.expr, it.expr_type, it.typ) + g.expr_with_cast(node.expr, node.expr_type, node.typ) } else { // styp := g.table.Type_to_str(it.typ) - styp := g.typ(it.typ) + styp := g.typ(node.typ) // g.write('($styp)(') g.write('(($styp)(') // if g.is_amp { // g.write('*') // } // g.write(')(') - g.expr(it.expr) + g.expr(node.expr) g.write('))') } } ast.CharLiteral { - g.write("'$it.val'") + g.write("'$node.val'") } ast.ComptimeCall { - g.comptime_call(it) + g.comptime_call(node) } ast.ConcatExpr { - g.concat_expr(it) + g.concat_expr(node) } ast.EnumVal { // g.write('${it.mod}${it.enum_name}_$it.val') - styp := g.typ(it.typ) - g.write('${styp}_$it.val') + styp := g.typ(node.typ) + g.write('${styp}_$node.val') } ast.FloatLiteral { - g.write(it.val) + g.write(node.val) } ast.Ident { - g.ident(it) + g.ident(node) } ast.IfExpr { - g.if_expr(it) + g.if_expr(node) } ast.IfGuardExpr { g.write('/* guard */') } ast.IndexExpr { - g.index_expr(it) + g.index_expr(node) } ast.InfixExpr { - g.infix_expr(it) + g.infix_expr(node) } ast.IntegerLiteral { - if it.val.starts_with('0o') { + if node.val.starts_with('0o') { g.write('0') - g.write(it.val[2..]) + g.write(node.val[2..]) } else { - g.write(it.val) // .int().str()) + g.write(node.val) // .int().str()) } } ast.MatchExpr { - g.match_expr(it) + g.match_expr(node) } ast.MapInit { - key_typ_str := g.typ(it.key_type) - value_typ_str := g.typ(it.value_type) - size := it.vals.len + key_typ_str := g.typ(node.key_type) + value_typ_str := g.typ(node.value_type) + size := node.vals.len if size > 0 { - g.write('new_map_init($size, sizeof($value_typ_str), _MOV((${key_typ_str}[$size]){') - for expr in it.keys { + g.write('new_map_init($size, sizeof($value_typ_str), _MOV(($key_typ_str[$size]){') + for expr in node.keys { g.expr(expr) g.write(', ') } - g.write('}), _MOV((${value_typ_str}[$size]){') - for expr in it.vals { + g.write('}), _MOV(($value_typ_str[$size]){') + for expr in node.vals { g.expr(expr) g.write(', ') } @@ -1531,21 +1541,21 @@ fn (mut g Gen) expr(node ast.Expr) { } ast.ParExpr { g.write('(') - g.expr(it.expr) + g.expr(node.expr) g.write(')') } ast.PostfixExpr { - g.expr(it.expr) - g.write(it.op.str()) + g.expr(node.expr) + g.write(node.op.str()) } ast.PrefixExpr { - if it.op == .amp { + if node.op == .amp { g.is_amp = true } // g.write('/*pref*/') - g.write(it.op.str()) + g.write(node.op.str()) // g.write('(') - g.expr(it.right) + g.expr(node.right) // g.write(')') g.is_amp = false } @@ -1553,11 +1563,11 @@ fn (mut g Gen) expr(node ast.Expr) { // Only used in IndexExpr } ast.SizeOf { - mut styp := it.type_name - if it.type_name == '' { - styp = g.typ(it.typ) + mut styp := node.type_name + if node.type_name == '' { + styp = g.typ(node.typ) } else { - sym := g.table.get_type_symbol(it.typ) + sym := g.table.get_type_symbol(node.typ) if sym.kind == .struct_ { info := sym.info as table.Struct if !info.is_typedef { @@ -1573,16 +1583,16 @@ fn (mut g Gen) expr(node ast.Expr) { g.write('sizeof($styp)') } ast.SqlExpr { - g.sql_select_expr(it) + g.sql_select_expr(node) } ast.StringLiteral { - if it.is_raw { - escaped_val := it.val.replace_each(['"', '\\"', '\\', '\\\\']) + if node.is_raw { + escaped_val := node.val.replace_each(['"', '\\"', '\\', '\\\\']) g.write('tos_lit("$escaped_val")') return } - escaped_val := it.val.replace_each(['"', '\\"', '\r\n', '\\n', '\n', '\\n']) - if g.is_c_call || it.language == .c { + escaped_val := node.val.replace_each(['"', '\\"', '\r\n', '\\n', '\n', '\\n']) + if g.is_c_call || node.language == .c { // In C calls we have to generate C strings // `C.printf("hi")` => `printf("hi");` g.write('"$escaped_val"') @@ -1598,43 +1608,43 @@ fn (mut g Gen) expr(node ast.Expr) { } } ast.StringInterLiteral { - g.string_inter_literal(it) + g.string_inter_literal(node) } ast.StructInit { // `user := User{name: 'Bob'}` - g.struct_init(it) + g.struct_init(node) } ast.SelectorExpr { - g.expr(it.expr) - if it.expr_type.is_ptr() { + g.expr(node.expr) + if node.expr_type.is_ptr() { g.write('->') } else { // g.write('. /*typ= $it.expr_type */') // ${g.typ(it.expr_type)} /') g.write('.') } - if it.expr_type == 0 { - verror('cgen: SelectorExpr | expr_type: 0 | it.expr: `${it.expr}` | field: `$it.field_name` | file: $g.file.path | line: $it.pos.line_nr') + if node.expr_type == 0 { + verror('cgen: SelectorExpr | expr_type: 0 | it.expr: `$node.expr` | field: `$node.field_name` | file: $g.file.path | line: $node.pos.line_nr') } - g.write(c_name(it.field_name)) + g.write(c_name(node.field_name)) } ast.Type { // match sum Type // g.write('/* Type */') - type_idx := it.typ.idx() - sym := g.table.get_type_symbol(it.typ) + type_idx := node.typ.idx() + sym := g.table.get_type_symbol(node.typ) g.write('$type_idx /* $sym.name */') } ast.TypeOf { - g.typeof_expr(it) + g.typeof_expr(node) } ast.Likely { - if it.is_likely { + if node.is_likely { g.write('_likely_') } else { g.write('_unlikely_') } g.write('(') - g.expr(it.expr) + g.expr(node.expr) g.write(')') } } @@ -1646,13 +1656,13 @@ fn (mut g Gen) typeof_expr(node ast.TypeOf) { // When encountering a .sum_type, typeof() should be done at runtime, // because the subtype of the expression may change: sum_type_idx := node.expr_type.idx() - g.write('tos3( /* ${sym.name} */ v_typeof_sumtype_${sum_type_idx}( (') + g.write('tos3( /* $sym.name */ v_typeof_sumtype_${sum_type_idx}( (') g.expr(node.expr) g.write(').typ ))') } else if sym.kind == .array_fixed { fixed_info := sym.info as table.ArrayFixed typ_name := g.table.get_type_name(fixed_info.elem_type) - g.write('tos_lit("[$fixed_info.size]${typ_name}")') + g.write('tos_lit("[$fixed_info.size]$typ_name")') } else if sym.kind == .function { info := sym.info as table.FnType fn_info := info.func @@ -1669,13 +1679,13 @@ fn (mut g Gen) typeof_expr(node ast.TypeOf) { } g.write('tos_lit("$repr")') } else { - g.write('tos_lit("${sym.name}")') + g.write('tos_lit("$sym.name")') } } fn (mut g Gen) enum_expr(node ast.Expr) { match node { - ast.EnumVal { g.write(it.val) } + ast.EnumVal { g.write(node.val) } else { g.expr(node) } } } @@ -1690,21 +1700,13 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) { // g.infix_op = node.op left_type := if node.left_type == table.t_type { g.cur_generic_type } else { node.left_type } left_sym := g.table.get_type_symbol(left_type) - unaliased_left := if left_sym.kind == .alias { - (left_sym.info as table.Alias).parent_typ - } else { - left_type - } + unaliased_left := if left_sym.kind == .alias { (left_sym.info as table.Alias).parent_typ } else { left_type } if node.op in [.key_is, .not_is] { g.is_expr(node) return } right_sym := g.table.get_type_symbol(node.right_type) - unaliased_right := if right_sym.kind == .alias { - (right_sym.info as table.Alias).parent_typ - } else { - node.right_type - } + unaliased_right := if right_sym.kind == .alias { (right_sym.info as table.Alias).parent_typ } else { node.right_type } if left_type == table.ustring_type_idx && node.op != .key_in && node.op != .not_in { fn_name := match node.op { .plus { @@ -1849,15 +1851,19 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) { } g.write(' }))') } - } else if unaliased_left.idx() in [table.u32_type_idx, table.u64_type_idx] && unaliased_right.is_signed() && node.op in [.eq, .ne, .gt, .lt, .ge, .le] { - bitsize := if unaliased_left.idx() == table.u32_type_idx && unaliased_right.idx() != table.i64_type_idx { 32 } else { 64 } + } else if unaliased_left.idx() in [table.u32_type_idx, table.u64_type_idx] && unaliased_right.is_signed() && + node.op in [.eq, .ne, .gt, .lt, .ge, .le] { + bitsize := if unaliased_left.idx() == table.u32_type_idx && unaliased_right.idx() != + table.i64_type_idx { 32 } else { 64 } g.write('_us${bitsize}_${cmp_str[int(node.op)-int(token.Kind.eq)]}(') g.expr(node.left) g.write(',') g.expr(node.right) g.write(')') - } else if unaliased_right.idx() in [table.u32_type_idx, table.u64_type_idx] && unaliased_left.is_signed() && node.op in [.eq, .ne, .gt, .lt, .ge, .le] { - bitsize := if unaliased_right.idx() == table.u32_type_idx && unaliased_left.idx() != table.i64_type_idx { 32 } else { 64 } + } else if unaliased_right.idx() in [table.u32_type_idx, table.u64_type_idx] && unaliased_left.is_signed() && + node.op in [.eq, .ne, .gt, .lt, .ge, .le] { + bitsize := if unaliased_right.idx() == table.u32_type_idx && unaliased_left.idx() != + table.i64_type_idx { 32 } else { 64 } g.write('_us${bitsize}_${cmp_rev[int(node.op)-int(token.Kind.eq)]}(') g.expr(node.right) g.write(',') @@ -1982,7 +1988,7 @@ fn (mut g Gen) match_expr(node ast.MatchExpr) { first_expr := branch.exprs[0] match first_expr { ast.Type { - it_type := g.typ(it.typ) + it_type := g.typ(first_expr.typ) // g.writeln('$it_type* it = ($it_type*)${tmp}.obj; // ST it') g.write('\t$it_type* it = ($it_type*)') g.expr(node.cond) @@ -2275,7 +2281,7 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) { [inline] fn (g Gen) expr_is_multi_return_call(expr ast.Expr) bool { match expr { - ast.CallExpr { return g.table.get_type_symbol(it.return_type).kind == .multi_return } + ast.CallExpr { return g.table.get_type_symbol(expr.return_type).kind == .multi_return } else { return false } } } @@ -2648,7 +2654,7 @@ fn (mut g Gen) gen_array_equality_fn(left table.Type) string { styp := g.table.value_type(left) left_sym := g.table.get_type_symbol(left) typ_name := g.typ(left) - ptr_typ := typ_name[typ_name.index_after('_', 0)+1..] + ptr_typ := typ_name[typ_name.index_after('_', 0) + 1..] elem_sym := g.table.get_type_symbol(left_sym.array_info().elem_type) mut elem_typ := '' if elem_sym.kind == .array { @@ -2659,19 +2665,19 @@ fn (mut g Gen) gen_array_equality_fn(left table.Type) string { return ptr_typ } g.array_fn_definitions << ptr_typ - g.definitions.writeln('bool ${ptr_typ}_arr_eq(array_${ptr_typ} a, array_${ptr_typ} b) {') + g.definitions.writeln('bool ${ptr_typ}_arr_eq(array_$ptr_typ a, array_$ptr_typ b) {') g.definitions.writeln('\tif (a.len != b.len) {') g.definitions.writeln('\t\treturn false;') g.definitions.writeln('\t}') g.definitions.writeln('\tfor (int i = 0; i < a.len; i++) {') if styp == table.string_type_idx { - g.definitions.writeln('\t\tif (string_ne(*((${ptr_typ}*)((byte*)a.data+(i*a.element_size))), *((${ptr_typ}*)((byte*)b.data+(i*b.element_size))))) {') + g.definitions.writeln('\t\tif (string_ne(*(($ptr_typ*)((byte*)a.data+(i*a.element_size))), *(($ptr_typ*)((byte*)b.data+(i*b.element_size))))) {') } else if elem_sym.kind == .struct_ { g.definitions.writeln('\t\tif (memcmp((byte*)a.data+(i*a.element_size), (byte*)b.data+(i*b.element_size), a.element_size)) {') } else if elem_sym.kind == .array { g.definitions.writeln('\t\tif (!${elem_typ}_arr_eq(a, b)) {') } else { - g.definitions.writeln('\t\tif (*((${ptr_typ}*)((byte*)a.data+(i*a.element_size))) != *((${ptr_typ}*)((byte*)b.data+(i*b.element_size)))) {') + g.definitions.writeln('\t\tif (*(($ptr_typ*)((byte*)a.data+(i*a.element_size))) != *(($ptr_typ*)((byte*)b.data+(i*b.element_size)))) {') } g.definitions.writeln('\t\t\treturn false;') g.definitions.writeln('\t\t}') @@ -2794,7 +2800,7 @@ fn (mut g Gen) write_types(types []table.TypeSymbol) { styp, base := g.optional_type_name(field.typ) g.optionals << styp g.typedefs2.writeln('typedef struct $styp $styp;') - g.type_definitions.writeln('${g.optional_type_text(styp, base)};') + g.type_definitions.writeln('${g.optional_type_text(styp,base)};') g.type_definitions.write(last_text) } type_name := g.typ(field.typ) @@ -2939,13 +2945,13 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) { if fspec == `p` { g.write('${fmt}p') } else { - g.write('${fmt}"PRI${fspec:c}PTR"') + g.write('$fmt"PRI${fspec:c}PTR"') } } else if node.expr_types[i].is_int() { if fspec == `c` { g.write('${fmt}c') } else { - g.write('${fmt}"PRI${fspec:c}') + g.write('$fmt"PRI${fspec:c}') if node.expr_types[i] in [table.i8_type, table.byte_type] { g.write('8') } else if node.expr_types[i] in [table.i16_type, table.u16_type] { @@ -2959,7 +2965,7 @@ fn (mut g Gen) string_inter_literal(node ast.StringInterLiteral) { } } else { // TODO: better check this case - g.write('${fmt}"PRId32"') + g.write('$fmt"PRId32"') } if i < node.exprs.len - 1 { g.write('\\000') @@ -3293,10 +3299,10 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type table. if i == stmts.len - 1 { expr_stmt := stmt as ast.ExprStmt g.stmt_path_pos << g.out.len - g.write('*(${mr_styp}*) ${cvar_name}.data = ') + g.write('*($mr_styp*) ${cvar_name}.data = ') is_opt_call := expr_stmt.expr is ast.CallExpr && expr_stmt.typ.has_flag(.optional) if is_opt_call { - g.write('*(${mr_styp}*) ') + g.write('*($mr_styp*) ') } g.expr(expr_stmt.expr) if is_opt_call { @@ -3339,7 +3345,7 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type table. fn (mut g Gen) type_of_call_expr(node ast.Expr) string { match node { - ast.CallExpr { return g.typ(it.return_type) } + ast.CallExpr { return g.typ(node.return_type) } else { return typeof(node) } } return '' @@ -3491,7 +3497,7 @@ fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) string { } else { if is_comptime_optional || (g.pref.compile_defines_all.len > 0 && name in g.pref.compile_defines_all) { - return 'CUSTOM_DEFINE_${name}' + return 'CUSTOM_DEFINE_$name' } verror('bad os ifdef name "$name"') } @@ -3593,7 +3599,7 @@ pub fn (mut g Gen) write_tests_main() { g.writeln('') all_tfuncs := g.get_all_test_function_names() if g.pref.is_stats { - g.writeln('\tBenchedTests bt = start_testing(${all_tfuncs.len}, tos_lit("$g.pref.path"));') + g.writeln('\tBenchedTests bt = start_testing($all_tfuncs.len, tos_lit("$g.pref.path"));') } for t in all_tfuncs { g.writeln('') @@ -3685,12 +3691,12 @@ fn (mut g Gen) go_stmt(node ast.GoStmt) { 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.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.write('$arg_tmp_var->arg${i+1} = ') g.expr(arg.expr) g.writeln(';') } @@ -3814,11 +3820,11 @@ fn (mut g Gen) gen_str_for_type_with_styp(typ table.Type, styp string) string { '| expects_ptr: $str_method_expects_ptr') */ str_fn_name_no_ptr := '${str_fn_name}_no_ptr' - already_generated_key_no_ptr := '${styp}:${str_fn_name_no_ptr}' + already_generated_key_no_ptr := '$styp:$str_fn_name_no_ptr' if already_generated_key_no_ptr !in g.str_types { g.str_types << already_generated_key_no_ptr - g.type_definitions.writeln('string ${str_fn_name_no_ptr}(${styp} it); // auto no_ptr version') - g.auto_str_funcs.writeln('string ${str_fn_name_no_ptr}(${styp} it){ return ${str_fn_name}(&it); }') + g.type_definitions.writeln('string ${str_fn_name_no_ptr}($styp it); // auto no_ptr version') + g.auto_str_funcs.writeln('string ${str_fn_name_no_ptr}($styp it){ return ${str_fn_name}(&it); }') } /* typ_is_ptr := typ.is_ptr() @@ -3828,7 +3834,7 @@ fn (mut g Gen) gen_str_for_type_with_styp(typ table.Type, styp string) string { */ return str_fn_name_no_ptr } - already_generated_key := '${styp}:${str_fn_name}' + already_generated_key := '$styp:$str_fn_name' if !sym_has_str_method && already_generated_key !in g.str_types { $if debugautostr ? { eprintln('> gen_str_for_type_with_styp: |typ: ${typ:5}, ${sym.name:20}|has_str: ${sym_has_str_method:5}|expects_ptr: ${str_method_expects_ptr:5}|nr_args: ${str_nr_args:1}|fn_name: ${str_fn_name:20}') @@ -3841,7 +3847,7 @@ fn (mut g Gen) gen_str_for_type_with_styp(typ table.Type, styp string) string { table.Enum { g.gen_str_for_enum(it, styp, str_fn_name) } table.Struct { g.gen_str_for_struct(it, styp, str_fn_name) } table.Map { g.gen_str_for_map(it, styp, str_fn_name) } - else { verror("could not generate string method $str_fn_name for type \'${styp}\'") } + else { verror("could not generate string method $str_fn_name for type \'$styp\'") } } } // if varg, generate str for varg @@ -3872,14 +3878,14 @@ fn (mut g Gen) gen_str_default(sym table.TypeSymbol, styp, str_fn_name string) { convertor = 'bool' typename_ = 'bool' } else { - verror("could not generate string method for type \'${styp}\'") + verror("could not generate string method for type \'$styp\'") } g.type_definitions.writeln('string ${str_fn_name}($styp it); // auto') g.auto_str_funcs.writeln('string ${str_fn_name}($styp it) {') if convertor == 'bool' { - g.auto_str_funcs.writeln('\tstring tmp1 = string_add(tos_lit("${styp}("), (${convertor})it ? tos_lit("true") : tos_lit("false"));') + g.auto_str_funcs.writeln('\tstring tmp1 = string_add(tos_lit("${styp}("), ($convertor)it ? tos_lit("true") : tos_lit("false"));') } else { - g.auto_str_funcs.writeln('\tstring tmp1 = string_add(tos_lit("${styp}("), tos3(${typename_}_str((${convertor})it).str));') + g.auto_str_funcs.writeln('\tstring tmp1 = string_add(tos_lit("${styp}("), tos3(${typename_}_str(($convertor)it).str));') } g.auto_str_funcs.writeln('\tstring tmp2 = string_add(tmp1, tos_lit(")"));') g.auto_str_funcs.writeln('\tstring_free(&tmp1);') @@ -3919,18 +3925,18 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) { mut clean_struct_v_type_name := styp.replace('__', '.') if styp.ends_with('*') { deref_typ := styp.replace('*', '') - g.auto_str_funcs.writeln('\t${deref_typ} *it = x;') + g.auto_str_funcs.writeln('\t$deref_typ *it = x;') clean_struct_v_type_name = '&' + clean_struct_v_type_name.replace('*', '') } else { deref_typ := styp - g.auto_str_funcs.writeln('\t${deref_typ} *it = &x;') + g.auto_str_funcs.writeln('\t$deref_typ *it = &x;') } // generate ident / indent length = 4 spaces g.auto_str_funcs.writeln('\tstring indents = tos_lit("");') g.auto_str_funcs.writeln('\tfor (int i = 0; i < indent_count; i++) {') g.auto_str_funcs.writeln('\t\tindents = string_add(indents, tos_lit(" "));') g.auto_str_funcs.writeln('\t}') - g.auto_str_funcs.writeln('\treturn _STR("${clean_struct_v_type_name} {\\n"') + g.auto_str_funcs.writeln('\treturn _STR("$clean_struct_v_type_name {\\n"') for field in info.fields { fmt := g.type_to_fmt(field.typ) g.auto_str_funcs.writeln('\t\t"%.*s\\000 ' + '$field.name: $fmt\\n"') @@ -3952,7 +3958,7 @@ fn (mut g Gen) gen_str_for_struct(info table.Struct, styp, str_fn_name string) { g.auto_str_funcs.write('${field_styp_fn_name}( it->${c_name(field.name)} ) ') } else if sym.kind == .struct_ { g.auto_str_funcs.write('indents, ') - g.auto_str_funcs.write('${field_styp_fn_name}( it->${c_name(field.name)}${second_str_param} ) ') + g.auto_str_funcs.write('${field_styp_fn_name}( it->${c_name(field.name)}$second_str_param ) ') } else if sym.kind in [.array, .array_fixed, .map] { g.auto_str_funcs.write('indents, ') g.auto_str_funcs.write('${field_styp_fn_name}( it->${c_name(field.name)}) ') @@ -3979,11 +3985,8 @@ fn (mut g Gen) gen_str_for_array(info table.Array, styp, str_fn_name string) { sym_has_str_method, str_method_expects_ptr, _ := sym.str_method_info() mut elem_str_fn_name := '' if sym_has_str_method { - elem_str_fn_name = if is_elem_ptr { - field_styp.replace('*', '') + '_str' - } else { - field_styp + '_str' - } + elem_str_fn_name = if is_elem_ptr { field_styp.replace('*', '') + '_str' } else { field_styp + + '_str' } } else { elem_str_fn_name = styp_to_str_fn_name(field_styp) } @@ -3996,7 +3999,7 @@ fn (mut g Gen) gen_str_for_array(info table.Array, styp, str_fn_name string) { g.auto_str_funcs.writeln('\tstrings__Builder sb = strings__new_builder(a.len * 10);') g.auto_str_funcs.writeln('\tstrings__Builder_write(&sb, tos_lit("["));') g.auto_str_funcs.writeln('\tfor (int i = 0; i < a.len; i++) {') - g.auto_str_funcs.writeln('\t\t${field_styp} it = (*(${field_styp}*)array_get(a, i));') + g.auto_str_funcs.writeln('\t\t$field_styp it = (*($field_styp*)array_get(a, i));') if sym.kind == .struct_ && !sym_has_str_method { if is_elem_ptr { g.auto_str_funcs.writeln('\t\tstring x = ${elem_str_fn_name}(*it,0);') @@ -4041,11 +4044,8 @@ fn (mut g Gen) gen_str_for_array_fixed(info table.ArrayFixed, styp, str_fn_name sym_has_str_method, str_method_expects_ptr, _ := sym.str_method_info() mut elem_str_fn_name := '' if sym_has_str_method { - elem_str_fn_name = if is_elem_ptr { - field_styp.replace('*', '') + '_str' - } else { - field_styp + '_str' - } + elem_str_fn_name = if is_elem_ptr { field_styp.replace('*', '') + '_str' } else { field_styp + + '_str' } } else { elem_str_fn_name = styp_to_str_fn_name(field_styp) } @@ -4203,9 +4203,9 @@ fn (g &Gen) interface_table() string { mut methods_struct := strings.new_builder(100) methods_struct.writeln('$methods_struct_name ${interface_name}_name_table[$inter_info.types.len] = {') mut cast_functions := strings.new_builder(100) - cast_functions.write('// Casting functions for interface "${interface_name}"') + cast_functions.write('// Casting functions for interface "$interface_name"') mut methods_wrapper := strings.new_builder(100) - methods_wrapper.writeln('// Methods wrapper for interface "${interface_name}"') + methods_wrapper.writeln('// Methods wrapper for interface "$interface_name"') for i, st in inter_info.types { // cctype is the Cleaned Concrete Type name, *without ptr*, // i.e. cctype is always just Cat, not Cat_ptr: @@ -4213,18 +4213,18 @@ fn (g &Gen) interface_table() string { // Speaker_Cat_index = 0 interface_index_name := '_${interface_name}_${cctype}_index' cast_functions.writeln(' -_Interface I_${cctype}_to_Interface_${interface_name}(${cctype}* x) { +_Interface I_${cctype}_to_Interface_${interface_name}($cctype* x) { return (_Interface) { ._object = (void*) (x), - ._interface_idx = ${interface_index_name} + ._interface_idx = $interface_index_name }; } -_Interface* I_${cctype}_to_Interface_${interface_name}_ptr(${cctype}* x) { +_Interface* I_${cctype}_to_Interface_${interface_name}_ptr($cctype* x) { /* TODO Remove memdup */ return (_Interface*) memdup(&(_Interface) { ._object = (void*) (x), - ._interface_idx = ${interface_index_name} + ._interface_idx = $interface_index_name }, sizeof(_Interface)); }') methods_struct.writeln('\t{') @@ -4242,12 +4242,12 @@ _Interface* I_${cctype}_to_Interface_${interface_name}_ptr(${cctype}* x) { continue } // .speak = Cat_speak - mut method_call := '${cctype}_${method.name}' + mut method_call := '${cctype}_$method.name' if !method.args[0].typ.is_ptr() { // inline void Cat_speak_method_wrapper(Cat c) { return Cat_speak(*c); } methods_wrapper.write('static inline ${g.typ(method.return_type)}') methods_wrapper.write(' ${method_call}_method_wrapper(') - methods_wrapper.write('${cctype}* ${method.args[0].name}') + methods_wrapper.write('$cctype* ${method.args[0].name}') // TODO g.fn_args for j in 1 .. method.args.len { arg := method.args[j] @@ -4270,7 +4270,7 @@ _Interface* I_${cctype}_to_Interface_${interface_name}_ptr(${cctype}* x) { methods_struct.writeln('\t\t.${c_name(method.name)} = $method_call,') } methods_struct.writeln('\t},') - sb.writeln('int ${interface_index_name} = $i;') + sb.writeln('int $interface_index_name = $i;') } methods_struct.writeln('};') // add line return after interface index declarations @@ -4366,7 +4366,7 @@ fn (mut g Gen) array_init(it ast.ArrayInit) { fn (mut g Gen) interface_call(typ, interface_type table.Type) { interface_styp := g.cc_type(interface_type) styp := g.cc_type(typ) - mut cast_fn_name := 'I_${styp}_to_Interface_${interface_styp}' + mut cast_fn_name := 'I_${styp}_to_Interface_$interface_styp' if interface_type.is_ptr() { cast_fn_name += '_ptr' } diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 1cb31e9568..96359157e2 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -11,11 +11,11 @@ import v.util pub fn (mut p Parser) call_expr(language table.Language, mod string) ast.CallExpr { first_pos := p.tok.position() fn_name := if language == .c { - 'C.${p.check_name()}' + 'C.$p.check_name()' } else if language == .js { - 'JS.${p.check_js_name()}' + 'JS.$p.check_js_name()' } else if mod.len > 0 { - '${mod}.${p.check_name()}' + '${mod}.$p.check_name()' } else { p.check_name() } @@ -181,11 +181,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl { mut name := '' if p.tok.kind == .name { // TODO high order fn - name = if language == .js { - p.check_js_name() - } else { - p.check_name() - } + name = if language == .js { p.check_js_name() } else { p.check_name() } if language == .v && !p.pref.translated && util.contains_capital(name) { p.error('function names cannot contain uppercase letters, use snake_case instead') } @@ -491,7 +487,7 @@ fn have_fn_main(stmts []ast.Stmt) bool { for stmt in stmts { match stmt { ast.FnDecl { - if it.name == 'main' { + if stmt.name == 'main' { has_main_fn = true } } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index 1d9405d729..a032d7834f 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -280,11 +280,11 @@ pub fn (mut p Parser) close_scope() { for _, obj in p.scope.objects { match obj { ast.Var { - if !it.is_used && it.name[0] != `_` { + if !obj.is_used && obj.name[0] != `_` { if p.pref.is_prod { - p.error_with_pos('unused variable: `$it.name`', it.pos) + p.error_with_pos('unused variable: `$obj.name`', obj.pos) } else { - p.warn_with_pos('unused variable: `$it.name`', it.pos) + p.warn_with_pos('unused variable: `$obj.name`', obj.pos) } } /* @@ -356,7 +356,7 @@ fn (mut p Parser) check(expected token.Kind) { // p.next() // } if p.tok.kind != expected { - p.error('unexpected `${p.tok.kind.str()}`, expecting `${expected.str()}`') + p.error('unexpected `$p.tok.kind.str()`, expecting `$expected.str()`') } p.next() } @@ -525,12 +525,10 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt { return ast.GotoLabel{ name: name } - } - else if p.peek_tok.kind == .name { + } else if p.peek_tok.kind == .name { p.error_with_pos('unexpected name `$p.peek_tok.lit`', p.peek_tok.position()) - } - else if !p.inside_if_expr && !p.inside_match_body && !p.inside_or_expr && p.peek_tok.kind in - [.rcbr, .eof] { + } else if !p.inside_if_expr && !p.inside_match_body && !p.inside_or_expr && + p.peek_tok.kind in [.rcbr, .eof] { p.error_with_pos('`$p.tok.lit` evaluated but not used', p.tok.position()) } } @@ -608,7 +606,6 @@ pub fn (mut p Parser) stmt(is_top_level bool) ast.Stmt { } } - fn (mut p Parser) expr_list() []ast.Expr { mut exprs := []ast.Expr{} for { @@ -641,7 +638,7 @@ fn (mut p Parser) attributes(is_top_stmt bool) []ast.Attr { p.next() break } - p.error('unexpected `${p.tok.kind.str()}`, expecting `${expected.str()}`') + p.error('unexpected `$p.tok.kind.str()`, expecting `$expected.str()`') } p.next() } @@ -752,11 +749,9 @@ fn (mut p Parser) parse_multi_expr(is_top_level bool) ast.Stmt { left0 := left[0] if p.tok.kind in [.assign, .decl_assign] || p.tok.kind.is_assign() { return p.partial_assign_stmt(left) - } - else if is_top_level && tok.kind !in [.key_if, .key_match] && - left0 !is ast.CallExpr && left0 !is ast.PostfixExpr && - !(left0 is ast.InfixExpr && (left0 as ast.InfixExpr).op == .left_shift) && - left0 !is ast.ComptimeCall { + } else if is_top_level && tok.kind !in [.key_if, .key_match] && left0 !is ast.CallExpr && + left0 !is ast.PostfixExpr && !(left0 is ast.InfixExpr && (left0 as ast.InfixExpr).op == .left_shift) && + left0 !is ast.ComptimeCall { p.error_with_pos('expression evaluated but not used', left0.position()) } if left.len == 1 { @@ -860,7 +855,7 @@ pub fn (mut p Parser) name_expr() ast.Expr { match mut obj { ast.Var { known_var = true - it.is_used = true + obj.is_used = true } else {} } @@ -1141,11 +1136,7 @@ fn (mut p Parser) string_expr() ast.Expr { node = ast.StringLiteral{ val: val is_raw: is_raw - language: if is_cstr { - table.Language.c - } else { - table.Language.v - } + language: if is_cstr { table.Language.c } else { table.Language.v } pos: pos } return node @@ -1222,7 +1213,7 @@ fn (mut p Parser) string_expr() ast.Expr { node = ast.StringInterLiteral{ vals: vals exprs: exprs - need_fmts: has_fmts // prelimery - until checker finds out if really needed + need_fmts: has_fmts fwidths: fwidths precisions: precisions pluss: visible_pluss @@ -1231,6 +1222,7 @@ fn (mut p Parser) string_expr() ast.Expr { fmt_poss: fposs pos: pos } + // need_fmts: prelimery - until checker finds out if really needed p.inside_str_interp = false return node } diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index 882b4a9cd2..c6f034d4f5 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -191,8 +191,7 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { op: tok.kind pos: pos } - } - else if p.tok.kind.is_infix() { + } else if p.tok.kind.is_infix() { // return early for deref assign `*x = 2` goes to prefix expr if p.tok.kind == .mul && p.tok.line_nr != p.prev_tok.line_nr && p.peek_tok2.kind == .assign {