lots of vfmt fixes

pull/2714/head
Alexander Medvednikov 2019-11-09 19:13:26 +03:00
parent aef02f61e1
commit b1d2c6c730
5 changed files with 90 additions and 35 deletions

View File

@ -179,7 +179,6 @@ fn (p mut Parser) clear_vars() {
// Function signatures are added to the top of the .c file in the first run. // Function signatures are added to the top of the .c file in the first run.
fn (p mut Parser) fn_decl() { fn (p mut Parser) fn_decl() {
p.clear_vars() // clear local vars every time a new fn is started p.clear_vars() // clear local vars every time a new fn is started
p.fgen('fn ')
//defer { p.fgenln('\n') } //defer { p.fgenln('\n') }
// If we are in the first pass, create a new function. // If we are in the first pass, create a new function.
@ -211,6 +210,7 @@ fn (p mut Parser) fn_decl() {
p.returns = false p.returns = false
//p.gen('/* returns $p.returns */') //p.gen('/* returns $p.returns */')
p.next() p.next()
p.fspace()
// Method receiver // Method receiver
mut receiver_typ := '' mut receiver_typ := ''
@ -360,8 +360,9 @@ fn (p mut Parser) fn_decl() {
} }
// `{` required only in normal function declarations // `{` required only in normal function declarations
if !is_c && !p.is_vh && !is_fn_header { if !is_c && !p.is_vh && !is_fn_header {
p.fgen(' ') p.fspace()
p.check(.lcbr) p.check(.lcbr)
//p.fgenln('')
} }
// Register ?option type // Register ?option type
if typ.starts_with('Option_') { if typ.starts_with('Option_') {

View File

@ -77,7 +77,7 @@ pub mut:
struct Preferences { struct Preferences {
pub mut: pub mut:
build_mode BuildMode build_mode BuildMode
nofmt bool // disable vfmt //nofmt bool // disable vfmt
is_test bool // `v test string_test.v` is_test bool // `v test string_test.v`
is_script bool // single file mode (`v program.v`), main function can be skipped is_script bool // single file mode (`v program.v`), main function can be skipped
is_live bool // for hot code reloading is_live bool // for hot code reloading
@ -271,9 +271,9 @@ pub fn (v mut V) compile() {
v.parse(file, .main) v.parse(file, .main)
//if p.pref.autofree { p.scanner.text.free() free(p.scanner) } //if p.pref.autofree { p.scanner.text.free() free(p.scanner) }
// Format all files (don't format automatically generated vlib headers) // Format all files (don't format automatically generated vlib headers)
if !v.pref.nofmt && !file.contains('/vlib/') { //if !v.pref.nofmt && !file.contains('/vlib/') {
// new vfmt is not ready yet // new vfmt is not ready yet
} //}
} }
// Generate .vh if we are building a module // Generate .vh if we are building a module
if v.pref.build_mode == .build_module { if v.pref.build_mode == .build_module {
@ -778,6 +778,7 @@ pub fn new_v(args[]string) &V {
joined_args := args.join(' ') joined_args := args.join(' ')
target_os := get_arg(joined_args, 'os', '') target_os := get_arg(joined_args, 'os', '')
comptime_define := get_arg(joined_args, 'd', '') comptime_define := get_arg(joined_args, 'd', '')
//println('comptimedefine=$comptime_define')
mut out_name := get_arg(joined_args, 'o', 'a.out') mut out_name := get_arg(joined_args, 'o', 'a.out')
mut dir := args.last() mut dir := args.last()
@ -931,7 +932,7 @@ pub fn new_v(args[]string) &V {
is_prof: '-prof' in args is_prof: '-prof' in args
is_live: '-live' in args is_live: '-live' in args
sanitize: '-sanitize' in args sanitize: '-sanitize' in args
nofmt: '-nofmt' in args //nofmt: '-nofmt' in args
show_c_cmd: '-show_c_cmd' in args show_c_cmd: '-show_c_cmd' in args
translated: 'translated' in args translated: 'translated' in args
is_run: 'run' in args is_run: 'run' in args
@ -986,6 +987,7 @@ pub fn env_vflags_and_os_args() []string {
} }
pub fn vfmt(args[]string) { pub fn vfmt(args[]string) {
println('running vfmt...')
file := args.last() file := args.last()
if !os.file_exists(file) { if !os.file_exists(file) {
println('"$file" does not exist') println('"$file" does not exist')
@ -995,7 +997,6 @@ pub fn vfmt(args[]string) {
println('v fmt can only be used on .v files') println('v fmt can only be used on .v files')
exit(1) exit(1)
} }
println('vfmt is temporarily disabled')
} }
pub fn create_symlink() { pub fn create_symlink() {

View File

@ -200,6 +200,11 @@ fn (p mut Parser) set_current_fn(f Fn) {
} }
fn (p mut Parser) next() { fn (p mut Parser) next() {
// Generate a formatted version of this token
// (only when vfmt compile time flag is enabled, otherwise this function
// is not even generatd)
p.fnext()
p.prev_tok2 = p.prev_tok p.prev_tok2 = p.prev_tok
p.prev_tok = p.tok p.prev_tok = p.tok
p.scanner.prev_tok = p.tok p.scanner.prev_tok = p.tok
@ -214,6 +219,8 @@ fn (p mut Parser) next() {
p.lit = res.lit p.lit = res.lit
p.scanner.line_nr = res.line_nr p.scanner.line_nr = res.line_nr
p.cgen.line = res.line_nr p.cgen.line = res.line_nr
} }
fn (p & Parser) peek() TokenKind { fn (p & Parser) peek() TokenKind {
@ -260,7 +267,7 @@ fn (p mut Parser) parse(pass Pass) {
// User may still specify `module main` // User may still specify `module main`
if p.tok == .key_module { if p.tok == .key_module {
p.next() p.next()
p.fgen('module ') p.fspace()
p.mod = p.check_name() p.mod = p.check_name()
} else { } else {
p.mod = 'main' p.mod = 'main'
@ -413,13 +420,18 @@ fn (p mut Parser) parse(pass Pass) {
if !p.first_pass() && !p.pref.is_repl { if !p.first_pass() && !p.pref.is_repl {
p.check_unused_imports() p.check_unused_imports()
} }
if false && !p.first_pass() && p.fileis('main.v') { if !p.first_pass() && p.fileis('main.v') {
out := os.create('/var/tmp/fmt.v') or { s := p.scanner.fmt_out.str().trim_space()
verror('failed to create fmt.v') if s.len > 0 {
return println('GENERATING MAIN.V')
out := os.create('/var/tmp/fmt.v') or {
verror('failed to create fmt.v')
return
}
//println(p.scanner.fmt_out.str())
out.writeln(p.scanner.fmt_out.str().trim_space())
out.close()
} }
out.writeln(p.scanner.fmt_out.str())
out.close()
} }
return return
} }
@ -462,6 +474,7 @@ fn (p mut Parser) parse(pass Pass) {
} }
fn (p mut Parser) imports() { fn (p mut Parser) imports() {
p.fgenln2('\n')
p.check(.key_import) p.check(.key_import)
// `import ()` // `import ()`
if p.tok == .lpar { if p.tok == .lpar {
@ -474,14 +487,16 @@ fn (p mut Parser) imports() {
} }
// `import foo` // `import foo`
p.import_statement() p.import_statement()
p.fgenln2('')
} }
fn (p mut Parser) import_statement() { fn (p mut Parser) import_statement() {
p.fspace()
if p.tok != .name { if p.tok != .name {
p.error('bad import format') p.error('bad import format')
} }
if p.peek() == .number { if p.peek() == .number {
p.error('bad import format. module/submodule names cannot begin with a number') p.error('bad import format: module/submodule names cannot begin with a number')
} }
import_tok_idx := p.token_idx-1 import_tok_idx := p.token_idx-1
mut mod := p.check_name().trim_space() mut mod := p.check_name().trim_space()
@ -505,6 +520,7 @@ fn (p mut Parser) import_statement() {
} }
// add import to file scope import table // add import to file scope import table
p.register_import_alias(mod_alias, mod, import_tok_idx) p.register_import_alias(mod_alias, mod, import_tok_idx)
p.fgenln2('')
// Make sure there are no duplicate imports // Make sure there are no duplicate imports
if mod in p.table.imports { if mod in p.table.imports {
return return
@ -512,8 +528,6 @@ fn (p mut Parser) import_statement() {
//p.log('adding import $mod') //p.log('adding import $mod')
p.table.imports << mod p.table.imports << mod
p.table.register_module(mod) p.table.register_module(mod)
p.fgenln(' ' + mod)
} }
fn (p mut Parser) const_decl() { fn (p mut Parser) const_decl() {
@ -742,8 +756,7 @@ fn (p mut Parser) check(expected TokenKind) {
} }
p.fgen(p.strtok()) p.fgen(p.strtok())
// vfmt: increase indentation on `{` unless it's `{}` // vfmt: increase indentation on `{` unless it's `{}`
// TODO if expected == .lcbr { //&& p.scanner.pos + 1 < p.scanner.text.len && p.scanner.text[p.scanner.pos + 1] != `}` {
if expected == .lcbr && p.scanner.pos + 1 < p.scanner.text.len && p.scanner.text[p.scanner.pos + 1] != `}` {
p.fgenln('') p.fgenln('')
p.fmt_inc() p.fmt_inc()
} }
@ -1000,7 +1013,7 @@ fn (p mut Parser) statements_no_rcbr() string {
// println('last st typ=$last_st_typ') // println('last st typ=$last_st_typ')
if !p.inside_if_expr { if !p.inside_if_expr {
p.genln('')// // end st tok= ${p.strtok()}') p.genln('')// // end st tok= ${p.strtok()}')
p.fgenln('') p.fgenln2('')
} }
i++ i++
if i > 50000 { if i > 50000 {
@ -2480,9 +2493,9 @@ fn (p mut Parser) if_st(is_expr bool, elif_depth int) string {
} }
else { else {
p.gen('if (') p.gen('if (')
p.fgen('if ')
} }
p.next() p.next()
p.fspace()
// `if a := opt() { }` syntax // `if a := opt() { }` syntax
if p.tok == .name && p.peek() == .decl_assign { if p.tok == .name && p.peek() == .decl_assign {
p.check_not_reserved() p.check_not_reserved()
@ -2522,7 +2535,7 @@ fn (p mut Parser) if_st(is_expr bool, elif_depth int) string {
else { else {
p.genln(') {') p.genln(') {')
} }
p.fgen(' ') p.fspace()
p.check(.lcbr) p.check(.lcbr)
mut typ := '' mut typ := ''
// if { if hack // if { if hack
@ -2537,7 +2550,7 @@ fn (p mut Parser) if_st(is_expr bool, elif_depth int) string {
p.returns = false p.returns = false
// println('IF TYp=$typ') // println('IF TYp=$typ')
if p.tok == .key_else { if p.tok == .key_else {
p.fgenln('') p.fgenln2('')
p.check(.key_else) p.check(.key_else)
p.fspace() p.fspace()
if p.tok == .key_if { if p.tok == .key_if {
@ -2642,7 +2655,7 @@ if (!$tmp) {
fn (p mut Parser) return_st() { fn (p mut Parser) return_st() {
p.check(.key_return) p.check(.key_return)
p.fgen(' ') p.fspace()
deferred_text := p.get_deferred_text() deferred_text := p.get_deferred_text()
fn_returns := p.cur_fn.typ != 'void' fn_returns := p.cur_fn.typ != 'void'
if fn_returns { if fn_returns {

View File

@ -22,9 +22,9 @@ fn (p mut Parser) struct_decl() {
if is_objc { if is_objc {
cat = .objc_interface cat = .objc_interface
} }
p.fgen(p.tok.str() + ' ')
// Get type name
p.next() p.next()
p.fspace()
// Get type name
mut name := p.check_name() mut name := p.check_name()
if name.contains('_') && !p.pref.translated { if name.contains('_') && !p.pref.translated {
p.error('type names cannot contain `_`') p.error('type names cannot contain `_`')
@ -105,7 +105,7 @@ fn (p mut Parser) struct_decl() {
p.table.register_type2(typ) p.table.register_type2(typ)
return return
} }
p.fgen(' ') p.fspace()
p.check(.lcbr) p.check(.lcbr)
// Struct fields // Struct fields
mut is_pub_field := false mut is_pub_field := false
@ -139,7 +139,7 @@ fn (p mut Parser) struct_decl() {
p.check(.colon) p.check(.colon)
} }
p.fmt_inc() p.fmt_inc()
p.fgenln('') p.fgenln2('')
} }
if p.tok == .key_mut { if p.tok == .key_mut {
if is_mut { if is_mut {
@ -152,7 +152,7 @@ fn (p mut Parser) struct_decl() {
p.check(.colon) p.check(.colon)
} }
p.fmt_inc() p.fmt_inc()
p.fgenln('') p.fgenln2('')
} }
// if is_pub { // if is_pub {
// } // }
@ -182,7 +182,7 @@ fn (p mut Parser) struct_decl() {
} }
// `pub` access mod // `pub` access mod
access_mod := if is_pub_field { AccessMod.public } else { AccessMod.private} access_mod := if is_pub_field { AccessMod.public } else { AccessMod.private}
p.fgen(' ') p.fspace()
field_type := p.get_type() field_type := p.get_type()
if field_type == name { if field_type == name {
p.error_with_token_index( 'cannot embed struct `$name` in itself (field `$field_name`)', field_name_token_idx) p.error_with_token_index( 'cannot embed struct `$name` in itself (field `$field_name`)', field_name_token_idx)
@ -195,6 +195,7 @@ fn (p mut Parser) struct_decl() {
// [ATTR] // [ATTR]
mut attr := '' mut attr := ''
if p.tok == .lsbr { if p.tok == .lsbr {
p.fspace()
p.next() p.next()
attr = p.check_name() attr = p.check_name()
if p.tok == .colon { if p.tok == .colon {
@ -219,13 +220,13 @@ fn (p mut Parser) struct_decl() {
if p.first_pass() { if p.first_pass() {
p.table.add_field(typ.name, field_name, field_type, is_mut, attr, access_mod) p.table.add_field(typ.name, field_name, field_type, is_mut, attr, access_mod)
} }
p.fgenln('') p.fgenln2('')
} }
p.check(.rcbr) p.check(.rcbr)
if !is_c && !did_gen_something && p.first_pass() { if !is_c && !did_gen_something && p.first_pass() {
p.table.add_field(typ.name, '', 'EMPTY_STRUCT_DECLARATION', false, '', .private) p.table.add_field(typ.name, '', 'EMPTY_STRUCT_DECLARATION', false, '', .private)
} }
p.fgenln('\n') p.fgenln2('\n')
} }
// `User{ foo: bar }` // `User{ foo: bar }`
@ -269,7 +270,7 @@ fn (p mut Parser) struct_init(typ string) string {
if p.tok != .rcbr { if p.tok != .rcbr {
p.gen(',') p.gen(',')
} }
p.fgenln('') p.fspace()
did_gen_something = true did_gen_something = true
} }
// If we already set some fields, need to prepend a comma // If we already set some fields, need to prepend a comma

View File

@ -6,7 +6,6 @@ module compiler
import strings import strings
// fmt helpers
[if vfmt] [if vfmt]
fn (scanner mut Scanner) fgen(s_ string) { fn (scanner mut Scanner) fgen(s_ string) {
mut s := s_ mut s := s_
@ -27,18 +26,36 @@ fn (scanner mut Scanner) fgenln(s_ string) {
scanner.fmt_line_empty = true scanner.fmt_line_empty = true
} }
[if vfmt] [if vfmt]
fn (p mut Parser) fgen(s string) { fn (p mut Parser) fgen(s string) {
}
[if vfmt]
fn (p mut Parser) fgen2(s string) {
if p.pass != .main {
return
}
p.scanner.fgen(s) p.scanner.fgen(s)
} }
[if vfmt] [if vfmt]
fn (p mut Parser) fspace() { fn (p mut Parser) fspace() {
p.fgen(' ') if p.first_pass() {
return
}
p.fgen2(' ')
} }
[if vfmt] [if vfmt]
fn (p mut Parser) fgenln(s string) { fn (p mut Parser) fgenln(s string) {
}
[if vfmt]
fn (p mut Parser) fgenln2(s string) {
if p.pass != .main {
return
}
p.scanner.fgenln(s) p.scanner.fgenln(s)
} }
@ -57,11 +74,33 @@ fn (p mut Parser) peek() TokenKind {
[if vfmt] [if vfmt]
fn (p mut Parser) fmt_inc() { fn (p mut Parser) fmt_inc() {
if p.pass != .main {
return
}
p.scanner.fmt_indent++ p.scanner.fmt_indent++
} }
[if vfmt] [if vfmt]
fn (p mut Parser) fmt_dec() { fn (p mut Parser) fmt_dec() {
if p.pass != .main {
return
}
p.scanner.fmt_indent-- p.scanner.fmt_indent--
} }
[if vfmt]
fn (p mut Parser) fnext() {
if p.tok == .eof {
return
}
if p.tok == .rcbr {
p.fmt_dec()
}
p.fgen2(p.strtok())
// vfmt: increase indentation on `{` unless it's `{}`
if p.tok == .lcbr { //&& p.scanner.pos + 1 < p.scanner.text.len && p.scanner.text[p.scanner.pos + 1] != `}` {
p.fgenln2('')
p.fmt_inc()
}
}