replace silly `exit(reason string)` with `exit(code int)`

pull/402/head
Alexander Medvednikov 2019-06-23 10:12:09 +02:00
parent 0d1d5612ae
commit 06a4dfa290
6 changed files with 26 additions and 108 deletions

View File

@ -4,12 +4,8 @@
module builtin module builtin
pub fn exit(reason string) { pub fn exit(code int) {
if reason == '' { C.exit(1)
panic('exit empty reason')
}
println('exit(): $reason')
C.exit(0)
} }
// isnil returns true if an object is nil (only for C objects). // isnil returns true if an object is nil (only for C objects).

View File

@ -17,8 +17,6 @@ struct CGen {
fns []string fns []string
so_fns []string so_fns []string
consts_init []string consts_init []string
// tmp_lines []string
// tmp_lines_pos int
lines []string lines []string
is_user bool is_user bool
mut: mut:
@ -30,19 +28,13 @@ mut:
is_tmp bool is_tmp bool
fn_main string fn_main string
stash string stash string
// st_start_pos int
} }
fn new_cgen(out_name_c string) *CGen { fn new_cgen(out_name_c string) *CGen {
// println('NEW CGENN($out_name_c)')
// println('$LANG_TMP/$out_name_c')
gen := &CGen { gen := &CGen {
out_path: '$TmpPath/$out_name_c' out_path: '$TmpPath/$out_name_c'
out: os.create_file('$TmpPath/$out_name_c') out: os.create_file('$TmpPath/$out_name_c')
} }
for i := 0; i < 10; i++ {
// gen.tmp_lines.push('')
}
return gen return gen
} }
@ -51,7 +43,6 @@ fn (g mut CGen) genln(s string) {
return return
} }
if g.is_tmp { if g.is_tmp {
// if g.tmp_lines_pos > 0 {
g.tmp_line = '$g.tmp_line $s\n' g.tmp_line = '$g.tmp_line $s\n'
return return
} }
@ -61,16 +52,13 @@ fn (g mut CGen) genln(s string) {
g.prev_line = g.cur_line g.prev_line = g.cur_line
g.cur_line = '' g.cur_line = ''
} }
// g.lines << s
} }
fn (g mut CGen) gen(s string) { fn (g mut CGen) gen(s string) {
// if g.nogen || g.run == RunType.RUN_DECLS {
if g.nogen || g.run == RUN_DECLS { if g.nogen || g.run == RUN_DECLS {
return return
} }
if g.is_tmp { if g.is_tmp {
// if g.tmp_lines_pos > 0 {
g.tmp_line = '$g.tmp_line $s' g.tmp_line = '$g.tmp_line $s'
} }
else { else {
@ -82,18 +70,16 @@ fn (g mut CGen) save() {
s := g.lines.join('\n') s := g.lines.join('\n')
g.out.appendln(s) g.out.appendln(s)
g.out.close() g.out.close()
// os.system('clang-format -i $g.out_path')
} }
fn (g mut CGen) start_tmp() { fn (g mut CGen) start_tmp() {
if g.is_tmp { if g.is_tmp {
println(g.tmp_line) println(g.tmp_line)
os.exit('start_tmp() already started. cur_line="$g.cur_line"') println('start_tmp() already started. cur_line="$g.cur_line"')
exit(1)
} }
// kg.tmp_lines_pos++ // kg.tmp_lines_pos++
g.tmp_line = '' g.tmp_line = ''
// g.tmp_lines[g.tmp_lines_pos] = ''
// g.tmp_lines.set(g.tmp_lines_pos, '')
g.is_tmp = true g.is_tmp = true
} }
@ -101,15 +87,10 @@ fn (g mut CGen) end_tmp() string {
g.is_tmp = false g.is_tmp = false
res := g.tmp_line res := g.tmp_line
g.tmp_line = '' g.tmp_line = ''
// g.tmp_lines_pos--
// g.tmp_line = g.tmp_lines[g.tmp_lines_pos]
return res return res
} }
fn (g mut CGen) add_placeholder() int { fn (g mut CGen) add_placeholder() int {
// g.genln('/*placeholder*/')
// g.genln('')
// return g.lines.len - 1
if g.is_tmp { if g.is_tmp {
return g.tmp_line.len return g.tmp_line.len
} }
@ -133,10 +114,10 @@ fn (g mut CGen) set_placeholder(pos int, val string) {
// g.genln('') // g.genln('')
} }
// /////////////////////
fn (g mut CGen) add_placeholder2() int { fn (g mut CGen) add_placeholder2() int {
if g.is_tmp { if g.is_tmp {
exit('tmp in addp2') println('tmp in addp2')
exit(1)
} }
g.lines << '' g.lines << ''
return g.lines.len - 1 return g.lines.len - 1
@ -147,65 +128,16 @@ fn (g mut CGen) set_placeholder2(pos int, val string) {
return return
} }
if g.is_tmp { if g.is_tmp {
exit('tmp in setp2') println('tmp in setp2')
exit(1)
} }
g.lines[pos] = val g.lines[pos] = val
} }
// /////////////////
// fn (g mut CGen) cut_lines_after(pos int) string {
// end := g.lines.len
// lines := g.lines.slice_fast(pos, end)
// body := lines.join('\n')
// g.lines = g.lines.slice_fast(0, pos)
// return body
// }
// fn (g mut CGen) set_prev_line(val string) {
// g.lines.set(g.lines.len - 3, val)
// }
// ////fn (g mut CGen) go_back() {
// ////g.stash = g.prev_line + g.cur_line
// g.lines.set(g.lin
// ////}
// fn (g mut CGen) end_statement() {
// last_lines := g.lines.slice_fast(g.st_start_pos, g.lines.len - 1)
// mut merged := last_lines.join(' ')
// merged += '/* M $last_lines.len */'
// merged = merged.replace('\n', '')
// // zero last N lines instead of deleting them
// for i := g.st_start_pos; i < g.lines.len; i++ {
// g.lines.set(i, '')
// }
// g.lines.set(g.lines.len - 1, merged)
// // g.genln('')
// g.st_start_pos = g.lines.len - 1
// // os.exitkmerged)
// }
// fn (g mut CGen) prepend_type(typ string) {
// g.cur_line = typ.add(g.cur_line)
// g.cur_line='!!!'
// }
fn (g mut CGen) insert_before(val string) { fn (g mut CGen) insert_before(val string) {
// g.cur_line = val.add(g.cur_line)
// return
// val += '/*inserted*/'
g.lines.insert(g.lines.len - 1, val) g.lines.insert(g.lines.len - 1, val)
} }
// fn (g mut CGen) swap_last_lines() {
// return
// if g.run == RUN_DECLS {
// return
// }
// i := g.lines.len - 1
// j := i - 1
// tmp := g.lines[i]
// // println('lines i = $tmp')
// // println('lines j = ${g.lines[j]}')
// // // os.exit('')
// g.lines.set(i, g.lines[j])
// g.lines.set(j, tmp)
// }
fn (g mut CGen) register_thread_fn(wrapper_name, wrapper_text, struct_text string) { fn (g mut CGen) register_thread_fn(wrapper_name, wrapper_text, struct_text string) {
for arg in g.thread_args { for arg in g.thread_args {
if arg.contains(wrapper_name) { if arg.contains(wrapper_name) {
@ -216,14 +148,6 @@ fn (g mut CGen) register_thread_fn(wrapper_name, wrapper_text, struct_text strin
g.thread_args << wrapper_text g.thread_args << wrapper_text
} }
/*
fn (g mut CGen) delete_all_after(pos int) {
if pos > g.cur_line.len - 1 {
return
}
g.cur_line = g.cur_line.substr(0, pos)
}
*/
fn (c mut V) prof_counters() string { fn (c mut V) prof_counters() string {
mut res := []string mut res := []string
// Global fns // Global fns

View File

@ -115,10 +115,12 @@ fn main() {
if args.contains('fmt') { if args.contains('fmt') {
file := args.last() file := args.last()
if !os.file_exists(file) { if !os.file_exists(file) {
os.exit1('"$file" does not exist') println('"$file" does not exist')
exit(1)
} }
if !file.ends_with('.v') { if !file.ends_with('.v') {
os.exit1('v fmt can only be used on .v files') println('v fmt can only be used on .v files')
exit(1)
} }
println('vfmt is temporarily disabled') println('vfmt is temporarily disabled')
return return
@ -136,7 +138,7 @@ fn main() {
// Generate the docs and exit // Generate the docs and exit
if args.contains('doc') { if args.contains('doc') {
// c.gen_doc_html_for_module(args.last()) // c.gen_doc_html_for_module(args.last())
os.exit('') exit(0)
} }
c.compile() c.compile()
} }
@ -380,7 +382,8 @@ string _STR_TMP(const char *fmt, ...) {
if ret != 0 { if ret != 0 {
s := os.system(cmd) s := os.system(cmd)
println(s) println(s)
os.exit1('ret not 0, exiting') println('ret not 0, exiting')
exit(1)
} }
} }
} }
@ -414,7 +417,7 @@ fn (c mut V) cc() {
libs = '$TmpPath/vlib/builtin.o' libs = '$TmpPath/vlib/builtin.o'
if !os.file_exists(libs) { if !os.file_exists(libs) {
println('`builtin.o` not found') println('`builtin.o` not found')
exit('') exit(1)
} }
for imp in c.table.imports { for imp in c.table.imports {
if imp == 'webview' { if imp == 'webview' {
@ -504,7 +507,7 @@ mut args := ''
'/usr/lib/x86_64-linux-gnu/crtn.o') '/usr/lib/x86_64-linux-gnu/crtn.o')
println(ress) println(ress)
if ress.contains('error:') { if ress.contains('error:') {
os.exit1('') os.exit(1)
} }
println('linux cross compilation done. resulting binary: "$c.out_name"') println('linux cross compilation done. resulting binary: "$c.out_name"')
} }
@ -585,7 +588,8 @@ fn (c mut V) add_user_v_files() {
} }
} }
if user_files.len == 0 { if user_files.len == 0 {
exit('No input .v files') println('No input .v files')
exit(1)
} }
if c.is_verbose { if c.is_verbose {
c.log('user_files:') c.log('user_files:')
@ -703,7 +707,8 @@ fn new_v(args[]string) *V {
is_test := dir.ends_with('_test.v') is_test := dir.ends_with('_test.v')
is_script := dir.ends_with('.v') is_script := dir.ends_with('.v')
if is_script && !os.file_exists(dir) { if is_script && !os.file_exists(dir) {
exit('`$dir` does not exist') println('`$dir` does not exist')
exit(1)
} }
// No -o provided? foo.v => foo // No -o provided? foo.v => foo
if out_name == 'a.out' && dir.ends_with('.v') { if out_name == 'a.out' && dir.ends_with('.v') {

View File

@ -456,14 +456,13 @@ fn (s mut Scanner) scan() ScanRes {
} }
fn (s &Scanner) error(msg string) { fn (s &Scanner) error(msg string) {
// println('!! SCANNER ERROR: $msg')
file := s.file_path.all_after('/') file := s.file_path.all_after('/')
println('panic: $file:${s.line_nr + 1}') println('panic: $file:${s.line_nr + 1}')
println(msg) println(msg)
// os.print_backtrace() // os.print_backtrace()
// println(file) // println(file)
// println(s.file_path) // println(s.file_path)
os.exit1(' ') exit(1)
} }
// println('array out of bounds $idx len=$a.len') // println('array out of bounds $idx len=$a.len')

View File

@ -634,7 +634,8 @@ fn (table &Table) cgen_name_type_pair(name, typ string) string {
else if typ.starts_with('fn (') { else if typ.starts_with('fn (') {
T := table.find_type(typ) T := table.find_type(typ)
if T.name == '' { if T.name == '' {
os.exit1('this should never happen') println('this should never happen')
exit(1)
} }
str_args := T.func.str_args(table) str_args := T.func.str_args(table)
return '$T.func.typ (*$name)( $str_args /*FFF*/ )' return '$T.func.typ (*$name)( $str_args /*FFF*/ )'

11
os/os.v
View File

@ -315,15 +315,8 @@ pub fn getenv(key string) string {
return tos2(s) return tos2(s)
} }
fn exit(reason string) { fn exit(code int) {
println('exit(): $reason') C.exit(code)
log(reason)
C.exit(0)
}
fn exit1(reason string) {
println('exit(): $reason')
C.exit(1)
} }
// `file_exists` returns true if `path` exists. // `file_exists` returns true if `path` exists.