Skip to content

Commit

Permalink
repl: handle exit(n) (#13930)
Browse files Browse the repository at this point in the history
  • Loading branch information
trufae committed Apr 4, 2022
1 parent 92bfd9b commit 5369379
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
19 changes: 15 additions & 4 deletions cmd/tools/vrepl.v
Expand Up @@ -259,7 +259,7 @@ fn print_welcome_screen() {
}
}

fn run_repl(workdir string, vrepl_prefix string) {
fn run_repl(workdir string, vrepl_prefix string) int {
if !is_stdin_a_pipe {
print_welcome_screen()
}
Expand Down Expand Up @@ -297,6 +297,16 @@ fn run_repl(workdir string, vrepl_prefix string) {
if line.len <= -1 || line == '' || line == 'exit' {
break
}
if exit_pos := line.index('exit') {
oparen := line[(exit_pos + 4)..].trim_space()
if oparen.starts_with('(') {
if closing := oparen.index(')') {
rc := oparen[1..closing].parse_int(0, 8) or { panic(err) }
return int(rc)
}
}
break
}
r.line = line
if r.line == '\n' {
continue
Expand Down Expand Up @@ -353,7 +363,7 @@ fn run_repl(workdir string, vrepl_prefix string) {
if r.line.starts_with('print') {
source_code := r.current_source_code(false, false) + '\n$r.line\n'
os.write_file(file, source_code) or { panic(err) }
s := repl_run_vfile(file) or { return }
s := repl_run_vfile(file) or { return 1 }
print_output(s)
} else {
mut temp_line := r.line
Expand Down Expand Up @@ -423,7 +433,7 @@ fn run_repl(workdir string, vrepl_prefix string) {
temp_source_code = r.current_source_code(true, false) + '\n$temp_line\n'
}
os.write_file(temp_file, temp_source_code) or { panic(err) }
s := repl_run_vfile(temp_file) or { return }
s := repl_run_vfile(temp_file) or { return 1 }
if !func_call && s.exit_code == 0 && !temp_flag {
for r.temp_lines.len > 0 {
if !r.temp_lines[0].starts_with('print') {
Expand All @@ -446,6 +456,7 @@ fn run_repl(workdir string, vrepl_prefix string) {
print_output(s)
}
}
return 0
}

fn convert_output(os_result os.Result) string {
Expand Down Expand Up @@ -493,7 +504,7 @@ fn main() {
if !is_stdin_a_pipe {
os.setenv('VCOLORS', 'always', true)
}
run_repl(replfolder, replprefix)
exit(run_repl(replfolder, replprefix))
}

fn rerror(s string) {
Expand Down
2 changes: 1 addition & 1 deletion vlib/os/os.c.v
Expand Up @@ -203,7 +203,7 @@ pub fn mv(src string, dst string) ? {
} $else {
ret := C.rename(&char(src.str), &char(rdst.str))
if ret != 0 {
return error_with_code('failed to rename $src to $dst', int(ret))
return error_with_code('failed to rename $src to $dst', ret)
}
}
}
Expand Down

0 comments on commit 5369379

Please sign in to comment.