Revert "fmt: `(var f Foo)`"

This reverts commit 4b110756e0.
pull/4414/head
Alexander Medvednikov 2020-04-15 04:15:42 +02:00
parent 4b110756e0
commit da28bc7026
6 changed files with 171 additions and 172 deletions

View File

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

View File

@ -31,7 +31,7 @@ mut:
} }
pub fn fmt(file ast.File, table &table.Table) string { pub fn fmt(file ast.File, table &table.Table) string {
var f := Fmt{ mut f := Fmt{
out: strings.new_builder(1000) out: strings.new_builder(1000)
out_imports: strings.new_builder(200) out_imports: strings.new_builder(200)
table: table table: table
@ -67,7 +67,7 @@ fn (f mut Fmt) find_comment(line_nr int) {
} }
} }
*/ */
pub fn (var f Fmt) write(s string) { pub fn (f mut Fmt) write(s string) {
if f.indent > 0 && f.empty_line { if f.indent > 0 && f.empty_line {
f.out.write(tabs[f.indent]) f.out.write(tabs[f.indent])
f.line_len += f.indent * 4 f.line_len += f.indent * 4
@ -77,7 +77,7 @@ pub fn (var f Fmt) write(s string) {
f.empty_line = false f.empty_line = false
} }
pub fn (var f Fmt) writeln(s string) { pub fn (f mut Fmt) writeln(s string) {
if f.indent > 0 && f.empty_line { if f.indent > 0 && f.empty_line {
// println(f.indent.str() + s) // println(f.indent.str() + s)
f.out.write(tabs[f.indent]) f.out.write(tabs[f.indent])
@ -87,7 +87,7 @@ pub fn (var f Fmt) writeln(s string) {
f.line_len = 0 f.line_len = 0
} }
fn (var f Fmt) mod(mod ast.Module) { fn (f mut Fmt) mod(mod ast.Module) {
f.cur_mod = mod.name f.cur_mod = mod.name
if mod.is_skipped { if mod.is_skipped {
return return
@ -95,7 +95,7 @@ fn (var f Fmt) mod(mod ast.Module) {
f.writeln('module $mod.name\n') f.writeln('module $mod.name\n')
} }
fn (var f Fmt) imports(imports []ast.Import) { fn (f mut Fmt) imports(imports []ast.Import) {
if f.did_imports || imports.len == 0 { if f.did_imports || imports.len == 0 {
return return
} }
@ -129,7 +129,7 @@ fn (f Fmt) imp_stmt_str(imp ast.Import) string {
return '${imp.mod}${imp_alias_suffix}' return '${imp.mod}${imp_alias_suffix}'
} }
fn (var f Fmt) stmts(stmts []ast.Stmt) { fn (f mut Fmt) stmts(stmts []ast.Stmt) {
f.indent++ f.indent++
for stmt in stmts { for stmt in stmts {
f.stmt(stmt) f.stmt(stmt)
@ -137,7 +137,7 @@ fn (var f Fmt) stmts(stmts []ast.Stmt) {
f.indent-- f.indent--
} }
fn (var f Fmt) stmt(node ast.Stmt) { fn (f mut Fmt) stmt(node ast.Stmt) {
match node { match node {
ast.AssignStmt { ast.AssignStmt {
for i, ident in it.left { for i, ident in it.left {
@ -205,7 +205,7 @@ fn (var f Fmt) stmt(node ast.Stmt) {
f.write('pub ') f.write('pub ')
} }
f.writeln('const (') f.writeln('const (')
var max := 0 mut max := 0
for field in it.fields { for field in it.fields {
if field.name.len > max { if field.name.len > max {
max = field.name.len max = field.name.len
@ -379,7 +379,7 @@ fn (var f Fmt) stmt(node ast.Stmt) {
} }
} }
fn (var f Fmt) type_decl(node ast.TypeDecl) { fn (f mut Fmt) type_decl(node ast.TypeDecl) {
match node { match node {
ast.AliasTypeDecl { ast.AliasTypeDecl {
if it.is_pub { if it.is_pub {
@ -393,7 +393,7 @@ fn (var f Fmt) type_decl(node ast.TypeDecl) {
f.write('pub ') f.write('pub ')
} }
f.write('type $it.name = ') f.write('type $it.name = ')
var sum_type_names := []string mut sum_type_names := []string
for t in it.sub_types { for t in it.sub_types {
sum_type_names << f.type_to_str(t) sum_type_names << f.type_to_str(t)
} }
@ -406,13 +406,13 @@ fn (var f Fmt) type_decl(node ast.TypeDecl) {
f.writeln('\n') f.writeln('\n')
} }
fn (var f Fmt) struct_decl(node ast.StructDecl) { fn (f mut Fmt) struct_decl(node ast.StructDecl) {
if node.is_pub { if node.is_pub {
f.write('pub ') f.write('pub ')
} }
name := node.name.after('.') name := node.name.after('.')
f.writeln('struct $name {') f.writeln('struct $name {')
var max := 0 mut max := 0
for field in node.fields { for field in node.fields {
if field.name.len > max { if field.name.len > max {
max = field.name.len max = field.name.len
@ -457,7 +457,7 @@ fn (f &Fmt) type_to_str(t table.Type) string {
return res.replace(f.cur_mod + '.', '') return res.replace(f.cur_mod + '.', '')
} }
fn (var f Fmt) expr(node ast.Expr) { fn (f mut Fmt) expr(node ast.Expr) {
match node { match node {
ast.ArrayInit { ast.ArrayInit {
if it.exprs.len == 0 && it.typ != 0 && it.typ != table.void_type { if it.exprs.len == 0 && it.typ != 0 && it.typ != table.void_type {
@ -685,7 +685,7 @@ fn (var f Fmt) expr(node ast.Expr) {
ast.StructInit { ast.StructInit {
type_sym := f.table.get_type_symbol(it.typ) type_sym := f.table.get_type_symbol(it.typ)
// f.write('<old name: $type_sym.name>') // f.write('<old name: $type_sym.name>')
var name := short_module(type_sym.name).replace(f.cur_mod + '.', '') // TODO f.type_to_str? mut name := short_module(type_sym.name).replace(f.cur_mod + '.', '') // TODO f.type_to_str?
if name == 'void' { if name == 'void' {
name = '' name = ''
} }
@ -731,7 +731,7 @@ fn (var f Fmt) expr(node ast.Expr) {
} }
} }
fn (var f Fmt) wrap_long_line() { fn (f mut Fmt) wrap_long_line() {
if f.line_len > max_len { if f.line_len > max_len {
if f.out.buf[f.out.buf.len - 1] == ` ` { if f.out.buf[f.out.buf.len - 1] == ` ` {
f.out.go_back(1) f.out.go_back(1)
@ -741,7 +741,7 @@ fn (var f Fmt) wrap_long_line() {
} }
} }
fn (var f Fmt) call_args(args []ast.CallArg) { fn (f mut Fmt) call_args(args []ast.CallArg) {
for i, arg in args { for i, arg in args {
if arg.is_mut { if arg.is_mut {
f.write('mut ') f.write('mut ')
@ -756,7 +756,7 @@ fn (var f Fmt) call_args(args []ast.CallArg) {
} }
} }
fn (var f Fmt) or_expr(or_block ast.OrExpr) { fn (f mut Fmt) or_expr(or_block ast.OrExpr) {
if or_block.stmts.len > 0 { if or_block.stmts.len > 0 {
f.writeln(' or {') f.writeln(' or {')
f.stmts(or_block.stmts) f.stmts(or_block.stmts)
@ -764,10 +764,10 @@ fn (var f Fmt) or_expr(or_block ast.OrExpr) {
} }
} }
fn (var f Fmt) comment(node ast.Comment) { fn (f mut Fmt) comment(node ast.Comment) {
if !node.text.contains('\n') { if !node.text.contains('\n') {
is_separate_line := node.text.starts_with('|') is_separate_line := node.text.starts_with('|')
var s := if is_separate_line { node.text[1..] } else { node.text } mut s := if is_separate_line { node.text[1..] } else { node.text }
if s == '' { if s == '' {
s = '//' s = '//'
} else { } else {
@ -800,7 +800,7 @@ fn short_module(name string) string {
return vals[vals.len - 2] + '.' + vals[vals.len - 1] return vals[vals.len - 2] + '.' + vals[vals.len - 1]
} }
fn (var f Fmt) if_expr(it ast.IfExpr) { fn (f mut Fmt) if_expr(it ast.IfExpr) {
single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == 1 && single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == 1 &&
it.branches[1].stmts.len == 1 && (it.is_expr || f.is_assign) it.branches[1].stmts.len == 1 && (it.is_expr || f.is_assign)
f.single_line_if = single_line f.single_line_if = single_line
@ -833,7 +833,7 @@ fn (var f Fmt) if_expr(it ast.IfExpr) {
f.single_line_if = false f.single_line_if = false
} }
fn (var f Fmt) call_expr(node ast.CallExpr) { fn (f mut Fmt) call_expr(node ast.CallExpr) {
if node.is_method { if node.is_method {
match node.left { match node.left {
ast.Ident { ast.Ident {
@ -870,13 +870,13 @@ fn (var f Fmt) call_expr(node ast.CallExpr) {
} }
} }
fn (var f Fmt) mark_types_module_as_used(typ table.Type) { fn (f mut Fmt) mark_types_module_as_used(typ table.Type) {
sym := f.table.get_type_symbol(typ) sym := f.table.get_type_symbol(typ)
f.mark_module_as_used(sym.name) f.mark_module_as_used(sym.name)
} }
// `name` is a function (`foo.bar()`) or type (`foo.Bar{}`) // `name` is a function (`foo.bar()`) or type (`foo.Bar{}`)
fn (var f Fmt) mark_module_as_used(name string) { fn (f mut Fmt) mark_module_as_used(name string) {
if !name.contains('.') { if !name.contains('.') {
return return
} }

View File

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

View File

@ -8,7 +8,7 @@ import v.table
import v.scanner import v.scanner
import v.token import v.token
pub fn (var p Parser) call_expr(is_c bool, mod string) ast.CallExpr { pub fn (p mut Parser) call_expr(is_c bool, mod string) ast.CallExpr {
first_pos := p.tok.position() first_pos := p.tok.position()
tok := p.tok tok := p.tok
name := p.check_name() name := p.check_name()
@ -59,7 +59,7 @@ pub fn (var p Parser) call_expr(is_c bool, mod string) ast.CallExpr {
return node return node
} }
pub fn (var p Parser) call_args() []ast.CallArg { pub fn (p mut Parser) call_args() []ast.CallArg {
var args := []ast.CallArg var args := []ast.CallArg
for p.tok.kind != .rpar { for p.tok.kind != .rpar {
var is_mut := false var is_mut := false
@ -79,7 +79,7 @@ pub fn (var p Parser) call_args() []ast.CallArg {
return args return args
} }
fn (var p Parser) fn_decl() ast.FnDecl { fn (p mut Parser) fn_decl() ast.FnDecl {
// p.table.clear_vars() // p.table.clear_vars()
pos := p.tok.position() pos := p.tok.position()
p.open_scope() p.open_scope()
@ -118,7 +118,7 @@ fn (var p Parser) fn_decl() ast.FnDecl {
// } // }
// TODO: talk to alex, should mut be parsed with the type like this? // TODO: talk to alex, should mut be parsed with the type like this?
// or should it be a property of the arg, like this ptr/mut becomes indistinguishable // or should it be a property of the arg, like this ptr/mut becomes indistinguishable
rec_type = p.parse_type_with_mut(rec_mut) rec_type = p.parse_type()
if is_amp && rec_mut { if is_amp && rec_mut {
p.error('use `(f mut Foo)` or `(f &Foo)` instead of `(f mut &Foo)`') p.error('use `(f mut Foo)` or `(f &Foo)` instead of `(f mut &Foo)`')
} }
@ -224,7 +224,7 @@ fn (var p Parser) fn_decl() ast.FnDecl {
} }
} }
fn (var p Parser) fn_args() ([]table.Arg, bool) { fn (p mut Parser) fn_args() ([]table.Arg, bool) {
p.check(.lpar) p.check(.lpar)
var args := []table.Arg var args := []table.Arg
var is_variadic := false var is_variadic := false

View File

@ -1,11 +1,12 @@
module parser module parser
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
import v.table import (
v.table
)
pub fn (var p Parser) parse_array_type() table.Type { pub fn (p mut Parser) parse_array_type() table.Type {
p.check(.lsbr) p.check(.lsbr)
// fixed array // fixed array
if p.tok.kind == .number { if p.tok.kind == .number {
@ -19,7 +20,7 @@ pub fn (var p Parser) parse_array_type() table.Type {
// array // array
p.check(.rsbr) p.check(.rsbr)
elem_type := p.parse_type() elem_type := p.parse_type()
var nr_dims := 1 mut nr_dims := 1
for p.tok.kind == .lsbr { for p.tok.kind == .lsbr {
p.check(.lsbr) p.check(.lsbr)
p.check(.rsbr) p.check(.rsbr)
@ -29,7 +30,7 @@ pub fn (var p Parser) parse_array_type() table.Type {
return table.new_type(idx) return table.new_type(idx)
} }
pub fn (var p Parser) parse_map_type() table.Type { pub fn (p mut Parser) parse_map_type() table.Type {
p.next() p.next()
if p.tok.kind != .lsbr { if p.tok.kind != .lsbr {
return table.map_type return table.map_type
@ -47,15 +48,16 @@ pub fn (var p Parser) parse_map_type() table.Type {
return table.new_type(idx) return table.new_type(idx)
} }
pub fn (var p Parser) parse_multi_return_type() table.Type { pub fn (p mut Parser) parse_multi_return_type() table.Type {
p.check(.lpar) p.check(.lpar)
var mr_types := []table.Type mut mr_types := []table.Type
for { for {
mr_type := p.parse_type() mr_type := p.parse_type()
mr_types << mr_type mr_types << mr_type
if p.tok.kind == .comma { if p.tok.kind == .comma {
p.check(.comma) p.check(.comma)
} else { }
else {
break break
} }
} }
@ -65,12 +67,12 @@ pub fn (var p Parser) parse_multi_return_type() table.Type {
} }
// given anon name based off signature when `name` is blank // given anon name based off signature when `name` is blank
pub fn (var p Parser) parse_fn_type(name string) table.Type { pub fn (p mut Parser) parse_fn_type(name string) table.Type {
// p.warn('parse fn') // p.warn('parse fn')
p.check(.key_fn) p.check(.key_fn)
line_nr := p.tok.line_nr line_nr := p.tok.line_nr
args, is_variadic := p.fn_args() args,is_variadic := p.fn_args()
var return_type := table.void_type mut return_type := table.void_type
if p.tok.line_nr == line_nr && p.tok.kind.is_start_of_type() { if p.tok.line_nr == line_nr && p.tok.kind.is_start_of_type() {
return_type = p.parse_type() return_type = p.parse_type()
} }
@ -84,22 +86,14 @@ pub fn (var p Parser) parse_fn_type(name string) table.Type {
return table.new_type(idx) return table.new_type(idx)
} }
pub fn (var p Parser) parse_type_with_mut(is_mut bool) table.Type { pub fn (p mut Parser) parse_type() table.Type {
typ := p.parse_type()
if is_mut {
return table.type_set_nr_muls(typ, 1)
}
return typ
}
pub fn (var p Parser) parse_type() table.Type {
// optional // optional
var is_optional := false mut is_optional := false
if p.tok.kind == .question { if p.tok.kind == .question {
p.next() p.next()
is_optional = true is_optional = true
} }
var nr_muls := 0 mut nr_muls := 0
if p.tok.kind == .key_mut { if p.tok.kind == .key_mut {
nr_muls++ nr_muls++
p.next() p.next()
@ -108,7 +102,8 @@ pub fn (var p Parser) parse_type() table.Type {
for p.tok.kind in [.and, .amp] { for p.tok.kind in [.and, .amp] {
if p.tok.kind == .and { if p.tok.kind == .and {
nr_muls += 2 nr_muls += 2
} else { }
else {
nr_muls++ nr_muls++
} }
p.next() p.next()
@ -118,7 +113,7 @@ pub fn (var p Parser) parse_type() table.Type {
p.next() p.next()
p.check(.dot) p.check(.dot)
} }
var typ := p.parse_any_type(is_c, nr_muls > 0) mut typ := p.parse_any_type(is_c, nr_muls > 0)
if is_optional { if is_optional {
typ = table.type_set(typ, .optional) typ = table.type_set(typ, .optional)
} }
@ -128,12 +123,13 @@ pub fn (var p Parser) parse_type() table.Type {
return typ return typ
} }
pub fn (var p Parser) parse_any_type(is_c, is_ptr bool) table.Type { pub fn (p mut Parser) parse_any_type(is_c, is_ptr bool) table.Type {
var name := p.tok.lit mut name := p.tok.lit
if is_c { if is_c {
name = 'C.$name' name = 'C.$name'
} else if p.peek_tok.kind == .dot { }
// `module.Type` // `module.Type`
else if p.peek_tok.kind == .dot {
// /if !(p.tok.lit in p.table.imports) { // /if !(p.tok.lit in p.table.imports) {
if !p.known_import(name) { if !p.known_import(name) {
println(p.table.imports) println(p.table.imports)
@ -143,10 +139,12 @@ pub fn (var p Parser) parse_any_type(is_c, is_ptr bool) table.Type {
p.check(.dot) p.check(.dot)
// prefix with full module // prefix with full module
name = '${p.imports[name]}.$p.tok.lit' name = '${p.imports[name]}.$p.tok.lit'
} else if p.expr_mod != '' { }
else if p.expr_mod != '' {
name = p.expr_mod + '.' + name name = p.expr_mod + '.' + name
} else if !(p.mod in ['builtin', 'main']) && !(name in table.builtin_type_names) { }
// `Foo` in module `mod` means `mod.Foo` // `Foo` in module `mod` means `mod.Foo`
else if !(p.mod in ['builtin', 'main']) && !(name in table.builtin_type_names) {
name = p.mod + '.' + name name = p.mod + '.' + name
} }
// p.warn('get type $name') // p.warn('get type $name')
@ -226,7 +224,7 @@ pub fn (var p Parser) parse_any_type(is_c, is_ptr bool) table.Type {
// struct / enum / placeholder // struct / enum / placeholder
else { else {
// struct / enum // struct / enum
var idx := p.table.find_type_idx(name) mut idx := p.table.find_type_idx(name)
if idx > 0 { if idx > 0 {
return table.new_type(idx) return table.new_type(idx)
} }
@ -235,7 +233,7 @@ pub fn (var p Parser) parse_any_type(is_c, is_ptr bool) table.Type {
// println('NOT FOUND: $name - adding placeholder - $idx') // println('NOT FOUND: $name - adding placeholder - $idx')
return table.new_type(idx) return table.new_type(idx)
} }
} }
} }
} }
} }

View File

@ -174,26 +174,26 @@ pub fn (p &Parser) init_parse_fns() {
println('') println('')
} }
pub fn (var p Parser) read_first_token() { pub fn (p mut Parser) read_first_token() {
// need to call next() twice to get peek token and current token // need to call next() twice to get peek token and current token
p.next() p.next()
p.next() p.next()
} }
pub fn (var p Parser) open_scope() { pub fn (p mut Parser) open_scope() {
p.scope = &ast.Scope{ p.scope = &ast.Scope{
parent: p.scope parent: p.scope
start_pos: p.tok.pos start_pos: p.tok.pos
} }
} }
pub fn (var p Parser) close_scope() { pub fn (p mut Parser) close_scope() {
p.scope.end_pos = p.tok.pos p.scope.end_pos = p.tok.pos
p.scope.parent.children << p.scope p.scope.parent.children << p.scope
p.scope = p.scope.parent p.scope = p.scope.parent
} }
pub fn (var p Parser) parse_block() []ast.Stmt { pub fn (p mut Parser) parse_block() []ast.Stmt {
p.open_scope() p.open_scope()
// println('parse block') // println('parse block')
stmts := p.parse_block_no_scope() stmts := p.parse_block_no_scope()
@ -202,7 +202,7 @@ pub fn (var p Parser) parse_block() []ast.Stmt {
return stmts return stmts
} }
pub fn (var p Parser) parse_block_no_scope() []ast.Stmt { pub fn (p mut Parser) parse_block_no_scope() []ast.Stmt {
p.check(.lcbr) p.check(.lcbr)
var stmts := []ast.Stmt var stmts := []ast.Stmt
if p.tok.kind != .rcbr { if p.tok.kind != .rcbr {
@ -224,7 +224,7 @@ fn (p mut Parser) next_with_comment() {
p.peek_tok = p.scanner.scan() p.peek_tok = p.scanner.scan()
} }
*/ */
fn (var p Parser) next() { fn (p mut Parser) next() {
p.tok = p.peek_tok p.tok = p.peek_tok
p.peek_tok = p.scanner.scan() p.peek_tok = p.scanner.scan()
/* /*
@ -235,7 +235,7 @@ fn (var p Parser) next() {
*/ */
} }
fn (var p Parser) check(expected token.Kind) { fn (p mut Parser) check(expected token.Kind) {
// for p.tok.kind in [.line_comment, .mline_comment] { // for p.tok.kind in [.line_comment, .mline_comment] {
// p.next() // p.next()
// } // }
@ -246,13 +246,13 @@ fn (var p Parser) check(expected token.Kind) {
p.next() p.next()
} }
fn (var p Parser) check_name() string { fn (p mut Parser) check_name() string {
name := p.tok.lit name := p.tok.lit
p.check(.name) p.check(.name)
return name return name
} }
pub fn (var p Parser) top_stmt() ast.Stmt { pub fn (p mut Parser) top_stmt() ast.Stmt {
match p.tok.kind { match p.tok.kind {
.key_pub { .key_pub {
match p.peek_tok.kind { match p.peek_tok.kind {
@ -335,14 +335,14 @@ pub fn (var p Parser) top_stmt() ast.Stmt {
} }
// TODO [if vfmt] // TODO [if vfmt]
pub fn (var p Parser) check_comment() ast.Comment { pub fn (p mut Parser) check_comment() ast.Comment {
if p.tok.kind == .comment { if p.tok.kind == .comment {
return p.comment() return p.comment()
} }
return ast.Comment{} return ast.Comment{}
} }
pub fn (var p Parser) comment() ast.Comment { pub fn (p mut Parser) comment() ast.Comment {
pos := p.tok.position() pos := p.tok.position()
text := p.tok.lit text := p.tok.lit
p.next() p.next()
@ -353,7 +353,7 @@ pub fn (var p Parser) comment() ast.Comment {
} }
} }
pub fn (var p Parser) stmt() ast.Stmt { pub fn (p mut Parser) stmt() ast.Stmt {
p.is_stmt_ident = p.tok.kind == .name p.is_stmt_ident = p.tok.kind == .name
match p.tok.kind { match p.tok.kind {
.lcbr { .lcbr {
@ -456,7 +456,7 @@ pub fn (var p Parser) stmt() ast.Stmt {
} }
// TODO: is it possible to merge with AssignStmt? // TODO: is it possible to merge with AssignStmt?
pub fn (var p Parser) assign_expr(left ast.Expr) ast.AssignExpr { pub fn (p mut Parser) assign_expr(left ast.Expr) ast.AssignExpr {
op := p.tok.kind op := p.tok.kind
p.next() p.next()
pos := p.tok.position() pos := p.tok.position()
@ -477,7 +477,7 @@ pub fn (var p Parser) assign_expr(left ast.Expr) ast.AssignExpr {
return node return node
} }
fn (var p Parser) attribute() ast.Attr { fn (p mut Parser) attribute() ast.Attr {
p.check(.lsbr) p.check(.lsbr)
if p.tok.kind == .key_if { if p.tok.kind == .key_if {
p.next() p.next()
@ -544,7 +544,7 @@ pub fn (p &Parser) warn_with_pos(s string, pos token.Position) {
eprintln(ferror) eprintln(ferror)
} }
pub fn (var p Parser) parse_ident(is_c bool) ast.Ident { pub fn (p mut Parser) parse_ident(is_c bool) ast.Ident {
// p.warn('name ') // p.warn('name ')
pos := p.tok.position() pos := p.tok.position()
var name := p.check_name() var name := p.check_name()
@ -568,7 +568,7 @@ pub fn (var p Parser) parse_ident(is_c bool) ast.Ident {
return ident return ident
} }
fn (var p Parser) struct_init(short_syntax bool) ast.StructInit { fn (p mut Parser) struct_init(short_syntax bool) ast.StructInit {
typ := if short_syntax { table.void_type } else { p.parse_type() } typ := if short_syntax { table.void_type } else { p.parse_type() }
p.expr_mod = '' p.expr_mod = ''
// sym := p.table.get_type_symbol(typ) // sym := p.table.get_type_symbol(typ)
@ -614,7 +614,7 @@ fn (var p Parser) struct_init(short_syntax bool) ast.StructInit {
return node return node
} }
pub fn (var p Parser) name_expr() ast.Expr { pub fn (p mut Parser) name_expr() ast.Expr {
var node := ast.Expr{} var node := ast.Expr{}
is_c := p.tok.lit == 'C' is_c := p.tok.lit == 'C'
var mod := '' var mod := ''
@ -723,7 +723,7 @@ pub fn (var p Parser) name_expr() ast.Expr {
return node return node
} }
pub fn (var p Parser) expr(precedence int) ast.Expr { pub fn (p mut Parser) expr(precedence int) ast.Expr {
// println('\n\nparser.expr()') // println('\n\nparser.expr()')
var typ := table.void_type var typ := table.void_type
var node := ast.Expr{} var node := ast.Expr{}
@ -889,7 +889,7 @@ pub fn (var p Parser) expr(precedence int) ast.Expr {
return node return node
} }
fn (var p Parser) prefix_expr() ast.PrefixExpr { fn (p mut Parser) prefix_expr() ast.PrefixExpr {
pos := p.tok.position() pos := p.tok.position()
op := p.tok.kind op := p.tok.kind
if op == .amp { if op == .amp {
@ -905,7 +905,7 @@ fn (var p Parser) prefix_expr() ast.PrefixExpr {
} }
} }
fn (var p Parser) index_expr(left ast.Expr) ast.IndexExpr { fn (p mut Parser) index_expr(left ast.Expr) ast.IndexExpr {
// left == `a` in `a[0]` // left == `a` in `a[0]`
p.next() // [ p.next() // [
var has_low := true var has_low := true
@ -956,13 +956,13 @@ fn (var p Parser) index_expr(left ast.Expr) ast.IndexExpr {
} }
} }
fn (var p Parser) filter() { fn (p mut Parser) filter() {
p.scope.register('it', ast.Var{ p.scope.register('it', ast.Var{
name: 'it' name: 'it'
}) })
} }
fn (var p Parser) dot_expr(left ast.Expr) ast.Expr { fn (p mut Parser) dot_expr(left ast.Expr) ast.Expr {
p.next() p.next()
var name_pos := p.tok.position() var name_pos := p.tok.position()
field_name := p.check_name() field_name := p.check_name()
@ -1035,7 +1035,7 @@ fn (var p Parser) dot_expr(left ast.Expr) ast.Expr {
return node return node
} }
fn (var p Parser) infix_expr(left ast.Expr) ast.Expr { fn (p mut Parser) infix_expr(left ast.Expr) ast.Expr {
op := p.tok.kind op := p.tok.kind
// mut typ := p. // mut typ := p.
// println('infix op=$op.str()') // println('infix op=$op.str()')
@ -1056,7 +1056,7 @@ fn (var p Parser) infix_expr(left ast.Expr) ast.Expr {
// `.green` // `.green`
// `pref.BuildMode.default_mode` // `pref.BuildMode.default_mode`
fn (var p Parser) enum_val() ast.EnumVal { fn (p mut Parser) enum_val() ast.EnumVal {
p.check(.dot) p.check(.dot)
val := p.check_name() val := p.check_name()
return ast.EnumVal{ return ast.EnumVal{
@ -1065,7 +1065,7 @@ fn (var p Parser) enum_val() ast.EnumVal {
} }
} }
fn (var p Parser) for_stmt() ast.Stmt { fn (p mut Parser) for_stmt() ast.Stmt {
p.check(.key_for) p.check(.key_for)
pos := p.tok.position() pos := p.tok.position()
p.open_scope() p.open_scope()
@ -1185,7 +1185,7 @@ fn (var p Parser) for_stmt() ast.Stmt {
} }
} }
fn (var p Parser) if_expr() ast.IfExpr { fn (p mut Parser) if_expr() ast.IfExpr {
pos := p.tok.position() pos := p.tok.position()
var branches := []ast.IfBranch var branches := []ast.IfBranch
var has_else := false var has_else := false
@ -1256,7 +1256,7 @@ fn (var p Parser) if_expr() ast.IfExpr {
} }
} }
fn (var p Parser) string_expr() ast.Expr { fn (p mut Parser) string_expr() ast.Expr {
is_raw := p.tok.kind == .name && p.tok.lit == 'r' is_raw := p.tok.kind == .name && p.tok.lit == 'r'
is_cstr := p.tok.kind == .name && p.tok.lit == 'c' is_cstr := p.tok.kind == .name && p.tok.lit == 'c'
if is_raw || is_cstr { if is_raw || is_cstr {
@ -1317,7 +1317,7 @@ fn (var p Parser) string_expr() ast.Expr {
return node return node
} }
fn (var p Parser) array_init() ast.ArrayInit { fn (p mut Parser) array_init() ast.ArrayInit {
first_pos := p.tok.position() first_pos := p.tok.position()
var last_pos := token.Position{} var last_pos := token.Position{}
p.check(.lsbr) p.check(.lsbr)
@ -1402,7 +1402,7 @@ fn (var p Parser) array_init() ast.ArrayInit {
} }
} }
fn (var p Parser) map_init() ast.MapInit { fn (p mut Parser) map_init() ast.MapInit {
pos := p.tok.position() pos := p.tok.position()
var keys := []ast.Expr var keys := []ast.Expr
var vals := []ast.Expr var vals := []ast.Expr
@ -1424,7 +1424,7 @@ fn (var p Parser) map_init() ast.MapInit {
} }
} }
fn (var p Parser) parse_number_literal() ast.Expr { fn (p mut Parser) parse_number_literal() ast.Expr {
lit := p.tok.lit lit := p.tok.lit
pos := p.tok.position() pos := p.tok.position()
var node := ast.Expr{} var node := ast.Expr{}
@ -1442,7 +1442,7 @@ fn (var p Parser) parse_number_literal() ast.Expr {
return node return node
} }
fn (var p Parser) module_decl() ast.Module { fn (p mut Parser) module_decl() ast.Module {
var name := 'main' var name := 'main'
is_skipped := p.tok.kind != .key_module is_skipped := p.tok.kind != .key_module
if !is_skipped { if !is_skipped {
@ -1458,7 +1458,7 @@ fn (var p Parser) module_decl() ast.Module {
} }
} }
fn (var p Parser) parse_import() ast.Import { fn (p mut Parser) parse_import() ast.Import {
pos := p.tok.position() pos := p.tok.position()
var mod_name := p.check_name() var mod_name := p.check_name()
var mod_alias := mod_name var mod_alias := mod_name
@ -1481,7 +1481,7 @@ fn (var p Parser) parse_import() ast.Import {
} }
} }
fn (var p Parser) import_stmt() []ast.Import { fn (p mut Parser) import_stmt() []ast.Import {
p.check(.key_import) p.check(.key_import)
var imports := []ast.Import var imports := []ast.Import
if p.tok.kind == .lpar { if p.tok.kind == .lpar {
@ -1500,7 +1500,7 @@ fn (var p Parser) import_stmt() []ast.Import {
return imports return imports
} }
fn (var p Parser) const_decl() ast.ConstDecl { fn (p mut Parser) const_decl() ast.ConstDecl {
is_pub := p.tok.kind == .key_pub is_pub := p.tok.kind == .key_pub
if is_pub { if is_pub {
p.next() p.next()
@ -1535,7 +1535,7 @@ fn (var p Parser) const_decl() ast.ConstDecl {
} }
// structs and unions // structs and unions
fn (var p Parser) struct_decl() ast.StructDecl { fn (p mut Parser) struct_decl() ast.StructDecl {
is_pub := p.tok.kind == .key_pub is_pub := p.tok.kind == .key_pub
if is_pub { if is_pub {
p.next() p.next()
@ -1679,7 +1679,7 @@ fn (var p Parser) struct_decl() ast.StructDecl {
} }
} }
fn (var p Parser) interface_decl() ast.InterfaceDecl { fn (p mut Parser) interface_decl() ast.InterfaceDecl {
is_pub := p.tok.kind == .key_pub is_pub := p.tok.kind == .key_pub
if is_pub { if is_pub {
p.next() p.next()
@ -1704,7 +1704,7 @@ fn (var p Parser) interface_decl() ast.InterfaceDecl {
} }
} }
fn (var p Parser) return_stmt() ast.Return { fn (p mut Parser) return_stmt() ast.Return {
p.next() p.next()
// return expressions // return expressions
var exprs := []ast.Expr var exprs := []ast.Expr
@ -1730,7 +1730,7 @@ fn (var p Parser) return_stmt() ast.Return {
} }
// left hand side of `=` or `:=` in `a,b,c := 1,2,3` // left hand side of `=` or `:=` in `a,b,c := 1,2,3`
fn (var p Parser) parse_assign_lhs() []ast.Ident { fn (p mut Parser) parse_assign_lhs() []ast.Ident {
var idents := []ast.Ident var idents := []ast.Ident
for { for {
is_mut := p.tok.kind == .key_mut || p.tok.kind == .key_var is_mut := p.tok.kind == .key_mut || p.tok.kind == .key_var
@ -1758,7 +1758,7 @@ fn (var p Parser) parse_assign_lhs() []ast.Ident {
} }
// right hand side of `=` or `:=` in `a,b,c := 1,2,3` // right hand side of `=` or `:=` in `a,b,c := 1,2,3`
fn (var p Parser) parse_assign_rhs() []ast.Expr { fn (p mut Parser) parse_assign_rhs() []ast.Expr {
var exprs := []ast.Expr var exprs := []ast.Expr
for { for {
expr := p.expr(0) expr := p.expr(0)
@ -1772,7 +1772,7 @@ fn (var p Parser) parse_assign_rhs() []ast.Expr {
return exprs return exprs
} }
fn (var p Parser) assign_stmt() ast.Stmt { fn (p mut Parser) assign_stmt() ast.Stmt {
is_static := p.tok.kind == .key_static is_static := p.tok.kind == .key_static
if is_static { if is_static {
p.next() p.next()
@ -1822,7 +1822,7 @@ fn (var p Parser) assign_stmt() ast.Stmt {
} }
} }
fn (var p Parser) global_decl() ast.GlobalDecl { fn (p mut Parser) global_decl() ast.GlobalDecl {
if !p.pref.translated && !p.pref.is_live && !p.builtin_mod && !p.pref.building_v && p.mod != 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 { 'ui' && p.mod != 'gg2' && p.mod != 'uiold' && !os.getwd().contains('/volt') && !p.pref.enable_globals {
p.error('use `v --enable-globals ...` to enable globals') p.error('use `v --enable-globals ...` to enable globals')
@ -1862,7 +1862,7 @@ fn (var p Parser) global_decl() ast.GlobalDecl {
return glob return glob
} }
fn (var p Parser) match_expr() ast.MatchExpr { fn (p mut Parser) match_expr() ast.MatchExpr {
match_first_pos := p.tok.position() match_first_pos := p.tok.position()
p.check(.key_match) p.check(.key_match)
is_mut := p.tok.kind in [.key_mut, .key_var] is_mut := p.tok.kind in [.key_mut, .key_var]
@ -1955,7 +1955,7 @@ fn (var p Parser) match_expr() ast.MatchExpr {
} }
} }
fn (var p Parser) enum_decl() ast.EnumDecl { fn (p mut Parser) enum_decl() ast.EnumDecl {
is_pub := p.tok.kind == .key_pub is_pub := p.tok.kind == .key_pub
if is_pub { if is_pub {
p.next() p.next()
@ -2002,7 +2002,7 @@ fn (var p Parser) enum_decl() ast.EnumDecl {
} }
} }
fn (var p Parser) type_decl() ast.TypeDecl { fn (p mut Parser) type_decl() ast.TypeDecl {
is_pub := p.tok.kind == .key_pub is_pub := p.tok.kind == .key_pub
if is_pub { if is_pub {
p.next() p.next()
@ -2067,7 +2067,7 @@ fn (var p Parser) type_decl() ast.TypeDecl {
} }
} }
fn (var p Parser) assoc() ast.Assoc { fn (p mut Parser) assoc() ast.Assoc {
var_name := p.check_name() var_name := p.check_name()
pos := p.tok.position() pos := p.tok.position()
v := p.scope.find_var(var_name) or { v := p.scope.find_var(var_name) or {