Skip to content

Commit 146c766

Browse files
authored
tools: fix vrepl for import mod { f } (#24340)
1 parent 880f216 commit 146c766

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

cmd/tools/vrepl.v

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ mut:
2323
folder string // the folder in which the repl will write its temporary source files
2424
last_output string // the last repl output
2525

26-
modules []string // all the import modules
27-
alias map[string]string // all the alias used in the import
28-
includes []string // all the #include statements
29-
functions []string // all the user function declarations
30-
functions_name []string // all the user function names
31-
structs []string // all the struct definitions
32-
enums []string // all the enum definitions
33-
consts []string // all the const definitions
34-
types []string // all the type definitions
35-
interfaces []string // all the interface definitions
36-
lines []string // all the other lines/statements
37-
temp_lines []string // all the temporary expressions/printlns
38-
vstartup_lines []string // lines in the `VSTARTUP` file
39-
eval_func_lines []string // same line of the `VSTARTUP` file, but used to test fn type
26+
modules map[string][]string // all the import modules
27+
alias map[string]string // all the alias used in the import
28+
includes []string // all the #include statements
29+
functions []string // all the user function declarations
30+
functions_name []string // all the user function names
31+
structs []string // all the struct definitions
32+
enums []string // all the enum definitions
33+
consts []string // all the const definitions
34+
types []string // all the type definitions
35+
interfaces []string // all the interface definitions
36+
lines []string // all the other lines/statements
37+
temp_lines []string // all the temporary expressions/printlns
38+
vstartup_lines []string // lines in the `VSTARTUP` file
39+
eval_func_lines []string // same line of the `VSTARTUP` file, but used to test fn type
4040
}
4141

4242
const is_stdin_a_pipe = os.is_atty(0) == 0
@@ -91,7 +91,11 @@ fn new_repl(folder string) Repl {
9191
skip_empty: true
9292
}
9393
folder: folder
94-
modules: ['os', 'time', 'math']
94+
modules: {
95+
'os': []
96+
'time': []
97+
'math': []
98+
}
9599
vstartup_lines: vstartup_source
96100
// Test file used to check if a function as a void return or a value return.
97101
eval_func_lines: vstartup_source
@@ -205,11 +209,18 @@ fn (r &Repl) is_function_call(line string) bool {
205209
// to a sequence of V source code lines
206210
fn (r &Repl) import_to_source_code() []string {
207211
mut imports_line := []string{}
208-
for mod in r.modules {
212+
for mod, value in r.modules {
209213
mut import_str := 'import ${mod}'
210214
if mod in r.alias {
211215
import_str += ' as ${r.alias[mod]}'
212216
}
217+
if value.len > 0 {
218+
import_str += '{ '
219+
for val in value {
220+
import_str += '${val}, '
221+
}
222+
import_str += '}'
223+
}
213224
imports_line << endline_if_missed(import_str)
214225
}
215226
return imports_line
@@ -312,17 +323,25 @@ fn (mut r Repl) parse_import(line string) {
312323
tokens := r.line.fields()
313324
// module name
314325
mod := tokens[1]
315-
if mod !in r.modules {
316-
r.modules << mod
317-
}
318-
// Check if the import contains an alias
319-
// import mod_name as alias_mod
326+
// set alias
320327
if line.contains('as ') && tokens.len >= 4 {
321328
alias := tokens[3]
322329
if mod !in r.alias {
323330
r.alias[mod] = alias
324331
}
325332
}
333+
334+
// set value
335+
if line.contains('{') && line.contains('}') {
336+
values := line.split('{')[1].split('}')[0]
337+
for value in values.split(',') {
338+
r.modules[mod] << value
339+
}
340+
} else {
341+
if mod !in r.modules {
342+
r.modules[mod] = []string{}
343+
}
344+
}
326345
}
327346

328347
// clear the screen, then list source code
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import json { encode }
2+
encode('123')
3+
===output===
4+
"123"

0 commit comments

Comments
 (0)