vfmt: minor fixes + tests

pull/4268/head
Alexander Medvednikov 2020-04-07 04:05:59 +02:00
parent 887f1a73f7
commit fd75cce0f3
5 changed files with 38 additions and 22 deletions

View File

@ -78,7 +78,7 @@ const (
'vlib/time/time_test.v',
'vlib/time/misc/misc_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/gen/cgen_test.v',
// 'vlib/v/parser/parser_test.v',

View File

@ -2,11 +2,8 @@
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
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
strings
@ -21,7 +18,10 @@ pub fn (node &FnDecl) str(t &table.Table) string {
if node.is_method {
sym := t.get_type_symbol(node.receiver.typ)
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) '
}
name := node.name.after('.')
@ -35,14 +35,13 @@ pub fn (node &FnDecl) str(t &table.Table) string {
continue
}
is_last_arg := i == node.args.len - 1
should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ ||
(node.is_variadic && i == node.args.len - 2)
should_add_type := is_last_arg || node.args[i + 1].typ != arg.typ || (node.is_variadic && i ==
node.args.len - 2)
f.write(arg.name)
if should_add_type {
if node.is_variadic && is_last_arg {
f.write(' ...' + t.type_to_str(arg.typ))
}
else {
} else {
f.write(' ' + t.type_to_str(arg.typ))
}
}
@ -82,7 +81,7 @@ pub fn (x Expr) str() string {
res << "'"
for i, val in it.vals {
res << val
if i>=it.exprs.len {
if i >= it.exprs.len {
continue
}
res << '$'
@ -91,7 +90,7 @@ pub fn (x Expr) str() string {
res << it.exprs[i].str()
res << it.expr_fmts[i]
res << '}'
}else{
} else {
res << it.exprs[i].str()
}
}
@ -117,20 +116,20 @@ pub fn (node Stmt) str() string {
match node {
AssignStmt {
mut out := ''
for i,ident in it.left {
for i, ident in it.left {
var_info := ident.var_info()
if var_info.is_mut {
out += 'mut '
}
out += ident.name
if i < it.left.len-1 {
if i < it.left.len - 1 {
out += ','
}
}
out += ' $it.op.str() '
for i,val in it.right {
for i, val in it.right {
out += val.str()
if i < it.right.len-1 {
if i < it.right.len - 1 {
out += ','
}
}

View File

@ -1,8 +1,3 @@
import os
fn foo(a []os.File) {
}
struct IfExpr {
}

View 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)
}

View File

@ -613,7 +613,7 @@ pub fn (table &Table) type_to_str(t Type) string {
if vals.len > 2 {
res = vals[vals.len - 2] + '.' + vals[vals.len - 1]
}
if sym.kind == .array {
if sym.kind == .array && !res.starts_with('[]') {
res = '[]' + res
}
}