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 (
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'],
['netbsd', '_bsd.v', '_netbsd.v'], ['openbsd', '_bsd.v', '_openbsd.v'], ['solaris', '_solaris.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.
module main
import (
//internal.compile
internal.help
os
os.cmdline
v.table
v.doc
v.pref
v.util
v.builder
)
import internal.help
import os
import os.cmdline
import v.table
import v.doc
import v.pref
import v.util
import v.builder
const (
simple_cmd = ['fmt',
'up', 'self',
'test', 'test-fmt', 'test-compiler', 'test-fixed',
'bin2v',
'repl',
'build-tools', 'build-examples', 'build-vbinaries',
'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'
simple_cmd = ['fmt', 'up'
'self', 'test'
'test-fmt', 'test-compiler', 'test-fixed', 'bin2v'
'repl'
'build-tools'
'build-examples', 'build-vbinaries', 'setup-freetype'
]
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() {
args := os.args[1..]
//args = 123
// args = 123
if args.len == 0 || args[0] in ['-', 'repl'] {
// Running `./v` without args launches repl
if args.len == 0 {
@ -43,7 +38,8 @@ fn main() {
util.launch_tool(false, 'vrepl')
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
println(util.full_v_version())
return
@ -55,10 +51,10 @@ fn main() {
println(util.full_v_version())
}
if prefs.is_verbose {
//println('args= ')
//println(args) // QTODO
//println('prefs= ')
//println(prefs) // QTODO
// println('args= ')
// println(args) // QTODO
// println('prefs= ')
// println(prefs) // QTODO
}
// Start calling the correct functions/external tools
// Note for future contributors: Please add new subcommands in the `match` block below.
@ -103,8 +99,8 @@ fn main() {
else {}
}
if command in ['run', 'build-module'] || command.ends_with('.v') || os.exists(command) {
//println('command')
//println(prefs.path)
// println('command')
// println(prefs.path)
builder.compile(command, prefs)
return
}
@ -113,35 +109,68 @@ fn main() {
}
fn parse_args(args []string) (&pref.Preferences, string) {
mut res := &pref.Preferences{}
mut command := ''
mut command_pos := 0
//for i, arg in args {
for i := 0 ; i < args.len; i ++ {
var res := &pref.Preferences{}
var command := ''
var command_pos := 0
// for i, arg in args {
for i := 0; i < args.len; i++ {
arg := args[i]
match arg {
'-v' { res.is_verbose = true }
'-cg' { res.is_debug = true }
'-live' { res.is_live = true }
'-sharedlive' { res.is_live = true res.is_shared = true }
'-shared' { res.is_shared = 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.translated = true }
'-v' {
res.is_verbose = true
}
'-cg' {
res.is_debug = true
}
'-live' {
res.is_live = true
}
'-sharedlive' {
res.is_live = true
res.is_shared = true
}
'-shared' {
res.is_shared = 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' {
//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', '')
tmp := pref.os_from_string(target_os) or {
println('unknown operating system target `$target_os`')
exit(1)
println('unknown operating system target `$target_os`')
exit(1)
}
res.os = tmp
i++
@ -155,7 +184,7 @@ fn parse_args(args []string) (&pref.Preferences, string) {
i++
}
'-o' {
res.out_name = cmdline.option(args, '-o', '')
res.out_name = cmdline.option(args, '-o', '')
i++
}
'-b' {
@ -166,7 +195,7 @@ fn parse_args(args []string) (&pref.Preferences, string) {
i++
}
else {
mut should_continue := false
var should_continue := false
for flag_with_param in list_of_flags_with_param {
if '-$flag_with_param' == arg {
should_continue = true
@ -186,15 +215,14 @@ fn parse_args(args []string) (&pref.Preferences, string) {
}
if command.ends_with('.v') || os.exists(command) {
res.path = command
}
else if command == 'run' {
} else if command == 'run' {
res.is_run = true
if command_pos > args.len {
eprintln('v run: no v files listed')
exit(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' {
res.build_mode = .build_module
@ -209,12 +237,8 @@ fn parse_args(args []string) (&pref.Preferences, string) {
fn invoke_help_and_exit(remaining []string) {
match remaining.len {
0, 1 {
help.print_and_exit('default')
}
2 {
help.print_and_exit(remaining[1])
}
0, 1 { help.print_and_exit('default') }
2 { help.print_and_exit(remaining[1]) }
else {}
}
println('V Error: Expected only one help topic to be provided.')
@ -227,21 +251,24 @@ fn create_symlink() {
return
}
vexe := pref.vexe_path()
mut link_path := '/usr/local/bin/v'
mut ret := os.exec('ln -sf $vexe $link_path') or { panic(err) }
var link_path := '/usr/local/bin/v'
var ret := os.exec('ln -sf $vexe $link_path') or {
panic(err)
}
if ret.exit_code == 0 {
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.')
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 {
println('Symlink "$link_path" has been created')
} else {
println('Failed to create symlink "$link_path". Try again with sudo.')
}
} 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 {
c.error('unknown fn: $fn_name', call_expr.pos)
c.error('unknown function: $fn_name', call_expr.pos)
return table.void_type
}
call_expr.return_type = f.return_type

View File

@ -17,10 +17,10 @@ const (
'char'
'default'
'do'
'double', 'extern', 'float', 'inline', 'int', 'long', 'register', 'restrict', 'short',
'signed'
'sizeof', 'static', 'switch', 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while'
]
'double'
'extern', 'float', 'inline', 'int', 'long', 'register', 'restrict', 'short', 'signed'
'sizeof'
'static', 'switch', 'typedef', 'union', 'unsigned', 'void', 'volatile', 'while']
)
fn foo(t token.Token) {
@ -1853,6 +1853,22 @@ fn (var g Gen) const_decl(node ast.ConstDecl) {
g.expr(field.expr)
val := g.out.after(pos)
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 {
ast.CharLiteral {
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{}
}
.key_sizeof {
p.next() // sizeof
p.next() // sizeof
p.check(.lpar)
if p.tok.lit == 'C' {
p.next()
@ -102,7 +102,7 @@ pub fn (var p Parser) expr(precedence int) ast.Expr {
if p.peek_tok.kind == .pipe {
node = p.assoc()
} 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 {
p.next()
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)
}
.key_fn {
// Anonymous function
node = p.anon_fn()
return node
}
else {
if p.tok.kind == .comment {
println(p.tok.lit)