mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vfmt: minor fixes + tests
This commit is contained in:
parent
887f1a73f7
commit
fd75cce0f3
@ -78,7 +78,7 @@ const (
|
|||||||
'vlib/time/time_test.v',
|
'vlib/time/time_test.v',
|
||||||
'vlib/time/misc/misc_test.v',
|
'vlib/time/misc/misc_test.v',
|
||||||
'vlib/v/doc/doc_test.v',
|
'vlib/v/doc/doc_test.v',
|
||||||
// 'vlib/v/fmt/fmt_keep_test.v',
|
'vlib/v/fmt/fmt_keep_test.v',
|
||||||
'vlib/v/fmt/fmt_test.v',
|
'vlib/v/fmt/fmt_test.v',
|
||||||
'vlib/v/gen/cgen_test.v',
|
'vlib/v/gen/cgen_test.v',
|
||||||
// 'vlib/v/parser/parser_test.v',
|
// 'vlib/v/parser/parser_test.v',
|
||||||
|
@ -2,11 +2,8 @@
|
|||||||
// 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.
|
||||||
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 (
|
import (
|
||||||
v.table
|
v.table
|
||||||
strings
|
strings
|
||||||
@ -21,7 +18,10 @@ pub fn (node &FnDecl) str(t &table.Table) string {
|
|||||||
if node.is_method {
|
if node.is_method {
|
||||||
sym := t.get_type_symbol(node.receiver.typ)
|
sym := t.get_type_symbol(node.receiver.typ)
|
||||||
name := sym.name.after('.')
|
name := sym.name.after('.')
|
||||||
m := if node.rec_mut { 'mut ' } else { '' }
|
mut m := if node.rec_mut { 'mut ' } else { '' }
|
||||||
|
if !node.rec_mut && table.type_is_ptr(node.receiver.typ) {
|
||||||
|
m = '&'
|
||||||
|
}
|
||||||
receiver = '($node.receiver.name $m$name) '
|
receiver = '($node.receiver.name $m$name) '
|
||||||
}
|
}
|
||||||
name := node.name.after('.')
|
name := node.name.after('.')
|
||||||
@ -35,14 +35,13 @@ pub fn (node &FnDecl) str(t &table.Table) string {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
is_last_arg := i == node.args.len - 1
|
is_last_arg := i == node.args.len - 1
|
||||||
should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ ||
|
should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ || (node.is_variadic && i ==
|
||||||
(node.is_variadic && i == node.args.len - 2)
|
node.args.len - 2)
|
||||||
f.write(arg.name)
|
f.write(arg.name)
|
||||||
if should_add_type {
|
if should_add_type {
|
||||||
if node.is_variadic && is_last_arg {
|
if node.is_variadic && is_last_arg {
|
||||||
f.write(' ...' + t.type_to_str(arg.typ))
|
f.write(' ...' + t.type_to_str(arg.typ))
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
f.write(' ' + t.type_to_str(arg.typ))
|
f.write(' ' + t.type_to_str(arg.typ))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -82,7 +81,7 @@ pub fn (x Expr) str() string {
|
|||||||
res << "'"
|
res << "'"
|
||||||
for i, val in it.vals {
|
for i, val in it.vals {
|
||||||
res << val
|
res << val
|
||||||
if i>=it.exprs.len {
|
if i >= it.exprs.len {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
res << '$'
|
res << '$'
|
||||||
@ -91,7 +90,7 @@ pub fn (x Expr) str() string {
|
|||||||
res << it.exprs[i].str()
|
res << it.exprs[i].str()
|
||||||
res << it.expr_fmts[i]
|
res << it.expr_fmts[i]
|
||||||
res << '}'
|
res << '}'
|
||||||
}else{
|
} else {
|
||||||
res << it.exprs[i].str()
|
res << it.exprs[i].str()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -117,20 +116,20 @@ pub fn (node Stmt) str() string {
|
|||||||
match node {
|
match node {
|
||||||
AssignStmt {
|
AssignStmt {
|
||||||
mut 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 {
|
||||||
out += 'mut '
|
out += 'mut '
|
||||||
}
|
}
|
||||||
out += ident.name
|
out += ident.name
|
||||||
if i < it.left.len-1 {
|
if i < it.left.len - 1 {
|
||||||
out += ','
|
out += ','
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out += ' $it.op.str() '
|
out += ' $it.op.str() '
|
||||||
for i,val in it.right {
|
for i, val in it.right {
|
||||||
out += val.str()
|
out += val.str()
|
||||||
if i < it.right.len-1 {
|
if i < it.right.len - 1 {
|
||||||
out += ','
|
out += ','
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,3 @@
|
|||||||
import os
|
|
||||||
|
|
||||||
fn foo(a []os.File) {
|
|
||||||
}
|
|
||||||
|
|
||||||
struct IfExpr {
|
struct IfExpr {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
22
vlib/v/fmt/tests/struct_keep.vv
Normal file
22
vlib/v/fmt/tests/struct_keep.vv
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
fn foo(a []os.File) {
|
||||||
|
}
|
||||||
|
|
||||||
|
struct User {
|
||||||
|
age int
|
||||||
|
name string
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_users(users []User) {
|
||||||
|
println(users.len)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn (u &User) foo(u2 &User) {
|
||||||
|
}
|
||||||
|
|
||||||
|
type Expr = IfExpr | IntegerLiteral
|
||||||
|
|
||||||
|
fn exprs(e []Expr) {
|
||||||
|
println(e.len)
|
||||||
|
}
|
@ -613,7 +613,7 @@ pub fn (table &Table) type_to_str(t Type) string {
|
|||||||
if vals.len > 2 {
|
if vals.len > 2 {
|
||||||
res = vals[vals.len - 2] + '.' + vals[vals.len - 1]
|
res = vals[vals.len - 2] + '.' + vals[vals.len - 1]
|
||||||
}
|
}
|
||||||
if sym.kind == .array {
|
if sym.kind == .array && !res.starts_with('[]') {
|
||||||
res = '[]' + res
|
res = '[]' + res
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user