repl: fix an issue with print and println after the execution of for or if (fix #20524) (#20525)

This commit is contained in:
Vinicius Silva 2024-01-14 06:37:49 -03:00 committed by GitHub
parent fd19458ca7
commit 175ede54b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 21 additions and 37 deletions

View file

@ -477,23 +477,13 @@ fn run_repl(workdir string, vrepl_prefix string) int {
} else if temp_line.starts_with('#include ') {
temp_source_code = '${temp_line}\n' + r.current_source_code(false, false)
} else {
for i, l in r.lines {
if (l.starts_with('for ') || l.starts_with('if ')) && l.contains('println') {
r.lines.delete(i)
break
}
}
temp_source_code = r.current_source_code(true, false) + '\n${temp_line}\n'
}
os.write_file(temp_file, temp_source_code) or { panic(err) }
s := repl_run_vfile(temp_file) or { return 1 }
if s.exit_code == 0 {
for r.temp_lines.len > 0 {
if !r.temp_lines[0].starts_with('print') {
r.lines << r.temp_lines[0]
}
r.temp_lines.delete(0)
}
r.lines << r.temp_lines
r.temp_lines.clear()
if r.line.starts_with('import ') {
r.parse_import(r.line)
} else if r.line.starts_with('#include ') {
@ -600,7 +590,7 @@ fn repl_run_vfile(file string) !os.Result {
$if trace_repl_temp_files ? {
eprintln('>> repl_run_vfile file: ${file}')
}
s := os.execute('${os.quoted_path(vexe)} -repl run ${os.quoted_path(file)}')
s := os.execute('${os.quoted_path(vexe)} -message-limit 1 -repl run ${os.quoted_path(file)}')
if s.exit_code < 0 {
rerror(s.output)
return error(s.output)

View file

@ -5,8 +5,3 @@ error: undefined ident: `a`
6 |
7 | println(a)
| ^
error: `println` can not print void expressions
5 | import math
6 |
7 | println(a)
| ~~~~~~~~~~

View file

@ -7,19 +7,9 @@ error: undefined ident: `a` (use `:=` to declare a variable)
6 |
7 | a = 3
| ^
error: cannot assign to `a`: expected `void`, not `int literal`
5 | import math
6 |
7 | a = 3
| ^
error: undefined ident: `b`
5 | import math
6 |
7 | println(b)
| ^
error: `println` can not print void expressions
5 | import math
6 |
7 | println(b)
| ~~~~~~~~~~
[4]

View file

@ -5,8 +5,3 @@ error: undefined ident: `exitasdfasdf`
6 |
7 | println(exitasdfasdf)
| ~~~~~~~~~~~~
error: `println` can not print void expressions
5 | import math
6 |
7 | println(exitasdfasdf)
| ~~~~~~~~~~~~~~~~~~~~~

View file

@ -1,4 +0,0 @@
fn test() { println('foo') } test() fn test2(a int) { println(a) } test2(42)
===output===
foo
42

View file

@ -0,0 +1,18 @@
mut numbers := [1,2,3,4,5]
if 1 in numbers {
println('yes')
}
println('hi')
for number in numbers {
println(number)
}
println(numbers)
===output===
yes
hi
1
2
3
4
5
[1, 2, 3, 4, 5]