rename ModPath to v_modules_path; do not allow long variable names without _

pull/2159/head
Alexander Medvednikov 2019-09-29 04:54:12 +03:00
parent 8b8cd13929
commit ee8ff39454
9 changed files with 61 additions and 48 deletions

View File

@ -64,13 +64,13 @@ fn (v mut V) cc() {
} }
if v.pref.build_mode == .build_module { if v.pref.build_mode == .build_module {
// Create the modules directory if it's not there. // Create the modules directory if it's not there.
if !os.file_exists(ModPath) { if !os.file_exists(v_modules_path) {
os.mkdir(ModPath) 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}...') println('Building ${v.out_name}...')
} }
mut debug_options := '-g' mut debug_options := '-g'
mut optimization_options := '-O2' mut optimization_options := '-O2'
if v.pref.ccompiler.contains('clang') { if v.pref.ccompiler.contains('clang') {
@ -113,7 +113,7 @@ fn (v mut V) cc() {
// //
} }
else if v.pref.build_mode == .default_mode { 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) { if !os.file_exists(libs) {
println('object file `$libs` not found') println('object file `$libs` not found')
exit(1) exit(1)
@ -122,7 +122,7 @@ fn (v mut V) cc() {
if imp == 'webview' { if imp == 'webview' {
continue continue
} }
libs += ' "$ModPath/vlib/${imp}.o"' libs += ' "$v_modules_path/vlib/${imp}.o"'
} }
} }
if v.pref.sanitize { if v.pref.sanitize {
@ -130,7 +130,7 @@ fn (v mut V) cc() {
} }
// Cross compiling linux TODO // Cross compiling linux TODO
/* /*
sysroot := '/Users/alex/tmp/lld/linuxroot/' sysroot := '/tmp/lld/linuxroot/'
if v.os == .linux && !linux_host { if v.os == .linux && !linux_host {
// Build file.o // Build file.o
a << '-c --sysroot=$sysroot -target x86_64-linux-gnu' 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() args += cflags.c_options_before_target()
mut libs := '' mut libs := ''
if c.pref.build_mode == .default_mode { if c.pref.build_mode == .default_mode {
libs = '"$ModPath/vlib/builtin.o"' libs = '"$v_modules_path/vlib/builtin.o"'
if !os.file_exists(libs) { if !os.file_exists(libs) {
println('`$libs` not found') println('`$libs` not found')
exit(1) exit(1)
} }
for imp in c.table.imports { for imp in c.table.imports {
libs += ' "$ModPath/vlib/${imp}.o"' libs += ' "$v_modules_path/vlib/${imp}.o"'
} }
} }
args += ' $c.out_name_c ' args += ' $c.out_name_c '
args += cflags.c_options_after_target() args += cflags.c_options_after_target()
println('Cross compiling for Windows...') println('Cross compiling for Windows...')
winroot := '$ModPath/winroot' winroot := '$v_modules_path/winroot'
if !os.dir_exists(winroot) { if !os.dir_exists(winroot) {
winroot_url := 'https://github.com/vlang/v/releases/download/v0.1.10/winroot.zip' winroot_url := 'https://github.com/vlang/v/releases/download/v0.1.10/winroot.zip'
println('"$winroot" not found.') 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('Unzip it afterwards.\n')
println('winroot.zip contains all library and header files needed '+ println('winroot.zip contains all library and header files needed '+
'to cross-compile for Windows.') '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('.exe', '')
obj_name = obj_name.replace('.o.o', '.o') obj_name = obj_name.replace('.o.o', '.o')
include := '-I $winroot/include ' 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 { if c.pref.show_c_cmd {
println(cmd) println(cmd)
} }

View File

@ -150,7 +150,7 @@ fn (p mut Parser) chash() {
mut flag := hash.right(5) mut flag := hash.right(5)
// expand `@VROOT` `@VMOD` to absolute path // expand `@VROOT` `@VMOD` to absolute path
flag = flag.replace('@VROOT', p.vroot) flag = flag.replace('@VROOT', p.vroot)
flag = flag.replace('@VMOD', ModPath) flag = flag.replace('@VMOD', v_modules_path)
//p.log('adding flag "$flag"') //p.log('adding flag "$flag"')
p.table.parse_cflag(flag, p.mod) p.table.parse_cflag(flag, p.mod)
return return

View File

@ -137,7 +137,7 @@ fn (p mut Parser) clear_vars() {
// vlib header file? // vlib header file?
fn (p mut Parser) is_sig() bool { fn (p mut Parser) is_sig() bool {
return (p.pref.build_mode == .default_mode || p.pref.build_mode == .build_module) && 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. // 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() return sb.str()
} }
// "fn foo(a int) stirng", for .vh module headers
fn (f &Fn) v_definition() string { fn (f &Fn) v_definition() string {
return 'todo' return 'fn '//$f.name(${f.str_args()})'
} }
// f.args => "int a, string b" // f.args => "int a, string b"

View File

@ -29,7 +29,7 @@ enum BuildMode {
const ( const (
supported_platforms = ['windows', 'mac', 'linux', 'freebsd', 'openbsd', supported_platforms = ['windows', 'mac', 'linux', 'freebsd', 'openbsd',
'netbsd', 'dragonfly', 'msvc', 'android', 'js', 'solaris'] 'netbsd', 'dragonfly', 'msvc', 'android', 'js', 'solaris']
ModPath = os.home_dir() + '/.vmodules/' v_modules_path = os.home_dir() + '/.vmodules/'
) )
enum OS { enum OS {
@ -40,8 +40,8 @@ enum OS {
openbsd openbsd
netbsd netbsd
dragonfly dragonfly
msvc msvc // TODO not an OS
js js // TODO
android android
solaris solaris
} }
@ -74,7 +74,7 @@ mut:
vroot string vroot string
mod string // module being built with -lib mod string // module being built with -lib
parsers []Parser parsers []Parser
vgen_buf strings.Builder vgen_buf strings.Builder // temporary buffer for generated V code (.str() etc)
} }
struct Preferences { struct Preferences {
@ -105,6 +105,11 @@ mut:
building_v bool building_v bool
autofree bool autofree bool
compress 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() { fn main() {
@ -143,19 +148,12 @@ fn main() {
// TODO quit if the compiler is too old // TODO quit if the compiler is too old
// u := os.file_last_mod_unix('v') // 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 // 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 // Just fmt and exit
if 'fmt' in args { if 'fmt' in args {
vfmt(args) vfmt(args)
return return
} }
// Construct the V object from command line arguments // Construct the V object from command line arguments
mut v := new_v(args) mut v := new_v(args)
if args.join(' ').contains(' test v') { 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++ { for i := 0; i < v.table.imports.len; i++ {
mod := v.table.imports[i] mod := v.table.imports[i]
mod_path := v.module_path(mod) 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) vfiles := v.v_files_from_dir(import_path)
if vfiles.len == 0 { if vfiles.len == 0 {
verror('cannot import module $mod (no .v files in "$import_path")') 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 // These were generated by vfmt
/* /*
if v.pref.build_mode == .default_mode || v.pref.build_mode == .build_module { 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 if mod == 'builtin' { continue } // builtin files were already added
@ -1025,12 +1023,12 @@ fn install_v(args[]string) {
if true { if true {
//println('Building vget...') //println('Building vget...')
os.chdir(vroot + '/tools') 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) verror(err)
return return
} }
if vgetcompilation.exit_code != 0 { if vget_compilation.exit_code != 0 {
verror( vgetcompilation.output ) verror( vget_compilation.output )
return return
} }
} }
@ -1052,7 +1050,7 @@ fn (v &V) test_vget() {
println('failed to run v install') println('failed to run v install')
exit(1) 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') println('v failed to install a test module')
exit(1) exit(1)
} }
@ -1080,7 +1078,7 @@ fn (v &V) test_v() {
for dot_relative_file in test_files { for dot_relative_file in test_files {
relative_file := dot_relative_file.replace('./', '') relative_file := dot_relative_file.replace('./', '')
file := os.realpath( relative_file ) 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"' mut cmd := '"$vexe" $joined_args -debug "$file"'
if os.user_os() == 'windows' { cmd = '"$cmd"' } if os.user_os() == 'windows' { cmd = '"$cmd"' }
@ -1100,7 +1098,7 @@ fn (v &V) test_v() {
tmark.ok() tmark.ok()
println(tmark.step_message('$relative_file OK')) println(tmark.step_message('$relative_file OK'))
} }
os.rm( tmpcfilepath ) os.rm( tmpc_filepath )
} }
tmark.stop() tmark.stop()
println( tmark.total_message('running V tests') ) println( tmark.total_message('running V tests') )
@ -1113,7 +1111,7 @@ fn (v &V) test_v() {
continue continue
} }
file := os.realpath( relative_file ) 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"' mut cmd := '"$vexe" $joined_args -debug "$file"'
if os.user_os() == 'windows' { cmd = '"$cmd"' } if os.user_os() == 'windows' { cmd = '"$cmd"' }
bmark.step() bmark.step()
@ -1131,7 +1129,7 @@ fn (v &V) test_v() {
bmark.ok() bmark.ok()
println(bmark.step_message('$relative_file OK')) println(bmark.step_message('$relative_file OK'))
} }
os.rm(tmpcfilepath) os.rm(tmpc_filepath)
} }
bmark.stop() bmark.stop()
println( bmark.total_message('building examples') ) println( bmark.total_message('building examples') )

View File

@ -43,7 +43,7 @@ fn (v &V) find_module_path(mod string) string {
//println('ip=$import_path') //println('ip=$import_path')
// Finally try modules installed with vpm (~/.vmodules) // Finally try modules installed with vpm (~/.vmodules)
if !os.dir_exists(import_path) { if !os.dir_exists(import_path) {
import_path = '$ModPath/$mod_path' import_path = '$v_modules_path/$mod_path'
if !os.dir_exists(import_path){ if !os.dir_exists(import_path){
verror('module "$mod" not found') verror('module "$mod" not found')
} }

View File

@ -255,7 +255,7 @@ pub fn (v mut V) cc_msvc() {
// //
} }
else if v.pref.build_mode == .default_mode { 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"' alibs << '"$b"'
if !os.file_exists(b) { if !os.file_exists(b) {
println('`builtin.obj` not found') println('`builtin.obj` not found')
@ -265,7 +265,7 @@ pub fn (v mut V) cc_msvc() {
if imp == 'webview' { if imp == 'webview' {
continue continue
} }
alibs << '"' + os.realpath( '$ModPath/vlib/${imp}.obj' ) + '"' alibs << '"' + os.realpath( '$v_modules_path/vlib/${imp}.obj' ) + '"'
} }
} }

View File

@ -168,7 +168,7 @@ fn (p mut Parser) scan_tokens() {
tok: res.tok tok: res.tok
lit: res.lit lit: res.lit
line_nr: p.scanner.line_nr 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 { if res.tok == .eof {
break break
@ -1487,6 +1487,7 @@ fn (p mut Parser) var_decl() {
mut names := []string mut names := []string
names << p.check_name() names << p.check_name()
p.scanner.validate_var_name(names[0])
for p.tok == .comma { for p.tok == .comma {
p.check(.comma) p.check(.comma)
names << p.check_name() names << p.check_name()

View File

@ -82,9 +82,9 @@ fn new_scanner(text string) &Scanner {
// The goal of ScannerPos is to track the current scanning position, // 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. // 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. // corresponding fields in Scanner.
struct ScannerPos { struct ScannerPos {
mut: mut:
@ -123,7 +123,7 @@ fn (s mut Scanner) get_scanner_pos_of_token(t Tok) ScannerPos {
s.file_lines = []string s.file_lines = []string
mut prevlinepos := 0 mut prevlinepos := 0
for { for {
prevlinepos = s.pos prevlinepos = s.pos
if s.pos >= s.text.len { break } if s.pos >= s.text.len { break }
if s.line_nr > tline + 10 { 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() { fn (s mut Scanner) skip_whitespace() {
for s.pos < s.text.len && s.text[s.pos].is_white() { for s.pos < s.text.len && s.text[s.pos].is_white() {
// Count \r\n as one line // 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.inc_line_number()
} }
s.pos++ s.pos++
@ -899,3 +899,16 @@ fn good_type_name(s string) bool {
} }
return true 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` ')
}
}

View File

@ -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 // get_raw_line returns a one-line string from stdin along with '\n' if there is any
pub fn get_raw_line() string { pub fn get_raw_line() string {
$if windows { $if windows {
maxlinechars := 256 max_line_chars := 256
buf := &byte(malloc(maxlinechars*2)) buf := &byte(malloc(max_line_chars*2))
res := int( C.fgetws(buf, maxlinechars, C.stdin ) ) res := int( C.fgetws(buf, max_line_chars, C.stdin ) )
len := int( C.wcslen(&u16(buf)) ) len := int( C.wcslen(&u16(buf)) )
if 0 != res { return string_from_wide2( &u16(buf), len ) } if 0 != res { return string_from_wide2( &u16(buf), len ) }
return '' return ''