v/vlib/v/fmt/fmt.v

877 lines
18 KiB
V
Raw Normal View History

2020-02-03 05:00:36 +01:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2019-12-22 02:34:37 +01:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module fmt
2020-02-17 22:50:04 +01:00
import (
v.ast
v.table
strings
)
const (
2020-04-09 15:33:46 +02:00
tabs = ['', '\t', '\t\t', '\t\t\t', '\t\t\t\t', '\t\t\t\t\t', '\t\t\t\t\t\t', '\t\t\t\t\t\t\t']
2020-04-07 18:51:39 +02:00
max_len = 90
2020-02-17 22:50:04 +01:00
)
2019-12-22 02:34:37 +01:00
struct Fmt {
2020-02-21 16:48:37 +01:00
out strings.Builder
out_imports strings.Builder
2020-02-21 16:48:37 +01:00
table &table.Table
2020-02-17 22:50:04 +01:00
mut:
2020-02-21 16:48:37 +01:00
indent int
empty_line bool
line_len int
single_line_if bool
2020-02-21 19:56:37 +01:00
cur_mod string
2020-04-05 18:03:36 +02:00
file ast.File
did_imports bool
is_assign bool
auto_imports []string // automatically inserted imports that the user forgot to specify
import_pos int // position of the imports in the resulting string for later autoimports insertion
2020-04-13 15:05:51 +02:00
used_imports []string // to remove unused imports
2020-02-17 22:50:04 +01:00
}
pub fn fmt(file ast.File, table &table.Table) string {
2020-04-09 15:33:46 +02:00
mut f := Fmt{
2020-02-17 22:50:04 +01:00
out: strings.new_builder(1000)
out_imports: strings.new_builder(200)
2020-02-17 22:50:04 +01:00
table: table
2020-02-18 22:35:14 +01:00
indent: 0
2020-04-05 02:08:10 +02:00
file: file
2020-02-17 22:50:04 +01:00
}
2020-04-05 21:53:00 +02:00
f.cur_mod = 'main'
for i, stmt in file.stmts {
// TODO `if stmt is ast.Import`
match stmt {
ast.Import {
// Just remember the position of the imports for now
f.import_pos = f.out.len
// f.imports(f.file.imports)
}
else {}
}
2020-02-19 16:12:39 +01:00
f.stmt(stmt)
}
2020-04-05 18:15:12 +02:00
// for comment in file.comments { println('$comment.line_nr $comment.text') }
f.imports(f.file.imports) // now that we have all autoimports, handle them
res := f.out.str().trim_space() + '\n'
return res[..f.import_pos] + f.out_imports.str() + res[f.import_pos..]
2020-02-17 22:50:04 +01:00
}
2020-04-05 02:08:10 +02:00
/*
fn (f mut Fmt) find_comment(line_nr int) {
for comment in f.file.comments {
if comment.line_nr == line_nr {
f.writeln('// FFF $comment.line_nr $comment.text')
return
}
}
}
*/
2020-02-17 22:50:04 +01:00
pub fn (f mut Fmt) write(s string) {
if f.indent > 0 && f.empty_line {
f.out.write(tabs[f.indent])
2020-02-22 16:59:50 +01:00
f.line_len += f.indent * 4
2020-02-17 22:50:04 +01:00
}
f.out.write(s)
2020-02-21 15:32:48 +01:00
f.line_len += s.len
2020-02-18 03:28:39 +01:00
f.empty_line = false
2020-02-17 22:50:04 +01:00
}
pub fn (f mut Fmt) writeln(s string) {
2020-02-18 03:28:39 +01:00
if f.indent > 0 && f.empty_line {
// println(f.indent.str() + s)
2020-02-17 22:50:04 +01:00
f.out.write(tabs[f.indent])
}
f.out.writeln(s)
f.empty_line = true
2020-02-21 15:32:48 +01:00
f.line_len = 0
2020-02-17 22:50:04 +01:00
}
2020-02-18 22:35:14 +01:00
fn (f mut Fmt) mod(mod ast.Module) {
2020-02-21 19:56:37 +01:00
f.cur_mod = mod.name
if mod.is_skipped {
return
}
f.writeln('module $mod.name\n')
2020-02-18 22:35:14 +01:00
}
fn (f mut Fmt) imports(imports []ast.Import) {
2020-04-05 21:53:00 +02:00
if f.did_imports {
return
}
f.did_imports = true
// f.import_pos = f.out.len
2020-02-18 22:35:14 +01:00
if imports.len == 1 {
imp_stmt_str := f.imp_stmt_str(imports[0])
f.out_imports.writeln('import ${imp_stmt_str}\n')
} else if imports.len > 1 {
f.out_imports.writeln('import (')
// f.indent++
2020-02-18 22:35:14 +01:00
for imp in imports {
2020-04-13 15:05:51 +02:00
if !(imp.mod in f.used_imports) {
continue
}
f.out_imports.write('\t')
f.out_imports.writeln(f.imp_stmt_str(imp))
2020-02-18 22:35:14 +01:00
}
// f.indent--
f.out_imports.writeln(')\n')
2020-02-18 22:35:14 +01:00
}
}
fn (f Fmt) imp_stmt_str(imp ast.Import) string {
2020-02-21 20:12:55 +01:00
is_diff := imp.alias != imp.mod && !imp.mod.ends_with('.' + imp.alias)
imp_alias_suffix := if is_diff { ' as ${imp.alias}' } else { '' }
2020-02-18 22:35:14 +01:00
return '${imp.mod}${imp_alias_suffix}'
}
2020-02-17 22:50:04 +01:00
fn (f mut Fmt) stmts(stmts []ast.Stmt) {
f.indent++
for stmt in stmts {
f.stmt(stmt)
}
f.indent--
}
fn (f mut Fmt) stmt(node ast.Stmt) {
match node {
ast.AssignStmt {
for i, ident in it.left {
var_info := ident.var_info()
if var_info.is_mut {
2020-02-28 14:41:19 +01:00
f.write('mut ')
}
f.expr(ident)
if i < it.left.len - 1 {
2020-02-17 22:50:04 +01:00
f.write(', ')
}
}
2020-04-05 23:00:17 +02:00
f.is_assign = true
f.write(' $it.op.str() ')
for i, val in it.right {
f.expr(val)
if i < it.right.len - 1 {
f.write(', ')
}
2020-02-17 22:50:04 +01:00
}
if !f.single_line_if {
f.writeln('')
}
2020-04-05 23:00:17 +02:00
f.is_assign = false
2020-02-17 22:50:04 +01:00
}
2020-04-07 18:51:39 +02:00
ast.AssertStmt {
f.write('assert ')
f.expr(it.expr)
f.writeln('')
}
2020-02-24 17:18:14 +01:00
ast.Attr {
f.writeln('[$it.name]')
}
2020-04-05 23:00:17 +02:00
ast.Block {
f.writeln('{')
f.stmts(it.stmts)
f.writeln('}')
}
2020-02-18 03:28:39 +01:00
ast.BranchStmt {
match it.tok.kind {
.key_break {
f.writeln('break')
}
.key_continue {
f.writeln('continue')
}
else {}
2020-04-05 18:03:36 +02:00
}
}
ast.Comment {
f.comment(it)
2020-02-18 03:28:39 +01:00
}
2020-04-07 18:51:39 +02:00
ast.CompIf {
inversion := if it.is_not { '!' } else { '' }
f.writeln('\$if ${inversion}${it.val} {')
f.stmts(it.stmts)
if it.has_else {
f.writeln('} \$else {')
f.stmts(it.else_stmts)
}
f.writeln('}')
}
2020-02-17 22:50:04 +01:00
ast.ConstDecl {
if it.is_pub {
f.write('pub ')
}
2020-02-17 22:50:04 +01:00
f.writeln('const (')
2020-04-09 01:10:08 +02:00
mut max := 0
for field in it.fields {
if field.name.len > max {
max = field.name.len
}
}
2020-02-17 22:50:04 +01:00
f.indent++
for i, field in it.fields {
2020-02-22 16:59:50 +01:00
name := field.name.after('.')
2020-04-09 01:10:08 +02:00
f.write('$name ')
f.write(strings.repeat(` `, max - field.name.len))
f.write('= ')
f.expr(field.expr)
2020-02-21 20:12:55 +01:00
f.writeln('')
2020-02-17 22:50:04 +01:00
}
f.indent--
2020-02-21 22:56:44 +01:00
f.writeln(')\n')
2020-02-17 22:50:04 +01:00
}
2020-02-26 20:44:42 +01:00
ast.DeferStmt {
f.writeln('defer {')
f.stmts(it.stmts)
f.writeln('}')
}
2020-02-28 17:21:20 +01:00
ast.EnumDecl {
if it.is_pub {
f.write('pub ')
}
2020-04-09 15:33:46 +02:00
name := it.name.after('.')
f.writeln('enum $name {')
2020-04-09 19:23:49 +02:00
for field in it.fields {
f.write('\t$field.name')
2020-04-10 14:44:01 +02:00
if field.has_expr {
2020-04-09 19:23:49 +02:00
f.write(' = ')
2020-04-10 14:44:01 +02:00
f.expr(field.expr)
2020-04-09 19:23:49 +02:00
}
f.writeln('')
2020-02-28 17:21:20 +01:00
}
f.writeln('}\n')
}
2020-02-17 22:50:04 +01:00
ast.ExprStmt {
f.expr(it.expr)
2020-02-21 16:48:37 +01:00
if !f.single_line_if {
f.writeln('')
}
2020-02-17 22:50:04 +01:00
}
ast.FnDecl {
2020-04-05 18:03:36 +02:00
// println('$it.name find_comment($it.pos.line_nr)')
// f.find_comment(it.pos.line_nr)
2020-02-28 17:21:20 +01:00
s := it.str(f.table)
// f.write(it.str(f.table))
2020-04-05 18:03:36 +02:00
f.write(s.replace(f.cur_mod + '.', '')) // `Expr` instead of `ast.Expr` in mod ast
if !it.is_c {
f.writeln(' {')
f.stmts(it.stmts)
f.writeln('}\n')
} else {
f.writeln('\n')
}
2020-04-13 15:05:51 +02:00
// Mark all function's used type so that they are not removed from imports
for arg in it.args {
f.mark_types_module_as_used(arg.typ)
}
f.mark_types_module_as_used(it.return_type)
2020-02-17 22:50:04 +01:00
}
ast.ForCStmt {
f.write('for ')
if it.has_init {
f.single_line_if = true // to keep all for ;; exprs on the same line
f.stmt(it.init)
f.single_line_if = false
}
f.write('; ')
f.expr(it.cond)
f.write('; ')
f.expr(it.inc)
f.writeln(' {')
f.stmts(it.stmts)
f.writeln('}')
}
2020-02-22 14:39:25 +01:00
ast.ForInStmt {
2020-03-20 20:19:43 +01:00
f.write('for ')
if it.key_var != '' {
f.write(it.key_var)
}
if it.val_var != '' {
2020-03-20 20:19:43 +01:00
if it.key_var != '' {
f.write(', ')
}
f.write(it.val_var)
}
f.write(' in ')
2020-02-22 16:59:50 +01:00
f.expr(it.cond)
if it.is_range {
f.write(' .. ')
f.expr(it.high)
}
2020-02-22 16:59:50 +01:00
f.writeln(' {')
2020-02-22 14:39:25 +01:00
f.stmts(it.stmts)
f.writeln('}')
}
2020-02-18 03:28:39 +01:00
ast.ForStmt {
f.write('for ')
f.expr(it.cond)
if it.is_inf {
f.writeln('{')
} else {
f.writeln(' {')
}
2020-02-18 03:28:39 +01:00
f.stmts(it.stmts)
f.writeln('}')
}
2020-04-07 18:51:39 +02:00
ast.GlobalDecl {
f.write('__global $it.name ')
2020-04-09 15:33:46 +02:00
f.write(f.type_to_str(it.typ))
2020-04-07 18:51:39 +02:00
if it.has_expr {
f.write(' = ')
f.expr(it.expr)
}
}
2020-03-02 17:41:32 +01:00
ast.GotoLabel {
f.writeln('$it.name:')
}
ast.GotoStmt {
f.writeln('goto $it.name')
}
2020-04-08 21:21:58 +02:00
ast.HashStmt {
f.writeln('#$it.val')
}
2020-04-05 18:03:36 +02:00
ast.Import {
// Imports are handled after the file is formatted, to automatically add necessary modules
// f.imports(f.file.imports)
2020-04-05 18:03:36 +02:00
}
ast.Module {
f.mod(it)
}
2020-02-17 22:50:04 +01:00
ast.Return {
f.write('return')
if it.exprs.len > 1 {
2020-04-05 18:03:36 +02:00
// multiple returns
2020-02-18 03:28:39 +01:00
f.write(' ')
2020-02-17 22:50:04 +01:00
for i, expr in it.exprs {
f.expr(expr)
if i < it.exprs.len - 1 {
f.write(', ')
}
}
2020-04-05 18:03:36 +02:00
} else if it.exprs.len == 1 {
// normal return
2020-02-17 22:50:04 +01:00
f.write(' ')
f.expr(it.exprs[0])
}
f.writeln('')
}
ast.StructDecl {
f.struct_decl(it)
}
ast.TypeDecl {
2020-04-05 02:08:10 +02:00
// already handled in f.imports
f.type_decl(it)
}
2020-04-07 18:51:39 +02:00
ast.UnsafeStmt {
f.writeln('unsafe {')
f.stmts(it.stmts)
f.writeln('}')
}
2020-02-17 22:50:04 +01:00
else {
eprintln('fmt stmt: unhandled node ' + typeof(node))
if typeof(node) != 'unknown v.ast.Expr' {
exit(1)
}
2020-02-17 22:50:04 +01:00
}
}
}
fn (f mut Fmt) type_decl(node ast.TypeDecl) {
match node {
ast.AliasTypeDecl {
if it.is_pub {
f.write('pub ')
}
2020-04-09 15:33:46 +02:00
ptype := f.type_to_str(it.parent_type)
f.write('type $it.name $ptype')
}
ast.SumTypeDecl {
if it.is_pub {
f.write('pub ')
}
f.write('type $it.name = ')
mut sum_type_names := []string
for t in it.sub_types {
2020-04-09 15:33:46 +02:00
sum_type_names << f.type_to_str(t)
}
f.write(sum_type_names.join(' | '))
}
else {
eprintln('fmt type_decl: unknown ' + typeof(node))
}
}
f.writeln('\n')
}
2020-02-17 22:50:04 +01:00
fn (f mut Fmt) struct_decl(node ast.StructDecl) {
2020-02-29 17:51:35 +01:00
if node.is_pub {
f.write('pub ')
}
2020-04-05 02:08:10 +02:00
name := node.name.after('.')
f.writeln('struct $name {')
2020-02-17 22:50:04 +01:00
mut max := 0
for field in node.fields {
if field.name.len > max {
max = field.name.len
}
}
2020-02-22 14:13:19 +01:00
for i, field in node.fields {
if i == node.mut_pos {
f.writeln('mut:')
2020-04-05 18:03:36 +02:00
} else if i == node.pub_pos {
2020-02-22 14:13:19 +01:00
f.writeln('pub:')
2020-04-05 18:03:36 +02:00
} else if i == node.pub_mut_pos {
2020-02-22 14:13:19 +01:00
f.writeln('pub mut:')
}
if field.comment.text != '' && field.comment.pos.line_nr < field.pos.line_nr {
// Comment on the previous line
2020-04-05 02:08:10 +02:00
f.write('\t')
f.comment(field.comment)
}
2020-02-17 22:50:04 +01:00
f.write('\t$field.name ')
f.write(strings.repeat(` `, max - field.name.len))
f.write(f.type_to_str(field.typ))
if field.has_default_expr {
f.write(' = ${field.default_expr.str()}')
2020-04-10 00:30:43 +02:00
}
2020-04-05 18:03:36 +02:00
// f.write('// $field.pos.line_nr')
if field.comment.text != '' && field.comment.pos.line_nr == field.pos.line_nr {
// Same line comment
2020-04-05 18:03:36 +02:00
f.write(' ')
f.comment(field.comment)
} else {
2020-04-05 18:03:36 +02:00
// if field.comment.text != '' {
// f.write (' // com linenr=$field.comment.pos.line_nr')
// }
f.writeln('')
}
2020-02-17 22:50:04 +01:00
}
f.writeln('}\n')
}
2020-04-09 15:33:46 +02:00
fn (f &Fmt) type_to_str(t table.Type) string {
2020-02-21 19:56:37 +01:00
res := f.table.type_to_str(t)
return res.replace(f.cur_mod + '.', '')
}
2020-02-17 22:50:04 +01:00
fn (f mut Fmt) expr(node ast.Expr) {
match node {
ast.ArrayInit {
if it.exprs.len == 0 && it.typ != 0 && it.typ != table.void_type {
2020-04-05 18:03:36 +02:00
// `x := []string`
2020-04-09 15:33:46 +02:00
f.write(f.type_to_str(it.typ))
2020-04-05 18:03:36 +02:00
} else {
// `[1,2,3]`
2020-02-27 21:00:33 +01:00
// type_sym := f.table.get_type_symbol(it.typ)
f.write('[')
for i, expr in it.exprs {
if i > 0 && it.exprs.len > 1 {
f.wrap_long_line()
}
f.expr(expr)
if i < it.exprs.len - 1 {
f.write(', ')
}
2020-02-17 22:50:04 +01:00
}
2020-02-27 21:00:33 +01:00
f.write(']')
2020-02-17 22:50:04 +01:00
}
}
2020-03-06 22:12:15 +01:00
ast.AsCast {
2020-04-09 15:33:46 +02:00
type_str := f.type_to_str(it.typ)
2020-03-06 22:12:15 +01:00
f.expr(it.expr)
f.write(' as $type_str')
}
2020-02-17 22:50:04 +01:00
ast.AssignExpr {
f.expr(it.left)
f.write(' $it.op.str() ')
f.expr(it.val)
}
2020-02-22 14:39:25 +01:00
ast.Assoc {
f.writeln('{')
// f.indent++
2020-02-29 18:07:29 +01:00
f.writeln('\t$it.var_name |')
2020-02-22 14:39:25 +01:00
// TODO StructInit copy pasta
for i, field in it.fields {
2020-04-09 15:59:19 +02:00
f.write('\t$field: ')
2020-02-22 14:39:25 +01:00
f.expr(it.exprs[i])
f.writeln('')
}
// f.indent--
f.write('}')
}
2020-02-17 22:50:04 +01:00
ast.BoolLiteral {
f.write(it.val.str())
}
ast.CastExpr {
2020-04-09 15:33:46 +02:00
f.write(f.type_to_str(it.typ) + '(')
f.expr(it.expr)
f.write(')')
}
2020-02-17 22:50:04 +01:00
ast.CallExpr {
f.call_expr(it)
2020-02-17 22:50:04 +01:00
}
2020-02-27 21:51:40 +01:00
ast.CharLiteral {
f.write('`$it.val`')
}
2020-02-18 03:28:39 +01:00
ast.EnumVal {
2020-04-08 19:15:16 +02:00
name := short_module(it.enum_name)
2020-04-08 18:55:50 +02:00
f.write(name + '.' + it.val)
2020-02-18 03:28:39 +01:00
}
2020-02-17 22:50:04 +01:00
ast.FloatLiteral {
f.write(it.val)
}
ast.IfExpr {
2020-04-05 18:03:36 +02:00
f.if_expr(it)
2020-02-17 22:50:04 +01:00
}
ast.Ident {
2020-02-27 21:51:40 +01:00
if it.kind == .blank_ident {
f.write('_')
2020-04-05 18:03:36 +02:00
} else {
name := short_module(it.name)
f.write(name)
2020-02-27 21:51:40 +01:00
}
2020-02-17 22:50:04 +01:00
}
ast.InfixExpr {
f.expr(it.left)
f.write(' $it.op.str() ')
2020-02-21 16:14:15 +01:00
f.wrap_long_line()
2020-02-17 22:50:04 +01:00
f.expr(it.right)
}
ast.IndexExpr {
2020-02-29 17:45:08 +01:00
f.expr(it.left)
f.write('[')
f.expr(it.index)
f.write(']')
2020-02-17 22:50:04 +01:00
}
ast.IntegerLiteral {
f.write(it.val)
2020-02-17 22:50:04 +01:00
}
2020-02-22 14:13:19 +01:00
ast.MapInit {
f.writeln('{')
f.indent++
for i, key in it.keys {
f.expr(key)
// f.write(strings.repeat(` `, max - field.name.len))
f.write(': ')
f.expr(it.vals[i])
f.writeln('')
}
f.indent--
f.write('}')
}
2020-02-29 20:43:15 +01:00
ast.MatchExpr {
f.write('match ')
if it.is_mut {
f.write('mut ')
}
2020-02-29 20:43:15 +01:00
f.expr(it.cond)
f.writeln(' {')
f.indent++
2020-03-04 12:14:37 +01:00
for i, branch in it.branches {
if branch.comment.text != '' {
f.comment(branch.comment)
}
2020-03-04 12:14:37 +01:00
if i < it.branches.len - 1 {
// normal branch
for j, expr in branch.exprs {
2020-03-04 12:14:37 +01:00
f.expr(expr)
if j < branch.exprs.len - 1 {
f.write(', ')
}
}
2020-04-05 18:03:36 +02:00
} else {
// else branch
2020-03-04 12:39:18 +01:00
f.write('else')
}
if branch.stmts.len == 0 {
2020-03-04 12:39:18 +01:00
f.writeln(' {}')
2020-04-05 18:03:36 +02:00
} else {
2020-04-11 04:09:41 +02:00
// TODO single line branches
// if branch.stmts.len < 2 {
// f.write(' { ')
// } else {
2020-03-04 12:39:18 +01:00
f.writeln(' {')
2020-04-11 04:09:41 +02:00
// f.single_line_if = true
// }
2020-03-04 12:39:18 +01:00
f.stmts(branch.stmts)
2020-04-11 04:09:41 +02:00
// f.single_line_if = false
2020-03-04 12:39:18 +01:00
f.writeln('}')
2020-03-04 12:14:37 +01:00
}
2020-02-29 20:43:15 +01:00
}
f.indent--
f.write('}')
}
2020-02-21 17:52:20 +01:00
ast.None {
f.write('none')
}
ast.IfGuardExpr {
f.write(it.var_name + ' := ')
f.expr(it.expr)
}
ast.ParExpr {
2020-02-28 14:41:19 +01:00
f.write('(')
f.expr(it.expr)
f.write(')')
}
2020-02-17 22:50:04 +01:00
ast.PostfixExpr {
f.expr(it.expr)
f.write(it.op.str())
}
ast.PrefixExpr {
f.write(it.op.str())
f.expr(it.right)
}
2020-02-29 17:45:08 +01:00
ast.RangeExpr {
f.expr(it.low)
f.write('..')
f.expr(it.high)
}
2020-02-17 22:50:04 +01:00
ast.SelectorExpr {
f.expr(it.expr)
f.write('.')
f.write(it.field)
}
2020-04-07 18:51:39 +02:00
ast.SizeOf {
2020-04-10 00:30:43 +02:00
f.write('sizeof(')
2020-04-07 18:51:39 +02:00
if it.type_name != '' {
2020-04-10 00:30:43 +02:00
f.write(it.type_name)
2020-04-07 18:51:39 +02:00
} else {
2020-04-10 00:30:43 +02:00
f.write(f.type_to_str(it.typ))
2020-04-07 18:51:39 +02:00
}
2020-04-10 00:30:43 +02:00
f.write(')')
2020-04-07 18:51:39 +02:00
}
2020-02-17 22:50:04 +01:00
ast.StringLiteral {
2020-04-05 19:35:10 +02:00
if it.val.contains("'") {
f.write('"$it.val"')
} else {
f.write("'$it.val'")
}
2020-02-17 22:50:04 +01:00
}
ast.StringInterLiteral {
f.write("'")
for i, val in it.vals {
f.write(val)
2020-04-05 18:03:36 +02:00
if i >= it.exprs.len {
continue
}
f.write('$')
if it.expr_fmts[i].len > 0 {
f.write('{')
f.expr(it.exprs[i])
f.write(it.expr_fmts[i])
f.write('}')
2020-04-05 18:03:36 +02:00
} else {
f.expr(it.exprs[i])
}
}
f.write("'")
}
2020-02-17 22:50:04 +01:00
ast.StructInit {
type_sym := f.table.get_type_symbol(it.typ)
2020-04-10 00:30:43 +02:00
mut name := short_module(type_sym.name).replace(f.cur_mod + '.', '') // TODO f.type_to_str?
if name == 'void' {
name = ''
}
if it.fields.len == 0 && it.exprs.len == 0 {
// `Foo{}` on one line if there are no fields
f.write('$name{}')
2020-04-10 00:30:43 +02:00
} else if it.fields.len == 0 {
// `Foo{1,2,3}` (short syntax )
f.write('$name{')
2020-04-10 00:30:43 +02:00
for i, expr in it.exprs {
f.expr(expr)
if i < it.exprs.len - 1 {
f.write(', ')
}
}
f.write('}')
2020-04-05 18:03:36 +02:00
} else {
f.writeln('$name{')
f.indent++
2020-02-22 16:59:50 +01:00
for i, field in it.fields {
f.write('$field: ')
2020-02-22 16:59:50 +01:00
f.expr(it.exprs[i])
f.writeln('')
}
f.indent--
2020-02-22 16:59:50 +01:00
f.write('}')
2020-02-17 22:50:04 +01:00
}
}
2020-04-05 02:08:10 +02:00
ast.Type {
2020-04-05 18:03:36 +02:00
f.write(f.type_to_str(it.typ))
2020-04-05 02:08:10 +02:00
}
2020-04-03 11:59:53 +02:00
ast.TypeOf {
f.write('typeof(')
f.expr(it.expr)
f.write(')')
}
else {
eprintln('fmt expr: unhandled node ' + typeof(node))
2020-04-05 02:08:10 +02:00
if typeof(node) != 'unknown v.ast.Expr' {
exit(1)
}
}
2020-02-17 22:50:04 +01:00
}
}
2019-12-22 02:34:37 +01:00
2020-02-21 16:14:15 +01:00
fn (f mut Fmt) wrap_long_line() {
if f.line_len > max_len {
2020-04-08 01:04:04 +02:00
if f.out.buf[f.out.buf.len - 1] == ` ` {
f.out.go_back(1)
}
2020-02-21 16:14:15 +01:00
f.write('\n' + tabs[f.indent + 1])
f.line_len = 0
}
}
2020-03-02 17:09:45 +01:00
fn (f mut Fmt) call_args(args []ast.CallArg) {
2020-03-02 17:09:45 +01:00
for i, arg in args {
if arg.is_mut {
2020-03-02 17:09:45 +01:00
f.write('mut ')
}
if i > 0 {
f.wrap_long_line()
}
f.expr(arg.expr)
2020-03-02 17:09:45 +01:00
if i < args.len - 1 {
f.write(', ')
}
}
}
fn (f mut Fmt) or_expr(or_block ast.OrExpr) {
if or_block.stmts.len > 0 {
f.writeln(' or {')
f.stmts(or_block.stmts)
f.write('}')
}
}
2020-04-05 02:08:10 +02:00
fn (f mut Fmt) comment(node ast.Comment) {
if !node.text.contains('\n') {
2020-04-05 16:38:29 +02:00
is_separate_line := node.text.starts_with('|')
mut s := if is_separate_line { node.text[1..] } else { node.text }
if s == '' {
s = '//'
2020-04-05 16:38:29 +02:00
} else {
s = '// ' + s
}
if !is_separate_line {
f.out.go_back(1) // delete the generated \n
2020-04-05 16:38:29 +02:00
}
f.writeln(s)
2020-04-05 02:08:10 +02:00
return
}
lines := node.text.split_into_lines()
f.writeln('/*')
for line in lines {
f.writeln(line)
2020-04-05 19:35:10 +02:00
f.empty_line = false
2020-04-05 02:08:10 +02:00
}
f.writeln('*/')
}
// foo.bar.fn() => bar.fn()
fn short_module(name string) string {
if !name.contains('.') {
return name
}
vals := name.split('.')
if vals.len < 2 {
return name
}
2020-04-05 18:03:36 +02:00
return vals[vals.len - 2] + '.' + vals[vals.len - 1]
}
fn (f mut Fmt) if_expr(it ast.IfExpr) {
single_line := it.branches.len == 2 && it.has_else && it.branches[0].stmts.len == 1 &&
it.branches[1].stmts.len == 1 && (it.is_expr || f.is_assign)
2020-04-05 18:03:36 +02:00
f.single_line_if = single_line
for i, branch in it.branches {
if branch.comment.text != '' {
f.comment(branch.comment)
}
2020-04-05 18:03:36 +02:00
if i == 0 {
f.write('if ')
f.expr(branch.cond)
f.write(' {')
} else if i < it.branches.len - 1 || !it.has_else {
f.write('} else if ')
f.expr(branch.cond)
f.write(' {')
} else if i == it.branches.len - 1 && it.has_else {
f.write('} else {')
}
2020-04-05 19:35:10 +02:00
if single_line {
f.write(' ')
} else {
f.writeln('')
}
2020-04-05 18:03:36 +02:00
f.stmts(branch.stmts)
if single_line {
f.write(' ')
}
}
f.write('}')
f.single_line_if = false
}
fn (f mut Fmt) call_expr(node ast.CallExpr) {
if node.is_method {
match node.left {
ast.Ident {
// `time.now()` without `time imported` is processed as a method call with `time` being
// a `node.left` expression. Import `time` automatically.
// TODO fetch all available modules
2020-04-13 15:05:51 +02:00
if it.name in ['time', 'os', 'strings', 'math', 'json', 'base64'] {
if !(it.name in f.auto_imports) {
f.auto_imports << it.name
f.file.imports << ast.Import{
mod: it.name
alias: it.name
}
}
2020-04-12 17:57:05 +02:00
// for imp in f.file.imports {
// println(imp.mod)
// }
}
}
else {}
}
f.expr(node.left)
f.write('.' + node.name + '(')
f.call_args(node.args)
f.write(')')
f.or_expr(node.or_block)
} else {
name := short_module(node.name)
2020-04-13 15:05:51 +02:00
f.mark_module_as_used(name)
f.write('${name}(')
f.call_args(node.args)
f.write(')')
f.or_expr(node.or_block)
}
}
2020-04-13 15:05:51 +02:00
fn (f mut Fmt) mark_types_module_as_used(typ table.Type) {
sym := f.table.get_type_symbol(typ)
f.mark_module_as_used(sym.name)
}
// `name` is a function (`foo.bar()`) or type (`foo.Bar{}`)
fn (f mut Fmt) mark_module_as_used(name string) {
if !name.contains('.') {
return
}
pos := name.last_index('.') or {
0
}
mod := name[..pos]
if mod in f.used_imports {
return
}
f.used_imports << mod
// println('marking module $mod as used')
}