vrepl: fix import and comment error in the middle of other lines

pull/4783/head
yuyi 2020-05-08 14:34:59 +08:00 committed by GitHub
parent 621429561c
commit 8866773f97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 23 deletions

View File

@ -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)
}

View File

@ -0,0 +1,6 @@
mut a := [1,2,3] // comment
// test comment
a << 4
a
===output===
[1, 2, 3, 4]

View File

@ -0,0 +1,6 @@
mut a := [1,2,3]
a << 4
import time
time.now().unix_time() > 160000
===output===
true