From 1d9916f93ed2b20437a63ccfcc0e619365653d41 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 30 Dec 2019 09:23:17 +0100 Subject: [PATCH] fix fn main generation --- v.v | 22 ++++++++++++---------- vlib/v/gen/cgen.v | 10 +++++++++- vlib/v/gen/tests/1.v | 3 +-- vlib/v/gen/tests/2.v | 3 +-- 4 files changed, 23 insertions(+), 15 deletions(-) diff --git a/v.v b/v.v index bb0d7f56e0..563e41d54b 100644 --- a/v.v +++ b/v.v @@ -13,21 +13,21 @@ import ( const ( known_commands = ['run', 'build', 'version', 'doc'] - simple_tools = ['fmt', 'up', 'create', 'test', 'test-fmt', 'test-compiler', 'build-tools', 'build-examples', 'build-vbinaries'] + simple_tools = ['fmt', 'up', 'create', 'test', 'test-fmt', 'test-compiler', 'build-tools', + 'build-examples', 'build-vbinaries'] ) fn main() { - is_verbose := '-verbose' in os.args || '--verbose' in os.args + is_verbose := '-verbose' in os.args || '--verbose' in os.args // t := time.ticks() // defer { println(time.ticks() - t) } args := compiler.env_vflags_and_os_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') - } - + 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 if command in simple_tools { compiler.launch_tool('v' + command) @@ -114,7 +114,9 @@ fn v_command(command string, args []string) { mod := args.last() os.system('$vexe build module vlib$os.path_separator' + args.last()) vhfile := filepath.join(compiler.v_modules_path,'vlib','${mod}.vh') - txt := os.read_file(vhfile) or { panic(err) } + txt := os.read_file(vhfile) or { + panic(err) + } println(txt) // v.gen_doc_html_for_module(args.last()) } diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index f646555b46..1c0f609b33 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -40,7 +40,12 @@ fn (g mut Gen) stmt(node ast.Stmt) { g.writeln(';') } ast.FnDecl { - g.write('$it.typ.name ${it.name}(') + if it.name == 'main' { + g.write('int ${it.name}(') + } + else { + g.write('$it.typ.name ${it.name}(') + } for arg in it.args { g.write(arg.typ.name + ' ' + arg.name) } @@ -48,6 +53,9 @@ fn (g mut Gen) stmt(node ast.Stmt) { for stmt in it.stmts { g.stmt(stmt) } + if it.name == 'main' { + g.writeln('return 0;') + } g.writeln('}') } ast.Return { diff --git a/vlib/v/gen/tests/1.v b/vlib/v/gen/tests/1.v index 1252c41344..2b684716cb 100644 --- a/vlib/v/gen/tests/1.v +++ b/vlib/v/gen/tests/1.v @@ -1,4 +1,3 @@ -fn main() int { +fn main() { a := 10 - return 0 } diff --git a/vlib/v/gen/tests/2.v b/vlib/v/gen/tests/2.v index 7b50a3aa12..48eaaee8c5 100644 --- a/vlib/v/gen/tests/2.v +++ b/vlib/v/gen/tests/2.v @@ -48,8 +48,7 @@ fn init_user() { } } -fn main() int { - return 0 +fn main() { }