mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vfmt2: fn args, comments
This commit is contained in:
parent
480af3f381
commit
31c1483b9d
@ -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))
|
||||
// }
|
||||
|
@ -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 }
|
||||
|
@ -15,7 +15,8 @@ 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
|
||||
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 {
|
||||
|
@ -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')
|
||||
}
|
||||
|
@ -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,12 +65,14 @@ 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{
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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{}
|
||||
}
|
||||
@ -631,7 +644,9 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
||||
p.check(.lpar)
|
||||
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 }`
|
||||
|
@ -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++
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user