From 31c1483b9dbd30c063d91bb0de1116879b746a6d Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 18 Feb 2020 20:20:15 +0100 Subject: [PATCH] vfmt2: fn args, comments --- vlib/builtin/string.v | 2 ++ vlib/compiler/aparser.v | 2 +- vlib/v/ast/ast.v | 25 +++++++++++++++++++------ vlib/v/fmt/fmt.v | 26 ++++++++++++++++++++------ vlib/v/parser/fn.v | 16 +++++++++++----- vlib/v/parser/parser.v | 35 +++++++++++++++++++++++++---------- vlib/v/scanner/scanner.v | 6 ++++-- 7 files changed, 82 insertions(+), 30 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 69bb5338dd..09fda6462f 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -1145,6 +1145,8 @@ pub fn (s string) all_after(dot string) string { return s.right(pos + dot.len) } +pub fn (s string) after(dot string) string { return s.all_after(dot) } + // fn (s []string) substr(a, b int) string { // return join_strings(s.slice_fast(a, b)) // } diff --git a/vlib/compiler/aparser.v b/vlib/compiler/aparser.v index 6c5bd57666..446b47328f 100644 --- a/vlib/compiler/aparser.v +++ b/vlib/compiler/aparser.v @@ -450,7 +450,7 @@ fn (p mut Parser) parse(pass Pass) { } p.fgen_nl() p.builtin_mod = p.mod == 'builtin' - p.can_chash = p.mod in ['gg2', 'ui', 'uiold', 'darwin', 'clipboard', 'webview'] // TODO tmp remove + p.can_chash = p.mod in ['parser', 'gg2', 'ui', 'uiold', 'darwin', 'clipboard', 'webview'] // TODO tmp remove // Import pass - the first and the smallest pass that only analyzes imports // if we are a building module get the full module name from v.mod fq_mod := if p.pref.build_mode == .build_module && p.v.pref.mod.ends_with(p.mod) { p.v.pref.mod } diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 4885974a91..37eeb07e2b 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -8,14 +8,15 @@ import ( v.table ) -pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral | -FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | -AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | +pub type Expr = InfixExpr | IfExpr | StringLiteral | IntegerLiteral | CharLiteral | +FloatLiteral | Ident | CallExpr | BoolLiteral | StructInit | ArrayInit | SelectorExpr | PostfixExpr | +AssignExpr | PrefixExpr | MethodCallExpr | IndexExpr | RangeExpr | MatchExpr | CastExpr | EnumVal | Assoc | SizeOf -pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | -ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | -HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt +pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt | +ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt | +HashStmt | AssignStmt | EnumDecl | TypeDecl | DeferStmt | GotoLabel | GotoStmt | +LineComment | MultiLineComment pub type Type = StructType | ArrayType @@ -130,6 +131,8 @@ pub: is_pub bool is_variadic bool receiver Field + is_method bool + rec_mut bool // is receiver mutable } pub struct BranchStmt { @@ -458,6 +461,16 @@ pub: type_name string } +pub struct LineComment { +pub: + text string +} + +pub struct MultiLineComment { +pub: + text string +} + // string representaiton of expr pub fn (x Expr) str() string { match x { diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index f37943ab80..fc6268a7a3 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -97,13 +97,27 @@ fn (f mut Fmt) stmt(node ast.Stmt) { f.writeln('') } ast.FnDecl { - return_type_sym := f.table.get_type_symbol(it.typ) - rtype_name := if return_type_sym.name == 'void' { - '' - } else { - '${return_type_sym.name} ' + mut receiver := '' + if it.is_method { + sym := f.table.get_type_symbol(it.receiver.typ) + name := sym.name.after('.') + m := if it.rec_mut { 'mut ' } else { '' } + receiver = '($it.receiver.name ${m}$name) ' } - f.writeln('fn ${it.name}() ${rtype_name}{') + f.write('fn ${receiver}${it.name}(') + for i, arg in it.args { + sym := f.table.get_type_symbol(arg.typ) + f.write('$arg.name $sym.name') + if i < it.args.len - 1 { + f.write(', ') + } + } + f.write(')') + if it.typ != table.void_type { + sym := f.table.get_type_symbol(it.typ) + f.write(' ' + sym.name) + } + f.writeln(' {') f.stmts(it.stmts) f.writeln('}\n') } diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 125021ee55..1a01e88ffa 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -17,6 +17,7 @@ pub fn (p mut Parser) call_expr(is_c bool) ast.CallExpr { name: fn_name args: args // tok: tok + pos: tok.position() is_c: is_c } @@ -47,6 +48,7 @@ pub fn (p mut Parser) call_args() []ast.Expr { } fn (p mut Parser) fn_decl() ast.FnDecl { + // p.table.clear_vars() p.open_scope() is_pub := p.tok.kind == .key_pub if is_pub { @@ -63,18 +65,20 @@ fn (p mut Parser) fn_decl() ast.FnDecl { mut rec_name := '' mut is_method := false mut rec_type := table.void_type + mut rec_mut := false if p.tok.kind == .lpar { is_method = true p.next() rec_name = p.check_name() if p.tok.kind == .key_mut { p.next() + rec_mut = true } rec_type = p.parse_type() - //p.table.register_var(table.Var{ - // name: rec_name - // typ: rec_type - //}) + // p.table.register_var(table.Var{ + // name: rec_name + // typ: rec_type + // }) p.scope.register_var(ast.VarDecl{ name: rec_name typ: rec_type @@ -106,7 +110,7 @@ fn (p mut Parser) fn_decl() ast.FnDecl { name: ast_arg.name typ: ast_arg.typ }) - //p.table.register_var(var) + // p.table.register_var(var) } // /* @@ -166,6 +170,8 @@ fn (p mut Parser) fn_decl() ast.FnDecl { name: rec_name typ: rec_type } + is_method: is_method + rec_mut: rec_mut } } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index c323a72480..34ebde3264 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -55,7 +55,7 @@ pub fn parse_stmt(text string, table &table.Table, scope &ast.Scope) ast.Stmt { pref: &pref.Preferences{} scope: scope // scope: &ast.Scope{start_pos: 0, parent: 0} - + } p.init_parse_fns() p.read_first_token() @@ -237,7 +237,20 @@ pub fn (p mut Parser) top_stmt() ast.Stmt { .key_enum { return p.enum_decl() } + .line_comment { + // p.next() + return ast.LineComment{ + text: p.scanner.line_comment + } + } + .mline_comment { + // p.next() + return ast.MultiLineComment{ + text: p.scanner.line_comment + } + } else { + // #printf(""); p.error('parser: bad top level statement') return ast.Stmt{} } @@ -306,7 +319,7 @@ pub fn (p mut Parser) stmt() ast.Stmt { return ast.ExprStmt{ expr: expr // typ: typ - + } } } @@ -459,7 +472,7 @@ pub fn (p mut Parser) parse_ident(is_c bool) ast.Ident { fn (p mut Parser) struct_init() ast.StructInit { typ := p.parse_type() sym := p.table.get_type_symbol(typ) - //p.warn('struct init typ=$sym.name') + // p.warn('struct init typ=$sym.name') p.check(.lcbr) mut field_names := []string mut exprs := []ast.Expr @@ -629,9 +642,11 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) { .key_sizeof { p.next() // sizeof p.check(.lpar) - type_name:= p.check_name() + type_name := p.check_name() p.check(.rpar) - node = ast.SizeOf{ type_name: type_name } + node = ast.SizeOf{ + type_name: type_name + } typ = table.int_type } // Map `{"age": 20}` or `{ x | foo:bar, a:10 }` @@ -1046,10 +1061,10 @@ fn (p mut Parser) if_expr() ast.Expr { stmts: stmts else_stmts: else_stmts // typ: typ - + pos: p.tok.position() // left: left - + } return node } @@ -1435,10 +1450,10 @@ fn (p mut Parser) var_decl() ast.VarDecl { node := ast.VarDecl{ name: name expr: expr // p.expr(token.lowest_prec) - + is_mut: is_mut // typ: typ - + pos: p.tok.position() } p.scope.register_var(node) @@ -1557,7 +1572,7 @@ fn (p mut Parser) match_expr() ast.Expr { blocks: blocks match_exprs: match_exprs // typ: typ - + cond: cond } return node diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 1cd4c59277..efe6dcc64b 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -66,6 +66,9 @@ fn new_scanner_file(file_path string) &Scanner { return s } +const ( + is_fmt = os.getenv('VEXE').contains('vfmt') +) // new scanner from string. pub fn new_scanner(text string) &Scanner { return &Scanner{ @@ -73,6 +76,7 @@ pub fn new_scanner(text string) &Scanner { print_line_on_error: true print_colored_error: true print_rel_paths_on_error: true + is_fmt: is_fmt } } @@ -211,11 +215,9 @@ fn (s mut Scanner) ident_dec_number() string { mut has_exponential_part := false if s.expect('e', s.pos) || s.expect('E', s.pos) { exp_start_pos := (s.pos++) - if s.text[s.pos] in [`-`, `+`] { s.pos++ } - for s.pos < s.text.len && s.text[s.pos].is_digit() { s.pos++ }