1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

vrepl: fix array method call errors (fix #15769) (#15786)

This commit is contained in:
yuyi 2022-09-17 15:12:01 +08:00 committed by GitHub
parent a3b60e6b55
commit 26443cf9fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 21 deletions

View File

@ -12,12 +12,13 @@ import v.util.version
struct Repl { struct Repl {
mut: mut:
readline readline.Readline readline readline.Readline
indent int // indentation level indent int // indentation level
in_func bool // are we inside a new custom user function in_func bool // are we inside a new custom user function
line string // the current line entered by the user line string // the current line entered by the user
is_pin bool // does the repl 'pin' entered source code is_pin bool // does the repl 'pin' entered source code
folder string // the folder in which the repl will write its temporary source files folder string // the folder in which the repl will write its temporary source files
last_output string // the last repl output
// //
modules []string // all the import modules modules []string // all the import modules
alias map[string]string // all the alias used in the import alias map[string]string // all the alias used in the import
@ -186,7 +187,7 @@ fn (r &Repl) check_fn_type_kind(new_line string) FnType {
// -w suppresses the unused import warnings // -w suppresses the unused import warnings
// -check just does syntax and checker analysis without generating/running code // -check just does syntax and checker analysis without generating/running code
os_response := os.execute('${os.quoted_path(vexe)} -w -check ${os.quoted_path(check_file)}') os_response := os.execute('${os.quoted_path(vexe)} -w -check ${os.quoted_path(check_file)}')
str_response := convert_output(os_response) str_response := convert_output(os_response.output)
if os_response.exit_code != 0 && str_response.contains('can not print void expressions') { if os_response.exit_code != 0 && str_response.contains('can not print void expressions') {
return FnType.void return FnType.void
} }
@ -290,7 +291,7 @@ fn run_repl(workdir string, vrepl_prefix string) int {
} }
} }
print('\n') print('\n')
print_output(result) print_output(result.output)
} }
file := os.join_path(workdir, '.${vrepl_prefix}vrepl.v') file := os.join_path(workdir, '.${vrepl_prefix}vrepl.v')
temp_file := os.join_path(workdir, '.${vrepl_prefix}vrepl_temp.v') temp_file := os.join_path(workdir, '.${vrepl_prefix}vrepl_temp.v')
@ -387,12 +388,11 @@ fn run_repl(workdir string, vrepl_prefix string) int {
} }
if r.line.starts_with('print') { if r.line.starts_with('print') {
source_code := r.current_source_code(false, false) + '\n$r.line\n' source_code := r.current_source_code(false, false) + '\n$r.line\n'
os.write_file(file, source_code) or { panic(err) } os.write_file(temp_file, source_code) or { panic(err) }
s := repl_run_vfile(file) or { return 1 } s := repl_run_vfile(temp_file) or { return 1 }
print_output(s) print_output(s.output)
} else { } else {
mut temp_line := r.line mut temp_line := r.line
mut temp_flag := false
func_call, fntype := r.function_call(r.line) func_call, fntype := 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('"',
'"'), '') '"'), '')
@ -438,7 +438,11 @@ fn run_repl(workdir string, vrepl_prefix string) int {
} }
if !is_statement && (!func_call || fntype == FnType.fn_type) && r.line != '' { if !is_statement && (!func_call || fntype == FnType.fn_type) && r.line != '' {
temp_line = 'println($r.line)' temp_line = 'println($r.line)'
temp_flag = true source_code := r.current_source_code(false, false) + '\n$temp_line\n'
os.write_file(temp_file, source_code) or { panic(err) }
s := repl_run_vfile(temp_file) or { return 1 }
print_output(s.output)
continue
} }
mut temp_source_code := '' mut temp_source_code := ''
if temp_line.starts_with('import ') { if temp_line.starts_with('import ') {
@ -459,7 +463,7 @@ fn run_repl(workdir string, vrepl_prefix string) int {
} }
os.write_file(temp_file, temp_source_code) or { panic(err) } os.write_file(temp_file, temp_source_code) or { panic(err) }
s := repl_run_vfile(temp_file) or { return 1 } s := repl_run_vfile(temp_file) or { return 1 }
if !func_call && s.exit_code == 0 && !temp_flag { if s.exit_code == 0 {
for r.temp_lines.len > 0 { for r.temp_lines.len > 0 {
if !r.temp_lines[0].starts_with('print') { if !r.temp_lines[0].starts_with('print') {
r.lines << r.temp_lines[0] r.lines << r.temp_lines[0]
@ -478,18 +482,23 @@ fn run_repl(workdir string, vrepl_prefix string) int {
r.temp_lines.delete(0) r.temp_lines.delete(0)
} }
} }
if r.is_pin && !temp_flag { if r.is_pin {
r.pin() r.pin()
println('') println('')
} }
print_output(s) if s.output.len > r.last_output.len {
len := r.last_output.len
r.last_output = s.output.clone()
cur_line_output := s.output[len..]
print_output(cur_line_output)
}
} }
} }
return 0 return 0
} }
fn convert_output(os_result os.Result) string { fn convert_output(os_result string) string {
lines := os_result.output.trim_right('\n\r').split_into_lines() lines := os_result.trim_right('\n\r').split_into_lines()
mut content := '' mut content := ''
for line in lines { for line in lines {
if line.contains('.vrepl_temp.v:') { if line.contains('.vrepl_temp.v:') {
@ -512,7 +521,7 @@ fn convert_output(os_result os.Result) string {
return content return content
} }
fn print_output(os_result os.Result) { fn print_output(os_result string) {
content := convert_output(os_result) content := convert_output(os_result)
print(content) print(content)
} }

View File

@ -0,0 +1,10 @@
mut arr := [1, 2, 3]
arr
arr.delete_last()
arr
arr.clear()
arr
===output===
[1, 2, 3]
[1, 2]
[]

View File

@ -1,6 +1,6 @@
println(a) println(a)
===output=== ===output===
.vrepl.v:7:9: error: undefined ident: `a` error: undefined ident: `a`
5 | import math 5 | import math
6 | 6 |
7 | println(a) 7 | println(a)

View File

@ -1,5 +1,9 @@
a a
33 33
===output=== ===output===
undefined: `a` error: undefined ident: `a`
5 | import math
6 |
7 | println(a)
| ^
33 33