1 | // Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved. |
2 | // Use of this source code is governed by an MIT license |
3 | // that can be found in the LICENSE file. |
4 | module main |
5 | |
6 | import os |
7 | import term |
8 | import rand |
9 | import readline |
10 | import os.cmdline |
11 | import v.util.version |
12 | |
13 | struct Repl { |
14 | mut: |
15 | readline readline.Readline |
16 | indent int // indentation level |
17 | in_func bool // are we inside a new custom user function |
18 | line string // the current line entered by the user |
19 | is_pin bool // does the repl 'pin' entered source code |
20 | folder string // the folder in which the repl will write its temporary source files |
21 | last_output string // the last repl output |
22 | // |
23 | modules []string // all the import modules |
24 | alias map[string]string // all the alias used in the import |
25 | includes []string // all the #include statements |
26 | functions []string // all the user function declarations |
27 | functions_name []string // all the user function names |
28 | lines []string // all the other lines/statements |
29 | temp_lines []string // all the temporary expressions/printlns |
30 | vstartup_lines []string // lines in the `VSTARTUP` file |
31 | eval_func_lines []string // same line of the `VSTARTUP` file, but used to test fn type |
32 | } |
33 | |
34 | const ( |
35 | is_stdin_a_pipe = os.is_atty(0) == 0 |
36 | vexe = os.getenv('VEXE') |
37 | vstartup = os.getenv('VSTARTUP') |
38 | repl_folder = os.join_path(os.vtmp_dir(), 'v', 'repl') |
39 | ) |
40 | |
41 | enum FnType { |
42 | @none |
43 | void |
44 | fn_type |
45 | } |
46 | |
47 | fn new_repl(folder string) Repl { |
48 | vstartup_source := os.read_file(vstartup) or { '' }.trim_right('\n\r').split_into_lines() |
49 | os.mkdir_all(folder) or {} |
50 | return Repl{ |
51 | readline: readline.Readline{ |
52 | skip_empty: true |
53 | } |
54 | folder: folder |
55 | modules: ['os', 'time', 'math'] |
56 | vstartup_lines: vstartup_source |
57 | // Test file used to check if a function as a void return or a value return. |
58 | eval_func_lines: vstartup_source |
59 | } |
60 | } |
61 | |
62 | fn endline_if_missed(line string) string { |
63 | if line.ends_with('\n') { |
64 | return line |
65 | } |
66 | return line + '\n' |
67 | } |
68 | |
69 | fn repl_help() { |
70 | println(version.full_v_version(false)) |
71 | println(' |
72 | |help Displays this information. |
73 | |list Show the program so far. |
74 | |reset Clears the accumulated program, so you can start a fresh. |
75 | |Ctrl-C, Ctrl-D, exit Exits the REPL. |
76 | |clear Clears the screen. |
77 | |pin Pins the entered program to the top. |
78 | '.strip_margin()) |
79 | } |
80 | |
81 | fn (mut r Repl) checks() bool { |
82 | mut in_string := false |
83 | was_indent := r.indent > 0 |
84 | for i := 0; i < r.line.len; i++ { |
85 | if r.line[i] == `'` && (i == 0 || r.line[i - 1] != `\\`) { |
86 | in_string = !in_string |
87 | } |
88 | if r.line[i] == `{` && !in_string { |
89 | r.line = r.line[..i + 1] + '\n' + r.line[i + 1..] |
90 | i++ |
91 | r.indent++ |
92 | } |
93 | if r.line[i] == `}` && !in_string { |
94 | r.line = r.line[..i] + '\n' + r.line[i..] |
95 | i++ |
96 | r.indent-- |
97 | if r.indent == 0 { |
98 | r.in_func = false |
99 | } |
100 | } |
101 | if i + 2 < r.line.len && r.indent == 0 && r.line[i + 1] == `f` && r.line[i + 2] == `n` { |
102 | r.in_func = true |
103 | } |
104 | } |
105 | return r.in_func || (was_indent && r.indent <= 0) || r.indent > 0 |
106 | } |
107 | |
108 | fn (r &Repl) function_call(line string) (bool, FnType) { |
109 | for function in r.functions_name { |
110 | is_function_definition := line.replace(' ', '').starts_with('${function}:=') |
111 | if line.starts_with(function) && !is_function_definition { |
112 | // TODO(vincenzopalazzo) store the type of the function here |
113 | fntype := r.check_fn_type_kind(line) |
114 | return true, fntype |
115 | } |
116 | } |
117 | |
118 | if line.contains(':=') { |
119 | // an assignment to a variable: |
120 | // `z := abc()` |
121 | return false, FnType.@none |
122 | } |
123 | |
124 | // Check if it is a Vlib call |
125 | // TODO(vincenzopalazzo): auto import the module? |
126 | if r.is_function_call(line) { |
127 | fntype := r.check_fn_type_kind(line) |
128 | return true, fntype |
129 | } |
130 | return false, FnType.@none |
131 | } |
132 | |
133 | // TODO(vincenzopalazzo) Remove this fancy check and add a regex |
134 | fn (r &Repl) is_function_call(line string) bool { |
135 | return !line.starts_with('[') && line.contains('.') && line.contains('(') |
136 | && (line.ends_with(')') || line.ends_with('?') || line.ends_with('!')) |
137 | } |
138 | |
139 | // Convert the list of modules that we parsed already, |
140 | // to a sequence of V source code lines |
141 | fn (r &Repl) import_to_source_code() []string { |
142 | mut imports_line := []string{} |
143 | for mod in r.modules { |
144 | mut import_str := 'import ${mod}' |
145 | if mod in r.alias { |
146 | import_str += ' as ${r.alias[mod]}' |
147 | } |
148 | imports_line << endline_if_missed(import_str) |
149 | } |
150 | return imports_line |
151 | } |
152 | |
153 | fn (r &Repl) current_source_code(should_add_temp_lines bool, not_add_print bool) string { |
154 | mut all_lines := []string{} |
155 | all_lines.insert(0, r.import_to_source_code()) |
156 | |
157 | if vstartup != '' { |
158 | mut lines := []string{} |
159 | if !not_add_print { |
160 | lines = r.vstartup_lines.filter(!it.starts_with('print')) |
161 | } else { |
162 | lines = r.vstartup_lines.clone() |
163 | } |
164 | all_lines << lines |
165 | } |
166 | all_lines << r.includes |
167 | all_lines << r.functions |
168 | all_lines << r.lines |
169 | |
170 | if should_add_temp_lines { |
171 | all_lines << r.temp_lines |
172 | } |
173 | return all_lines.join('\n') |
174 | } |
175 | |
176 | // the new_line is probably a function call, but some function calls |
177 | // do not return anything, while others return results. |
178 | // This function checks which one we have: |
179 | fn (r &Repl) check_fn_type_kind(new_line string) FnType { |
180 | source_code := r.current_source_code(true, false) + '\nprintln(${new_line})' |
181 | check_file := os.join_path(r.folder, '${rand.ulid()}.vrepl.check.v') |
182 | os.write_file(check_file, source_code) or { panic(err) } |
183 | defer { |
184 | os.rm(check_file) or {} |
185 | } |
186 | // -w suppresses the unused import warnings |
187 | // -check just does syntax and checker analysis without generating/running code |
188 | os_response := os.execute('${os.quoted_path(vexe)} -w -check ${os.quoted_path(check_file)}') |
189 | str_response := convert_output(os_response.output) |
190 | if os_response.exit_code != 0 && str_response.contains('can not print void expressions') { |
191 | return FnType.void |
192 | } |
193 | return FnType.fn_type |
194 | } |
195 | |
196 | // parse the import statement in `line`, updating the Repl alias maps |
197 | fn (mut r Repl) parse_import(line string) { |
198 | if !line.contains('import') { |
199 | eprintln("the line doesn't contain an `import` keyword") |
200 | return |
201 | } |
202 | tokens := r.line.fields() |
203 | // module name |
204 | mod := tokens[1] |
205 | if mod !in r.modules { |
206 | r.modules << mod |
207 | } |
208 | // Check if the import contains an alias |
209 | // import mod_name as alias_mod |
210 | if line.contains('as ') && tokens.len >= 4 { |
211 | alias := tokens[3] |
212 | if mod !in r.alias { |
213 | r.alias[mod] = alias |
214 | } |
215 | } |
216 | } |
217 | |
218 | // clear the screen, then list source code |
219 | fn (mut r Repl) pin() { |
220 | term.erase_clear() |
221 | r.list_source() |
222 | } |
223 | |
224 | // print source code |
225 | fn (mut r Repl) list_source() { |
226 | source_code := r.current_source_code(true, true) |
227 | println('\n${source_code.replace('\n\n', '\n')}') |
228 | } |
229 | |
230 | fn highlight_console_command(command string) string { |
231 | return term.bright_white(term.bright_bg_black(' ${command} ')) |
232 | } |
233 | |
234 | fn highlight_repl_command(command string) string { |
235 | return term.bright_white(term.bg_blue(' ${command} ')) |
236 | } |
237 | |
238 | fn print_welcome_screen() { |
239 | cmd_exit := highlight_repl_command('exit') |
240 | cmd_list := highlight_repl_command('list') |
241 | cmd_help := highlight_repl_command('help') |
242 | cmd_v_help := highlight_console_command('v help') |
243 | cmd_v_run := highlight_console_command('v run main.v') |
244 | file_main := highlight_console_command('main.v') |
245 | vbar := term.bright_green('|') |
246 | width, _ := term.get_terminal_size() // get the size of the terminal |
247 | vlogo := [ |
248 | term.bright_blue(r' ____ ____ '), |
249 | term.bright_blue(r' \ \ / / '), |
250 | term.bright_blue(r' \ \/ / '), |
251 | term.bright_blue(r' \ / '), |
252 | term.bright_blue(r' \ / '), |
253 | term.bright_blue(r' \__/ '), |
254 | ] |
255 | help_text := [ |
256 | 'Welcome to the V REPL (for help with V itself, type ${cmd_exit}, then run ${cmd_v_help}).', |
257 | 'Note: the REPL is highly experimental. For best V experience, use a text editor, ', |
258 | 'save your code in a ${file_main} file and execute: ${cmd_v_run}', |
259 | '${version.full_v_version(false)} . Use ${cmd_list} to see the accumulated program so far.', |
260 | 'Use Ctrl-C or ${cmd_exit} to exit, or ${cmd_help} to see other available commands.', |
261 | ] |
262 | if width >= 97 { |
263 | eprintln('${vlogo[0]}') |
264 | eprintln('${vlogo[1]} ${vbar} ${help_text[0]}') |
265 | eprintln('${vlogo[2]} ${vbar} ${help_text[1]}') |
266 | eprintln('${vlogo[3]} ${vbar} ${help_text[2]}') |
267 | eprintln('${vlogo[4]} ${vbar} ${help_text[3]}') |
268 | eprintln('${vlogo[5]} ${vbar} ${help_text[4]}') |
269 | eprintln('') |
270 | } else { |
271 | if width >= 14 { |
272 | left_margin := ' '.repeat(int(width / 2 - 7)) |
273 | for l in vlogo { |
274 | println(left_margin + l) |
275 | } |
276 | } |
277 | println(help_text.join('\n')) |
278 | } |
279 | } |
280 | |
281 | fn run_repl(workdir string, vrepl_prefix string) int { |
282 | if !is_stdin_a_pipe { |
283 | print_welcome_screen() |
284 | } |
285 | |
286 | if vstartup != '' { |
287 | result := repl_run_vfile(vstartup) or { |
288 | os.Result{ |
289 | output: '${vstartup} file not found' |
290 | } |
291 | } |
292 | print('\n') |
293 | print_output(result.output) |
294 | } |
295 | temp_file := os.join_path(workdir, '.${vrepl_prefix}vrepl_temp.v') |
296 | mut prompt := '>>> ' |
297 | defer { |
298 | if !is_stdin_a_pipe { |
299 | println('') |
300 | } |
301 | cleanup_files(temp_file) |
302 | } |
303 | mut r := new_repl(workdir) |
304 | for { |
305 | if r.indent == 0 { |
306 | prompt = '>>> ' |
307 | } else { |
308 | prompt = '... ' |
309 | } |
310 | oline := r.get_one_line(prompt) or { break } |
311 | line := oline.trim_space() |
312 | if line == '' && oline.ends_with('\n') { |
313 | continue |
314 | } |
315 | if line.len <= -1 || line == 'exit' { |
316 | break |
317 | } |
318 | if exit_pos := line.index('exit') { |
319 | oparen := line[(exit_pos + 4)..].trim_space() |
320 | if oparen.starts_with('(') { |
321 | if closing := oparen.index(')') { |
322 | rc := oparen[1..closing].parse_int(0, 8) or { panic(err) } |
323 | return int(rc) |
324 | } |
325 | } |
326 | } |
327 | r.line = line |
328 | if r.line == '\n' { |
329 | continue |
330 | } |
331 | if r.line == 'clear' { |
332 | term.erase_clear() |
333 | continue |
334 | } |
335 | if r.line == 'help' { |
336 | repl_help() |
337 | continue |
338 | } |
339 | if r.line.contains(':=') && r.line.contains('fn(') { |
340 | r.in_func = true |
341 | r.functions_name << r.line.all_before(':= fn(').trim_space() |
342 | } |
343 | if r.line.starts_with('fn') { |
344 | r.in_func = true |
345 | r.functions_name << r.line.all_after('fn').all_before('(').trim_space() |
346 | } |
347 | was_func := r.in_func |
348 | if r.checks() { |
349 | for rline in r.line.split('\n') { |
350 | if r.in_func || was_func { |
351 | r.functions << rline |
352 | } else { |
353 | r.temp_lines << rline |
354 | } |
355 | } |
356 | if r.indent > 0 { |
357 | continue |
358 | } |
359 | r.line = '' |
360 | } |
361 | if r.line == 'debug_repl' { |
362 | eprintln('repl: ${r}') |
363 | continue |
364 | } |
365 | if r.line == 'reset' { |
366 | r = new_repl(workdir) |
367 | continue |
368 | } |
369 | if r.line == 'list' { |
370 | r.list_source() |
371 | continue |
372 | } |
373 | if r.line == 'pin' { |
374 | r.is_pin = !r.is_pin |
375 | if r.is_pin { |
376 | r.pin() |
377 | println('') |
378 | } |
379 | continue |
380 | } |
381 | // Save the source only if the user is printing something, |
382 | // but don't add this print call to the `lines` array, |
383 | // so that it doesn't get called during the next print. |
384 | if r.line.starts_with('=') { |
385 | r.line = 'println(' + r.line[1..] + ')' |
386 | } |
387 | if r.line.starts_with('print') { |
388 | source_code := r.current_source_code(false, false) + '\n${r.line}\n' |
389 | os.write_file(temp_file, source_code) or { panic(err) } |
390 | s := repl_run_vfile(temp_file) or { return 1 } |
391 | if s.output.len > r.last_output.len { |
392 | cur_line_output := s.output[r.last_output.len..] |
393 | print_output(cur_line_output) |
394 | } |
395 | } else { |
396 | mut temp_line := r.line |
397 | func_call, fntype := r.function_call(r.line) |
398 | filter_line := r.line.replace(r.line.find_between("'", "'"), '').replace(r.line.find_between('"', |
399 | '"'), '') |
400 | possible_statement_patterns := [ |
401 | '++', |
402 | '--', |
403 | '<<', |
404 | '//', |
405 | '/*', |
406 | 'fn ', |
407 | 'pub ', |
408 | 'mut ', |
409 | 'enum ', |
410 | 'const ', |
411 | 'struct ', |
412 | 'interface ', |
413 | 'import ', |
414 | '#include ', |
415 | 'for ', |
416 | 'or ', |
417 | 'insert(', |
418 | 'delete(', |
419 | 'prepend(', |
420 | 'sort(', |
421 | 'clear(', |
422 | 'trim(', |
423 | ' as ', |
424 | ] |
425 | mut is_statement := false |
426 | if filter_line.count('=') % 2 == 1 |
427 | && (filter_line.count('!=') + filter_line.count('>=') + filter_line.count('<=')) == 0 { |
428 | is_statement = true |
429 | } else { |
430 | for pattern in possible_statement_patterns { |
431 | if filter_line.contains(pattern) { |
432 | is_statement = true |
433 | break |
434 | } |
435 | } |
436 | } |
437 | // Note: starting a line with 2 spaces escapes the println heuristic |
438 | if oline.starts_with(' ') { |
439 | is_statement = true |
440 | } |
441 | if !is_statement && (!func_call || fntype == FnType.fn_type) && r.line != '' { |
442 | temp_line = 'println(${r.line})' |
443 | source_code := r.current_source_code(false, false) + '\n${temp_line}\n' |
444 | os.write_file(temp_file, source_code) or { panic(err) } |
445 | s := repl_run_vfile(temp_file) or { return 1 } |
446 | if s.output.len > r.last_output.len { |
447 | cur_line_output := s.output[r.last_output.len..] |
448 | print_output(cur_line_output) |
449 | } |
450 | continue |
451 | } |
452 | mut temp_source_code := '' |
453 | if temp_line.starts_with('import ') { |
454 | mod := r.line.fields()[1] |
455 | if mod !in r.modules { |
456 | temp_source_code = '${temp_line}\n' + r.current_source_code(false, true) |
457 | } |
458 | } else if temp_line.starts_with('#include ') { |
459 | temp_source_code = '${temp_line}\n' + r.current_source_code(false, false) |
460 | } else { |
461 | for i, l in r.lines { |
462 | if (l.starts_with('for ') || l.starts_with('if ')) && l.contains('println') { |
463 | r.lines.delete(i) |
464 | break |
465 | } |
466 | } |
467 | temp_source_code = r.current_source_code(true, false) + '\n${temp_line}\n' |
468 | } |
469 | os.write_file(temp_file, temp_source_code) or { panic(err) } |
470 | s := repl_run_vfile(temp_file) or { return 1 } |
471 | if s.exit_code == 0 { |
472 | for r.temp_lines.len > 0 { |
473 | if !r.temp_lines[0].starts_with('print') { |
474 | r.lines << r.temp_lines[0] |
475 | } |
476 | r.temp_lines.delete(0) |
477 | } |
478 | if r.line.starts_with('import ') { |
479 | r.parse_import(r.line) |
480 | } else if r.line.starts_with('#include ') { |
481 | r.includes << r.line |
482 | } else { |
483 | r.lines << r.line |
484 | } |
485 | } else { |
486 | for r.temp_lines.len > 0 { |
487 | r.temp_lines.delete(0) |
488 | } |
489 | } |
490 | if r.is_pin { |
491 | r.pin() |
492 | println('') |
493 | } |
494 | if s.output.len > r.last_output.len { |
495 | len := r.last_output.len |
496 | if s.exit_code == 0 { |
497 | r.last_output = s.output.clone() |
498 | } |
499 | cur_line_output := s.output[len..] |
500 | print_output(cur_line_output) |
501 | } |
502 | } |
503 | } |
504 | return 0 |
505 | } |
506 | |
507 | fn convert_output(os_result string) string { |
508 | lines := os_result.trim_right('\n\r').split_into_lines() |
509 | mut content := '' |
510 | for line in lines { |
511 | if line.contains('.vrepl_temp.v:') { |
512 | // Hide the temporary file name |
513 | sline := line.all_after('.vrepl_temp.v:') |
514 | idx := sline.index(' ') or { |
515 | content += endline_if_missed(sline) |
516 | return content |
517 | } |
518 | content += endline_if_missed(sline[idx + 1..]) |
519 | } else { |
520 | content += endline_if_missed(line) |
521 | } |
522 | } |
523 | return content |
524 | } |
525 | |
526 | fn print_output(os_result string) { |
527 | content := convert_output(os_result) |
528 | print(content) |
529 | } |
530 | |
531 | fn main() { |
532 | // Support for the parameters replfolder and replprefix is needed |
533 | // so that the repl can be launched in parallel by several different |
534 | // threads by the REPL test runner. |
535 | args := cmdline.options_after(os.args, ['repl']) |
536 | replfolder := os.real_path(cmdline.option(args, '-replfolder', repl_folder)) |
537 | replprefix := cmdline.option(args, '-replprefix', 'noprefix.${rand.ulid()}.') |
538 | if !os.exists(os.getenv('VEXE')) { |
539 | println('Usage:') |
540 | println(' VEXE=vexepath vrepl\n') |
541 | println(' ... where vexepath is the full path to the v executable file') |
542 | return |
543 | } |
544 | if !is_stdin_a_pipe { |
545 | os.setenv('VCOLORS', 'always', true) |
546 | } |
547 | exit(run_repl(replfolder, replprefix)) |
548 | } |
549 | |
550 | fn rerror(s string) { |
551 | println('V repl error: ${s}') |
552 | os.flush() |
553 | } |
554 | |
555 | fn (mut r Repl) get_one_line(prompt string) ?string { |
556 | if is_stdin_a_pipe { |
557 | iline := os.get_raw_line() |
558 | if iline.len == 0 { |
559 | return none |
560 | } |
561 | return iline |
562 | } |
563 | rline := r.readline.read_line(prompt) or { return none } |
564 | return rline |
565 | } |
566 | |
567 | fn cleanup_files(file string) { |
568 | os.rm(file) or {} |
569 | $if windows { |
570 | os.rm(file[..file.len - 2] + '.exe') or {} |
571 | $if msvc { |
572 | os.rm(file[..file.len - 2] + '.ilk') or {} |
573 | os.rm(file[..file.len - 2] + '.pdb') or {} |
574 | } |
575 | } $else { |
576 | os.rm(file[..file.len - 2]) or {} |
577 | } |
578 | } |
579 | |
580 | fn repl_run_vfile(file string) !os.Result { |
581 | $if trace_repl_temp_files ? { |
582 | eprintln('>> repl_run_vfile file: ${file}') |
583 | } |
584 | s := os.execute('${os.quoted_path(vexe)} -repl run ${os.quoted_path(file)}') |
585 | if s.exit_code < 0 { |
586 | rerror(s.output) |
587 | return error(s.output) |
588 | } |
589 | return s |
590 | } |