2022-01-04 12:21:08 +03:00
|
|
|
// Copyright (c) 2019-2022 Alexander Medvednikov. All rights reserved.
|
2019-08-18 22:50:38 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
2019-10-31 19:01:04 +03:00
|
|
|
module main
|
2019-08-18 22:50:38 +03:00
|
|
|
|
2020-04-26 09:32:05 +03:00
|
|
|
import os
|
|
|
|
import term
|
2021-02-08 10:44:04 +03:00
|
|
|
import rand
|
2020-04-26 09:32:05 +03:00
|
|
|
import readline
|
|
|
|
import os.cmdline
|
2021-07-27 12:35:54 +03:00
|
|
|
import v.util.version
|
2019-08-18 22:50:38 +03:00
|
|
|
|
|
|
|
struct Repl {
|
|
|
|
mut:
|
2022-09-17 10:12:01 +03:00
|
|
|
readline readline.Readline
|
|
|
|
indent int // indentation level
|
|
|
|
in_func bool // are we inside a new custom user function
|
|
|
|
line string // the current line entered by the user
|
|
|
|
is_pin bool // does the repl 'pin' entered source code
|
|
|
|
folder string // the folder in which the repl will write its temporary source files
|
|
|
|
last_output string // the last repl output
|
2020-05-16 22:03:28 +03:00
|
|
|
//
|
2022-02-06 09:05:25 +03:00
|
|
|
modules []string // all the import modules
|
2022-02-11 16:03:14 +03:00
|
|
|
alias map[string]string // all the alias used in the import
|
2022-02-06 09:05:25 +03:00
|
|
|
includes []string // all the #include 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
|
|
|
|
vstartup_lines []string // lines in the `VSTARTUP` file
|
|
|
|
eval_func_lines []string // same line of the `VSTARTUP` file, but used to test fn type
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
|
|
|
|
2022-09-18 18:07:16 +03:00
|
|
|
const (
|
|
|
|
is_stdin_a_pipe = os.is_atty(0) == 0
|
|
|
|
vexe = os.getenv('VEXE')
|
|
|
|
vstartup = os.getenv('VSTARTUP')
|
2022-11-03 10:24:52 +03:00
|
|
|
repl_folder = os.join_path(os.vtmp_dir(), 'v', 'repl')
|
2022-09-18 18:07:16 +03:00
|
|
|
)
|
2022-09-16 04:56:19 +03:00
|
|
|
|
2022-02-06 09:05:25 +03:00
|
|
|
enum FnType {
|
|
|
|
@none
|
|
|
|
void
|
|
|
|
fn_type
|
|
|
|
}
|
|
|
|
|
2022-09-16 04:56:19 +03:00
|
|
|
fn new_repl(folder string) Repl {
|
|
|
|
vstartup_source := os.read_file(vstartup) or { '' }.trim_right('\n\r').split_into_lines()
|
|
|
|
os.mkdir_all(folder) or {}
|
2021-01-02 14:17:39 +03:00
|
|
|
return Repl{
|
2022-02-14 01:12:25 +03:00
|
|
|
readline: readline.Readline{
|
|
|
|
skip_empty: true
|
|
|
|
}
|
2022-09-16 04:56:19 +03:00
|
|
|
folder: folder
|
2020-10-11 10:16:49 +03:00
|
|
|
modules: ['os', 'time', 'math']
|
2022-09-16 04:56:19 +03:00
|
|
|
vstartup_lines: vstartup_source
|
|
|
|
// Test file used to check if a function as a void return or a value return.
|
|
|
|
eval_func_lines: vstartup_source
|
2022-02-06 09:05:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn endline_if_missed(line string) string {
|
|
|
|
if line.ends_with('\n') {
|
|
|
|
return line
|
2020-10-11 10:16:49 +03:00
|
|
|
}
|
2022-02-06 09:05:25 +03:00
|
|
|
return line + '\n'
|
|
|
|
}
|
|
|
|
|
|
|
|
fn repl_help() {
|
|
|
|
println(version.full_v_version(false))
|
|
|
|
println('
|
|
|
|
|help Displays this information.
|
|
|
|
|list Show the program so far.
|
|
|
|
|reset Clears the accumulated program, so you can start a fresh.
|
|
|
|
|Ctrl-C, Ctrl-D, exit Exits the REPL.
|
|
|
|
|clear Clears the screen.
|
2022-08-07 14:06:50 +03:00
|
|
|
|pin Pins the entered program to the top.
|
2022-02-06 09:05:25 +03:00
|
|
|
'.strip_margin())
|
2020-10-11 10:16:49 +03:00
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut r Repl) checks() bool {
|
2019-08-18 22:50:38 +03:00
|
|
|
mut in_string := false
|
|
|
|
was_indent := r.indent > 0
|
2019-10-06 17:21:10 +03:00
|
|
|
for i := 0; i < r.line.len; i++ {
|
2021-08-06 06:21:28 +03:00
|
|
|
if r.line[i] == `'` && (i == 0 || r.line[i - 1] != `\\`) {
|
2019-08-18 22:50:38 +03:00
|
|
|
in_string = !in_string
|
|
|
|
}
|
2019-10-06 17:21:10 +03:00
|
|
|
if r.line[i] == `{` && !in_string {
|
2019-10-27 10:03:15 +03:00
|
|
|
r.line = r.line[..i + 1] + '\n' + r.line[i + 1..]
|
2019-10-06 17:21:10 +03:00
|
|
|
i++
|
2019-08-18 22:50:38 +03:00
|
|
|
r.indent++
|
|
|
|
}
|
2019-10-06 17:21:10 +03:00
|
|
|
if r.line[i] == `}` && !in_string {
|
2019-10-27 10:03:15 +03:00
|
|
|
r.line = r.line[..i] + '\n' + r.line[i..]
|
2019-10-06 17:21:10 +03:00
|
|
|
i++
|
2019-08-18 22:50:38 +03:00
|
|
|
r.indent--
|
|
|
|
if r.indent == 0 {
|
|
|
|
r.in_func = false
|
|
|
|
}
|
|
|
|
}
|
2019-10-06 17:21:10 +03:00
|
|
|
if i + 2 < r.line.len && r.indent == 0 && r.line[i + 1] == `f` && r.line[i + 2] == `n` {
|
2019-08-18 22:50:38 +03:00
|
|
|
r.in_func = true
|
|
|
|
}
|
|
|
|
}
|
2020-05-11 07:15:08 +03:00
|
|
|
return r.in_func || (was_indent && r.indent <= 0) || r.indent > 0
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
|
|
|
|
2022-02-06 09:05:25 +03:00
|
|
|
fn (r &Repl) function_call(line string) (bool, FnType) {
|
2019-08-18 22:50:38 +03:00
|
|
|
for function in r.functions_name {
|
2022-11-15 16:53:13 +03:00
|
|
|
is_function_definition := line.replace(' ', '').starts_with('${function}:=')
|
2020-08-05 17:18:28 +03:00
|
|
|
if line.starts_with(function) && !is_function_definition {
|
2022-02-06 09:05:25 +03:00
|
|
|
// TODO(vincenzopalazzo) store the type of the function here
|
|
|
|
fntype := r.check_fn_type_kind(line)
|
|
|
|
return true, fntype
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-06 09:05:25 +03:00
|
|
|
|
|
|
|
if line.contains(':=') {
|
|
|
|
// an assignment to a variable:
|
|
|
|
// `z := abc()`
|
|
|
|
return false, FnType.@none
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if it is a Vlib call
|
|
|
|
// TODO(vincenzopalazzo): auto import the module?
|
|
|
|
if r.is_function_call(line) {
|
|
|
|
fntype := r.check_fn_type_kind(line)
|
|
|
|
return true, fntype
|
|
|
|
}
|
|
|
|
return false, FnType.@none
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(vincenzopalazzo) Remove this fancy check and add a regex
|
|
|
|
fn (r &Repl) is_function_call(line string) bool {
|
|
|
|
return !line.starts_with('[') && line.contains('.') && line.contains('(')
|
2022-10-16 09:28:57 +03:00
|
|
|
&& (line.ends_with(')') || line.ends_with('?') || line.ends_with('!'))
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
|
|
|
|
2022-02-11 16:03:14 +03:00
|
|
|
// Convert the list of modules that we parsed already,
|
|
|
|
// to a sequence of V source code lines
|
|
|
|
fn (r &Repl) import_to_source_code() []string {
|
|
|
|
mut imports_line := []string{}
|
2020-07-03 19:06:36 +03:00
|
|
|
for mod in r.modules {
|
2022-11-15 16:53:13 +03:00
|
|
|
mut import_str := 'import ${mod}'
|
2022-02-11 16:03:14 +03:00
|
|
|
if mod in r.alias {
|
|
|
|
import_str += ' as ${r.alias[mod]}'
|
|
|
|
}
|
|
|
|
imports_line << endline_if_missed(import_str)
|
2020-07-03 19:06:36 +03:00
|
|
|
}
|
2022-02-11 16:03:14 +03:00
|
|
|
return imports_line
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (r &Repl) current_source_code(should_add_temp_lines bool, not_add_print bool) string {
|
|
|
|
mut all_lines := []string{}
|
|
|
|
all_lines.insert(0, r.import_to_source_code())
|
|
|
|
|
2021-03-03 08:53:05 +03:00
|
|
|
if vstartup != '' {
|
|
|
|
mut lines := []string{}
|
|
|
|
if !not_add_print {
|
|
|
|
lines = r.vstartup_lines.filter(!it.starts_with('print'))
|
|
|
|
} else {
|
2022-08-25 08:52:13 +03:00
|
|
|
lines = r.vstartup_lines.clone()
|
2021-03-03 08:53:05 +03:00
|
|
|
}
|
|
|
|
all_lines << lines
|
|
|
|
}
|
2020-07-03 19:06:36 +03:00
|
|
|
all_lines << r.includes
|
2020-05-16 22:03:28 +03:00
|
|
|
all_lines << r.functions
|
|
|
|
all_lines << r.lines
|
2021-03-03 08:53:05 +03:00
|
|
|
|
2020-05-16 22:03:28 +03:00
|
|
|
if should_add_temp_lines {
|
|
|
|
all_lines << r.temp_lines
|
|
|
|
}
|
|
|
|
return all_lines.join('\n')
|
|
|
|
}
|
|
|
|
|
2022-02-06 09:05:25 +03:00
|
|
|
// the new_line is probably a function call, but some function calls
|
|
|
|
// do not return anything, while others return results.
|
|
|
|
// This function checks which one we have:
|
|
|
|
fn (r &Repl) check_fn_type_kind(new_line string) FnType {
|
2022-11-15 16:53:13 +03:00
|
|
|
source_code := r.current_source_code(true, false) + '\nprintln(${new_line})'
|
2022-09-16 04:56:19 +03:00
|
|
|
check_file := os.join_path(r.folder, '${rand.ulid()}.vrepl.check.v')
|
2022-02-06 09:05:25 +03:00
|
|
|
os.write_file(check_file, source_code) or { panic(err) }
|
|
|
|
defer {
|
|
|
|
os.rm(check_file) or {}
|
|
|
|
}
|
|
|
|
// -w suppresses the unused import warnings
|
|
|
|
// -check just does syntax and checker analysis without generating/running code
|
|
|
|
os_response := os.execute('${os.quoted_path(vexe)} -w -check ${os.quoted_path(check_file)}')
|
2022-09-17 10:12:01 +03:00
|
|
|
str_response := convert_output(os_response.output)
|
2022-02-06 09:05:25 +03:00
|
|
|
if os_response.exit_code != 0 && str_response.contains('can not print void expressions') {
|
|
|
|
return FnType.void
|
|
|
|
}
|
|
|
|
return FnType.fn_type
|
2019-08-22 05:36:24 +03:00
|
|
|
}
|
|
|
|
|
2022-02-11 16:03:14 +03:00
|
|
|
// parse the import statement in `line`, updating the Repl alias maps
|
|
|
|
fn (mut r Repl) parse_import(line string) {
|
|
|
|
if !line.contains('import') {
|
|
|
|
eprintln("the line doesn't contain an `import` keyword")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tokens := r.line.fields()
|
|
|
|
// module name
|
|
|
|
mod := tokens[1]
|
|
|
|
if mod !in r.modules {
|
|
|
|
r.modules << mod
|
|
|
|
}
|
|
|
|
// Check if the import contains an alias
|
|
|
|
// import mod_name as alias_mod
|
|
|
|
if line.contains('as ') && tokens.len >= 4 {
|
|
|
|
alias := tokens[3]
|
|
|
|
if mod !in r.alias {
|
|
|
|
r.alias[mod] = alias
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 14:06:50 +03:00
|
|
|
// clear the screen, then list source code
|
|
|
|
fn (mut r Repl) pin() {
|
|
|
|
term.erase_clear()
|
|
|
|
r.list_source()
|
|
|
|
}
|
|
|
|
|
|
|
|
// print source code
|
|
|
|
fn (mut r Repl) list_source() {
|
|
|
|
source_code := r.current_source_code(true, true)
|
|
|
|
println('\n${source_code.replace('\n\n', '\n')}')
|
|
|
|
}
|
|
|
|
|
2022-03-21 00:22:46 +03:00
|
|
|
fn highlight_console_command(command string) string {
|
2022-11-15 16:53:13 +03:00
|
|
|
return term.bright_white(term.bright_bg_black(' ${command} '))
|
2022-03-21 00:22:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn highlight_repl_command(command string) string {
|
2022-11-15 16:53:13 +03:00
|
|
|
return term.bright_white(term.bg_blue(' ${command} '))
|
2022-03-21 00:22:46 +03:00
|
|
|
}
|
|
|
|
|
2022-02-11 16:03:14 +03:00
|
|
|
fn print_welcome_screen() {
|
2022-03-21 00:22:46 +03:00
|
|
|
cmd_exit := highlight_repl_command('exit')
|
|
|
|
cmd_list := highlight_repl_command('list')
|
|
|
|
cmd_help := highlight_repl_command('help')
|
|
|
|
cmd_v_help := highlight_console_command('v help')
|
|
|
|
cmd_v_run := highlight_console_command('v run main.v')
|
|
|
|
file_main := highlight_console_command('main.v')
|
2022-02-13 12:41:36 +03:00
|
|
|
vbar := term.bright_green('|')
|
2022-02-14 17:18:20 +03:00
|
|
|
width, _ := term.get_terminal_size() // get the size of the terminal
|
2022-02-13 12:41:36 +03:00
|
|
|
vlogo := [
|
|
|
|
term.bright_blue(r' ____ ____ '),
|
|
|
|
term.bright_blue(r' \ \ / / '),
|
|
|
|
term.bright_blue(r' \ \/ / '),
|
|
|
|
term.bright_blue(r' \ / '),
|
|
|
|
term.bright_blue(r' \ / '),
|
|
|
|
term.bright_blue(r' \__/ '),
|
|
|
|
]
|
2022-02-14 17:18:20 +03:00
|
|
|
help_text := [
|
2022-11-15 16:53:13 +03:00
|
|
|
'Welcome to the V REPL (for help with V itself, type ${cmd_exit}, then run ${cmd_v_help}).',
|
2022-03-06 20:01:22 +03:00
|
|
|
'Note: the REPL is highly experimental. For best V experience, use a text editor, ',
|
2022-11-15 16:53:13 +03:00
|
|
|
'save your code in a ${file_main} file and execute: ${cmd_v_run}',
|
|
|
|
'${version.full_v_version(false)} . Use ${cmd_list} to see the accumulated program so far.',
|
|
|
|
'Use Ctrl-C or ${cmd_exit} to exit, or ${cmd_help} to see other available commands.',
|
2022-02-14 17:18:20 +03:00
|
|
|
]
|
|
|
|
if width >= 97 {
|
|
|
|
eprintln('${vlogo[0]}')
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('${vlogo[1]} ${vbar} ${help_text[0]}')
|
|
|
|
eprintln('${vlogo[2]} ${vbar} ${help_text[1]}')
|
|
|
|
eprintln('${vlogo[3]} ${vbar} ${help_text[2]}')
|
|
|
|
eprintln('${vlogo[4]} ${vbar} ${help_text[3]}')
|
|
|
|
eprintln('${vlogo[5]} ${vbar} ${help_text[4]}')
|
2022-02-14 17:18:20 +03:00
|
|
|
eprintln('')
|
|
|
|
} else {
|
|
|
|
if width >= 14 {
|
|
|
|
left_margin := ' '.repeat(int(width / 2 - 7))
|
|
|
|
for l in vlogo {
|
|
|
|
println(left_margin + l)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
println(help_text.join('\n'))
|
|
|
|
}
|
2022-02-11 16:03:14 +03:00
|
|
|
}
|
|
|
|
|
2022-04-04 16:22:34 +03:00
|
|
|
fn run_repl(workdir string, vrepl_prefix string) int {
|
2020-10-11 10:16:49 +03:00
|
|
|
if !is_stdin_a_pipe {
|
2022-02-11 16:03:14 +03:00
|
|
|
print_welcome_screen()
|
2020-10-11 10:16:49 +03:00
|
|
|
}
|
2021-03-03 08:53:05 +03:00
|
|
|
|
|
|
|
if vstartup != '' {
|
2021-03-04 13:24:14 +03:00
|
|
|
result := repl_run_vfile(vstartup) or {
|
|
|
|
os.Result{
|
2022-11-15 16:53:13 +03:00
|
|
|
output: '${vstartup} file not found'
|
2021-03-04 13:24:14 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-03 08:53:05 +03:00
|
|
|
print('\n')
|
2022-09-17 10:12:01 +03:00
|
|
|
print_output(result.output)
|
2021-03-03 08:53:05 +03:00
|
|
|
}
|
2020-03-09 04:23:34 +03:00
|
|
|
temp_file := os.join_path(workdir, '.${vrepl_prefix}vrepl_temp.v')
|
2020-02-16 14:42:28 +03:00
|
|
|
mut prompt := '>>> '
|
2019-08-18 22:50:38 +03:00
|
|
|
defer {
|
2020-10-11 10:16:49 +03:00
|
|
|
if !is_stdin_a_pipe {
|
|
|
|
println('')
|
|
|
|
}
|
2022-09-18 18:07:16 +03:00
|
|
|
cleanup_files(temp_file)
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
2022-09-16 04:56:19 +03:00
|
|
|
mut r := new_repl(workdir)
|
2019-08-18 22:50:38 +03:00
|
|
|
for {
|
|
|
|
if r.indent == 0 {
|
2019-10-31 19:01:04 +03:00
|
|
|
prompt = '>>> '
|
2020-05-08 09:34:59 +03:00
|
|
|
} else {
|
2019-10-31 19:01:04 +03:00
|
|
|
prompt = '... '
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
2020-12-27 13:41:48 +03:00
|
|
|
oline := r.get_one_line(prompt) or { break }
|
2020-05-16 21:20:47 +03:00
|
|
|
line := oline.trim_space()
|
|
|
|
if line == '' && oline.ends_with('\n') {
|
2019-08-18 22:50:38 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-10-31 19:01:04 +03:00
|
|
|
if line.len <= -1 || line == '' || line == 'exit' {
|
2019-08-18 22:50:38 +03:00
|
|
|
break
|
|
|
|
}
|
2022-04-04 16:22:34 +03:00
|
|
|
if exit_pos := line.index('exit') {
|
|
|
|
oparen := line[(exit_pos + 4)..].trim_space()
|
|
|
|
if oparen.starts_with('(') {
|
|
|
|
if closing := oparen.index(')') {
|
|
|
|
rc := oparen[1..closing].parse_int(0, 8) or { panic(err) }
|
|
|
|
return int(rc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-31 19:01:04 +03:00
|
|
|
r.line = line
|
2019-10-06 17:21:10 +03:00
|
|
|
if r.line == '\n' {
|
2019-08-18 22:50:38 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-10-06 17:21:10 +03:00
|
|
|
if r.line == 'clear' {
|
2020-12-27 14:43:36 +03:00
|
|
|
term.erase_clear()
|
2019-08-22 05:36:24 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-10-06 17:21:10 +03:00
|
|
|
if r.line == 'help' {
|
2019-08-22 05:36:24 +03:00
|
|
|
repl_help()
|
|
|
|
continue
|
|
|
|
}
|
2020-05-19 14:15:01 +03:00
|
|
|
if r.line.contains(':=') && r.line.contains('fn(') {
|
|
|
|
r.in_func = true
|
|
|
|
r.functions_name << r.line.all_before(':= fn(').trim_space()
|
|
|
|
}
|
2019-10-06 17:21:10 +03:00
|
|
|
if r.line.starts_with('fn') {
|
2019-08-18 22:50:38 +03:00
|
|
|
r.in_func = true
|
2019-10-06 17:21:10 +03:00
|
|
|
r.functions_name << r.line.all_after('fn').all_before('(').trim_space()
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
|
|
|
was_func := r.in_func
|
2019-10-06 17:21:10 +03:00
|
|
|
if r.checks() {
|
2019-12-14 19:58:55 +03:00
|
|
|
for rline in r.line.split('\n') {
|
2019-10-06 17:21:10 +03:00
|
|
|
if r.in_func || was_func {
|
2019-12-14 19:58:55 +03:00
|
|
|
r.functions << rline
|
2020-02-22 14:41:57 +03:00
|
|
|
} else {
|
2019-12-14 19:58:55 +03:00
|
|
|
r.temp_lines << rline
|
2019-10-06 17:21:10 +03:00
|
|
|
}
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
|
|
|
if r.indent > 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-06 17:21:10 +03:00
|
|
|
r.line = ''
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
2020-05-16 22:03:28 +03:00
|
|
|
if r.line == 'debug_repl' {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('repl: ${r}')
|
2020-05-16 22:03:28 +03:00
|
|
|
continue
|
|
|
|
}
|
2020-05-16 21:20:47 +03:00
|
|
|
if r.line == 'reset' {
|
2022-09-16 04:56:19 +03:00
|
|
|
r = new_repl(workdir)
|
2020-05-16 21:20:47 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if r.line == 'list' {
|
2022-08-07 14:06:50 +03:00
|
|
|
r.list_source()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if r.line == 'pin' {
|
|
|
|
r.is_pin = !r.is_pin
|
|
|
|
if r.is_pin {
|
|
|
|
r.pin()
|
|
|
|
println('')
|
|
|
|
}
|
2020-05-16 21:20:47 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-08-18 22:50:38 +03:00
|
|
|
// Save the source only if the user is printing something,
|
|
|
|
// but don't add this print call to the `lines` array,
|
|
|
|
// so that it doesn't get called during the next print.
|
2020-10-11 10:16:49 +03:00
|
|
|
if r.line.starts_with('=') {
|
2020-05-24 14:16:38 +03:00
|
|
|
r.line = 'println(' + r.line[1..] + ')'
|
|
|
|
}
|
2019-10-06 17:21:10 +03:00
|
|
|
if r.line.starts_with('print') {
|
2022-11-15 16:53:13 +03:00
|
|
|
source_code := r.current_source_code(false, false) + '\n${r.line}\n'
|
2022-09-17 10:12:01 +03:00
|
|
|
os.write_file(temp_file, source_code) or { panic(err) }
|
|
|
|
s := repl_run_vfile(temp_file) or { return 1 }
|
2022-09-17 15:58:53 +03:00
|
|
|
if s.output.len > r.last_output.len {
|
|
|
|
cur_line_output := s.output[r.last_output.len..]
|
|
|
|
print_output(cur_line_output)
|
|
|
|
}
|
2020-02-22 14:41:57 +03:00
|
|
|
} else {
|
2019-10-06 17:21:10 +03:00
|
|
|
mut temp_line := r.line
|
2022-02-06 09:05:25 +03:00
|
|
|
func_call, fntype := r.function_call(r.line)
|
2021-01-26 17:43:10 +03:00
|
|
|
filter_line := r.line.replace(r.line.find_between("'", "'"), '').replace(r.line.find_between('"',
|
2020-10-11 10:16:49 +03:00
|
|
|
'"'), '')
|
2020-05-16 14:40:54 +03:00
|
|
|
possible_statement_patterns := [
|
2020-10-11 10:16:49 +03:00
|
|
|
'++',
|
|
|
|
'--',
|
|
|
|
'<<',
|
|
|
|
'//',
|
|
|
|
'/*',
|
|
|
|
'fn ',
|
|
|
|
'pub ',
|
|
|
|
'mut ',
|
|
|
|
'enum ',
|
|
|
|
'const ',
|
|
|
|
'struct ',
|
|
|
|
'interface ',
|
|
|
|
'import ',
|
|
|
|
'#include ',
|
|
|
|
'for ',
|
|
|
|
'or ',
|
2022-06-02 19:59:57 +03:00
|
|
|
'insert(',
|
|
|
|
'delete(',
|
|
|
|
'prepend(',
|
|
|
|
'sort(',
|
|
|
|
'clear(',
|
|
|
|
'trim(',
|
|
|
|
' as ',
|
2020-05-16 14:40:54 +03:00
|
|
|
]
|
|
|
|
mut is_statement := false
|
2021-03-04 13:24:14 +03:00
|
|
|
if filter_line.count('=') % 2 == 1 {
|
|
|
|
is_statement = true
|
|
|
|
} else {
|
|
|
|
for pattern in possible_statement_patterns {
|
|
|
|
if filter_line.contains(pattern) {
|
|
|
|
is_statement = true
|
|
|
|
break
|
|
|
|
}
|
2020-05-16 14:40:54 +03:00
|
|
|
}
|
|
|
|
}
|
2022-03-06 20:01:22 +03:00
|
|
|
// Note: starting a line with 2 spaces escapes the println heuristic
|
2020-05-16 14:40:54 +03:00
|
|
|
if oline.starts_with(' ') {
|
|
|
|
is_statement = true
|
|
|
|
}
|
2022-02-06 09:05:25 +03:00
|
|
|
if !is_statement && (!func_call || fntype == FnType.fn_type) && r.line != '' {
|
2022-11-15 16:53:13 +03:00
|
|
|
temp_line = 'println(${r.line})'
|
|
|
|
source_code := r.current_source_code(false, false) + '\n${temp_line}\n'
|
2022-09-17 10:12:01 +03:00
|
|
|
os.write_file(temp_file, source_code) or { panic(err) }
|
|
|
|
s := repl_run_vfile(temp_file) or { return 1 }
|
2022-09-17 15:58:53 +03:00
|
|
|
if s.output.len > r.last_output.len {
|
|
|
|
cur_line_output := s.output[r.last_output.len..]
|
|
|
|
print_output(cur_line_output)
|
|
|
|
}
|
2022-09-17 10:12:01 +03:00
|
|
|
continue
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
2020-05-08 09:34:59 +03:00
|
|
|
mut temp_source_code := ''
|
2020-07-03 19:06:36 +03:00
|
|
|
if temp_line.starts_with('import ') {
|
|
|
|
mod := r.line.fields()[1]
|
|
|
|
if mod !in r.modules {
|
2022-11-15 16:53:13 +03:00
|
|
|
temp_source_code = '${temp_line}\n' + r.current_source_code(false, true)
|
2020-07-03 19:06:36 +03:00
|
|
|
}
|
|
|
|
} else if temp_line.starts_with('#include ') {
|
2022-11-15 16:53:13 +03:00
|
|
|
temp_source_code = '${temp_line}\n' + r.current_source_code(false, false)
|
2020-05-08 09:34:59 +03:00
|
|
|
} else {
|
2020-05-25 11:35:55 +03:00
|
|
|
for i, l in r.lines {
|
|
|
|
if (l.starts_with('for ') || l.starts_with('if ')) && l.contains('println') {
|
|
|
|
r.lines.delete(i)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-11-15 16:53:13 +03:00
|
|
|
temp_source_code = r.current_source_code(true, false) + '\n${temp_line}\n'
|
2020-05-08 09:34:59 +03:00
|
|
|
}
|
2021-03-01 02:18:14 +03:00
|
|
|
os.write_file(temp_file, temp_source_code) or { panic(err) }
|
2022-04-04 16:22:34 +03:00
|
|
|
s := repl_run_vfile(temp_file) or { return 1 }
|
2022-09-17 10:12:01 +03:00
|
|
|
if s.exit_code == 0 {
|
2019-08-18 22:50:38 +03:00
|
|
|
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)
|
|
|
|
}
|
2020-07-03 19:06:36 +03:00
|
|
|
if r.line.starts_with('import ') {
|
2022-02-11 16:03:14 +03:00
|
|
|
r.parse_import(r.line)
|
2020-07-03 19:06:36 +03:00
|
|
|
} else if r.line.starts_with('#include ') {
|
|
|
|
r.includes << r.line
|
2020-05-08 09:34:59 +03:00
|
|
|
} else {
|
|
|
|
r.lines << r.line
|
|
|
|
}
|
2020-02-22 14:41:57 +03:00
|
|
|
} else {
|
2019-08-18 22:50:38 +03:00
|
|
|
for r.temp_lines.len > 0 {
|
|
|
|
r.temp_lines.delete(0)
|
|
|
|
}
|
|
|
|
}
|
2022-09-17 10:12:01 +03:00
|
|
|
if r.is_pin {
|
2022-08-07 14:06:50 +03:00
|
|
|
r.pin()
|
|
|
|
println('')
|
|
|
|
}
|
2022-09-17 10:12:01 +03:00
|
|
|
if s.output.len > r.last_output.len {
|
|
|
|
len := r.last_output.len
|
2022-09-18 14:09:31 +03:00
|
|
|
if s.exit_code == 0 {
|
|
|
|
r.last_output = s.output.clone()
|
|
|
|
}
|
2022-09-17 10:12:01 +03:00
|
|
|
cur_line_output := s.output[len..]
|
|
|
|
print_output(cur_line_output)
|
|
|
|
}
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
2022-04-04 16:22:34 +03:00
|
|
|
return 0
|
2019-08-29 03:30:17 +03:00
|
|
|
}
|
2019-10-31 19:01:04 +03:00
|
|
|
|
2022-09-17 10:12:01 +03:00
|
|
|
fn convert_output(os_result string) string {
|
|
|
|
lines := os_result.trim_right('\n\r').split_into_lines()
|
2022-02-06 09:05:25 +03:00
|
|
|
mut content := ''
|
2019-11-13 20:14:30 +03:00
|
|
|
for line in lines {
|
2020-01-20 19:06:36 +03:00
|
|
|
if line.contains('.vrepl_temp.v:') {
|
2019-11-13 20:14:30 +03:00
|
|
|
// Hide the temporary file name
|
2020-01-20 19:06:36 +03:00
|
|
|
sline := line.all_after('.vrepl_temp.v:')
|
|
|
|
idx := sline.index(' ') or {
|
2022-02-06 09:05:25 +03:00
|
|
|
content += endline_if_missed(sline)
|
|
|
|
return content
|
2019-11-30 15:27:16 +03:00
|
|
|
}
|
2022-02-06 09:05:25 +03:00
|
|
|
content += endline_if_missed(sline[idx + 1..])
|
2020-01-20 19:06:36 +03:00
|
|
|
} else {
|
2022-02-06 09:05:25 +03:00
|
|
|
content += endline_if_missed(line)
|
2019-11-13 20:14:30 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-06 09:05:25 +03:00
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2022-09-17 10:12:01 +03:00
|
|
|
fn print_output(os_result string) {
|
2022-02-06 09:05:25 +03:00
|
|
|
content := convert_output(os_result)
|
|
|
|
print(content)
|
2019-11-13 20:14:30 +03:00
|
|
|
}
|
|
|
|
|
2019-10-31 19:01:04 +03:00
|
|
|
fn main() {
|
2020-01-20 19:06:36 +03:00
|
|
|
// Support for the parameters replfolder and replprefix is needed
|
|
|
|
// so that the repl can be launched in parallel by several different
|
|
|
|
// threads by the REPL test runner.
|
2020-02-16 14:42:28 +03:00
|
|
|
args := cmdline.options_after(os.args, ['repl'])
|
2022-09-16 04:56:19 +03:00
|
|
|
replfolder := os.real_path(cmdline.option(args, '-replfolder', repl_folder))
|
2021-02-08 10:44:04 +03:00
|
|
|
replprefix := cmdline.option(args, '-replprefix', 'noprefix.${rand.ulid()}.')
|
2019-12-23 13:02:50 +03:00
|
|
|
if !os.exists(os.getenv('VEXE')) {
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 15:19:04 +03:00
|
|
|
println('Usage:')
|
2019-12-23 13:02:50 +03:00
|
|
|
println(' VEXE=vexepath vrepl\n')
|
tools/vget => tools/v , search, install, etc
* compiler: rename vget to tools/vpm, implement draft support for v vpm search, v vpm update, v vpm install, v vpm remove, v vpm help .
* compiler: use "v pm" instead of "v vpm" to reduce the redundancy of typing, as suggested by slapden
* Use 'v install modulename', 'v search keywords', 'v update modulename', 'v remove modulename' instead of the longer 'v pm install modulename' etc.
2019-11-01 15:19:04 +03:00
|
|
|
println(' ... where vexepath is the full path to the v executable file')
|
2019-10-31 19:01:04 +03:00
|
|
|
return
|
|
|
|
}
|
2022-03-21 00:04:20 +03:00
|
|
|
if !is_stdin_a_pipe {
|
|
|
|
os.setenv('VCOLORS', 'always', true)
|
|
|
|
}
|
2022-04-04 16:22:34 +03:00
|
|
|
exit(run_repl(replfolder, replprefix))
|
2019-10-31 19:01:04 +03:00
|
|
|
}
|
2019-11-05 15:10:15 +03:00
|
|
|
|
2020-05-08 09:34:59 +03:00
|
|
|
fn rerror(s string) {
|
2022-11-15 16:53:13 +03:00
|
|
|
println('V repl error: ${s}')
|
2020-03-03 02:00:30 +03:00
|
|
|
os.flush()
|
2019-11-05 15:10:15 +03:00
|
|
|
}
|
2020-10-11 10:16:49 +03:00
|
|
|
|
|
|
|
fn (mut r Repl) get_one_line(prompt string) ?string {
|
|
|
|
if is_stdin_a_pipe {
|
|
|
|
iline := os.get_raw_line()
|
|
|
|
if iline.len == 0 {
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
return iline
|
|
|
|
}
|
2020-12-27 13:41:48 +03:00
|
|
|
rline := r.readline.read_line(prompt) or { return none }
|
2020-10-11 10:16:49 +03:00
|
|
|
return rline
|
|
|
|
}
|
2021-01-26 17:43:10 +03:00
|
|
|
|
2022-09-18 18:07:16 +03:00
|
|
|
fn cleanup_files(file string) {
|
|
|
|
os.rm(file) or {}
|
|
|
|
$if windows {
|
|
|
|
os.rm(file[..file.len - 2] + '.exe') or {}
|
|
|
|
$if msvc {
|
|
|
|
os.rm(file[..file.len - 2] + '.ilk') or {}
|
|
|
|
os.rm(file[..file.len - 2] + '.pdb') or {}
|
2021-01-26 17:43:10 +03:00
|
|
|
}
|
2022-09-18 18:07:16 +03:00
|
|
|
} $else {
|
|
|
|
os.rm(file[..file.len - 2]) or {}
|
2021-01-26 17:43:10 +03:00
|
|
|
}
|
|
|
|
}
|
2021-02-08 10:44:04 +03:00
|
|
|
|
|
|
|
fn repl_run_vfile(file string) ?os.Result {
|
|
|
|
$if trace_repl_temp_files ? {
|
2022-11-15 16:53:13 +03:00
|
|
|
eprintln('>> repl_run_vfile file: ${file}')
|
2021-02-08 10:44:04 +03:00
|
|
|
}
|
2022-01-22 22:13:16 +03:00
|
|
|
s := os.execute('${os.quoted_path(vexe)} -repl run ${os.quoted_path(file)}')
|
2021-03-08 21:52:13 +03:00
|
|
|
if s.exit_code < 0 {
|
|
|
|
rerror(s.output)
|
|
|
|
return error(s.output)
|
2021-02-08 10:44:04 +03:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|