repl: treat middle imports in a more forgiving way

pull/4922/head
Delyan Angelov 2020-05-16 22:03:28 +03:00
parent a3a19e899d
commit 62cb48d252
2 changed files with 38 additions and 21 deletions

View File

@ -12,13 +12,15 @@ import v.util
struct Repl {
mut:
indent int
in_func bool
line string
lines []string
temp_lines []string
functions_name []string
functions []string
indent int // indentation level
in_func bool // are we inside a new custom user function
line string // the current line entered by the user
//
imports []string // all the import statements
functions []string // all the user function declarations
functions_name []string // all the user function names
lines []string // all the other lines/statements
temp_lines []string // all the temporary expressions/printlns
}
fn (r mut Repl) checks() bool {
@ -58,6 +60,17 @@ fn (r &Repl) function_call(line string) bool {
return false
}
fn (r &Repl) current_source_code(should_add_temp_lines bool) string {
mut all_lines := []string{}
all_lines << r.imports
all_lines << r.functions
all_lines << r.lines
if should_add_temp_lines {
all_lines << r.temp_lines
}
return all_lines.join('\n')
}
fn repl_help() {
println(util.full_v_version())
println('
@ -143,16 +156,16 @@ fn run_repl(workdir string, vrepl_prefix string) {
}
r.line = ''
}
if r.line == 'debug_repl' {
eprintln('repl: $r')
continue
}
if r.line == 'reset' {
r = Repl{}
continue
}
if r.line == 'list' {
mut all_lines := []string{}
all_lines << r.functions
all_lines << r.lines
all_lines << r.temp_lines
source_code := all_lines.join('\n')
source_code := r.current_source_code(true)
println('//////////////////////////////////////////////////////////////////////////////////////')
println(source_code)
println('//////////////////////////////////////////////////////////////////////////////////////')
@ -162,7 +175,7 @@ fn run_repl(workdir string, vrepl_prefix string) {
// but don't add this print call to the `lines` array,
// so that it doesn't get called during the next print.
if r.line.starts_with('print') {
source_code := r.functions.join('\n') + r.lines.join('\n') + '\n' + r.line + '\n'
source_code := r.current_source_code(false) + '\n${r.line}\n'
os.write_file(file, source_code)
s := os.exec('"$vexe" -repl run $file') or {
rerror(err)
@ -195,10 +208,10 @@ fn run_repl(workdir string, vrepl_prefix string) {
temp_flag = true
}
mut temp_source_code := ''
if temp_line.starts_with('import') {
temp_source_code = r.functions.join('\n') + temp_line + '\n'
if temp_line.starts_with('import ') {
temp_source_code = '${temp_line}\n' + r.current_source_code(false)
} else {
temp_source_code = r.functions.join('\n') + r.lines.join('\n') + '\n' + r.temp_lines.join('\n') + '\n' + temp_line + '\n'
temp_source_code = r.current_source_code(true) + '\n${temp_line}\n'
}
os.write_file(temp_file, temp_source_code)
s := os.exec('"$vexe" -repl run $temp_file') or {
@ -212,11 +225,10 @@ fn run_repl(workdir string, vrepl_prefix string) {
}
r.temp_lines.delete(0)
}
if r.line.starts_with('import') {
mut lines := []string{cap: r.lines.len+1}
lines << r.line
lines << r.lines
r.lines = lines
if r.line.starts_with('import ') {
mut imports := r.imports
r.imports = [r.line]
r.imports << imports
} else {
r.lines << r.line
}

View File

@ -39,5 +39,10 @@ fn (p mut Parser) check_unused_imports() {
if output == '' {
return
}
if p.pref.is_repl {
// The REPL should be much more liberal, and should not warn about
// unused imports, because they probably will be in the next few lines...
return
}
eprintln('`$p.file_name` warning: the following imports were never used: $output')
}