cgen: add support for `v -printfn main file.v`

pull/4587/head
Delyan Angelov 2020-04-25 11:15:02 +03:00
parent 900d96c638
commit e0ab318f56
4 changed files with 20 additions and 4 deletions

View File

@ -145,6 +145,10 @@ fn parse_args(args []string) (&pref.Preferences, string) {
'-freestanding' {
res.is_bare = true
}
'-prof', '-profile' {
eprintln('TODO: -prof')
res.is_prof = true
}
'-prod' {
res.is_prod = true
}
@ -182,8 +186,12 @@ fn parse_args(args []string) (&pref.Preferences, string) {
}
res.os = target_os_kind
}
'-printfn' {
res.printfn_list << cmdline.option(current_args, '-printfn', '')
i++
}
'-cflags' {
res.cflags = cmdline.option(current_args, '-cflags', '')
res.cflags += ' ' + cmdline.option(current_args, '-cflags', '')
i++
}
'-define', '-d' {

View File

@ -60,6 +60,7 @@ struct Gen {
mut:
file ast.File
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0
last_fn_c_name string
tmp_count int
variadic_args map[string]int
is_c_call bool // e.g. `C.printf("v")`
@ -479,8 +480,12 @@ fn (mut g Gen) stmt(node ast.Stmt) {
}
}
ast.FnDecl {
fn_start_pos := g.out.len
g.fn_decl = it // &it
g.gen_fn_decl(it)
if g.last_fn_c_name in g.pref.printfn_list {
println(g.out.after(fn_start_pos))
}
g.writeln('')
}
ast.ForCStmt {

View File

@ -19,12 +19,15 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl) {
if g.is_gui_app() {
// GUI application
g.writeln('int WINAPI wWinMain(HINSTANCE instance, HINSTANCE prev_instance, LPWSTR cmd_line, int show_cmd')
g.last_fn_c_name = 'wWinMain'
} else {
// Console application
g.writeln('int wmain(int ___argc, wchar_t* ___argv[], wchar_t* ___envp[]')
g.last_fn_c_name = 'wmain'
}
} else {
g.write('int ${it.name}(int ___argc, char** ___argv')
g.last_fn_c_name = it.name
}
} else {
mut name := it.name
@ -46,6 +49,7 @@ fn (mut g Gen) gen_fn_decl(it ast.FnDecl) {
// type_name := g.table.Type_to_str(it.return_type)
type_name := g.typ(it.return_type)
g.write('$type_name ${name}(')
g.last_fn_c_name = name
g.definitions.write('$type_name ${name}(')
}
// Receiver is the first argument

View File

@ -77,8 +77,8 @@ pub mut:
compile_defines_all []string // contains both: ['vfmt','another']
mod string
run_args []string // `v run x.v 1 2 3` => `1 2 3`
run_args []string // `v run x.v 1 2 3` => `1 2 3`
printfn_list []string // a list of generated function names, whose source should be shown, for debugging
}
pub fn backend_from_string(s string) ?Backend {
@ -97,4 +97,3 @@ pub fn backend_from_string(s string) ?Backend {
}
}
}