repl: implement list and reset commands

pull/4922/head
Delyan Angelov 2020-05-16 18:20:47 +00:00
parent 6855996cca
commit a3a19e899d
1 changed files with 19 additions and 2 deletions

View File

@ -62,6 +62,8 @@ fn repl_help() {
println(util.full_v_version())
println('
help Displays this information.
list Show the program so far.
reset Clears the accumulated program, so you can start a fresh.
Ctrl-C, Ctrl-D, exit Exits the REPL.
clear Clears the screen.
')
@ -104,10 +106,10 @@ fn run_repl(workdir string, vrepl_prefix string) {
oline := readline.read_line(prompt) or {
break
}
if oline.trim_space() == '' && oline.ends_with('\n') {
line := oline.trim_space()
if line == '' && oline.ends_with('\n') {
continue
}
line := oline.trim_space()
if line.len <= -1 || line == '' || line == 'exit' {
break
}
@ -141,6 +143,21 @@ fn run_repl(workdir string, vrepl_prefix string) {
}
r.line = ''
}
if r.line == 'reset' {
r = Repl{}
continue
}
if r.line == 'list' {
mut all_lines := []string{}
all_lines << r.functions
all_lines << r.lines
all_lines << r.temp_lines
source_code := all_lines.join('\n')
println('//////////////////////////////////////////////////////////////////////////////////////')
println(source_code)
println('//////////////////////////////////////////////////////////////////////////////////////')
continue
}
// Save the source only if the user is printing something,
// but don't add this print call to the `lines` array,
// so that it doesn't get called during the next print.