From 8866773f97a82994ceec56586040f43e1ebaa942 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 8 May 2020 14:34:59 +0800 Subject: [PATCH] vrepl: fix import and comment error in the middle of other lines --- cmd/tools/vrepl.v | 51 +++++++++++++++------------- vlib/v/tests/repl/comment.repl | 6 ++++ vlib/v/tests/repl/import_middle.repl | 6 ++++ 3 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 vlib/v/tests/repl/comment.repl create mode 100644 vlib/v/tests/repl/import_middle.repl diff --git a/cmd/tools/vrepl.v b/cmd/tools/vrepl.v index e51b478f71..df56365887 100644 --- a/cmd/tools/vrepl.v +++ b/cmd/tools/vrepl.v @@ -61,7 +61,7 @@ fn (r &Repl) function_call(line string) bool { return false } -pub fn repl_help() { +fn repl_help() { println(util.full_v_version()) println(' help Displays this information. @@ -70,7 +70,7 @@ pub fn repl_help() { ') } -pub fn run_repl(workdir string, vrepl_prefix string) []string { +fn run_repl(workdir string, vrepl_prefix string) { println(util.full_v_version()) println('Use Ctrl-C or `exit` to exit') @@ -101,8 +101,7 @@ pub fn run_repl(workdir string, vrepl_prefix string) []string { for { if r.indent == 0 { prompt = '>>> ' - } - else { + } else { prompt = '... ' } mut line := readline.read_line(prompt) or { @@ -153,7 +152,7 @@ pub fn run_repl(workdir string, vrepl_prefix string) []string { os.write_file(file, source_code) s := os.exec('"$vexe" -repl run $file') or { rerror(err) - return [] + return } print_output(s) } else { @@ -161,23 +160,24 @@ pub fn run_repl(workdir string, vrepl_prefix string) []string { mut temp_flag := false func_call := r.function_call(r.line) filter_line := r.line.replace(r.line.find_between('\'', '\''), '').replace(r.line.find_between('"', '"'), '') - if !(filter_line.contains(':') || - filter_line.contains('=') || - filter_line.contains(',') || - filter_line.contains('++') || - filter_line.contains('--') || - filter_line.contains('<<') || - filter_line.starts_with('import') || - r.line == '') && !func_call { + if !(filter_line.contains(':') || filter_line.contains('=') || + filter_line.contains(',') || filter_line.contains('++') || + filter_line.contains('--') || filter_line.contains('<<') || + filter_line.contains('//') || filter_line.contains('/*') || + filter_line.starts_with('import') || r.line == '') && !func_call { temp_line = 'println($r.line)' temp_flag = true } - temp_source_code := r.functions.join('\n') + r.lines.join('\n') + '\n' + r.temp_lines.join('\n') + '\n' + temp_line + mut temp_source_code := '' + if temp_line.starts_with('import') { + temp_source_code = r.functions.join('\n') + temp_line + '\n' + } else { + temp_source_code = r.functions.join('\n') + r.lines.join('\n') + '\n' + r.temp_lines.join('\n') + '\n' + temp_line + '\n' + } os.write_file(temp_file, temp_source_code) s := os.exec('"$vexe" -repl run $temp_file') or { - println("SDFSDF") rerror(err) - return [] + return } if !func_call && s.exit_code == 0 && !temp_flag { for r.temp_lines.len > 0 { @@ -186,7 +186,14 @@ pub fn run_repl(workdir string, vrepl_prefix string) []string { } r.temp_lines.delete(0) } - r.lines << r.line + if r.line.starts_with('import') { + mut lines := []string{cap: r.lines.len+1} + lines << r.line + lines << r.lines + r.lines = lines + } else { + r.lines << r.line + } } else { for r.temp_lines.len > 0 { r.temp_lines.delete(0) @@ -195,7 +202,6 @@ pub fn run_repl(workdir string, vrepl_prefix string) []string { print_output(s) } } - return r.lines } fn print_output(s os.Result) { @@ -225,20 +231,19 @@ fn main() { // so that the repl can be launched in parallel by several different // threads by the REPL test runner. args := cmdline.options_after(os.args, ['repl']) - replfolder := os.real_path( cmdline.option(args, '-replfolder', '.') ) + replfolder := os.real_path(cmdline.option(args, '-replfolder', '.')) replprefix := cmdline.option(args, '-replprefix', 'noprefix.') - os.chdir( replfolder ) + os.chdir(replfolder) if !os.exists(os.getenv('VEXE')) { println('Usage:') println(' VEXE=vexepath vrepl\n') println(' ... where vexepath is the full path to the v executable file') return } - run_repl( replfolder, replprefix ) + run_repl(replfolder, replprefix) } -pub fn rerror(s string) { +fn rerror(s string) { println('V repl error: $s') os.flush() - exit(1) } diff --git a/vlib/v/tests/repl/comment.repl b/vlib/v/tests/repl/comment.repl new file mode 100644 index 0000000000..0850e31f50 --- /dev/null +++ b/vlib/v/tests/repl/comment.repl @@ -0,0 +1,6 @@ +mut a := [1,2,3] // comment +// test comment +a << 4 +a +===output=== +[1, 2, 3, 4] diff --git a/vlib/v/tests/repl/import_middle.repl b/vlib/v/tests/repl/import_middle.repl new file mode 100644 index 0000000000..ed70bea15a --- /dev/null +++ b/vlib/v/tests/repl/import_middle.repl @@ -0,0 +1,6 @@ +mut a := [1,2,3] +a << 4 +import time +time.now().unix_time() > 160000 +===output=== +true