diff --git a/vlib/compiler/fn.v b/vlib/compiler/fn.v index a79951023c..bbed6297d9 100644 --- a/vlib/compiler/fn.v +++ b/vlib/compiler/fn.v @@ -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. fn (p mut Parser) fn_decl() { p.clear_vars() // clear local vars every time a new fn is started - p.fgen('fn ') //defer { p.fgenln('\n') } // 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.gen('/* returns $p.returns */') p.next() + p.fspace() // Method receiver mut receiver_typ := '' @@ -360,8 +360,9 @@ fn (p mut Parser) fn_decl() { } // `{` required only in normal function declarations if !is_c && !p.is_vh && !is_fn_header { - p.fgen(' ') + p.fspace() p.check(.lcbr) + //p.fgenln('') } // Register ?option type if typ.starts_with('Option_') { diff --git a/vlib/compiler/main.v b/vlib/compiler/main.v index 9896278088..a2922f4530 100644 --- a/vlib/compiler/main.v +++ b/vlib/compiler/main.v @@ -77,7 +77,7 @@ pub mut: struct Preferences { pub mut: build_mode BuildMode - nofmt bool // disable vfmt + //nofmt bool // disable vfmt is_test bool // `v test string_test.v` is_script bool // single file mode (`v program.v`), main function can be skipped is_live bool // for hot code reloading @@ -271,9 +271,9 @@ pub fn (v mut V) compile() { v.parse(file, .main) //if p.pref.autofree { p.scanner.text.free() free(p.scanner) } // 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 - } + //} } // Generate .vh if we are building a module if v.pref.build_mode == .build_module { @@ -778,6 +778,7 @@ pub fn new_v(args[]string) &V { joined_args := args.join(' ') target_os := get_arg(joined_args, 'os', '') comptime_define := get_arg(joined_args, 'd', '') + //println('comptimedefine=$comptime_define') mut out_name := get_arg(joined_args, 'o', 'a.out') mut dir := args.last() @@ -931,7 +932,7 @@ pub fn new_v(args[]string) &V { is_prof: '-prof' in args is_live: '-live' in args sanitize: '-sanitize' in args - nofmt: '-nofmt' in args + //nofmt: '-nofmt' in args show_c_cmd: '-show_c_cmd' in args translated: 'translated' in args is_run: 'run' in args @@ -986,6 +987,7 @@ pub fn env_vflags_and_os_args() []string { } pub fn vfmt(args[]string) { + println('running vfmt...') file := args.last() if !os.file_exists(file) { println('"$file" does not exist') @@ -995,7 +997,6 @@ pub fn vfmt(args[]string) { println('v fmt can only be used on .v files') exit(1) } - println('vfmt is temporarily disabled') } pub fn create_symlink() { diff --git a/vlib/compiler/parser.v b/vlib/compiler/parser.v index 0ab2339a06..144b915207 100644 --- a/vlib/compiler/parser.v +++ b/vlib/compiler/parser.v @@ -200,6 +200,11 @@ fn (p mut Parser) set_current_fn(f Fn) { } 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_tok = p.tok p.scanner.prev_tok = p.tok @@ -214,6 +219,8 @@ fn (p mut Parser) next() { p.lit = res.lit p.scanner.line_nr = res.line_nr p.cgen.line = res.line_nr + + } fn (p & Parser) peek() TokenKind { @@ -260,7 +267,7 @@ fn (p mut Parser) parse(pass Pass) { // User may still specify `module main` if p.tok == .key_module { p.next() - p.fgen('module ') + p.fspace() p.mod = p.check_name() } else { p.mod = 'main' @@ -413,13 +420,18 @@ fn (p mut Parser) parse(pass Pass) { if !p.first_pass() && !p.pref.is_repl { p.check_unused_imports() } - if false && !p.first_pass() && p.fileis('main.v') { - out := os.create('/var/tmp/fmt.v') or { - verror('failed to create fmt.v') - return + if !p.first_pass() && p.fileis('main.v') { + s := p.scanner.fmt_out.str().trim_space() + if s.len > 0 { + 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 } @@ -462,6 +474,7 @@ fn (p mut Parser) parse(pass Pass) { } fn (p mut Parser) imports() { + p.fgenln2('\n') p.check(.key_import) // `import ()` if p.tok == .lpar { @@ -474,14 +487,16 @@ fn (p mut Parser) imports() { } // `import foo` p.import_statement() + p.fgenln2('') } fn (p mut Parser) import_statement() { + p.fspace() if p.tok != .name { p.error('bad import format') } 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 mut mod := p.check_name().trim_space() @@ -505,6 +520,7 @@ fn (p mut Parser) import_statement() { } // add import to file scope import table p.register_import_alias(mod_alias, mod, import_tok_idx) + p.fgenln2('') // Make sure there are no duplicate imports if mod in p.table.imports { return @@ -512,8 +528,6 @@ fn (p mut Parser) import_statement() { //p.log('adding import $mod') p.table.imports << mod p.table.register_module(mod) - - p.fgenln(' ' + mod) } fn (p mut Parser) const_decl() { @@ -742,8 +756,7 @@ fn (p mut Parser) check(expected TokenKind) { } p.fgen(p.strtok()) // 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.fmt_inc() } @@ -1000,7 +1013,7 @@ fn (p mut Parser) statements_no_rcbr() string { // println('last st typ=$last_st_typ') if !p.inside_if_expr { p.genln('')// // end st tok= ${p.strtok()}') - p.fgenln('') + p.fgenln2('') } i++ if i > 50000 { @@ -2480,9 +2493,9 @@ fn (p mut Parser) if_st(is_expr bool, elif_depth int) string { } else { p.gen('if (') - p.fgen('if ') } p.next() + p.fspace() // `if a := opt() { }` syntax if p.tok == .name && p.peek() == .decl_assign { p.check_not_reserved() @@ -2522,7 +2535,7 @@ fn (p mut Parser) if_st(is_expr bool, elif_depth int) string { else { p.genln(') {') } - p.fgen(' ') + p.fspace() p.check(.lcbr) mut typ := '' // if { if hack @@ -2537,7 +2550,7 @@ fn (p mut Parser) if_st(is_expr bool, elif_depth int) string { p.returns = false // println('IF TYp=$typ') if p.tok == .key_else { - p.fgenln('') + p.fgenln2('') p.check(.key_else) p.fspace() if p.tok == .key_if { @@ -2642,7 +2655,7 @@ if (!$tmp) { fn (p mut Parser) return_st() { p.check(.key_return) - p.fgen(' ') + p.fspace() deferred_text := p.get_deferred_text() fn_returns := p.cur_fn.typ != 'void' if fn_returns { diff --git a/vlib/compiler/struct.v b/vlib/compiler/struct.v index c6b584386a..fc914ea891 100644 --- a/vlib/compiler/struct.v +++ b/vlib/compiler/struct.v @@ -22,9 +22,9 @@ fn (p mut Parser) struct_decl() { if is_objc { cat = .objc_interface } - p.fgen(p.tok.str() + ' ') - // Get type name p.next() + p.fspace() + // Get type name mut name := p.check_name() if name.contains('_') && !p.pref.translated { p.error('type names cannot contain `_`') @@ -105,7 +105,7 @@ fn (p mut Parser) struct_decl() { p.table.register_type2(typ) return } - p.fgen(' ') + p.fspace() p.check(.lcbr) // Struct fields mut is_pub_field := false @@ -139,7 +139,7 @@ fn (p mut Parser) struct_decl() { p.check(.colon) } p.fmt_inc() - p.fgenln('') + p.fgenln2('') } if p.tok == .key_mut { if is_mut { @@ -152,7 +152,7 @@ fn (p mut Parser) struct_decl() { p.check(.colon) } p.fmt_inc() - p.fgenln('') + p.fgenln2('') } // if is_pub { // } @@ -182,7 +182,7 @@ fn (p mut Parser) struct_decl() { } // `pub` access mod access_mod := if is_pub_field { AccessMod.public } else { AccessMod.private} - p.fgen(' ') + p.fspace() field_type := p.get_type() if field_type == name { 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] mut attr := '' if p.tok == .lsbr { + p.fspace() p.next() attr = p.check_name() if p.tok == .colon { @@ -219,13 +220,13 @@ fn (p mut Parser) struct_decl() { if p.first_pass() { p.table.add_field(typ.name, field_name, field_type, is_mut, attr, access_mod) } - p.fgenln('') + p.fgenln2('') } p.check(.rcbr) if !is_c && !did_gen_something && p.first_pass() { p.table.add_field(typ.name, '', 'EMPTY_STRUCT_DECLARATION', false, '', .private) } - p.fgenln('\n') + p.fgenln2('\n') } // `User{ foo: bar }` @@ -269,7 +270,7 @@ fn (p mut Parser) struct_init(typ string) string { if p.tok != .rcbr { p.gen(',') } - p.fgenln('') + p.fspace() did_gen_something = true } // If we already set some fields, need to prepend a comma diff --git a/vlib/compiler/vfmt.v b/vlib/compiler/vfmt.v index 02e27309ff..1316b0bb29 100644 --- a/vlib/compiler/vfmt.v +++ b/vlib/compiler/vfmt.v @@ -6,7 +6,6 @@ module compiler import strings -// fmt helpers [if vfmt] fn (scanner mut Scanner) fgen(s_ string) { mut s := s_ @@ -27,18 +26,36 @@ fn (scanner mut Scanner) fgenln(s_ string) { scanner.fmt_line_empty = true } + [if vfmt] 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) } [if vfmt] fn (p mut Parser) fspace() { - p.fgen(' ') + if p.first_pass() { + return + } + p.fgen2(' ') } + [if vfmt] 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) } @@ -57,11 +74,33 @@ fn (p mut Parser) peek() TokenKind { [if vfmt] fn (p mut Parser) fmt_inc() { + if p.pass != .main { + return + } p.scanner.fmt_indent++ } [if vfmt] fn (p mut Parser) fmt_dec() { + if p.pass != .main { + return + } 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() + } +} +