fmt: run on cmd/v; cgen: fix anonymous functions

pull/4518/head
Alexander Medvednikov 2020-04-20 07:04:31 +02:00
parent efff96d622
commit c1fc768c1b
5 changed files with 129 additions and 81 deletions

View File

@ -27,7 +27,7 @@ struct FormatOptions {
} }
const ( const (
platform_and_file_extensions = [['windows', '_win.v', '_windows.v'], ['linux', '_lin.v', platform_and_file_extensions = [['windows', '_windows.v'], ['linux', '_lin.v',
'_linux.v', '_nix.v'], ['macos', '_mac.v', '_darwin.v'], ['freebsd', '_bsd.v', '_freebsd.v'], '_linux.v', '_nix.v'], ['macos', '_mac.v', '_darwin.v'], ['freebsd', '_bsd.v', '_freebsd.v'],
['netbsd', '_bsd.v', '_netbsd.v'], ['openbsd', '_bsd.v', '_openbsd.v'], ['solaris', '_solaris.v'], ['netbsd', '_bsd.v', '_netbsd.v'], ['openbsd', '_bsd.v', '_openbsd.v'], ['solaris', '_solaris.v'],
['haiku', '_haiku.v'], ['qnx', '_qnx.v']] ['haiku', '_haiku.v'], ['qnx', '_qnx.v']]

173
cmd/v/v.v
View File

@ -3,38 +3,33 @@
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module main module main
import ( import internal.help
//internal.compile import os
internal.help import os.cmdline
os import v.table
os.cmdline import v.doc
v.table import v.pref
v.doc import v.util
v.pref import v.builder
v.util
v.builder
)
const ( const (
simple_cmd = ['fmt', simple_cmd = ['fmt', 'up'
'up', 'self', 'self', 'test'
'test', 'test-fmt', 'test-compiler', 'test-fixed', 'test-fmt', 'test-compiler', 'test-fixed', 'bin2v'
'bin2v', 'repl'
'repl', 'build-tools'
'build-tools', 'build-examples', 'build-vbinaries', 'build-examples', 'build-vbinaries', 'setup-freetype'
'setup-freetype']
list_of_flags_that_allow_duplicates = ['cc','d','define','cf','cflags']
//list_of_flags contains a list of flags where an argument is expected past it.
list_of_flags_with_param = [
'o', 'output', 'd', 'define', 'b', 'backend', 'cc', 'os', 'target-os', 'arch',
'csource', 'cf', 'cflags', 'path'
] ]
list_of_flags_that_allow_duplicates = ['cc', 'd', 'define', 'cf', 'cflags']
list_of_flags_with_param = [
'o'
'output', 'd', 'define', 'b', 'backend', 'cc', 'os', 'target-os', 'arch', 'csource'
'cf', 'cflags', 'path']
) )
fn main() { fn main() {
args := os.args[1..] args := os.args[1..]
//args = 123 // args = 123
if args.len == 0 || args[0] in ['-', 'repl'] { if args.len == 0 || args[0] in ['-', 'repl'] {
// Running `./v` without args launches repl // Running `./v` without args launches repl
if args.len == 0 { if args.len == 0 {
@ -43,7 +38,8 @@ fn main() {
util.launch_tool(false, 'vrepl') util.launch_tool(false, 'vrepl')
return return
} }
if args.len > 0 && (args[0] in ['version', '-V', '-version', '--version'] || (args[0] == '-v' && args.len == 1) ) { if args.len > 0 && (args[0] in ['version', '-V', '-version', '--version'] || (args[0] ==
'-v' && args.len == 1)) {
// `-v` flag is for setting verbosity, but without any args it prints the version, like Clang // `-v` flag is for setting verbosity, but without any args it prints the version, like Clang
println(util.full_v_version()) println(util.full_v_version())
return return
@ -55,10 +51,10 @@ fn main() {
println(util.full_v_version()) println(util.full_v_version())
} }
if prefs.is_verbose { if prefs.is_verbose {
//println('args= ') // println('args= ')
//println(args) // QTODO // println(args) // QTODO
//println('prefs= ') // println('prefs= ')
//println(prefs) // QTODO // println(prefs) // QTODO
} }
// Start calling the correct functions/external tools // Start calling the correct functions/external tools
// Note for future contributors: Please add new subcommands in the `match` block below. // Note for future contributors: Please add new subcommands in the `match` block below.
@ -103,8 +99,8 @@ fn main() {
else {} else {}
} }
if command in ['run', 'build-module'] || command.ends_with('.v') || os.exists(command) { if command in ['run', 'build-module'] || command.ends_with('.v') || os.exists(command) {
//println('command') // println('command')
//println(prefs.path) // println(prefs.path)
builder.compile(command, prefs) builder.compile(command, prefs)
return return
} }
@ -113,35 +109,68 @@ fn main() {
} }
fn parse_args(args []string) (&pref.Preferences, string) { fn parse_args(args []string) (&pref.Preferences, string) {
mut res := &pref.Preferences{} var res := &pref.Preferences{}
mut command := '' var command := ''
mut command_pos := 0 var command_pos := 0
//for i, arg in args { // for i, arg in args {
for i := 0 ; i < args.len; i ++ { for i := 0; i < args.len; i++ {
arg := args[i] arg := args[i]
match arg { match arg {
'-v' { res.is_verbose = true } '-v' {
'-cg' { res.is_debug = true } res.is_verbose = true
'-live' { res.is_live = true } }
'-sharedlive' { res.is_live = true res.is_shared = true } '-cg' {
'-shared' { res.is_shared = true } res.is_debug = true
'-autofree' { res.autofree = true } }
'-compress' { res.compress = true } '-live' {
'-freestanding' { res.is_bare = true } res.is_live = true
'-prod' { res.is_prod = true } }
'-stats' { res.is_stats = true } '-sharedlive' {
'-obfuscate' { res.obfuscate = true } res.is_live = true
'-translated' { res.translated = true } res.is_shared = true
'-showcc' { res.show_cc = true } }
'-cache' { res.is_cache = true } '-shared' {
'-keepc' { res.is_keep_c = true } res.is_shared = true
//'-x64' { res.translated = true } }
'-autofree' {
res.autofree = true
}
'-compress' {
res.compress = true
}
'-freestanding' {
res.is_bare = true
}
'-prod' {
res.is_prod = true
}
'-stats' {
res.is_stats = true
}
'-obfuscate' {
res.obfuscate = true
}
'-translated' {
res.translated = true
}
'-showcc' {
res.show_cc = true
}
'-cache' {
res.is_cache = true
}
'-keepc' {
res.is_keep_c = true
}
'-x64' {
res.backend = .x64
}
'-os' { '-os' {
//TODO Remove `tmp` variable when it doesn't error out in C. // TODO Remove `tmp` variable when it doesn't error out in C.
target_os := cmdline.option(args, '-os', '') target_os := cmdline.option(args, '-os', '')
tmp := pref.os_from_string(target_os) or { tmp := pref.os_from_string(target_os) or {
println('unknown operating system target `$target_os`') println('unknown operating system target `$target_os`')
exit(1) exit(1)
} }
res.os = tmp res.os = tmp
i++ i++
@ -155,7 +184,7 @@ fn parse_args(args []string) (&pref.Preferences, string) {
i++ i++
} }
'-o' { '-o' {
res.out_name = cmdline.option(args, '-o', '') res.out_name = cmdline.option(args, '-o', '')
i++ i++
} }
'-b' { '-b' {
@ -166,7 +195,7 @@ fn parse_args(args []string) (&pref.Preferences, string) {
i++ i++
} }
else { else {
mut should_continue := false var should_continue := false
for flag_with_param in list_of_flags_with_param { for flag_with_param in list_of_flags_with_param {
if '-$flag_with_param' == arg { if '-$flag_with_param' == arg {
should_continue = true should_continue = true
@ -186,15 +215,14 @@ fn parse_args(args []string) (&pref.Preferences, string) {
} }
if command.ends_with('.v') || os.exists(command) { if command.ends_with('.v') || os.exists(command) {
res.path = command res.path = command
} } else if command == 'run' {
else if command == 'run' {
res.is_run = true res.is_run = true
if command_pos > args.len { if command_pos > args.len {
eprintln('v run: no v files listed') eprintln('v run: no v files listed')
exit(1) exit(1)
} }
res.path = args[command_pos + 1] res.path = args[command_pos + 1]
res.run_args = args[command_pos+2..] res.run_args = args[command_pos + 2..]
} }
if command == 'build-module' { if command == 'build-module' {
res.build_mode = .build_module res.build_mode = .build_module
@ -209,12 +237,8 @@ fn parse_args(args []string) (&pref.Preferences, string) {
fn invoke_help_and_exit(remaining []string) { fn invoke_help_and_exit(remaining []string) {
match remaining.len { match remaining.len {
0, 1 { 0, 1 { help.print_and_exit('default') }
help.print_and_exit('default') 2 { help.print_and_exit(remaining[1]) }
}
2 {
help.print_and_exit(remaining[1])
}
else {} else {}
} }
println('V Error: Expected only one help topic to be provided.') println('V Error: Expected only one help topic to be provided.')
@ -227,21 +251,24 @@ fn create_symlink() {
return return
} }
vexe := pref.vexe_path() vexe := pref.vexe_path()
mut link_path := '/usr/local/bin/v' var link_path := '/usr/local/bin/v'
mut ret := os.exec('ln -sf $vexe $link_path') or { panic(err) } var ret := os.exec('ln -sf $vexe $link_path') or {
panic(err)
}
if ret.exit_code == 0 { if ret.exit_code == 0 {
println('Symlink "$link_path" has been created') println('Symlink "$link_path" has been created')
} } else if os.system("uname -o | grep -q \'[A/a]ndroid\'") == 0 {
else if os.system('uname -o | grep -q \'[A/a]ndroid\'') == 0 {
println('Failed to create symlink "$link_path". Trying again with Termux path for Android.') println('Failed to create symlink "$link_path". Trying again with Termux path for Android.')
link_path = '/data/data/com.termux/files/usr/bin/v' link_path = '/data/data/com.termux/files/usr/bin/v'
ret = os.exec('ln -sf $vexe $link_path') or { panic(err) } ret = os.exec('ln -sf $vexe $link_path') or {
panic(err)
}
if ret.exit_code == 0 { if ret.exit_code == 0 {
println('Symlink "$link_path" has been created') println('Symlink "$link_path" has been created')
} else { } else {
println('Failed to create symlink "$link_path". Try again with sudo.') println('Failed to create symlink "$link_path". Try again with sudo.')
} }
} else { } else {
println('Failed to create symlink "$link_path". Try again with sudo.') println('Failed to create symlink "$link_path". Try again with sudo.')
} }
} }

View File

@ -551,7 +551,7 @@ pub fn (c mut Checker) call_fn(call_expr mut ast.CallExpr) table.Type {
} }
} }
if !found { if !found {
c.error('unknown fn: $fn_name', call_expr.pos) c.error('unknown function: $fn_name', call_expr.pos)
return table.void_type return table.void_type
} }
call_expr.return_type = f.return_type call_expr.return_type = f.return_type

View File

@ -17,10 +17,10 @@ const (
'char' 'char'
'default' 'default'
'do' 'do'
'double', 'extern', 'float', 'inline', 'int', 'long', 'register', 'restrict', 'short', 'double'
'signed' 'extern', 'float', 'inline', 'int', 'long', 'register', 'restrict', 'short', 'signed'
'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while' 'sizeof'
] 'static', 'switch', 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while']
) )
fn foo(t token.Token) { fn foo(t token.Token) {
@ -1853,6 +1853,22 @@ fn (var g Gen) const_decl(node ast.ConstDecl) {
g.expr(field.expr) g.expr(field.expr)
val := g.out.after(pos) val := g.out.after(pos)
g.out.go_back(val.len) g.out.go_back(val.len)
/*
if field.typ == table.byte_type {
g.const_decl_simple_define(name, val)
return
}
*/
/*
if table.is_number(field.typ) {
g.const_decl_simple_define(name, val)
} else if field.typ == table.string_type {
g.definitions.writeln('string _const_$name; // a string literal, inited later')
if g.pref.build_mode != .build_module {
g.stringliterals.writeln('\t_const_$name = $val;')
}
} else {
*/
match field.expr { match field.expr {
ast.CharLiteral { ast.CharLiteral {
g.const_decl_simple_define(name, val) g.const_decl_simple_define(name, val)

View File

@ -67,7 +67,7 @@ pub fn (var p Parser) expr(precedence int) ast.Expr {
node = ast.None{} node = ast.None{}
} }
.key_sizeof { .key_sizeof {
p.next() // sizeof p.next() // sizeof
p.check(.lpar) p.check(.lpar)
if p.tok.lit == 'C' { if p.tok.lit == 'C' {
p.next() p.next()
@ -102,7 +102,7 @@ pub fn (var p Parser) expr(precedence int) ast.Expr {
if p.peek_tok.kind == .pipe { if p.peek_tok.kind == .pipe {
node = p.assoc() node = p.assoc()
} else if p.peek_tok.kind == .colon || p.tok.kind == .rcbr { } else if p.peek_tok.kind == .colon || p.tok.kind == .rcbr {
node = p.struct_init(true) // short_syntax: true node = p.struct_init(true) // short_syntax: true
} else if p.tok.kind == .name { } else if p.tok.kind == .name {
p.next() p.next()
lit := if p.tok.lit != '' { p.tok.lit } else { p.tok.kind.str() } lit := if p.tok.lit != '' { p.tok.lit } else { p.tok.kind.str() }
@ -113,6 +113,11 @@ pub fn (var p Parser) expr(precedence int) ast.Expr {
} }
p.check(.rcbr) p.check(.rcbr)
} }
.key_fn {
// Anonymous function
node = p.anon_fn()
return node
}
else { else {
if p.tok.kind == .comment { if p.tok.kind == .comment {
println(p.tok.lit) println(p.tok.lit)