2020-01-23 23:04:46 +03:00
|
|
|
// Copyright (c) 2019-2020 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
|
|
|
|
import readline
|
|
|
|
import os.cmdline
|
|
|
|
import v.util
|
2019-08-18 22:50:38 +03:00
|
|
|
|
|
|
|
struct Repl {
|
|
|
|
mut:
|
2020-12-27 13:41:48 +03:00
|
|
|
readline readline.Readline
|
2020-10-11 10:16:49 +03:00
|
|
|
indent int // indentation level
|
|
|
|
in_func bool // are we inside a new custom user function
|
|
|
|
line string // the current line entered by the user
|
2020-05-16 22:03:28 +03:00
|
|
|
//
|
2020-07-03 19:06:36 +03:00
|
|
|
modules []string // all the import modules
|
|
|
|
includes []string // all the #include statements
|
2020-05-16 22:03:28 +03:00
|
|
|
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
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
|
|
|
|
2020-10-11 10:16:49 +03:00
|
|
|
const (
|
|
|
|
is_stdin_a_pipe = (is_atty(0) == 0)
|
|
|
|
)
|
|
|
|
|
|
|
|
fn new_repl() &Repl {
|
|
|
|
return &Repl{
|
2020-12-27 13:41:48 +03:00
|
|
|
readline: readline.Readline{}
|
2020-10-11 10:16:49 +03:00
|
|
|
modules: ['os', 'time', 'math']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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++ {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-09-17 13:09:58 +03:00
|
|
|
fn (r &Repl) function_call(line string) bool {
|
2019-08-18 22:50:38 +03:00
|
|
|
for function in r.functions_name {
|
2020-10-11 10:16:49 +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 {
|
2019-08-18 22:50:38 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-05-16 22:03:28 +03:00
|
|
|
fn (r &Repl) current_source_code(should_add_temp_lines bool) string {
|
|
|
|
mut all_lines := []string{}
|
2020-07-03 19:06:36 +03:00
|
|
|
for mod in r.modules {
|
|
|
|
all_lines << 'import $mod\n'
|
|
|
|
}
|
|
|
|
all_lines << r.includes
|
2020-05-16 22:03:28 +03:00
|
|
|
all_lines << r.functions
|
|
|
|
all_lines << r.lines
|
|
|
|
if should_add_temp_lines {
|
|
|
|
all_lines << r.temp_lines
|
|
|
|
}
|
|
|
|
return all_lines.join('\n')
|
|
|
|
}
|
|
|
|
|
2020-05-08 09:34:59 +03:00
|
|
|
fn repl_help() {
|
2020-07-03 23:30:34 +03:00
|
|
|
println(util.full_v_version(false))
|
2019-11-05 15:10:15 +03:00
|
|
|
println('
|
2020-10-21 15:24:45 +03:00
|
|
|
|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.
|
|
|
|
'.strip_margin())
|
2019-08-22 05:36:24 +03:00
|
|
|
}
|
|
|
|
|
2020-10-15 16:17:52 +03:00
|
|
|
fn run_repl(workdir string, vrepl_prefix string) {
|
2020-10-11 10:16:49 +03:00
|
|
|
if !is_stdin_a_pipe {
|
|
|
|
println(util.full_v_version(false))
|
|
|
|
println('Use Ctrl-C or `exit` to exit, or `help` to see other available commands')
|
|
|
|
}
|
2020-03-09 04:23:34 +03:00
|
|
|
file := os.join_path(workdir, '.${vrepl_prefix}vrepl.v')
|
|
|
|
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('')
|
|
|
|
}
|
2019-08-18 22:50:38 +03:00
|
|
|
os.rm(file)
|
|
|
|
os.rm(temp_file)
|
2020-05-03 19:13:37 +03:00
|
|
|
$if windows {
|
|
|
|
os.rm(file[..file.len - 2] + '.exe')
|
|
|
|
os.rm(temp_file[..temp_file.len - 2] + '.exe')
|
|
|
|
$if msvc {
|
|
|
|
os.rm(file[..file.len - 2] + '.ilk')
|
|
|
|
os.rm(file[..file.len - 2] + '.pdb')
|
|
|
|
os.rm(temp_file[..temp_file.len - 2] + '.ilk')
|
|
|
|
os.rm(temp_file[..temp_file.len - 2] + '.pdb')
|
|
|
|
}
|
|
|
|
} $else {
|
|
|
|
os.rm(file[..file.len - 2])
|
|
|
|
os.rm(temp_file[..temp_file.len - 2])
|
|
|
|
}
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
2020-10-11 10:16:49 +03:00
|
|
|
mut r := new_repl()
|
2019-12-23 13:02:50 +03:00
|
|
|
vexe := os.getenv('VEXE')
|
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
|
|
|
|
}
|
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' {
|
|
|
|
eprintln('repl: $r')
|
|
|
|
continue
|
|
|
|
}
|
2020-05-16 21:20:47 +03:00
|
|
|
if r.line == 'reset' {
|
2020-10-11 10:16:49 +03:00
|
|
|
r = new_repl()
|
2020-05-16 21:20:47 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if r.line == 'list' {
|
2020-05-16 22:03:28 +03:00
|
|
|
source_code := r.current_source_code(true)
|
2020-05-16 21:20:47 +03:00
|
|
|
println('//////////////////////////////////////////////////////////////////////////////////////')
|
|
|
|
println(source_code)
|
|
|
|
println('//////////////////////////////////////////////////////////////////////////////////////')
|
|
|
|
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') {
|
2020-10-11 10:16:49 +03:00
|
|
|
source_code := r.current_source_code(false) + '\n$r.line\n'
|
2019-08-18 22:50:38 +03:00
|
|
|
os.write_file(file, source_code)
|
2020-06-08 11:29:51 +03:00
|
|
|
s := os.exec('"$vexe" -repl run "$file"') or {
|
2019-11-05 15:10:15 +03:00
|
|
|
rerror(err)
|
2020-05-08 09:34:59 +03:00
|
|
|
return
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
2019-11-13 20:14:30 +03:00
|
|
|
print_output(s)
|
2020-02-22 14:41:57 +03:00
|
|
|
} else {
|
2019-10-06 17:21:10 +03:00
|
|
|
mut temp_line := r.line
|
2019-08-18 22:50:38 +03:00
|
|
|
mut temp_flag := false
|
2019-10-06 17:21:10 +03:00
|
|
|
func_call := r.function_call(r.line)
|
2020-10-11 10:16:49 +03:00
|
|
|
filter_line := r.line.replace(r.line.find_between("\'", "\'"), '').replace(r.line.find_between('"',
|
|
|
|
'"'), '')
|
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 ',
|
|
|
|
'insert',
|
|
|
|
'delete',
|
|
|
|
'prepend',
|
|
|
|
'sort',
|
|
|
|
'clear',
|
|
|
|
'trim',
|
2020-05-16 14:40:54 +03:00
|
|
|
]
|
|
|
|
mut is_statement := false
|
|
|
|
for pattern in possible_statement_patterns {
|
|
|
|
if filter_line.contains(pattern) {
|
|
|
|
is_statement = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// NB: starting a line with 2 spaces escapes the println heuristic
|
|
|
|
if oline.starts_with(' ') {
|
|
|
|
is_statement = true
|
|
|
|
}
|
|
|
|
if !is_statement && !func_call && r.line != '' {
|
2019-10-06 17:21:10 +03:00
|
|
|
temp_line = 'println($r.line)'
|
2019-08-18 22:50:38 +03:00
|
|
|
temp_flag = true
|
|
|
|
}
|
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 {
|
2020-10-11 10:16:49 +03:00
|
|
|
temp_source_code = '$temp_line\n' + r.current_source_code(false)
|
2020-07-03 19:06:36 +03:00
|
|
|
}
|
|
|
|
} else if temp_line.starts_with('#include ') {
|
2020-10-11 10:16:49 +03:00
|
|
|
temp_source_code = '$temp_line\n' + r.current_source_code(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
|
|
|
|
}
|
|
|
|
}
|
2020-10-11 10:16:49 +03:00
|
|
|
temp_source_code = r.current_source_code(true) + '\n$temp_line\n'
|
2020-05-08 09:34:59 +03:00
|
|
|
}
|
2019-08-18 22:50:38 +03:00
|
|
|
os.write_file(temp_file, temp_source_code)
|
2020-06-08 11:29:51 +03:00
|
|
|
s := os.exec('"$vexe" -repl run "$temp_file"') or {
|
2019-11-05 15:10:15 +03:00
|
|
|
rerror(err)
|
2020-05-08 09:34:59 +03:00
|
|
|
return
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
2019-10-06 17:21:10 +03:00
|
|
|
if !func_call && s.exit_code == 0 && !temp_flag {
|
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 ') {
|
|
|
|
mod := r.line.fields()[1]
|
|
|
|
if mod !in r.modules {
|
|
|
|
r.modules << mod
|
|
|
|
}
|
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
}
|
2019-11-13 20:14:30 +03:00
|
|
|
print_output(s)
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-29 03:30:17 +03:00
|
|
|
}
|
2019-10-31 19:01:04 +03:00
|
|
|
|
2019-11-13 20:14:30 +03:00
|
|
|
fn print_output(s os.Result) {
|
2020-12-08 11:51:47 +03:00
|
|
|
lines := s.output.trim_right('\n\r').split_into_lines()
|
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 {
|
|
|
|
println(sline)
|
2019-11-30 15:27:16 +03:00
|
|
|
return
|
|
|
|
}
|
2020-10-11 10:16:49 +03:00
|
|
|
println(sline[idx + 1..])
|
2020-01-20 19:06:36 +03:00
|
|
|
} else if line.contains('.vrepl.v:') {
|
|
|
|
// Ensure that .vrepl.v: is at the start, ignore the path
|
|
|
|
// This is needed to have stable .repl tests.
|
2020-12-27 13:41:48 +03:00
|
|
|
idx := line.index('.vrepl.v:') or { return }
|
2020-01-20 19:06:36 +03:00
|
|
|
println(line[idx..])
|
|
|
|
} else {
|
2019-11-13 20:14:30 +03:00
|
|
|
println(line)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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'])
|
2020-05-08 09:34:59 +03:00
|
|
|
replfolder := os.real_path(cmdline.option(args, '-replfolder', '.'))
|
2020-01-20 19:06:36 +03:00
|
|
|
replprefix := cmdline.option(args, '-replprefix', 'noprefix.')
|
2020-05-08 09:34:59 +03:00
|
|
|
os.chdir(replfolder)
|
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
|
|
|
|
}
|
2020-05-08 09:34:59 +03:00
|
|
|
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) {
|
2019-11-05 15:10:15 +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
|
|
|
|
}
|