tools: fix vrepl for import mod { f } (#24340)

This commit is contained in:
sudopacman 2025-04-28 16:01:48 +08:00 committed by GitHub
parent 880f2169b7
commit 146c766dc9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 21 deletions

View File

@ -23,20 +23,20 @@ mut:
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 last_output string // the last repl output
modules []string // all the import modules modules map[string][]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
includes []string // all the #include statements includes []string // all the #include statements
functions []string // all the user function declarations functions []string // all the user function declarations
functions_name []string // all the user function names functions_name []string // all the user function names
structs []string // all the struct definitions structs []string // all the struct definitions
enums []string // all the enum definitions enums []string // all the enum definitions
consts []string // all the const definitions consts []string // all the const definitions
types []string // all the type definitions types []string // all the type definitions
interfaces []string // all the interface definitions interfaces []string // all the interface definitions
lines []string // all the other lines/statements lines []string // all the other lines/statements
temp_lines []string // all the temporary expressions/printlns temp_lines []string // all the temporary expressions/printlns
vstartup_lines []string // lines in the `VSTARTUP` file vstartup_lines []string // lines in the `VSTARTUP` file
eval_func_lines []string // same line of the `VSTARTUP` file, but used to test fn type eval_func_lines []string // same line of the `VSTARTUP` file, but used to test fn type
} }
const is_stdin_a_pipe = os.is_atty(0) == 0 const is_stdin_a_pipe = os.is_atty(0) == 0
@ -91,7 +91,11 @@ fn new_repl(folder string) Repl {
skip_empty: true skip_empty: true
} }
folder: folder folder: folder
modules: ['os', 'time', 'math'] modules: {
'os': []
'time': []
'math': []
}
vstartup_lines: vstartup_source vstartup_lines: vstartup_source
// Test file used to check if a function as a void return or a value return. // Test file used to check if a function as a void return or a value return.
eval_func_lines: vstartup_source eval_func_lines: vstartup_source
@ -205,11 +209,18 @@ fn (r &Repl) is_function_call(line string) bool {
// to a sequence of V source code lines // to a sequence of V source code lines
fn (r &Repl) import_to_source_code() []string { fn (r &Repl) import_to_source_code() []string {
mut imports_line := []string{} mut imports_line := []string{}
for mod in r.modules { for mod, value in r.modules {
mut import_str := 'import ${mod}' mut import_str := 'import ${mod}'
if mod in r.alias { if mod in r.alias {
import_str += ' as ${r.alias[mod]}' import_str += ' as ${r.alias[mod]}'
} }
if value.len > 0 {
import_str += '{ '
for val in value {
import_str += '${val}, '
}
import_str += '}'
}
imports_line << endline_if_missed(import_str) imports_line << endline_if_missed(import_str)
} }
return imports_line return imports_line
@ -312,17 +323,25 @@ fn (mut r Repl) parse_import(line string) {
tokens := r.line.fields() tokens := r.line.fields()
// module name // module name
mod := tokens[1] mod := tokens[1]
if mod !in r.modules { // set alias
r.modules << mod
}
// Check if the import contains an alias
// import mod_name as alias_mod
if line.contains('as ') && tokens.len >= 4 { if line.contains('as ') && tokens.len >= 4 {
alias := tokens[3] alias := tokens[3]
if mod !in r.alias { if mod !in r.alias {
r.alias[mod] = alias r.alias[mod] = alias
} }
} }
// set value
if line.contains('{') && line.contains('}') {
values := line.split('{')[1].split('}')[0]
for value in values.split(',') {
r.modules[mod] << value
}
} else {
if mod !in r.modules {
r.modules[mod] = []string{}
}
}
} }
// clear the screen, then list source code // clear the screen, then list source code

View File

@ -0,0 +1,4 @@
import json { encode }
encode('123')
===output===
"123"