fmt: array_init(); parser: fn_redefinition_error()

pull/4602/head
Alexander Medvednikov 2020-04-26 07:16:58 +02:00
parent 620fc962d7
commit f7153ca4fa
2 changed files with 54 additions and 39 deletions

View File

@ -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(']')
}

View File

@ -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`')
}