From ee8ff3945409e011b6cbd76d0860e93046ea6f28 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sun, 29 Sep 2019 04:54:12 +0300 Subject: [PATCH] rename ModPath to v_modules_path; do not allow long variable names without _ --- compiler/cc.v | 24 ++++++++++++------------ compiler/comptime.v | 2 +- compiler/fn.v | 5 +++-- compiler/main.v | 42 ++++++++++++++++++++---------------------- compiler/modules.v | 2 +- compiler/msvc.v | 4 ++-- compiler/parser.v | 3 ++- compiler/scanner.v | 21 +++++++++++++++++---- vlib/os/os.v | 6 +++--- 9 files changed, 61 insertions(+), 48 deletions(-) diff --git a/compiler/cc.v b/compiler/cc.v index dc5dcc57f3..237d41c2e5 100644 --- a/compiler/cc.v +++ b/compiler/cc.v @@ -64,13 +64,13 @@ fn (v mut V) cc() { } if v.pref.build_mode == .build_module { // Create the modules directory if it's not there. - if !os.file_exists(ModPath) { - os.mkdir(ModPath) + if !os.file_exists(v_modules_path) { + os.mkdir(v_modules_path) } - v.out_name = ModPath + v.dir + '.o' //v.out_name + v.out_name = v_modules_path + v.dir + '.o' //v.out_name println('Building ${v.out_name}...') } - + mut debug_options := '-g' mut optimization_options := '-O2' if v.pref.ccompiler.contains('clang') { @@ -113,7 +113,7 @@ fn (v mut V) cc() { // } else if v.pref.build_mode == .default_mode { - libs = '$ModPath/vlib/builtin.o' + libs = '$v_modules_path/vlib/builtin.o' if !os.file_exists(libs) { println('object file `$libs` not found') exit(1) @@ -122,7 +122,7 @@ fn (v mut V) cc() { if imp == 'webview' { continue } - libs += ' "$ModPath/vlib/${imp}.o"' + libs += ' "$v_modules_path/vlib/${imp}.o"' } } if v.pref.sanitize { @@ -130,7 +130,7 @@ fn (v mut V) cc() { } // Cross compiling linux TODO /* - sysroot := '/Users/alex/tmp/lld/linuxroot/' + sysroot := '/tmp/lld/linuxroot/' if v.os == .linux && !linux_host { // Build file.o a << '-c --sysroot=$sysroot -target x86_64-linux-gnu' @@ -288,23 +288,23 @@ fn (c mut V) cc_windows_cross() { args += cflags.c_options_before_target() mut libs := '' if c.pref.build_mode == .default_mode { - libs = '"$ModPath/vlib/builtin.o"' + libs = '"$v_modules_path/vlib/builtin.o"' if !os.file_exists(libs) { println('`$libs` not found') exit(1) } for imp in c.table.imports { - libs += ' "$ModPath/vlib/${imp}.o"' + libs += ' "$v_modules_path/vlib/${imp}.o"' } } args += ' $c.out_name_c ' args += cflags.c_options_after_target() println('Cross compiling for Windows...') - winroot := '$ModPath/winroot' + winroot := '$v_modules_path/winroot' if !os.dir_exists(winroot) { winroot_url := 'https://github.com/vlang/v/releases/download/v0.1.10/winroot.zip' println('"$winroot" not found.') - println('Download it from $winroot_url and save it in $ModPath') + println('Download it from $winroot_url and save it in $v_modules_path') println('Unzip it afterwards.\n') println('winroot.zip contains all library and header files needed '+ 'to cross-compile for Windows.') @@ -314,7 +314,7 @@ fn (c mut V) cc_windows_cross() { obj_name = obj_name.replace('.exe', '') obj_name = obj_name.replace('.o.o', '.o') include := '-I $winroot/include ' - cmd := 'clang -o $obj_name -w $include -m32 -c -target x86_64-win32 $ModPath/$c.out_name_c' + cmd := 'clang -o $obj_name -w $include -m32 -c -target x86_64-win32 $v_modules_path/$c.out_name_c' if c.pref.show_c_cmd { println(cmd) } diff --git a/compiler/comptime.v b/compiler/comptime.v index c50b27bc5f..99c8e1802d 100644 --- a/compiler/comptime.v +++ b/compiler/comptime.v @@ -150,7 +150,7 @@ fn (p mut Parser) chash() { mut flag := hash.right(5) // expand `@VROOT` `@VMOD` to absolute path flag = flag.replace('@VROOT', p.vroot) - flag = flag.replace('@VMOD', ModPath) + flag = flag.replace('@VMOD', v_modules_path) //p.log('adding flag "$flag"') p.table.parse_cflag(flag, p.mod) return diff --git a/compiler/fn.v b/compiler/fn.v index 8ba5779a89..a836d58492 100644 --- a/compiler/fn.v +++ b/compiler/fn.v @@ -137,7 +137,7 @@ fn (p mut Parser) clear_vars() { // vlib header file? fn (p mut Parser) is_sig() bool { return (p.pref.build_mode == .default_mode || p.pref.build_mode == .build_module) && - (p.file_path.contains(ModPath)) + (p.file_path.contains(v_modules_path)) } // Function signatures are added to the top of the .c file in the first run. @@ -1027,8 +1027,9 @@ fn (f &Fn) typ_str() string { return sb.str() } +// "fn foo(a int) stirng", for .vh module headers fn (f &Fn) v_definition() string { - return 'todo' + return 'fn '//$f.name(${f.str_args()})' } // f.args => "int a, string b" diff --git a/compiler/main.v b/compiler/main.v index 7067a2596c..e926c478cc 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -29,7 +29,7 @@ enum BuildMode { const ( supported_platforms = ['windows', 'mac', 'linux', 'freebsd', 'openbsd', 'netbsd', 'dragonfly', 'msvc', 'android', 'js', 'solaris'] - ModPath = os.home_dir() + '/.vmodules/' + v_modules_path = os.home_dir() + '/.vmodules/' ) enum OS { @@ -40,8 +40,8 @@ enum OS { openbsd netbsd dragonfly - msvc - js + msvc // TODO not an OS + js // TODO android solaris } @@ -74,7 +74,7 @@ mut: vroot string mod string // module being built with -lib parsers []Parser - vgen_buf strings.Builder + vgen_buf strings.Builder // temporary buffer for generated V code (.str() etc) } struct Preferences { @@ -105,6 +105,11 @@ mut: building_v bool autofree bool compress bool + // Skips re-compilation of the builtin module + // to increase compilation time. + // This is on by default, since a vast majority of users do not + // work on the builtin module itself. + skip_builtin bool } fn main() { @@ -143,19 +148,12 @@ fn main() { // TODO quit if the compiler is too old // u := os.file_last_mod_unix('v') // If there's no tmp path with current version yet, the user must be using a pre-built package - // Copy the `vlib` directory to the tmp path. -/* - // TODO - if !os.file_exists(TmpPath) && os.file_exists('vlib') { - } -*/ + // // Just fmt and exit if 'fmt' in args { vfmt(args) return } - - // Construct the V object from command line arguments mut v := new_v(args) if args.join(' ').contains(' test v') { @@ -629,7 +627,7 @@ fn (v mut V) add_v_files_to_compile() { for i := 0; i < v.table.imports.len; i++ { mod := v.table.imports[i] mod_path := v.module_path(mod) - import_path := '$ModPath/vlib/$mod_path' + import_path := '$v_modules_path/vlib/$mod_path' vfiles := v.v_files_from_dir(import_path) if vfiles.len == 0 { verror('cannot import module $mod (no .v files in "$import_path")') @@ -686,7 +684,7 @@ fn (v mut V) add_v_files_to_compile() { // These were generated by vfmt /* if v.pref.build_mode == .default_mode || v.pref.build_mode == .build_module { - module_path = '$ModPath/vlib/$mod_p' + module_path = '$v_modules_path/vlib/$mod_p' } */ if mod == 'builtin' { continue } // builtin files were already added @@ -1025,12 +1023,12 @@ fn install_v(args[]string) { if true { //println('Building vget...') os.chdir(vroot + '/tools') - vgetcompilation := os.exec('$vexec -o $vget vget.v') or { + vget_compilation := os.exec('$vexec -o $vget vget.v') or { verror(err) return } - if vgetcompilation.exit_code != 0 { - verror( vgetcompilation.output ) + if vget_compilation.exit_code != 0 { + verror( vget_compilation.output ) return } } @@ -1052,7 +1050,7 @@ fn (v &V) test_vget() { println('failed to run v install') exit(1) } - if !os.file_exists(ModPath + '/nedpals/args') { + if !os.file_exists(v_modules_path + '/nedpals/args') { println('v failed to install a test module') exit(1) } @@ -1080,7 +1078,7 @@ fn (v &V) test_v() { for dot_relative_file in test_files { relative_file := dot_relative_file.replace('./', '') file := os.realpath( relative_file ) - tmpcfilepath := file.replace('_test.v', '_test.tmp.c') + tmpc_filepath := file.replace('_test.v', '_test.tmp.c') mut cmd := '"$vexe" $joined_args -debug "$file"' if os.user_os() == 'windows' { cmd = '"$cmd"' } @@ -1100,7 +1098,7 @@ fn (v &V) test_v() { tmark.ok() println(tmark.step_message('$relative_file OK')) } - os.rm( tmpcfilepath ) + os.rm( tmpc_filepath ) } tmark.stop() println( tmark.total_message('running V tests') ) @@ -1113,7 +1111,7 @@ fn (v &V) test_v() { continue } file := os.realpath( relative_file ) - tmpcfilepath := file.replace('.v', '.tmp.c') + tmpc_filepath := file.replace('.v', '.tmp.c') mut cmd := '"$vexe" $joined_args -debug "$file"' if os.user_os() == 'windows' { cmd = '"$cmd"' } bmark.step() @@ -1131,7 +1129,7 @@ fn (v &V) test_v() { bmark.ok() println(bmark.step_message('$relative_file OK')) } - os.rm(tmpcfilepath) + os.rm(tmpc_filepath) } bmark.stop() println( bmark.total_message('building examples') ) diff --git a/compiler/modules.v b/compiler/modules.v index cf050a7677..2e607d7203 100644 --- a/compiler/modules.v +++ b/compiler/modules.v @@ -43,7 +43,7 @@ fn (v &V) find_module_path(mod string) string { //println('ip=$import_path') // Finally try modules installed with vpm (~/.vmodules) if !os.dir_exists(import_path) { - import_path = '$ModPath/$mod_path' + import_path = '$v_modules_path/$mod_path' if !os.dir_exists(import_path){ verror('module "$mod" not found') } diff --git a/compiler/msvc.v b/compiler/msvc.v index d2937f25cf..67a4a64afe 100644 --- a/compiler/msvc.v +++ b/compiler/msvc.v @@ -255,7 +255,7 @@ pub fn (v mut V) cc_msvc() { // } else if v.pref.build_mode == .default_mode { - b := os.realpath( '$ModPath/vlib/builtin.obj' ) + b := os.realpath( '$v_modules_path/vlib/builtin.obj' ) alibs << '"$b"' if !os.file_exists(b) { println('`builtin.obj` not found') @@ -265,7 +265,7 @@ pub fn (v mut V) cc_msvc() { if imp == 'webview' { continue } - alibs << '"' + os.realpath( '$ModPath/vlib/${imp}.obj' ) + '"' + alibs << '"' + os.realpath( '$v_modules_path/vlib/${imp}.obj' ) + '"' } } diff --git a/compiler/parser.v b/compiler/parser.v index 982a7a241e..e777c3f9d8 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -168,7 +168,7 @@ fn (p mut Parser) scan_tokens() { tok: res.tok lit: res.lit line_nr: p.scanner.line_nr - col: p.scanner.pos - p.scanner.last_nl_pos + col: p.scanner.pos - p.scanner.last_nl_pos } if res.tok == .eof { break @@ -1487,6 +1487,7 @@ fn (p mut Parser) var_decl() { mut names := []string names << p.check_name() + p.scanner.validate_var_name(names[0]) for p.tok == .comma { p.check(.comma) names << p.check_name() diff --git a/compiler/scanner.v b/compiler/scanner.v index b8bc2f1708..ecdd7d45c3 100644 --- a/compiler/scanner.v +++ b/compiler/scanner.v @@ -82,9 +82,9 @@ fn new_scanner(text string) &Scanner { // The goal of ScannerPos is to track the current scanning position, -// so that if there is an error found later, v could show a more accurate +// so that if there is an error found later, v could show a more accurate // position about where the error initially was. -// NB: The fields of ScannerPos *should be kept synchronized* with the +// NB: The fields of ScannerPos *should be kept synchronized* with the // corresponding fields in Scanner. struct ScannerPos { mut: @@ -123,7 +123,7 @@ fn (s mut Scanner) get_scanner_pos_of_token(t Tok) ScannerPos { s.file_lines = []string mut prevlinepos := 0 for { - prevlinepos = s.pos + prevlinepos = s.pos if s.pos >= s.text.len { break } if s.line_nr > tline + 10 { break } //////////////////////////////////////// @@ -287,7 +287,7 @@ fn (s mut Scanner) ident_number() string { fn (s mut Scanner) skip_whitespace() { for s.pos < s.text.len && s.text[s.pos].is_white() { // Count \r\n as one line - if is_nl(s.text[s.pos]) && !s.expect('\r\n', s.pos-1) { + if is_nl(s.text[s.pos]) && !s.expect('\r\n', s.pos-1) { s.inc_line_number() } s.pos++ @@ -899,3 +899,16 @@ fn good_type_name(s string) bool { } return true } + +// registration_date good +// registrationdate bad +fn (s &Scanner) validate_var_name(name string) { + if name.len > 11 && !name.contains('_') { + s.error('bad variable name `$name`\n' + +'looks like you have a multi-word name without separating them with `_`' + +'\nfor example, use `registration_date` instead of `registrationdate` ') + + } +} + + diff --git a/vlib/os/os.v b/vlib/os/os.v index 7c7ec2013c..20c2532683 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -468,9 +468,9 @@ pub fn get_line() string { // get_raw_line returns a one-line string from stdin along with '\n' if there is any pub fn get_raw_line() string { $if windows { - maxlinechars := 256 - buf := &byte(malloc(maxlinechars*2)) - res := int( C.fgetws(buf, maxlinechars, C.stdin ) ) + max_line_chars := 256 + buf := &byte(malloc(max_line_chars*2)) + res := int( C.fgetws(buf, max_line_chars, C.stdin ) ) len := int( C.wcslen(&u16(buf)) ) if 0 != res { return string_from_wide2( &u16(buf), len ) } return ''