1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

move vfmt frontend program to tools/vfmt.v

This commit is contained in:
Delyan Angelov 2019-12-23 12:02:50 +02:00 committed by Alexander Medvednikov
parent 28594a65a8
commit 42b1660c7e
12 changed files with 234 additions and 120 deletions

View File

@ -11,7 +11,7 @@ jobs:
- name: Install dependencies - name: Install dependencies
run: sudo rm -f /etc/apt/sources.list.d/dotnetdev.list /etc/apt/sources.list.d/microsoft-prod.list; sudo apt-get update; sudo apt-get install --quiet -y libglfw3 libglfw3-dev libfreetype6-dev libssl-dev sqlite3 libsqlite3-dev libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev run: sudo rm -f /etc/apt/sources.list.d/dotnetdev.list /etc/apt/sources.list.d/microsoft-prod.list; sudo apt-get update; sudo apt-get install --quiet -y libglfw3 libglfw3-dev libfreetype6-dev libssl-dev sqlite3 libsqlite3-dev libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev
- name: Build v - name: Build v
run: echo $VFLAGS && make -j4 && ./v -o v v.v run: echo $VFLAGS && make -j4 && ./v -cg -o v v.v
- name: Test v->c - name: Test v->c
run: | run: |
sudo ln -s /var/tmp/tcc/bin/tcc /usr/local/bin/tcc sudo ln -s /var/tmp/tcc/bin/tcc /usr/local/bin/tcc
@ -54,7 +54,7 @@ jobs:
brew install freetype glfw openssl postgres sdl2 sdl2_ttf sdl2_mixer sdl2_image brew install freetype glfw openssl postgres sdl2 sdl2_ttf sdl2_mixer sdl2_image
export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/opt/openssl/lib/" export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/opt/openssl/lib/"
- name: Build V - name: Build V
run: make -j4 && ./v -o v v.v run: make -j4 && ./v -cg -o v v.v
- name: Build V using V - name: Build V using V
run: ./v -o v2 v.v && ./v2 -o v3 v.v run: ./v -o v2 v.v && ./v2 -o v3 v.v
- name: Test symlink - name: Test symlink
@ -152,7 +152,7 @@ jobs:
- name: Install dependencies - name: Install dependencies
run: sudo rm -f /etc/apt/sources.list.d/dotnetdev.list /etc/apt/sources.list.d/microsoft-prod.list; sudo apt-get update; sudo apt-get install --quiet -y musl musl-tools libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev run: sudo rm -f /etc/apt/sources.list.d/dotnetdev.list /etc/apt/sources.list.d/microsoft-prod.list; sudo apt-get update; sudo apt-get install --quiet -y musl musl-tools libsdl2-dev libsdl2-ttf-dev libsdl2-mixer-dev libsdl2-image-dev
- name: Build v - name: Build v
run: echo $VFLAGS && make -j4 && ./v -o v v.v run: echo $VFLAGS && make -j4 && ./v -cg -o v v.v
- name: Test v binaries - name: Test v binaries
run: ./v build-vbinaries run: ./v build-vbinaries
# - name: Test v->js # - name: Test v->js

1
.gitignore vendored
View File

@ -12,6 +12,7 @@ fns.txt
/tools/vrepl /tools/vrepl
/tools/vtest /tools/vtest
/tools/vtest-compiler /tools/vtest-compiler
/tools/vfmt
/tools/vup /tools/vup
/tools/vpm /tools/vpm
/tools/vcreate /tools/vcreate

View File

@ -88,7 +88,7 @@ $(TMPVC)/.git/config:
$(MAKE) fresh_vc $(MAKE) fresh_vc
selfcompile: selfcompile:
./v -o v v.v ./v -cg -o v v.v
modules: module_builtin module_strings module_strconv modules: module_builtin module_strings module_strconv
module_builtin: module_builtin:

View File

@ -27,11 +27,11 @@ pub fn new_test_session(vargs string) TestSession {
} }
pub fn vexe_path() string { pub fn vexe_path() string {
// NB: tools extracted from v require that the first // NB: tools extracted from v require that the VEXE
// argument to them to be the v executable location. // environment variable contains the path to the v executable location.
// They are usually launched by vlib/compiler/vtools.v, // They are usually launched by vlib/compiler/vtools.v,
// launch_tool/1 , which provides it. // launch_tool/1 , which provides it.
return os.args[1] return os.getenv('VEXE')
} }

101
tools/vfmt.v Normal file
View File

@ -0,0 +1,101 @@
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module main
import (
os
compiler
)
struct FormatOptions {
is_w bool
is_diff bool
is_verbose bool
is_all bool
}
fn main() {
foptions := FormatOptions{
is_w: '-w' in os.args
is_diff: '-diff' in os.args
is_verbose: '-verbose' in os.args || '--verbose' in os.args
is_all: '-all' in os.args || '--all' in os.args
}
toolexe := os.executable()
compiler.set_vroot_folder(os.dir(os.dir(toolexe)))
args := compiler.env_vflags_and_os_args()
if foptions.is_verbose {
eprintln('vfmt toolexe: $toolexe')
eprintln('vfmt args: ' + os.args.str())
eprintln('vfmt env_vflags_and_os_args: ' + args.str())
}
mut files := []string
for i := 1; i < args.len; i++ {
a := args[i]
if a == 'fmt' {
continue
}
if !a.starts_with('-') {
file := a
if !os.exists(file) {
compiler.verror('"$file" does not exist.')
}
if !file.ends_with('.v') {
compiler.verror('v fmt can only be used on .v files.\nOffending file: "$file" .')
}
files << a
}
}
if files.len == 0 {
usage()
exit(0)
}
if foptions.is_all {
os.setenv('VFMT_OPTION_ALL', 'yes', true)
}
for file in files {
format_file(file, foptions)
}
}
fn format_file(file string, foptions FormatOptions) {
mut v := compiler.new_v_compiler_with_args([file])
if foptions.is_verbose {
eprintln('vfmt format_file: $file | v.dir: $v.dir')
}
v.compile()
formatted_file_path := os.getenv('VFMT_FILE_RESULT')
// eprintln('File: $file .')
// eprintln('Formatted file is: $formatted_file_path .')
if foptions.is_diff {
if find_diff:=os.exec('diff -v'){
os.system('diff "$formatted_file_path" "$file" ')
return
}
eprintln('No working "diff" CLI command found.')
return
}
if foptions.is_w {
os.mv_by_cp(formatted_file_path, file) or {
panic(err)
}
eprintln('Reformatted file in place: $file .')
}
else {
content := os.read_file(formatted_file_path) or {
panic(err)
}
print(content)
}
}
fn usage() {
print('Usage: tools/vfmt [flags] path_to_source.v [path_to_other_source.v]
Formats the given V source files, and prints their formatted source to stdout.
Options:
-diff display only diffs between the formatted source and the original source.
-w write result to (source) file(s) instead of to stdout.
')
}

View File

@ -23,21 +23,31 @@ fn main() {
ensure_vmodules_dir_exist() ensure_vmodules_dir_exist()
change_to_vmodules_dir() change_to_vmodules_dir()
// This tool is intended to be launched by the v frontend, // This tool is intended to be launched by the v frontend,
// so its first argument is the path to the v frontend executable. // which provides the path to V inside os.getenv('VEXE')
args := os.args // args are: vpm vexepath SUBCOMMAND module names args := os.args // args are: vpm SUBCOMMAND module names
if args.len < 3 { if args.len < 2 {
vpm_help([]) vpm_help([])
exit(5) exit(5)
} }
vpm_command := args[2] vpm_command := args[1]
module_names := args[3..] module_names := args[2..]
// println('module names: ') println(module_names) // println('module names: ') println(module_names)
match vpm_command { match vpm_command {
'help' { vpm_help(module_names) } 'help' {
'search' { vpm_search(module_names) } vpm_help(module_names)
'install' { vpm_install(module_names) } }
'update' { vpm_update(module_names) } 'search' {
'remove' { vpm_remove(module_names) } vpm_search(module_names)
}
'install' {
vpm_install(module_names)
}
'update' {
vpm_update(module_names)
}
'remove' {
vpm_remove(module_names)
}
else { else {
println('Error: you tried to run "v $vpm_command"') println('Error: you tried to run "v $vpm_command"')
println('... but the v package management tool vpm only knows about these commands:') println('... but the v package management tool vpm only knows about these commands:')
@ -45,8 +55,7 @@ fn main() {
println(' v $validcmd') println(' v $validcmd')
} }
exit(3) exit(3)
} }}
}
} }
fn vpm_search(module_names []string) { fn vpm_search(module_names []string) {
@ -75,40 +84,35 @@ fn vpm_install(module_names []string){
println(' v install requires *at least one* module name') println(' v install requires *at least one* module name')
exit(2) exit(2)
} }
mut errors := 0 mut errors := 0
for name in module_names { for name in module_names {
modurl := url + '/jsmod/$name' modurl := url + '/jsmod/$name'
r := http.get(modurl) or { panic(err) } r := http.get(modurl) or {
panic(err)
}
if r.status_code == 404 { if r.status_code == 404 {
println('Skipping module "$name", since $url reported that "$name" does not exist.') println('Skipping module "$name", since $url reported that "$name" does not exist.')
errors++ errors++
continue continue
} }
if r.status_code != 200 { if r.status_code != 200 {
println('Skipping module "$name", since $url responded with $r.status_code http status code. Please try again later.') println('Skipping module "$name", since $url responded with $r.status_code http status code. Please try again later.')
errors++ errors++
continue continue
} }
s := r.text s := r.text
mod := json.decode(Mod,s) or { mod := json.decode(Mod,s) or {
errors++ errors++
println('Skipping module "$name", since its information is not in json format.') println('Skipping module "$name", since its information is not in json format.')
continue continue
} }
if ('' == mod.url || '' == mod.name) { if ('' == mod.url || '' == mod.name) {
errors++ errors++
// a possible 404 error, which means a missing module? // a possible 404 error, which means a missing module?
println('Skipping module "$name", since it is missing name or url information.') println('Skipping module "$name", since it is missing name or url information.')
continue continue
} }
final_module_path := get_vmodules_dir_path() + '/' + mod.name.replace('.', '/') final_module_path := get_vmodules_dir_path() + '/' + mod.name.replace('.', '/')
println('Installing module "$name" from $mod.url to $final_module_path ...') println('Installing module "$name" from $mod.url to $final_module_path ...')
_ = os.exec('git clone --depth=1 $mod.url $final_module_path') or { _ = os.exec('git clone --depth=1 $mod.url $final_module_path') or {
errors++ errors++
@ -154,7 +158,9 @@ fn ensure_vmodules_dir_exist() {
home_vmodules := get_vmodules_dir_path() home_vmodules := get_vmodules_dir_path()
if !os.is_dir(home_vmodules) { if !os.is_dir(home_vmodules) {
println('Creating $home_vmodules/ ...') println('Creating $home_vmodules/ ...')
os.mkdir(home_vmodules) or { panic(err) } os.mkdir(home_vmodules) or {
panic(err)
}
} }
} }
@ -173,10 +179,11 @@ fn user_asks_for_help(module_names []string) bool {
fn vpm_help(module_names []string) { fn vpm_help(module_names []string) {
println('Usage:') println('Usage:')
println(' b) v search keyword1 [keyword2] [...]') println(' a) v install module [module] [module] [...]')
println(' c) v install module [module] [module] [...]') println(' b) v update [module] [...]')
println(' d) v update [module] [...]') println(' c) v remove [module] [...]')
println(' e) v remove [module] [...]') println(' d) v search keyword1 [keyword2] [...]')
println('') println('')
println(' You can also pass -h or --help after each vpm command from the above, to see more details about it.') println(' You can also pass -h or --help after each vpm command from the above, to see more details about it.')
} }

View File

@ -86,7 +86,7 @@ pub fn run_repl() []string {
} }
mut r := Repl{} mut r := Repl{}
mut readline := readline.Readline{} mut readline := readline.Readline{}
vexe := os.args[1] vexe := os.getenv('VEXE')
for { for {
if r.indent == 0 { if r.indent == 0 {
prompt = '>>> ' prompt = '>>> '
@ -200,9 +200,9 @@ fn print_output(s os.Result) {
} }
fn main() { fn main() {
if os.args.len < 2 || !os.exists(os.args[1]) { if !os.exists(os.getenv('VEXE')) {
println('Usage:') println('Usage:')
println(' vrepl vexepath\n') println(' VEXE=vexepath vrepl\n')
println(' ... where vexepath is the full path to the v executable file') println(' ... where vexepath is the full path to the v executable file')
return return
} }
@ -216,7 +216,7 @@ pub fn rerror(s string) {
} }
fn v_version() string { fn v_version() string {
vexe := os.args[1] vexe := os.getenv('VEXE')
vversion_res := os.exec('$vexe --version') or { panic('"$vexe --version" is not working') } vversion_res := os.exec('$vexe --version') or { panic('"$vexe --version" is not working') }
return vversion_res.output return vversion_res.output
} }

View File

@ -2,7 +2,7 @@ import os
fn main() { fn main() {
println('Updating V...') println('Updating V...')
vroot := os.dir(os.args[1]) vroot := os.getenv('VEXE')
os.chdir(vroot) os.chdir(vroot)
s := os.exec('git -C "$vroot" pull --rebase origin master') or { panic(err) } s := os.exec('git -C "$vroot" pull --rebase origin master') or { panic(err) }
println(s.output) println(s.output)

23
v.v
View File

@ -13,14 +13,21 @@ import (
const ( const (
known_commands = ['run', 'build', 'version', 'doc'] known_commands = ['run', 'build', 'version', 'doc']
simple_tools = ['up', 'create', 'test', 'test-compiler', 'build-tools', 'build-examples', 'build-vbinaries'] simple_tools = ['fmt', 'up', 'create', 'test', 'test-compiler', 'build-tools', 'build-examples', 'build-vbinaries']
) )
fn main() { fn main() {
is_verbose := '-verbose' in os.args || '--verbose' in os.args
// t := time.ticks() // t := time.ticks()
// defer { println(time.ticks() - t) } // defer { println(time.ticks() - t) }
args := compiler.env_vflags_and_os_args() args := compiler.env_vflags_and_os_args()
options, command := compiler.get_v_options_and_main_command( args ) options, command := compiler.get_v_options_and_main_command( args )
if is_verbose {
eprintln('v args: $args')
eprintln('v command: $command')
eprintln('v options: $options')
}
// external tool // external tool
if command in simple_tools { if command in simple_tools {
compiler.launch_tool('v' + command) compiler.launch_tool('v' + command)
@ -88,7 +95,7 @@ fn v_command(command string, args []string) {
'translate' { 'translate' {
println('Translating C to V will be available in V 0.3 (January)') println('Translating C to V will be available in V 0.3 (January)')
} }
'search', 'install', 'update' { 'search', 'install', 'update', 'remove' {
compiler.launch_tool('vpm') compiler.launch_tool('vpm')
} }
'get' { 'get' {
@ -97,9 +104,6 @@ fn v_command(command string, args []string) {
'symlink' { 'symlink' {
compiler.create_symlink() compiler.create_symlink()
} }
'fmt' {
compiler.vfmt(args)
}
'runrepl' { 'runrepl' {
compiler.launch_tool('vrepl') compiler.launch_tool('vrepl')
} }
@ -109,16 +113,15 @@ fn v_command(command string, args []string) {
os.chdir(vdir) os.chdir(vdir)
mod := args.last() mod := args.last()
os.system('$vexe build module vlib$os.path_separator' + args.last()) os.system('$vexe build module vlib$os.path_separator' + args.last())
txt := os.read_file(filepath.join(compiler.v_modules_path,'vlib','${mod}.vh'))or{ vhfile := filepath.join(compiler.v_modules_path,'vlib','${mod}.vh')
panic(err) txt := os.read_file(vhfile) or { panic(err) }
}
println(txt) println(txt)
// v.gen_doc_html_for_module(args.last()) // v.gen_doc_html_for_module(args.last())
} }
else { else {
println('v $command: unknown command') println('v $command: unknown command')
println('Run "v help" for usage.') println('Run "v help" for usage.')
}} }
}
exit(0) exit(0)
} }

View File

@ -1132,32 +1132,6 @@ pub fn env_vflags_and_os_args() []string {
return non_empty(args) return non_empty(args)
} }
pub fn vfmt(args []string) {
file := args.last()
if !os.exists(file) {
println('"$file" does not exist')
exit(1)
}
if !file.ends_with('.v') {
println('v fmt can only be used on .v files')
exit(1)
}
vexe := vexe_path()
// launch_tool('vfmt', '-d vfmt')
vroot := os.dir(vexe)
os.chdir(vroot)
println('building vfmt... (it will be cached soon)')
ret := os.system('$vexe -o $vroot/tools/vfmt -d vfmt v.v')
if ret != 0 {
println('err')
return
}
println('running vfmt...')
os.exec('$vroot/tools/vfmt $file')or{
panic(err)
}
// if !os.exists('
}
pub fn create_symlink() { pub fn create_symlink() {
$if windows { $if windows {

View File

@ -240,7 +240,6 @@ fn (p mut Parser) fremove_last() {
} }
[if vfmt] [if vfmt]
fn (p &Parser) gen_fmt() { fn (p &Parser) gen_fmt() {
if p.pass != .main { if p.pass != .main {
@ -250,6 +249,11 @@ fn (p &Parser) gen_fmt() {
if p.file_name == '' { if p.file_name == '' {
return return
} }
is_all := os.getenv('VFMT_OPTION_ALL') == 'yes'
if p.file_path != p.v.dir && !is_all {
// skip everything except the last file (given by the CLI argument)
return
}
//s := p.scanner.fmt_out.str().replace('\n\n\n', '\n').trim_space() //s := p.scanner.fmt_out.str().replace('\n\n\n', '\n').trim_space()
//s := p.scanner.fmt_out.str().trim_space() //s := p.scanner.fmt_out.str().trim_space()
//p.scanner.fgenln('// nice') //p.scanner.fgenln('// nice')
@ -271,18 +275,29 @@ fn (p &Parser) gen_fmt() {
return return
} }
//files := ['get_type.v'] //files := ['get_type.v']
if p.file_path.contains('vfmt') {return} if p.file_path.contains('compiler/vfmt.v') {return}
//if !(p.file_name in files) { return } //if !(p.file_name in files) { return }
path := os.tmpdir() + '/' + p.file_name if is_all {
println('generating ${path}') if p.file_path.len > 0 {
mut out := os.create(path) or { path := write_formatted_source( p.file_name, s )
verror('failed to create os_nix.v') os.cp( path, p.file_path ) or { panic(err) }
return eprintln('Written fmt file to: $p.file_path')
}
}
if p.file_path == p.v.dir {
res_path := write_formatted_source( p.file_name, s )
os.setenv('VFMT_FILE_RESULT', res_path, true )
} }
println('replacing ${p.file_path}...\n')
out.writeln(s.trim_space())//p.scanner.fmt_out.str().trim_space())
out.writeln('')
out.close()
os.mv(path, p.file_path)
} }
fn write_formatted_source(file_name string, s string) string {
path := os.tmpdir() + '/' + file_name
mut out := os.create(path) or {
verror('failed to create file $path')
return ''
}
//eprintln('replacing ${p.file_path} ...\n')
out.writeln(s.trim_space())//p.scanner.fmt_out.str().trim_space())
out.close()
return path
}

View File

@ -3,22 +3,25 @@ module compiler
import os import os
pub fn launch_tool(tname string) { pub fn launch_tool(tname string) {
is_verbose := '-verbose' in os.args || '--verbose' in os.args
vexe := vexe_path() vexe := vexe_path()
vroot := os.dir(vexe) vroot := os.dir(vexe)
mut oargs := os.args set_vroot_folder( vroot ) // needed by tools to find back v
oargs[0] = '"$vexe"' // make it more explicit tool_args := os.args[1..].join(' ')
tool_exe := os.realpath('$vroot/tools/$tname') tool_exe := os.realpath('$vroot/tools/$tname')
tool_source := os.realpath('$vroot/tools/${tname}.v') tool_source := os.realpath('$vroot/tools/${tname}.v')
// ////////////////////////////////////////////////////
tool_args := oargs.join(' ')
tool_command := '"$tool_exe" $tool_args' tool_command := '"$tool_exe" $tool_args'
// println('Launching: "$tool_command" ...') if is_verbose {
eprintln('launch_tool vexe : $vroot')
eprintln('launch_tool vroot : $vroot')
eprintln('launch_tool tool_args : $tool_args')
eprintln('launch_tool tool_command: $tool_command')
}
mut tool_should_be_recompiled := false mut tool_should_be_recompiled := false
if !os.exists(tool_exe) { if !os.exists(tool_exe) {
// fresh checkout // fresh checkout
tool_should_be_recompiled = true tool_should_be_recompiled = true
} } else {
else {
if os.file_last_mod_unix(tool_exe) <= os.file_last_mod_unix(vexe) { if os.file_last_mod_unix(tool_exe) <= os.file_last_mod_unix(vexe) {
// v was recompiled, maybe after v up ... // v was recompiled, maybe after v up ...
// rebuild the tool too just in case // rebuild the tool too just in case
@ -29,16 +32,26 @@ pub fn launch_tool(tname string) {
tool_should_be_recompiled = true tool_should_be_recompiled = true
} }
} }
if tool_should_be_recompiled {
compilation_command := '"$vexe" "$tool_source"' if is_verbose {
// println('Compiling $tname with: "$compilation_command"') eprintln('launch_tool tool_should_be_recompiled: $tool_should_be_recompiled')
tool_compilation := os.exec(compilation_command)or{
panic(err)
} }
if tool_should_be_recompiled {
mut compilation_options := ''
if tname == 'vfmt' { compilation_options = '-d vfmt' }
compilation_command := '"$vexe" $compilation_options "$tool_source"'
if is_verbose {
eprintln('Compiling $tname with: "$compilation_command"')
}
tool_compilation := os.exec(compilation_command) or { panic(err) }
if tool_compilation.exit_code != 0 { if tool_compilation.exit_code != 0 {
panic('V tool "$tool_source" could not be compiled\n' + tool_compilation.output) panic('V tool "$tool_source" could not be compiled\n' + tool_compilation.output)
} }
} }
exit(os.system(tool_command)) if is_verbose {
eprintln('launch_tool running tool command: $tool_command ...')
} }
exit( os.system(tool_command) )
}