compiler: streamline main function handling

* compiler: streamline C main function generation

* fix most tests

* compiler: fix for 'go update()' in graph.v . More precise parser error messages.

* Fix temporarily examples/hot_reload/message.v by using os inside it (os.clear).

* Make graph.v easier to quickly modify by defining y outside the loop.

* Fix failure of /v/nv/compiler/tests/defer_test.v when run with 'v -g' (#line directive was not on its own line, but right after } ).

* Do not pass the os.args to tests, even if the tests import os (they are more stable when run in a controlled environment).

* fix declared and not used in the js backend.

* fix js main => main__main too.
This commit is contained in:
Delyan Angelov 2019-09-28 20:42:29 +03:00 committed by Alexander Medvednikov
parent 0160c7a89d
commit a4cbe78d97
10 changed files with 141 additions and 92 deletions

View file

@ -368,7 +368,7 @@ fn (v mut V) compile() {
}
}
$if js {
cgen.genln('main();')
cgen.genln('main__main();')
}
cgen.save()
v.cc()
@ -439,35 +439,53 @@ string _STR_TMP(const char *fmt, ...) {
// It can be skipped in single file programs
if v.pref.is_script {
//println('Generating main()...')
cgen.genln('int main() { init_consts();')
v.gen_main_start(true)
cgen.genln('$cgen.fn_main;')
cgen.genln('return 0; }')
v.gen_main_end('return 0')
}
else {
println('panic: function `main` is undeclared in the main module')
exit(1)
verror('function `main` is not declared in the main module')
}
}
else if v.pref.is_test {
if v.table.main_exists() {
verror('test files cannot have function `main`')
}
// make sure there's at least on test function
}
if !v.table.has_at_least_one_test_fn() {
verror('test files need to have at least one test function')
}
// Generate `main` which calls every single test function
cgen.genln('int main() { init_consts();')
}
// Generate a C `main`, which calls every single test function
v.gen_main_start(false)
for _, f in v.table.fns {
if f.name.starts_with('test_') {
if f.name.starts_with('main__test_') {
cgen.genln('$f.name();')
}
}
cgen.genln('return g_test_ok == 0; }')
v.gen_main_end('return g_test_ok == 0')
}
else if v.table.main_exists() {
v.gen_main_start(true)
cgen.genln(' main__main();')
v.gen_main_end('return 0')
}
}
}
fn (v mut V) gen_main_start(add_os_args bool){
v.cgen.genln('int main(int argc, char** argv) { ')
v.cgen.genln(' init_consts();')
if add_os_args && 'os' in v.table.imports {
v.cgen.genln(' os__args = os__init_os_args(argc, (byteptr*)argv);')
}
v.generate_hotcode_reloading_main_caller()
v.cgen.genln('')
}
fn (v mut V) gen_main_end(return_statement string){
v.cgen.genln('')
v.cgen.genln(' $return_statement;')
v.cgen.genln('}')
}
fn final_target_out_name(out_name string) string {
mut cmd := if out_name.starts_with('/') {
out_name