lots of vfmt fixes

pull/959/head
Alexander Medvednikov 2019-07-03 13:20:43 +02:00
parent 05ef1e0681
commit d9753ee2c6
4 changed files with 53 additions and 58 deletions

View File

@ -128,7 +128,7 @@ fn (p mut Parser) fn_decl() {
is_mut := p.tok == MUT is_mut := p.tok == MUT
is_amp := p.tok == AMP is_amp := p.tok == AMP
if is_mut || is_amp { if is_mut || is_amp {
p.next() p.check_space(p.tok)
} }
receiver_typ = p.get_type() receiver_typ = p.get_type()
T := p.table.find_type(receiver_typ) T := p.table.find_type(receiver_typ)
@ -151,6 +151,7 @@ fn (p mut Parser) fn_decl() {
receiver_typ += '*' receiver_typ += '*'
} }
p.check(RPAR) p.check(RPAR)
p.fspace()
receiver := Var { receiver := Var {
name: receiver_name name: receiver_name
is_arg: true is_arg: true

View File

@ -95,6 +95,7 @@ mut:
fn main() { fn main() {
// There's no `flags` module yet, so args have to be parsed manually
args := os.args args := os.args
// Print the version and exit. // Print the version and exit.
if '-v' in args || '--version' in args || 'version' in args { if '-v' in args || '--version' in args || 'version' in args {

View File

@ -142,6 +142,7 @@ fn (p mut Parser) parse() {
} }
else { else {
p.check(PACKAGE) p.check(PACKAGE)
p.fspace()
p.pkg = p.check_name() p.pkg = p.check_name()
} }
p.fgenln('\n') p.fgenln('\n')
@ -237,7 +238,7 @@ fn (p mut Parser) parse() {
p.cur_fn = MainFn p.cur_fn = MainFn
p.check_unused_variables() p.check_unused_variables()
} }
if true && !p.first_run() && p.fileis('test') { if false && !p.first_run() && p.fileis('main.v') {
out := os.create('/var/tmp/fmt.v') out := os.create('/var/tmp/fmt.v')
out.writeln(p.scanner.fmt_out.str()) out.writeln(p.scanner.fmt_out.str())
out.close() out.close()
@ -326,7 +327,7 @@ fn (p mut Parser) const_decl() {
p.fspace() p.fspace()
p.check(LPAR) p.check(LPAR)
p.fgenln('') p.fgenln('')
p.scanner.fmt_indent++ p.fmt_inc()
for p.tok == NAME { for p.tok == NAME {
// `Age = 20` // `Age = 20`
mut name := p.check_name() mut name := p.check_name()
@ -368,7 +369,7 @@ fn (p mut Parser) const_decl() {
} }
p.fgenln('') p.fgenln('')
} }
p.scanner.fmt_indent-- p.fmt_dec()
p.check(RPAR) p.check(RPAR)
p.fgenln('\n') p.fgenln('\n')
p.inside_const = false p.inside_const = false
@ -485,12 +486,12 @@ fn (p mut Parser) struct_decl() {
p.error('structs can only have one `pub:`, all public fields have to be grouped') p.error('structs can only have one `pub:`, all public fields have to be grouped')
} }
is_pub = true is_pub = true
p.scanner.fmt_indent-- p.fmt_dec()
p.check(PUB) p.check(PUB)
if p.tok != MUT { if p.tok != MUT {
p.check(COLON) p.check(COLON)
} }
p.scanner.fmt_indent++ p.fmt_inc()
p.fgenln('') p.fgenln('')
} }
if p.tok == MUT { if p.tok == MUT {
@ -498,12 +499,12 @@ fn (p mut Parser) struct_decl() {
p.error('structs can only have one `mut:`, all private mutable fields have to be grouped') p.error('structs can only have one `mut:`, all private mutable fields have to be grouped')
} }
is_mut = true is_mut = true
p.scanner.fmt_indent-- p.fmt_dec()
p.check(MUT) p.check(MUT)
if p.tok != MUT { if p.tok != MUT {
p.check(COLON) p.check(COLON)
} }
p.scanner.fmt_indent++ p.fmt_inc()
p.fgenln('') p.fgenln('')
} }
// if is_pub { // if is_pub {
@ -654,18 +655,19 @@ fn (p mut Parser) check(expected Token) {
p.error(s) p.error(s)
} }
if expected == RCBR { if expected == RCBR {
p.scanner.fmt_indent-- p.fmt_dec()
} }
p.fgen(p.strtok()) p.fgen(p.strtok())
// vfmt: increase indentation on `{` unless it's `{}` // vfmt: increase indentation on `{` unless it's `{}`
if expected == LCBR && p.scanner.text[p.scanner.pos + 1] != `}` { if expected == LCBR && p.scanner.text[p.scanner.pos + 1] != `}` {
p.fgenln('') p.fgenln('')
p.scanner.fmt_indent++ p.fmt_inc()
} }
p.next() p.next()
} }
fn (p mut Parser) error(s string) { fn (p mut Parser) error(s string) {
//q := "SDF"
// Dump all vars and types for debugging // Dump all vars and types for debugging
if false { if false {
file_types := os.create('$TmpPath/types') file_types := os.create('$TmpPath/types')
@ -786,12 +788,12 @@ fn (p mut Parser) get_type() string {
for p.tok == MUL { for p.tok == MUL {
mul = true mul = true
nr_muls++ nr_muls++
p.next() p.check(MUL)
} }
if p.tok == AMP { if p.tok == AMP {
mul = true mul = true
nr_muls++ nr_muls++
p.next() p.check(AMP)
} }
typ += p.lit typ += p.lit
if !p.is_struct_init { if !p.is_struct_init {
@ -925,7 +927,7 @@ fn (p mut Parser) statements_no_curly_end() string {
else { else {
// p.check(RCBR) // p.check(RCBR)
} }
p.scanner.fmt_indent-- //p.fmt_dec()
// println('close scope line=$p.scanner.line_nr') // println('close scope line=$p.scanner.line_nr')
p.cur_fn.close_scope() p.cur_fn.close_scope()
return last_st_typ return last_st_typ
@ -1014,13 +1016,13 @@ fn (p mut Parser) statement(add_semi bool) string {
p.error('`continue` statement outside `for`') p.error('`continue` statement outside `for`')
} }
p.genln('continue') p.genln('continue')
p.next() p.check(CONTINUE)
case BREAK: case BREAK:
if p.for_expr_cnt == 0 { if p.for_expr_cnt == 0 {
p.error('`break` statement outside `for`') p.error('`break` statement outside `for`')
} }
p.genln('break') p.genln('break')
p.next() p.check(BREAK)
case GO: case GO:
p.go_statement() p.go_statement()
case ASSERT: case ASSERT:
@ -1064,17 +1066,14 @@ fn (p mut Parser) assign_statement(v Var, ph int, is_map bool) {
if is_str { if is_str {
p.gen('= string_add($v.name, ')// TODO can't do `foo.bar += '!'` p.gen('= string_add($v.name, ')// TODO can't do `foo.bar += '!'`
} }
else if !is_map { else {
p.gen(' += ') p.gen(' += ')
} }
default: default: p.gen(' ' + p.tok.str() + ' ')
if tok != MINUS_ASSIGN && tok != MULT_ASSIGN && tok != XOR_ASSIGN
&& tok != MOD_ASSIGN && tok != AND_ASSIGN && tok != OR_ASSIGN
&& tok != RIGHT_SHIFT_ASSIGN && tok != LEFT_SHIFT_ASSIGN && tok != DIV_ASSIGN {
p.gen(' ' + p.tok.str() + ' ')
}
} }
p.fgen(' ' + p.tok.str() + ' ') p.fspace()
p.fgen(tok.str())
p.fspace()
p.next() p.next()
pos := p.cgen.cur_line.len pos := p.cgen.cur_line.len
expr_type := p.bool_expression() expr_type := p.bool_expression()
@ -1165,7 +1164,7 @@ fn (p mut Parser) bool_expression() string {
typ := p.bterm() typ := p.bterm()
for p.tok == AND || p.tok == OR { for p.tok == AND || p.tok == OR {
p.gen(' ${p.tok.str()} ') p.gen(' ${p.tok.str()} ')
p.next() p.check_space(p.tok)
p.check_types(p.bterm(), typ) p.check_types(p.bterm(), typ)
} }
if typ == '' { if typ == '' {
@ -1646,7 +1645,7 @@ fn (p mut Parser) index_expr(typ string, fn_ph int) string {
} }
} }
if is_arr { if is_arr {
p.fgen('[') //p.fgen('[')
// array_int a; a[0] // array_int a; a[0]
// type is "array_int", need "int" // type is "array_int", need "int"
// typ = typ.replace('array_', '') // typ = typ.replace('array_', '')
@ -1802,7 +1801,7 @@ fn (p mut Parser) expression() string {
// _PUSH(&a, expression(), tmp, string) // _PUSH(&a, expression(), tmp, string)
tmp := p.get_tmp() tmp := p.get_tmp()
tmp_typ := typ.right(6)// skip "array_" tmp_typ := typ.right(6)// skip "array_"
p.next() p.check_space(LEFT_SHIFT)
// Get the value we are pushing // Get the value we are pushing
p.gen(', (') p.gen(', (')
// Immutable? Can we push? // Immutable? Can we push?
@ -1860,7 +1859,7 @@ fn (p mut Parser) expression() string {
// for p.tok in [PLUS, MINUS, PIPE, AMP, XOR] { // for p.tok in [PLUS, MINUS, PIPE, AMP, XOR] {
tok_op := p.tok tok_op := p.tok
is_num := typ == 'void*' || typ == 'byte*' || is_number_type(typ) is_num := typ == 'void*' || typ == 'byte*' || is_number_type(typ)
p.next() p.check_space(p.tok)
if is_str && tok_op == PLUS { if is_str && tok_op == PLUS {
p.cgen.set_placeholder(ph, 'string_add(') p.cgen.set_placeholder(ph, 'string_add(')
p.gen(',') p.gen(',')
@ -1946,12 +1945,12 @@ fn (p mut Parser) unary() string {
switch tok { switch tok {
case NOT: case NOT:
p.gen('!') p.gen('!')
p.next() p.check(NOT)
typ = 'bool' typ = 'bool'
p.bool_expression() p.bool_expression()
case BIT_NOT: case BIT_NOT:
p.gen('~') p.gen('~')
p.next() p.check(BIT_NOT)
typ = p.bool_expression() typ = p.bool_expression()
default: default:
typ = p.factor() typ = p.factor()
@ -2021,7 +2020,7 @@ fn (p mut Parser) factor() string {
return 'T' return 'T'
case LPAR: case LPAR:
p.gen('(/*lpar*/') p.gen('(/*lpar*/')
p.next()// ( p.check(LPAR)
typ = p.bool_expression() typ = p.bool_expression()
// Hack. If this `)` referes to a ptr cast `(*int__)__`, it was already checked // Hack. If this `)` referes to a ptr cast `(*int__)__`, it was already checked
// TODO: fix parser so that it doesn't think it's a par expression when it sees `(` in // TODO: fix parser so that it doesn't think it's a par expression when it sees `(` in
@ -2295,8 +2294,9 @@ fn (p mut Parser) array_init() string {
} }
} }
if p.tok != RSBR && p.tok != SEMICOLON { if p.tok != RSBR && p.tok != SEMICOLON {
p.gen(',') p.gen(', ')
p.check(COMMA) p.check(COMMA)
p.fspace()
} }
i++ i++
// Repeat (a = [0;5] ) // Repeat (a = [0;5] )
@ -2564,14 +2564,16 @@ fn os_name_to_ifdef(name string) string {
} }
fn (p mut Parser) comp_time() { fn (p mut Parser) comp_time() {
p.next() p.check(DOLLAR)
if p.tok == IF { if p.tok == IF {
p.next() p.check(IF)
p.fspace()
not := p.tok == NOT not := p.tok == NOT
if not { if not {
p.next() p.check(NOT)
} }
name := p.check_name() name := p.check_name()
p.fspace()
if name in SupportedPlatforms { if name in SupportedPlatforms {
ifdef_name := os_name_to_ifdef(name) ifdef_name := os_name_to_ifdef(name)
if not { if not {
@ -2721,7 +2723,6 @@ fn (p mut Parser) if_st(is_expr bool) string {
} }
else { else {
p.genln(') {') p.genln(') {')
p.fgenln('{')
p.genln('/*if*/') p.genln('/*if*/')
} }
p.fgen(' ') p.fgen(' ')
@ -2739,7 +2740,9 @@ fn (p mut Parser) if_st(is_expr bool) string {
} }
// println('IF TYp=$typ') // println('IF TYp=$typ')
if p.tok == ELSE { if p.tok == ELSE {
p.next() p.fgenln('')
p.check(ELSE)
p.fspace()
if p.tok == IF { if p.tok == IF {
p.gen(' else ') p.gen(' else ')
return p.if_st(is_expr) return p.if_st(is_expr)
@ -2820,7 +2823,6 @@ fn (p mut Parser) for_st() {
if debug { if debug {
println('for 4') println('for 4')
} }
p.fgen(' ')
p.genln(') { ') p.genln(') { ')
} }
// for i, val in array // for i, val in array
@ -2931,6 +2933,7 @@ fn (p mut Parser) for_st() {
p.check_types(p.bool_expression(), 'bool') p.check_types(p.bool_expression(), 'bool')
p.genln(') {') p.genln(') {')
} }
p.fspace()
p.check(LCBR) p.check(LCBR)
p.statements() p.statements()
p.cur_fn.close_scope() p.cur_fn.close_scope()
@ -2938,7 +2941,7 @@ fn (p mut Parser) for_st() {
} }
fn (p mut Parser) switch_statement() { fn (p mut Parser) switch_statement() {
p.next() p.check(SWITCH)
p.cgen.start_tmp() p.cgen.start_tmp()
typ := p.bool_expression() typ := p.bool_expression()
expr := p.cgen.end_tmp() expr := p.cgen.end_tmp()
@ -2947,7 +2950,7 @@ fn (p mut Parser) switch_statement() {
for p.tok == CASE || p.tok == DEFAULT { for p.tok == CASE || p.tok == DEFAULT {
if p.tok == DEFAULT { if p.tok == DEFAULT {
p.genln('else { // default:') p.genln('else { // default:')
p.next() p.check(DEFAULT)
p.check(COLON) p.check(COLON)
p.statements() p.statements()
break break
@ -2969,7 +2972,7 @@ fn (p mut Parser) switch_statement() {
p.gen('($expr == ') p.gen('($expr == ')
} }
if p.tok == CASE || p.tok == DEFAULT { if p.tok == CASE || p.tok == DEFAULT {
p.next() p.check(p.tok)
} }
p.bool_expression() p.bool_expression()
if p.tok != COMMA { if p.tok != COMMA {
@ -3166,33 +3169,29 @@ fn (p &Parser) building_v() bool {
// fmt helpers // fmt helpers
fn (scanner mut Scanner) fgen(s string) { fn (scanner mut Scanner) fgen(s string) {
/*
if scanner.fmt_line_empty { if scanner.fmt_line_empty {
s = repeat_char(`\t`, scanner.fmt_indent) + s s = repeat_char(`\t`, scanner.fmt_indent) + s
} }
scanner.fmt_out.write(s) scanner.fmt_out.write(s)
scanner.fmt_line_empty = false scanner.fmt_line_empty = false
*/
} }
fn (scanner mut Scanner) fgenln(s string) { fn (scanner mut Scanner) fgenln(s string) {
/*
if scanner.fmt_line_empty { if scanner.fmt_line_empty {
s = repeat_char(`\t`, scanner.fmt_indent) + s s = repeat_char(`\t`, scanner.fmt_indent) + s
} }
scanner.fmt_out.writeln(s) scanner.fmt_out.writeln(s)
scanner.fmt_line_empty = true scanner.fmt_line_empty = true
*/
} }
fn (p mut Parser) fgen(s string) { fn (p mut Parser) fgen(s string) {
//p.scanner.fgen(s) p.scanner.fgen(s)
} }
fn (p mut Parser) fspace() { fn (p mut Parser) fspace() {
//p.fgen(' ') p.fgen(' ')
} }
fn (p mut Parser) fgenln(s string) { fn (p mut Parser) fgenln(s string) {
//p.scanner.fgenln(s) p.scanner.fgenln(s)
} }

View File

@ -239,7 +239,6 @@ fn (s mut Scanner) scan() ScanRes {
} }
else if nextc == `=` { else if nextc == `=` {
s.pos++ s.pos++
s.cao_change('+')
return scan_res(PLUS_ASSIGN, '') return scan_res(PLUS_ASSIGN, '')
} }
return scan_res(PLUS, '') return scan_res(PLUS, '')
@ -250,28 +249,24 @@ fn (s mut Scanner) scan() ScanRes {
} }
else if nextc == `=` { else if nextc == `=` {
s.pos++ s.pos++
s.cao_change('-')
return scan_res(MINUS_ASSIGN, '') return scan_res(MINUS_ASSIGN, '')
} }
return scan_res(MINUS, '') return scan_res(MINUS, '')
case `*`: case `*`:
if nextc == `=` { if nextc == `=` {
s.pos++ s.pos++
s.cao_change('*')
return scan_res(MULT_ASSIGN, '') return scan_res(MULT_ASSIGN, '')
} }
return scan_res(MUL, '') return scan_res(MUL, '')
case `^`: case `^`:
if nextc == `=` { if nextc == `=` {
s.pos++ s.pos++
s.cao_change('^')
return scan_res(XOR_ASSIGN, '') return scan_res(XOR_ASSIGN, '')
} }
return scan_res(XOR, '') return scan_res(XOR, '')
case `%`: case `%`:
if nextc == `=` { if nextc == `=` {
s.pos++ s.pos++
s.cao_change('%')
return scan_res(MOD_ASSIGN, '') return scan_res(MOD_ASSIGN, '')
} }
return scan_res(MOD, '') return scan_res(MOD, '')
@ -318,7 +313,6 @@ fn (s mut Scanner) scan() ScanRes {
case `&`: case `&`:
if nextc == `=` { if nextc == `=` {
s.pos++ s.pos++
s.cao_change('&')
return scan_res(AND_ASSIGN, '') return scan_res(AND_ASSIGN, '')
} }
if nextc == `&` { if nextc == `&` {
@ -333,7 +327,6 @@ fn (s mut Scanner) scan() ScanRes {
} }
if nextc == `=` { if nextc == `=` {
s.pos++ s.pos++
s.cao_change('|')
return scan_res(OR_ASSIGN, '') return scan_res(OR_ASSIGN, '')
} }
return scan_res(PIPE, '') return scan_res(PIPE, '')
@ -372,7 +365,6 @@ fn (s mut Scanner) scan() ScanRes {
else if nextc == `>` { else if nextc == `>` {
if s.pos + 2 < s.text.len && s.text[s.pos + 2] == `=` { if s.pos + 2 < s.text.len && s.text[s.pos + 2] == `=` {
s.pos += 2 s.pos += 2
s.cao_change('>>')
return scan_res(RIGHT_SHIFT_ASSIGN, '') return scan_res(RIGHT_SHIFT_ASSIGN, '')
} }
s.pos++ s.pos++
@ -389,7 +381,6 @@ fn (s mut Scanner) scan() ScanRes {
else if nextc == `<` { else if nextc == `<` {
if s.pos + 2 < s.text.len && s.text[s.pos + 2] == `=` { if s.pos + 2 < s.text.len && s.text[s.pos + 2] == `=` {
s.pos += 2 s.pos += 2
s.cao_change('<<')
return scan_res(LEFT_SHIFT_ASSIGN, '') return scan_res(LEFT_SHIFT_ASSIGN, '')
} }
s.pos++ s.pos++
@ -429,7 +420,6 @@ fn (s mut Scanner) scan() ScanRes {
case `/`: case `/`:
if nextc == `=` { if nextc == `=` {
s.pos++ s.pos++
s.cao_change('/')
return scan_res(DIV_ASSIGN, '') return scan_res(DIV_ASSIGN, '')
} }
if nextc == `/` { if nextc == `/` {
@ -493,7 +483,11 @@ fn (s mut Scanner) scan() ScanRes {
} }
} }
println('(char code=$c) pos=$s.pos len=$s.text.len') println('(char code=$c) pos=$s.pos len=$s.text.len')
s.error('invalid character `${c.str()}`') mut msg := 'invalid character `${c.str()}`'
if c == `"` {
msg += ', use \' to denote strings'
}
s.error(msg)
return scan_res(EOF, '') return scan_res(EOF, '')
} }