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)
|
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 {
|
// fn (s []string) substr(a, b int) string {
|
||||||
// return join_strings(s.slice_fast(a, b))
|
// return join_strings(s.slice_fast(a, b))
|
||||||
// }
|
// }
|
||||||
|
@ -450,7 +450,7 @@ fn (p mut Parser) parse(pass Pass) {
|
|||||||
}
|
}
|
||||||
p.fgen_nl()
|
p.fgen_nl()
|
||||||
p.builtin_mod = p.mod == 'builtin'
|
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
|
// 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
|
// 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 }
|
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 |
|
pub type Stmt = VarDecl | GlobalDecl | FnDecl | Return | Module | Import | ExprStmt |
|
||||||
ForStmt | StructDecl | ForCStmt | ForInStmt | CompIf | ConstDecl | Attr | BranchStmt |
|
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
|
pub type Type = StructType | ArrayType
|
||||||
|
|
||||||
@ -130,6 +131,8 @@ pub:
|
|||||||
is_pub bool
|
is_pub bool
|
||||||
is_variadic bool
|
is_variadic bool
|
||||||
receiver Field
|
receiver Field
|
||||||
|
is_method bool
|
||||||
|
rec_mut bool // is receiver mutable
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct BranchStmt {
|
pub struct BranchStmt {
|
||||||
@ -458,6 +461,16 @@ pub:
|
|||||||
type_name string
|
type_name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct LineComment {
|
||||||
|
pub:
|
||||||
|
text string
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MultiLineComment {
|
||||||
|
pub:
|
||||||
|
text string
|
||||||
|
}
|
||||||
|
|
||||||
// string representaiton of expr
|
// string representaiton of expr
|
||||||
pub fn (x Expr) str() string {
|
pub fn (x Expr) str() string {
|
||||||
match x {
|
match x {
|
||||||
|
@ -97,13 +97,27 @@ fn (f mut Fmt) stmt(node ast.Stmt) {
|
|||||||
f.writeln('')
|
f.writeln('')
|
||||||
}
|
}
|
||||||
ast.FnDecl {
|
ast.FnDecl {
|
||||||
return_type_sym := f.table.get_type_symbol(it.typ)
|
mut receiver := ''
|
||||||
rtype_name := if return_type_sym.name == 'void' {
|
if it.is_method {
|
||||||
''
|
sym := f.table.get_type_symbol(it.receiver.typ)
|
||||||
} else {
|
name := sym.name.after('.')
|
||||||
'${return_type_sym.name} '
|
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.stmts(it.stmts)
|
||||||
f.writeln('}\n')
|
f.writeln('}\n')
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ pub fn (p mut Parser) call_expr(is_c bool) ast.CallExpr {
|
|||||||
name: fn_name
|
name: fn_name
|
||||||
args: args
|
args: args
|
||||||
// tok: tok
|
// tok: tok
|
||||||
|
|
||||||
pos: tok.position()
|
pos: tok.position()
|
||||||
is_c: is_c
|
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 {
|
fn (p mut Parser) fn_decl() ast.FnDecl {
|
||||||
|
// p.table.clear_vars()
|
||||||
p.open_scope()
|
p.open_scope()
|
||||||
is_pub := p.tok.kind == .key_pub
|
is_pub := p.tok.kind == .key_pub
|
||||||
if is_pub {
|
if is_pub {
|
||||||
@ -63,18 +65,20 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
|
|||||||
mut rec_name := ''
|
mut rec_name := ''
|
||||||
mut is_method := false
|
mut is_method := false
|
||||||
mut rec_type := table.void_type
|
mut rec_type := table.void_type
|
||||||
|
mut rec_mut := false
|
||||||
if p.tok.kind == .lpar {
|
if p.tok.kind == .lpar {
|
||||||
is_method = true
|
is_method = true
|
||||||
p.next()
|
p.next()
|
||||||
rec_name = p.check_name()
|
rec_name = p.check_name()
|
||||||
if p.tok.kind == .key_mut {
|
if p.tok.kind == .key_mut {
|
||||||
p.next()
|
p.next()
|
||||||
|
rec_mut = true
|
||||||
}
|
}
|
||||||
rec_type = p.parse_type()
|
rec_type = p.parse_type()
|
||||||
//p.table.register_var(table.Var{
|
// p.table.register_var(table.Var{
|
||||||
// name: rec_name
|
// name: rec_name
|
||||||
// typ: rec_type
|
// typ: rec_type
|
||||||
//})
|
// })
|
||||||
p.scope.register_var(ast.VarDecl{
|
p.scope.register_var(ast.VarDecl{
|
||||||
name: rec_name
|
name: rec_name
|
||||||
typ: rec_type
|
typ: rec_type
|
||||||
@ -106,7 +110,7 @@ fn (p mut Parser) fn_decl() ast.FnDecl {
|
|||||||
name: ast_arg.name
|
name: ast_arg.name
|
||||||
typ: ast_arg.typ
|
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
|
name: rec_name
|
||||||
typ: rec_type
|
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 {
|
.key_enum {
|
||||||
return p.enum_decl()
|
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 {
|
else {
|
||||||
|
// #printf("");
|
||||||
p.error('parser: bad top level statement')
|
p.error('parser: bad top level statement')
|
||||||
return ast.Stmt{}
|
return ast.Stmt{}
|
||||||
}
|
}
|
||||||
@ -459,7 +472,7 @@ pub fn (p mut Parser) parse_ident(is_c bool) ast.Ident {
|
|||||||
fn (p mut Parser) struct_init() ast.StructInit {
|
fn (p mut Parser) struct_init() ast.StructInit {
|
||||||
typ := p.parse_type()
|
typ := p.parse_type()
|
||||||
sym := p.table.get_type_symbol(typ)
|
sym := p.table.get_type_symbol(typ)
|
||||||
//p.warn('struct init typ=$sym.name')
|
// p.warn('struct init typ=$sym.name')
|
||||||
p.check(.lcbr)
|
p.check(.lcbr)
|
||||||
mut field_names := []string
|
mut field_names := []string
|
||||||
mut exprs := []ast.Expr
|
mut exprs := []ast.Expr
|
||||||
@ -629,9 +642,11 @@ pub fn (p mut Parser) expr(precedence int) (ast.Expr,table.Type) {
|
|||||||
.key_sizeof {
|
.key_sizeof {
|
||||||
p.next() // sizeof
|
p.next() // sizeof
|
||||||
p.check(.lpar)
|
p.check(.lpar)
|
||||||
type_name:= p.check_name()
|
type_name := p.check_name()
|
||||||
p.check(.rpar)
|
p.check(.rpar)
|
||||||
node = ast.SizeOf{ type_name: type_name }
|
node = ast.SizeOf{
|
||||||
|
type_name: type_name
|
||||||
|
}
|
||||||
typ = table.int_type
|
typ = table.int_type
|
||||||
}
|
}
|
||||||
// Map `{"age": 20}` or `{ x | foo:bar, a:10 }`
|
// Map `{"age": 20}` or `{ x | foo:bar, a:10 }`
|
||||||
|
@ -66,6 +66,9 @@ fn new_scanner_file(file_path string) &Scanner {
|
|||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
is_fmt = os.getenv('VEXE').contains('vfmt')
|
||||||
|
)
|
||||||
// new scanner from string.
|
// new scanner from string.
|
||||||
pub fn new_scanner(text string) &Scanner {
|
pub fn new_scanner(text string) &Scanner {
|
||||||
return &Scanner{
|
return &Scanner{
|
||||||
@ -73,6 +76,7 @@ pub fn new_scanner(text string) &Scanner {
|
|||||||
print_line_on_error: true
|
print_line_on_error: true
|
||||||
print_colored_error: true
|
print_colored_error: true
|
||||||
print_rel_paths_on_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
|
mut has_exponential_part := false
|
||||||
if s.expect('e', s.pos) || s.expect('E', s.pos) {
|
if s.expect('e', s.pos) || s.expect('E', s.pos) {
|
||||||
exp_start_pos := (s.pos++)
|
exp_start_pos := (s.pos++)
|
||||||
|
|
||||||
if s.text[s.pos] in [`-`, `+`] {
|
if s.text[s.pos] in [`-`, `+`] {
|
||||||
s.pos++
|
s.pos++
|
||||||
}
|
}
|
||||||
|
|
||||||
for s.pos < s.text.len && s.text[s.pos].is_digit() {
|
for s.pos < s.text.len && s.text[s.pos].is_digit() {
|
||||||
s.pos++
|
s.pos++
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user