fmt: use the new receiver syntax

pull/4447/head
Alexander Medvednikov 2020-04-16 15:42:40 +02:00
parent 4b5acfd960
commit 714ff50322
4 changed files with 115 additions and 117 deletions

View File

@ -4,24 +4,22 @@
module ast
// These methods are used only by vfmt, vdoc, and for debugging.
import (
v.table
strings
)
import v.table
import strings
pub fn (node &FnDecl) str(t &table.Table) string {
mut f := strings.new_builder(30)
var f := strings.new_builder(30)
if node.is_pub {
f.write('pub ')
}
mut receiver := ''
var receiver := ''
if node.is_method {
mut styp := t.type_to_str(node.receiver.typ)
mut m := if node.rec_mut { 'mut ' } else { '' }
var styp := t.type_to_str(node.receiver.typ)
var m := if node.rec_mut { 'var ' } else { '' }
if node.rec_mut {
styp = styp[1..] // remove &
}
receiver = '($node.receiver.name $m$styp) '
receiver = '($m$node.receiver.name $styp) '
/*
sym := t.get_type_symbol(node.receiver.typ)
name := sym.name.after('.')
@ -32,7 +30,7 @@ pub fn (node &FnDecl) str(t &table.Table) string {
receiver = '($node.receiver.name $m$name) '
*/
}
mut name := node.name.after('.')
var name := node.name.after('.')
if node.is_c {
name = 'C.$name'
}
@ -49,7 +47,7 @@ pub fn (node &FnDecl) str(t &table.Table) string {
should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ || (node.is_variadic &&
i == node.args.len - 2)
f.write(arg.name)
mut s := t.type_to_str(arg.typ)
var s := t.type_to_str(arg.typ)
if arg.is_mut {
f.write(' mut')
if s.starts_with('&') {
@ -163,7 +161,7 @@ pub fn (a CallArg) str() string {
}
pub fn args2str(args []CallArg) string {
mut res := []string
var res := []string
for a in args {
res << a.str()
}
@ -173,7 +171,7 @@ pub fn args2str(args []CallArg) string {
pub fn (node Stmt) str() string {
match node {
AssignStmt {
mut out := ''
var out := ''
for i, ident in it.left {
var_info := ident.var_info()
if var_info.is_mut {

View File

@ -138,7 +138,7 @@ pub fn (g Gen) hashes() string {
return res
}
pub fn (g mut Gen) init() {
pub fn (var g Gen) init() {
g.cheaders.writeln('// Generated by the V compiler')
g.cheaders.writeln('#include <inttypes.h>') // int64_t etc
g.cheaders.writeln(c_builtin_types)
@ -163,7 +163,7 @@ pub fn (g mut Gen) init() {
}
}
pub fn (g mut Gen) finish() {
pub fn (var g Gen) finish() {
if g.pref.build_mode != .build_module {
g.stringliterals.writeln('}')
}
@ -171,7 +171,7 @@ pub fn (g mut Gen) finish() {
g.stringliterals.writeln('')
}
pub fn (g mut Gen) write_typeof_functions() {
pub fn (var g Gen) write_typeof_functions() {
g.writeln('')
g.writeln('// >> typeof() support for sum types')
for typ in g.table.types {
@ -195,7 +195,7 @@ pub fn (g mut Gen) write_typeof_functions() {
}
// V type to C type
pub fn (g mut Gen) typ(t table.Type) string {
pub fn (var g Gen) typ(t table.Type) string {
nr_muls := table.type_nr_muls(t)
sym := g.table.get_type_symbol(t)
var styp := sym.name.replace('.', '__')
@ -228,7 +228,7 @@ pub fn (g mut Gen) typ(t table.Type) string {
}
//
pub fn (g mut Gen) write_typedef_types() {
pub fn (var g Gen) write_typedef_types() {
for typ in g.table.types {
match typ.kind {
.alias {
@ -267,7 +267,7 @@ pub fn (g mut Gen) write_typedef_types() {
}
}
pub fn (g mut Gen) write_multi_return_types() {
pub fn (var g Gen) write_multi_return_types() {
g.definitions.writeln('// multi return structs')
for typ in g.table.types {
// sym := g.table.get_type_symbol(typ)
@ -288,7 +288,7 @@ pub fn (g mut Gen) write_multi_return_types() {
}
}
pub fn (g mut Gen) write_variadic_types() {
pub fn (var g Gen) write_variadic_types() {
if g.variadic_args.size > 0 {
g.definitions.writeln('// variadic structs')
}
@ -307,7 +307,7 @@ pub fn (g mut Gen) write_variadic_types() {
pub fn (g Gen) save() {
}
pub fn (g mut Gen) write(s string) {
pub fn (var g Gen) write(s string) {
if g.indent > 0 && g.empty_line {
g.out.write(tabs[g.indent])
// g.line_len += g.indent * 4
@ -316,7 +316,7 @@ pub fn (g mut Gen) write(s string) {
g.empty_line = false
}
pub fn (g mut Gen) writeln(s string) {
pub fn (var g Gen) writeln(s string) {
if g.indent > 0 && g.empty_line {
g.out.write(tabs[g.indent])
}
@ -324,16 +324,16 @@ pub fn (g mut Gen) writeln(s string) {
g.empty_line = true
}
pub fn (g mut Gen) new_tmp_var() string {
pub fn (var g Gen) new_tmp_var() string {
g.tmp_count++
return 'tmp$g.tmp_count'
}
pub fn (g mut Gen) reset_tmp_count() {
pub fn (var g Gen) reset_tmp_count() {
g.tmp_count = 0
}
fn (g mut Gen) stmts(stmts []ast.Stmt) {
fn (var g Gen) stmts(stmts []ast.Stmt) {
g.indent++
if g.inside_ternary {
g.write(' ( ')
@ -350,7 +350,7 @@ fn (g mut Gen) stmts(stmts []ast.Stmt) {
g.indent--
}
fn (g mut Gen) stmt(node ast.Stmt) {
fn (var g Gen) stmt(node ast.Stmt) {
g.stmt_start_pos = g.out.len
// println('cgen.stmt()')
// g.writeln('//// stmt start')
@ -530,7 +530,7 @@ fn (g mut Gen) stmt(node ast.Stmt) {
}
}
fn (g mut Gen) write_defer_stmts() {
fn (var g Gen) write_defer_stmts() {
for defer_stmt in g.defer_stmts {
g.writeln('// defer')
if defer_stmt.ifdef.len > 0 {
@ -544,7 +544,7 @@ fn (g mut Gen) write_defer_stmts() {
}
}
fn (g mut Gen) for_in(it ast.ForInStmt) {
fn (var g Gen) for_in(it ast.ForInStmt) {
if it.is_range {
// `for x in 1..10 {`
i := g.new_tmp_var()
@ -624,7 +624,7 @@ fn (g mut Gen) for_in(it ast.ForInStmt) {
}
// use instead of expr() when you need to cast to sum type (can add other casts also)
fn (g mut Gen) expr_with_cast(expr ast.Expr, got_type, exp_type table.Type) {
fn (var g Gen) expr_with_cast(expr ast.Expr, got_type, exp_type table.Type) {
// cast to sum type
if exp_type != table.void_type {
exp_sym := g.table.get_type_symbol(exp_type)
@ -646,7 +646,7 @@ fn (g mut Gen) expr_with_cast(expr ast.Expr, got_type, exp_type table.Type) {
g.expr(expr)
}
fn (g mut Gen) gen_assert_stmt(a ast.AssertStmt) {
fn (var g Gen) gen_assert_stmt(a ast.AssertStmt) {
g.writeln('// assert')
g.inside_ternary = true
g.write('if (')
@ -677,7 +677,7 @@ fn (g mut Gen) gen_assert_stmt(a ast.AssertStmt) {
g.writeln('}')
}
fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
fn (var g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
// g.write('/*assign_stmt*/')
if assign_stmt.is_static {
g.write('static ')
@ -790,7 +790,7 @@ fn (g mut Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
}
}
fn (g mut Gen) gen_clone_assignment(val ast.Expr, right_sym table.TypeSymbol, add_eq bool) bool {
fn (var g Gen) gen_clone_assignment(val ast.Expr, right_sym table.TypeSymbol, add_eq bool) bool {
var is_ident := false
match val {
ast.Ident {
@ -823,7 +823,7 @@ fn (g mut Gen) gen_clone_assignment(val ast.Expr, right_sym table.TypeSymbol, ad
return true
}
fn (g mut Gen) free_scope_vars(pos int) {
fn (var g Gen) free_scope_vars(pos int) {
scope := g.file.scope.innermost(pos)
for _, obj in scope.objects {
match obj {
@ -864,7 +864,7 @@ fn (g mut Gen) free_scope_vars(pos int) {
}
}
fn (g mut Gen) expr(node ast.Expr) {
fn (var g Gen) expr(node ast.Expr) {
// println('cgen expr() line_nr=$node.pos.line_nr')
match node {
ast.ArrayInit {
@ -1092,7 +1092,7 @@ fn (g mut Gen) expr(node ast.Expr) {
}
}
fn (g mut Gen) typeof_expr(node ast.TypeOf) {
fn (var g Gen) typeof_expr(node ast.TypeOf) {
sym := g.table.get_type_symbol(node.expr_type)
if sym.kind == .sum_type {
// When encountering a .sum_type, typeof() should be done at runtime,
@ -1110,7 +1110,7 @@ fn (g mut Gen) typeof_expr(node ast.TypeOf) {
}
}
fn (g mut Gen) enum_expr(node ast.Expr) {
fn (var g Gen) enum_expr(node ast.Expr) {
match node {
ast.EnumVal {
g.write(it.val)
@ -1121,7 +1121,7 @@ fn (g mut Gen) enum_expr(node ast.Expr) {
}
}
fn (g mut Gen) assign_expr(node ast.AssignExpr) {
fn (var g Gen) assign_expr(node ast.AssignExpr) {
// g.write('/*assign_expr*/')
var is_call := false
var or_stmts := []ast.Stmt
@ -1197,7 +1197,7 @@ fn (g mut Gen) assign_expr(node ast.AssignExpr) {
g.is_assign_rhs = false
}
fn (g mut Gen) infix_expr(node ast.InfixExpr) {
fn (var g Gen) infix_expr(node ast.InfixExpr) {
// println('infix_expr() op="$node.op.str()" line_nr=$node.pos.line_nr')
// g.write('/*infix*/')
// if it.left_type == table.string_type_idx {
@ -1363,7 +1363,7 @@ fn (g mut Gen) infix_expr(node ast.InfixExpr) {
}
}
fn (g mut Gen) match_expr(node ast.MatchExpr) {
fn (var g Gen) match_expr(node ast.MatchExpr) {
// println('match expr typ=$it.expr_type')
// TODO
if node.cond_type == 0 {
@ -1467,7 +1467,7 @@ fn (g mut Gen) match_expr(node ast.MatchExpr) {
g.inside_ternary = was_inside_ternary
}
fn (g mut Gen) ident(node ast.Ident) {
fn (var g Gen) ident(node ast.Ident) {
if node.name == 'lld' {
return
}
@ -1499,7 +1499,7 @@ fn (g mut Gen) ident(node ast.Ident) {
g.write(name)
}
fn (g mut Gen) if_expr(node ast.IfExpr) {
fn (var g Gen) if_expr(node ast.IfExpr) {
// println('if_expr pos=$node.pos.line_nr')
// g.writeln('/* if is_expr=$node.is_expr */')
// If expression? Assign the value to a temp var.
@ -1571,7 +1571,7 @@ fn (g mut Gen) if_expr(node ast.IfExpr) {
}
}
fn (g mut Gen) index_expr(node ast.IndexExpr) {
fn (var g Gen) index_expr(node ast.IndexExpr) {
// TODO else doesn't work with sum types
var is_range := false
match node.index {
@ -1763,7 +1763,7 @@ fn (g mut Gen) index_expr(node ast.IndexExpr) {
}
}
fn (g mut Gen) return_statement(node ast.Return) {
fn (var g Gen) return_statement(node ast.Return) {
g.write('return')
if g.fn_decl.name == 'main' {
g.writeln(' 0;')
@ -1841,7 +1841,7 @@ fn (g mut Gen) return_statement(node ast.Return) {
g.writeln(';')
}
fn (g mut Gen) const_decl(node ast.ConstDecl) {
fn (var g Gen) const_decl(node ast.ConstDecl) {
for i, field in node.fields {
name := c_name(field.name)
// TODO hack. Cut the generated value and paste it into definitions.
@ -1873,7 +1873,7 @@ fn (g mut Gen) const_decl(node ast.ConstDecl) {
}
}
fn (g mut Gen) const_decl_simple_define(name, val string) {
fn (var g Gen) const_decl_simple_define(name, val string) {
// Simple expressions should use a #define
// so that we don't pollute the binary with unnecessary global vars
// Do not do this when building a module, otherwise the consts
@ -1882,7 +1882,7 @@ fn (g mut Gen) const_decl_simple_define(name, val string) {
g.definitions.writeln(val)
}
fn (g mut Gen) struct_init(struct_init ast.StructInit) {
fn (var g Gen) struct_init(struct_init ast.StructInit) {
var info := table.Struct{}
var is_struct := false
sym := g.table.get_type_symbol(struct_init.typ)
@ -1948,7 +1948,7 @@ fn (g mut Gen) struct_init(struct_init ast.StructInit) {
}
// { user | name: 'new name' }
fn (g mut Gen) assoc(node ast.Assoc) {
fn (var g Gen) assoc(node ast.Assoc) {
g.writeln('// assoc')
if node.typ == 0 {
return
@ -1977,7 +1977,7 @@ fn (g mut Gen) assoc(node ast.Assoc) {
}
}
fn (g mut Gen) generate_array_equality_fn(ptr_typ string, styp table.Type, sym &table.TypeSymbol) {
fn (var g Gen) generate_array_equality_fn(ptr_typ string, styp table.Type, sym &table.TypeSymbol) {
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('\tif (a.len != b.len) {')
@ -2002,7 +2002,7 @@ fn verror(s string) {
util.verror('cgen error', s)
}
fn (g mut Gen) write_init_function() {
fn (var g Gen) write_init_function() {
g.writeln('void _vinit() {')
g.writeln('\tbuiltin_init();')
g.writeln('\tvinit_string_literals();')
@ -2028,7 +2028,7 @@ fn (g mut Gen) write_init_function() {
}
}
fn (g mut Gen) write_str_fn_definitions() {
fn (var g Gen) write_str_fn_definitions() {
// _STR function can't be defined in vlib
g.writeln('
string _STR(const char *fmt, ...) {
@ -2069,7 +2069,7 @@ const (
builtins = ['string', 'array', 'KeyValue', 'DenseArray', 'map', 'Option']
)
fn (g mut Gen) write_builtin_types() {
fn (var g Gen) write_builtin_types() {
var builtin_types := []table.TypeSymbol // builtin types
// builtin types need to be on top
// everything except builtin will get sorted
@ -2082,7 +2082,7 @@ fn (g mut Gen) write_builtin_types() {
// C struct definitions, ordered
// Sort the types, make sure types that are referenced by other types
// are added before them.
fn (g mut Gen) write_sorted_types() {
fn (var g Gen) write_sorted_types() {
var types := []table.TypeSymbol // structs that need to be sorted
for typ in g.table.types {
if !(typ.name in builtins) {
@ -2097,7 +2097,7 @@ fn (g mut Gen) write_sorted_types() {
g.write_types(types_sorted)
}
fn (g mut Gen) write_types(types []table.TypeSymbol) {
fn (var g Gen) write_types(types []table.TypeSymbol) {
for typ in types {
if typ.name.starts_with('C.') {
continue
@ -2203,7 +2203,7 @@ fn (g Gen) sort_structs(typesa []table.TypeSymbol) []table.TypeSymbol {
return types_sorted
}
fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) {
fn (var g Gen) string_inter_literal(node ast.StringInterLiteral) {
g.write('_STR("')
// Build the string with %
for i, val in node.vals {
@ -2318,7 +2318,7 @@ fn (g mut Gen) string_inter_literal(node ast.StringInterLiteral) {
}
// `nums.filter(it % 2 == 0)`
fn (g mut Gen) gen_filter(node ast.CallExpr) {
fn (var g Gen) gen_filter(node ast.CallExpr) {
tmp := g.new_tmp_var()
s := g.out.after(g.stmt_start_pos) // the already generated part of current statement
g.out.go_back(s.len)
@ -2346,7 +2346,7 @@ fn (g mut Gen) gen_filter(node ast.CallExpr) {
g.write(tmp)
}
fn (g mut Gen) insert_before(s string) {
fn (var g Gen) insert_before(s string) {
cur_line := g.out.after(g.stmt_start_pos)
g.out.go_back(cur_line.len)
g.writeln(s)
@ -2357,7 +2357,7 @@ fn (g mut Gen) insert_before(s string) {
// If the user is not using the optional return value. We need to pass a temp var
// to access its fields (`.ok`, `.error` etc)
// `os.cp(...)` => `Option bool tmp = os__cp(...); if (!tmp.ok) { ... }`
fn (g mut Gen) or_block(var_name string, stmts []ast.Stmt, return_type table.Type) {
fn (var g Gen) or_block(var_name string, stmts []ast.Stmt, return_type table.Type) {
mr_styp := g.typ(return_type)
mr_styp2 := mr_styp[7..] // remove Option_
g.writeln(';') // or')
@ -2380,7 +2380,7 @@ fn (g mut Gen) or_block(var_name string, stmts []ast.Stmt, return_type table.Typ
g.write('}')
}
fn (g mut Gen) type_of_last_statement(stmts []ast.Stmt) (string, string) {
fn (var g Gen) type_of_last_statement(stmts []ast.Stmt) (string, string) {
var last_type := ''
var last_expr_result_type := ''
if stmts.len > 0 {
@ -2408,7 +2408,7 @@ fn (g mut Gen) type_of_last_statement(stmts []ast.Stmt) (string, string) {
return last_type, last_expr_result_type
}
fn (g mut Gen) type_of_call_expr(node ast.Expr) string {
fn (var g Gen) type_of_call_expr(node ast.Expr) string {
match node {
ast.CallExpr {
return g.typ(it.return_type)
@ -2421,7 +2421,7 @@ fn (g mut Gen) type_of_call_expr(node ast.Expr) string {
}
// `a in [1,2,3]` => `a == 1 || a == 2 || a == 3`
fn (g mut Gen) in_optimization(left ast.Expr, right ast.ArrayInit) {
fn (var g Gen) in_optimization(left ast.Expr, right ast.ArrayInit) {
is_str := right.elem_type == table.string_type
for i, array_expr in right.exprs {
if is_str {
@ -2638,7 +2638,7 @@ fn (g Gen) type_default(typ table.Type) string {
*/
}
pub fn (g mut Gen) write_tests_main() {
pub fn (var g Gen) write_tests_main() {
g.definitions.writeln('int g_test_oks = 0;')
g.definitions.writeln('int g_test_fails = 0;')
$if windows {
@ -2725,7 +2725,7 @@ fn (g Gen) is_importing_os() bool {
return 'os' in g.table.imports
}
fn (g mut Gen) comp_if(it ast.CompIf) {
fn (var g Gen) comp_if(it ast.CompIf) {
ifdef := comp_if_to_ifdef(it.val)
if it.is_not {
g.writeln('\n#ifndef ' + ifdef)
@ -2757,7 +2757,7 @@ fn (g mut Gen) comp_if(it ast.CompIf) {
g.writeln('\n#endif')
}
fn (g mut Gen) go_stmt(node ast.GoStmt) {
fn (var g Gen) go_stmt(node ast.GoStmt) {
tmp := g.new_tmp_var()
// x := node.call_expr as ast.CallEpxr // TODO
match node.call_expr {
@ -2827,14 +2827,14 @@ fn (g mut Gen) go_stmt(node ast.GoStmt) {
}
}
fn (g mut Gen) is_expr(node ast.InfixExpr) {
fn (var g Gen) is_expr(node ast.InfixExpr) {
g.expr(node.left)
g.write('.typ == ')
g.expr(node.right)
}
// already generated styp, reuse it
fn (g mut Gen) gen_str_for_type(sym table.TypeSymbol, styp string) {
fn (var g Gen) gen_str_for_type(sym table.TypeSymbol, styp string) {
if sym.has_method('str') || styp in g.str_types {
return
}
@ -2855,7 +2855,7 @@ fn (g mut Gen) gen_str_for_type(sym table.TypeSymbol, styp string) {
}
}
fn (g mut Gen) gen_str_default(sym table.TypeSymbol, styp string) {
fn (var g Gen) gen_str_default(sym table.TypeSymbol, styp string) {
var convertor := ''
var typename := ''
if sym.parent_idx in table.integer_type_idxs {
@ -2885,7 +2885,7 @@ fn (g mut Gen) gen_str_default(sym table.TypeSymbol, styp string) {
g.definitions.writeln('}')
}
fn (g mut Gen) gen_str_for_enum(info table.Enum, styp string) {
fn (var g Gen) gen_str_for_enum(info table.Enum, styp string) {
s := styp.replace('.', '__')
g.definitions.write('string ${s}_str($styp it) {\n\tswitch(it) {\n')
for i, val in info.vals {
@ -2894,7 +2894,7 @@ fn (g mut Gen) gen_str_for_enum(info table.Enum, styp string) {
g.definitions.write('\t\tdefault: return tos3("unknown enum value"); } }\n')
}
fn (g mut Gen) gen_str_for_struct(info table.Struct, styp string) {
fn (var g Gen) gen_str_for_struct(info table.Struct, styp string) {
// TODO: short it if possible
// generates all definitions of substructs
for i, field in info.fields {

View File

@ -9,7 +9,7 @@ import v.scanner
import v.token
import v.util
pub fn (p mut Parser) call_expr(is_c, is_js bool, mod string) ast.CallExpr {
pub fn (var p Parser) call_expr(is_c, is_js bool, mod string) ast.CallExpr {
first_pos := p.tok.position()
tok := p.tok
name := p.check_name()
@ -63,7 +63,7 @@ pub fn (p mut Parser) call_expr(is_c, is_js bool, mod string) ast.CallExpr {
return node
}
pub fn (p mut Parser) call_args() []ast.CallArg {
pub fn (var p Parser) call_args() []ast.CallArg {
var args := []ast.CallArg
for p.tok.kind != .rpar {
var is_mut := false
@ -83,7 +83,7 @@ pub fn (p mut Parser) call_args() []ast.CallArg {
return args
}
fn (p mut Parser) fn_decl() ast.FnDecl {
fn (var p Parser) fn_decl() ast.FnDecl {
// p.table.clear_vars()
pos := p.tok.position()
p.open_scope()
@ -232,7 +232,7 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
}
}
fn (p mut Parser) fn_args() ([]table.Arg, bool) {
fn (var p Parser) fn_args() ([]table.Arg, bool) {
p.check(.lpar)
var args := []table.Arg
var is_variadic := false

View File

@ -177,26 +177,26 @@ pub fn (p &Parser) init_parse_fns() {
println('')
}
pub fn (p mut Parser) read_first_token() {
pub fn (var p Parser) read_first_token() {
// need to call next() twice to get peek token and current token
p.next()
p.next()
}
pub fn (p mut Parser) open_scope() {
pub fn (var p Parser) open_scope() {
p.scope = &ast.Scope{
parent: p.scope
start_pos: p.tok.pos
}
}
pub fn (p mut Parser) close_scope() {
pub fn (var p Parser) close_scope() {
p.scope.end_pos = p.tok.pos
p.scope.parent.children << p.scope
p.scope = p.scope.parent
}
pub fn (p mut Parser) parse_block() []ast.Stmt {
pub fn (var p Parser) parse_block() []ast.Stmt {
p.open_scope()
// println('parse block')
stmts := p.parse_block_no_scope()
@ -205,7 +205,7 @@ pub fn (p mut Parser) parse_block() []ast.Stmt {
return stmts
}
pub fn (p mut Parser) parse_block_no_scope() []ast.Stmt {
pub fn (var p Parser) parse_block_no_scope() []ast.Stmt {
p.check(.lcbr)
var stmts := []ast.Stmt
if p.tok.kind != .rcbr {
@ -227,7 +227,7 @@ fn (p mut Parser) next_with_comment() {
p.peek_tok = p.scanner.scan()
}
*/
fn (p mut Parser) next() {
fn (var p Parser) next() {
p.tok = p.peek_tok
p.peek_tok = p.scanner.scan()
/*
@ -238,7 +238,7 @@ fn (p mut Parser) next() {
*/
}
fn (p mut Parser) check(expected token.Kind) {
fn (var p Parser) check(expected token.Kind) {
// for p.tok.kind in [.line_comment, .mline_comment] {
// p.next()
// }
@ -248,13 +248,13 @@ fn (p mut Parser) check(expected token.Kind) {
p.next()
}
fn (p mut Parser) check_name() string {
fn (var p Parser) check_name() string {
name := p.tok.lit
p.check(.name)
return name
}
pub fn (p mut Parser) top_stmt() ast.Stmt {
pub fn (var p Parser) top_stmt() ast.Stmt {
match p.tok.kind {
.key_pub {
match p.peek_tok.kind {
@ -337,14 +337,14 @@ pub fn (p mut Parser) top_stmt() ast.Stmt {
}
// TODO [if vfmt]
pub fn (p mut Parser) check_comment() ast.Comment {
pub fn (var p Parser) check_comment() ast.Comment {
if p.tok.kind == .comment {
return p.comment()
}
return ast.Comment{}
}
pub fn (p mut Parser) comment() ast.Comment {
pub fn (var p Parser) comment() ast.Comment {
pos := p.tok.position()
text := p.tok.lit
p.next()
@ -355,7 +355,7 @@ pub fn (p mut Parser) comment() ast.Comment {
}
}
pub fn (p mut Parser) stmt() ast.Stmt {
pub fn (var p Parser) stmt() ast.Stmt {
p.is_stmt_ident = p.tok.kind == .name
match p.tok.kind {
.lcbr {
@ -456,7 +456,7 @@ pub fn (p mut Parser) stmt() ast.Stmt {
}
// TODO: is it possible to merge with AssignStmt?
pub fn (p mut Parser) assign_expr(left ast.Expr) ast.AssignExpr {
pub fn (var p Parser) assign_expr(left ast.Expr) ast.AssignExpr {
op := p.tok.kind
p.next()
pos := p.tok.position()
@ -477,7 +477,7 @@ pub fn (p mut Parser) assign_expr(left ast.Expr) ast.AssignExpr {
return node
}
fn (p mut Parser) attribute() ast.Attr {
fn (var p Parser) attribute() ast.Attr {
p.check(.lsbr)
if p.tok.kind == .key_if {
p.next()
@ -544,7 +544,7 @@ pub fn (p &Parser) warn_with_pos(s string, pos token.Position) {
eprintln(ferror)
}
pub fn (p mut Parser) parse_ident(is_c, is_js bool) ast.Ident {
pub fn (var p Parser) parse_ident(is_c, is_js bool) ast.Ident {
// p.warn('name ')
pos := p.tok.position()
var name := p.check_name()
@ -569,7 +569,7 @@ pub fn (p mut Parser) parse_ident(is_c, is_js bool) ast.Ident {
return ident
}
fn (p mut Parser) struct_init(short_syntax bool) ast.StructInit {
fn (var p Parser) struct_init(short_syntax bool) ast.StructInit {
typ := if short_syntax { table.void_type } else { p.parse_type() }
p.expr_mod = ''
// sym := p.table.get_type_symbol(typ)
@ -615,7 +615,7 @@ fn (p mut Parser) struct_init(short_syntax bool) ast.StructInit {
return node
}
pub fn (p mut Parser) name_expr() ast.Expr {
pub fn (var p Parser) name_expr() ast.Expr {
var node := ast.Expr{}
if p.inside_is {
p.inside_is = false
@ -731,7 +731,7 @@ pub fn (p mut Parser) name_expr() ast.Expr {
return node
}
pub fn (p mut Parser) expr(precedence int) ast.Expr {
pub fn (var p Parser) expr(precedence int) ast.Expr {
// println('\n\nparser.expr()')
var typ := table.void_type
var node := ast.Expr{}
@ -897,7 +897,7 @@ pub fn (p mut Parser) expr(precedence int) ast.Expr {
return node
}
fn (p mut Parser) prefix_expr() ast.PrefixExpr {
fn (var p Parser) prefix_expr() ast.PrefixExpr {
pos := p.tok.position()
op := p.tok.kind
if op == .amp {
@ -913,7 +913,7 @@ fn (p mut Parser) prefix_expr() ast.PrefixExpr {
}
}
fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
fn (var p Parser) index_expr(left ast.Expr) ast.IndexExpr {
// left == `a` in `a[0]`
p.next() // [
var has_low := true
@ -964,13 +964,13 @@ fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
}
}
fn (p mut Parser) filter() {
fn (var p Parser) filter() {
p.scope.register('it', ast.Var{
name: 'it'
})
}
fn (p mut Parser) dot_expr(left ast.Expr) ast.Expr {
fn (var p Parser) dot_expr(left ast.Expr) ast.Expr {
p.next()
var name_pos := p.tok.position()
field_name := p.check_name()
@ -1043,7 +1043,7 @@ fn (p mut Parser) dot_expr(left ast.Expr) ast.Expr {
return node
}
fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
fn (var p Parser) infix_expr(left ast.Expr) ast.Expr {
op := p.tok.kind
// mut typ := p.
// println('infix op=$op.str()')
@ -1067,7 +1067,7 @@ fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
// `.green`
// `pref.BuildMode.default_mode`
fn (p mut Parser) enum_val() ast.EnumVal {
fn (var p Parser) enum_val() ast.EnumVal {
p.check(.dot)
val := p.check_name()
return ast.EnumVal{
@ -1076,7 +1076,7 @@ fn (p mut Parser) enum_val() ast.EnumVal {
}
}
fn (p mut Parser) for_stmt() ast.Stmt {
fn (var p Parser) for_stmt() ast.Stmt {
p.check(.key_for)
pos := p.tok.position()
p.open_scope()
@ -1196,7 +1196,7 @@ fn (p mut Parser) for_stmt() ast.Stmt {
}
}
fn (p mut Parser) if_expr() ast.IfExpr {
fn (var p Parser) if_expr() ast.IfExpr {
pos := p.tok.position()
var branches := []ast.IfBranch
var has_else := false
@ -1267,7 +1267,7 @@ fn (p mut Parser) if_expr() ast.IfExpr {
}
}
fn (p mut Parser) string_expr() ast.Expr {
fn (var p Parser) string_expr() ast.Expr {
is_raw := p.tok.kind == .name && p.tok.lit == 'r'
is_cstr := p.tok.kind == .name && p.tok.lit == 'c'
if is_raw || is_cstr {
@ -1328,7 +1328,7 @@ fn (p mut Parser) string_expr() ast.Expr {
return node
}
fn (p mut Parser) array_init() ast.ArrayInit {
fn (var p Parser) array_init() ast.ArrayInit {
first_pos := p.tok.position()
var last_pos := token.Position{}
p.check(.lsbr)
@ -1413,7 +1413,7 @@ fn (p mut Parser) array_init() ast.ArrayInit {
}
}
fn (p mut Parser) map_init() ast.MapInit {
fn (var p Parser) map_init() ast.MapInit {
pos := p.tok.position()
var keys := []ast.Expr
var vals := []ast.Expr
@ -1435,7 +1435,7 @@ fn (p mut Parser) map_init() ast.MapInit {
}
}
fn (p mut Parser) parse_number_literal() ast.Expr {
fn (var p Parser) parse_number_literal() ast.Expr {
lit := p.tok.lit
pos := p.tok.position()
var node := ast.Expr{}
@ -1453,7 +1453,7 @@ fn (p mut Parser) parse_number_literal() ast.Expr {
return node
}
fn (p mut Parser) module_decl() ast.Module {
fn (var p Parser) module_decl() ast.Module {
var name := 'main'
is_skipped := p.tok.kind != .key_module
if !is_skipped {
@ -1469,7 +1469,7 @@ fn (p mut Parser) module_decl() ast.Module {
}
}
fn (p mut Parser) parse_import() ast.Import {
fn (var p Parser) parse_import() ast.Import {
pos := p.tok.position()
var mod_name := p.check_name()
var mod_alias := mod_name
@ -1492,7 +1492,7 @@ fn (p mut Parser) parse_import() ast.Import {
}
}
fn (p mut Parser) import_stmt() []ast.Import {
fn (var p Parser) import_stmt() []ast.Import {
p.check(.key_import)
var imports := []ast.Import
if p.tok.kind == .lpar {
@ -1511,7 +1511,7 @@ fn (p mut Parser) import_stmt() []ast.Import {
return imports
}
fn (p mut Parser) const_decl() ast.ConstDecl {
fn (var p Parser) const_decl() ast.ConstDecl {
is_pub := p.tok.kind == .key_pub
if is_pub {
p.next()
@ -1549,7 +1549,7 @@ fn (p mut Parser) const_decl() ast.ConstDecl {
}
// structs and unions
fn (p mut Parser) struct_decl() ast.StructDecl {
fn (var p Parser) struct_decl() ast.StructDecl {
first_pos := p.tok.position()
is_pub := p.tok.kind == .key_pub
if is_pub {
@ -1705,7 +1705,7 @@ fn (p mut Parser) struct_decl() ast.StructDecl {
}
}
fn (p mut Parser) interface_decl() ast.InterfaceDecl {
fn (var p Parser) interface_decl() ast.InterfaceDecl {
is_pub := p.tok.kind == .key_pub
if is_pub {
p.next()
@ -1730,7 +1730,7 @@ fn (p mut Parser) interface_decl() ast.InterfaceDecl {
}
}
fn (p mut Parser) return_stmt() ast.Return {
fn (var p Parser) return_stmt() ast.Return {
p.next()
// return expressions
var exprs := []ast.Expr
@ -1756,7 +1756,7 @@ fn (p mut Parser) return_stmt() ast.Return {
}
// left hand side of `=` or `:=` in `a,b,c := 1,2,3`
fn (p mut Parser) parse_assign_lhs() []ast.Ident {
fn (var p Parser) parse_assign_lhs() []ast.Ident {
var idents := []ast.Ident
for {
is_mut := p.tok.kind == .key_mut || p.tok.kind == .key_var
@ -1784,7 +1784,7 @@ fn (p mut Parser) parse_assign_lhs() []ast.Ident {
}
// right hand side of `=` or `:=` in `a,b,c := 1,2,3`
fn (p mut Parser) parse_assign_rhs() []ast.Expr {
fn (var p Parser) parse_assign_rhs() []ast.Expr {
var exprs := []ast.Expr
for {
expr := p.expr(0)
@ -1798,7 +1798,7 @@ fn (p mut Parser) parse_assign_rhs() []ast.Expr {
return exprs
}
fn (p mut Parser) assign_stmt() ast.Stmt {
fn (var p Parser) assign_stmt() ast.Stmt {
is_static := p.tok.kind == .key_static
if is_static {
p.next()
@ -1841,7 +1841,7 @@ fn (p mut Parser) assign_stmt() ast.Stmt {
}
}
fn (p mut Parser) global_decl() ast.GlobalDecl {
fn (var p Parser) global_decl() ast.GlobalDecl {
if !p.pref.translated && !p.pref.is_live && !p.builtin_mod && !p.pref.building_v && p.mod !=
'ui' && p.mod != 'gg2' && p.mod != 'uiold' && !os.getwd().contains('/volt') && !p.pref.enable_globals {
p.error('use `v --enable-globals ...` to enable globals')
@ -1881,7 +1881,7 @@ fn (p mut Parser) global_decl() ast.GlobalDecl {
return glob
}
fn (p mut Parser) match_expr() ast.MatchExpr {
fn (var p Parser) match_expr() ast.MatchExpr {
match_first_pos := p.tok.position()
p.inside_match = true
p.check(.key_match)
@ -1976,7 +1976,7 @@ fn (p mut Parser) match_expr() ast.MatchExpr {
}
}
fn (p mut Parser) enum_decl() ast.EnumDecl {
fn (var p Parser) enum_decl() ast.EnumDecl {
is_pub := p.tok.kind == .key_pub
if is_pub {
p.next()
@ -2023,7 +2023,7 @@ fn (p mut Parser) enum_decl() ast.EnumDecl {
}
}
fn (p mut Parser) type_decl() ast.TypeDecl {
fn (var p Parser) type_decl() ast.TypeDecl {
is_pub := p.tok.kind == .key_pub
if is_pub {
p.next()
@ -2088,7 +2088,7 @@ fn (p mut Parser) type_decl() ast.TypeDecl {
}
}
fn (p mut Parser) assoc() ast.Assoc {
fn (var p Parser) assoc() ast.Assoc {
var_name := p.check_name()
pos := p.tok.position()
v := p.scope.find_var(var_name) or {