From f7153ca4faa1c918feffe04b9640b232eecbb1c1 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 26 Apr 2020 07:16:58 +0200 Subject: [PATCH] fmt: array_init(); parser: fn_redefinition_error() --- vlib/v/fmt/fmt.v | 78 ++++++++++++++++++++++++---------------------- vlib/v/parser/fn.v | 15 +++++++-- 2 files changed, 54 insertions(+), 39 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index e7411bd623..926f91b8f7 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -492,43 +492,7 @@ fn (mut f Fmt) expr(node ast.Expr) { f.fn_decl(it.decl) } ast.ArrayInit { - if it.exprs.len == 0 && it.typ != 0 && it.typ != table.void_type { - // `x := []string` - f.write(f.type_to_str(it.typ)) - } else { - // `[1,2,3]` - // type_sym := f.table.get_type_symbol(it.typ) - f.write('[') - mut inc_indent := false - mut last_line_nr := node.position().line_nr // to have the same newlines between array elements - for i, expr in it.exprs { - line_nr := expr.position().line_nr - if last_line_nr < line_nr { - if !inc_indent { - f.indent++ - inc_indent = true - } - f.writeln('') - } - is_new_line := last_line_nr < line_nr || f.wrap_long_line() - if !is_new_line && i > 0 { - f.write(' ') - } - f.expr(expr) - if i == it.exprs.len - 1 { - if is_new_line { - f.writeln('') - } - } else { - f.write(',') - } - last_line_nr = line_nr - } - if inc_indent { - f.indent-- - } - f.write(']') - } + f.array_init(it) } ast.AsCast { type_str := f.type_to_str(it.typ) @@ -1020,3 +984,43 @@ fn expr_is_single_line(expr ast.Expr) bool { } return true } + +fn (mut f Fmt) array_init(it ast.ArrayInit) { + if it.exprs.len == 0 && it.typ != 0 && it.typ != table.void_type { + // `x := []string` + f.write(f.type_to_str(it.typ)) + return + } + // `[1,2,3]` + // type_sym := f.table.get_type_symbol(it.typ) + f.write('[') + mut inc_indent := false + mut last_line_nr := it.pos.line_nr // to have the same newlines between array elements + for i, expr in it.exprs { + line_nr := expr.position().line_nr + if last_line_nr < line_nr { + if !inc_indent { + f.indent++ + inc_indent = true + } + f.writeln('') + } + is_new_line := last_line_nr < line_nr || f.wrap_long_line() + if !is_new_line && i > 0 { + f.write(' ') + } + f.expr(expr) + if i == it.exprs.len - 1 { + if is_new_line { + f.writeln('') + } + } else { + f.write(',') + } + last_line_nr = line_nr + } + if inc_indent { + f.indent-- + } + f.write(']') +} diff --git a/vlib/v/parser/fn.v b/vlib/v/parser/fn.v index 6839bf82ed..6fec9af7cc 100644 --- a/vlib/v/parser/fn.v +++ b/vlib/v/parser/fn.v @@ -193,7 +193,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl { name = p.prepend_mod(name) } if _ := p.table.find_fn(name) { - p.error('redefinition of function `$name`') + p.fn_redefinition_error(name) } p.table.register_fn(table.Fn{ name: name @@ -364,6 +364,17 @@ fn (mut p Parser) fn_args() ([]table.Arg, bool) { return args, is_variadic } -fn (p Parser) fileis(s string) bool { +fn (p &Parser) fileis(s string) bool { return p.file_name.contains(s) } + +fn (mut p Parser) fn_redefinition_error(name string) { + // Find where this function was already declared + // TODO + /* + for file in p.ast_files { + + } + */ + p.error('redefinition of function `$name`') +}