mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
vrepl: fix import and comment error in the middle of other lines
This commit is contained in:
parent
621429561c
commit
8866773f97
@ -61,7 +61,7 @@ fn (r &Repl) function_call(line string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn repl_help() {
|
fn repl_help() {
|
||||||
println(util.full_v_version())
|
println(util.full_v_version())
|
||||||
println('
|
println('
|
||||||
help Displays this information.
|
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(util.full_v_version())
|
||||||
println('Use Ctrl-C or `exit` to exit')
|
println('Use Ctrl-C or `exit` to exit')
|
||||||
|
|
||||||
@ -101,8 +101,7 @@ pub fn run_repl(workdir string, vrepl_prefix string) []string {
|
|||||||
for {
|
for {
|
||||||
if r.indent == 0 {
|
if r.indent == 0 {
|
||||||
prompt = '>>> '
|
prompt = '>>> '
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
prompt = '... '
|
prompt = '... '
|
||||||
}
|
}
|
||||||
mut line := readline.read_line(prompt) or {
|
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)
|
os.write_file(file, source_code)
|
||||||
s := os.exec('"$vexe" -repl run $file') or {
|
s := os.exec('"$vexe" -repl run $file') or {
|
||||||
rerror(err)
|
rerror(err)
|
||||||
return []
|
return
|
||||||
}
|
}
|
||||||
print_output(s)
|
print_output(s)
|
||||||
} else {
|
} else {
|
||||||
@ -161,23 +160,24 @@ pub fn run_repl(workdir string, vrepl_prefix string) []string {
|
|||||||
mut temp_flag := false
|
mut temp_flag := false
|
||||||
func_call := r.function_call(r.line)
|
func_call := r.function_call(r.line)
|
||||||
filter_line := r.line.replace(r.line.find_between('\'', '\''), '').replace(r.line.find_between('"', '"'), '')
|
filter_line := r.line.replace(r.line.find_between('\'', '\''), '').replace(r.line.find_between('"', '"'), '')
|
||||||
if !(filter_line.contains(':') ||
|
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.contains('++') ||
|
filter_line.contains('//') || filter_line.contains('/*') ||
|
||||||
filter_line.contains('--') ||
|
filter_line.starts_with('import') || r.line == '') && !func_call {
|
||||||
filter_line.contains('<<') ||
|
|
||||||
filter_line.starts_with('import') ||
|
|
||||||
r.line == '') && !func_call {
|
|
||||||
temp_line = 'println($r.line)'
|
temp_line = 'println($r.line)'
|
||||||
temp_flag = true
|
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)
|
os.write_file(temp_file, temp_source_code)
|
||||||
s := os.exec('"$vexe" -repl run $temp_file') or {
|
s := os.exec('"$vexe" -repl run $temp_file') or {
|
||||||
println("SDFSDF")
|
|
||||||
rerror(err)
|
rerror(err)
|
||||||
return []
|
return
|
||||||
}
|
}
|
||||||
if !func_call && s.exit_code == 0 && !temp_flag {
|
if !func_call && s.exit_code == 0 && !temp_flag {
|
||||||
for r.temp_lines.len > 0 {
|
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.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 {
|
} else {
|
||||||
for r.temp_lines.len > 0 {
|
for r.temp_lines.len > 0 {
|
||||||
r.temp_lines.delete(0)
|
r.temp_lines.delete(0)
|
||||||
@ -195,7 +202,6 @@ pub fn run_repl(workdir string, vrepl_prefix string) []string {
|
|||||||
print_output(s)
|
print_output(s)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return r.lines
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_output(s os.Result) {
|
fn print_output(s os.Result) {
|
||||||
@ -225,20 +231,19 @@ fn main() {
|
|||||||
// so that the repl can be launched in parallel by several different
|
// so that the repl can be launched in parallel by several different
|
||||||
// threads by the REPL test runner.
|
// threads by the REPL test runner.
|
||||||
args := cmdline.options_after(os.args, ['repl'])
|
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.')
|
replprefix := cmdline.option(args, '-replprefix', 'noprefix.')
|
||||||
os.chdir( replfolder )
|
os.chdir(replfolder)
|
||||||
if !os.exists(os.getenv('VEXE')) {
|
if !os.exists(os.getenv('VEXE')) {
|
||||||
println('Usage:')
|
println('Usage:')
|
||||||
println(' VEXE=vexepath vrepl\n')
|
println(' VEXE=vexepath vrepl\n')
|
||||||
println(' ... where vexepath is the full path to the v executable file')
|
println(' ... where vexepath is the full path to the v executable file')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
run_repl( replfolder, replprefix )
|
run_repl(replfolder, replprefix)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rerror(s string) {
|
fn rerror(s string) {
|
||||||
println('V repl error: $s')
|
println('V repl error: $s')
|
||||||
os.flush()
|
os.flush()
|
||||||
exit(1)
|
|
||||||
}
|
}
|
||||||
|
6
vlib/v/tests/repl/comment.repl
Normal file
6
vlib/v/tests/repl/comment.repl
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
mut a := [1,2,3] // comment
|
||||||
|
// test comment
|
||||||
|
a << 4
|
||||||
|
a
|
||||||
|
===output===
|
||||||
|
[1, 2, 3, 4]
|
6
vlib/v/tests/repl/import_middle.repl
Normal file
6
vlib/v/tests/repl/import_middle.repl
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
mut a := [1,2,3]
|
||||||
|
a << 4
|
||||||
|
import time
|
||||||
|
time.now().unix_time() > 160000
|
||||||
|
===output===
|
||||||
|
true
|
Loading…
Reference in New Issue
Block a user