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
|
|
|
|
2019-10-31 19:01:04 +03:00
|
|
|
import (
|
|
|
|
os
|
|
|
|
term
|
|
|
|
readline
|
2020-01-20 19:06:36 +03:00
|
|
|
os.cmdline
|
|
|
|
filepath
|
2019-10-31 19:01:04 +03:00
|
|
|
)
|
2019-08-18 22:50:38 +03:00
|
|
|
|
|
|
|
struct Repl {
|
|
|
|
mut:
|
|
|
|
indent int
|
|
|
|
in_func bool
|
2019-10-06 17:21:10 +03:00
|
|
|
line string
|
2019-08-18 22:50:38 +03:00
|
|
|
lines []string
|
|
|
|
temp_lines []string
|
|
|
|
functions_name []string
|
|
|
|
functions []string
|
|
|
|
}
|
|
|
|
|
2019-10-06 17:21:10 +03:00
|
|
|
fn (r mut Repl) checks() bool {
|
2019-08-18 22:50:38 +03:00
|
|
|
mut in_string := false
|
2019-10-06 17:21:10 +03:00
|
|
|
mut is_cut := false
|
2019-08-18 22:50:38 +03:00
|
|
|
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
|
|
|
is_cut = true
|
|
|
|
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
|
|
|
is_cut = true
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2019-10-06 17:21:10 +03:00
|
|
|
return r.in_func || (was_indent && r.indent <= 0) || r.indent > 0 || is_cut
|
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 {
|
|
|
|
if line.starts_with(function) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-10-13 16:37:43 +03:00
|
|
|
pub fn repl_help() {
|
2019-11-05 15:10:15 +03:00
|
|
|
version := v_version()
|
|
|
|
println(version)
|
|
|
|
println('
|
2019-08-22 05:36:24 +03:00
|
|
|
help Displays this information.
|
|
|
|
Ctrl-C, Ctrl-D, exit Exits the REPL.
|
|
|
|
clear Clears the screen.
|
|
|
|
')
|
|
|
|
}
|
|
|
|
|
2020-01-20 19:06:36 +03:00
|
|
|
pub fn run_repl(workdir string, vrepl_prefix string) []string {
|
2019-11-05 15:10:15 +03:00
|
|
|
version := v_version()
|
|
|
|
println(version)
|
2019-08-18 22:50:38 +03:00
|
|
|
println('Use Ctrl-C or `exit` to exit')
|
2020-01-20 19:06:36 +03:00
|
|
|
|
|
|
|
file := filepath.join( workdir, '.${vrepl_prefix}vrepl.v' )
|
|
|
|
temp_file := filepath.join( workdir, '.${vrepl_prefix}vrepl_temp.v')
|
|
|
|
mut prompt := '>>> '
|
2019-08-18 22:50:38 +03:00
|
|
|
defer {
|
|
|
|
os.rm(file)
|
|
|
|
os.rm(temp_file)
|
2019-10-27 10:03:15 +03:00
|
|
|
os.rm(file[..file.len - 2])
|
|
|
|
os.rm(temp_file[..temp_file.len - 2])
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
|
|
|
mut r := Repl{}
|
2019-10-31 19:01:04 +03:00
|
|
|
mut readline := readline.Readline{}
|
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 = '>>> '
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
|
|
|
else {
|
2019-10-31 19:01:04 +03:00
|
|
|
prompt = '... '
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
2019-10-31 19:01:04 +03:00
|
|
|
mut line := readline.read_line(prompt) or {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if line.trim_space() == '' && line.ends_with('\n') {
|
2019-08-18 22:50:38 +03:00
|
|
|
continue
|
|
|
|
}
|
2019-10-31 19:01:04 +03:00
|
|
|
line = line.trim_space()
|
|
|
|
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' {
|
2019-08-22 05:36:24 +03:00
|
|
|
term.erase_display('2')
|
|
|
|
continue
|
|
|
|
}
|
2019-10-06 17:21:10 +03:00
|
|
|
if r.line == 'help' {
|
2019-08-22 05:36:24 +03:00
|
|
|
repl_help()
|
|
|
|
continue
|
|
|
|
}
|
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
|
2019-10-06 17:21:10 +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
|
|
|
}
|
|
|
|
// 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.
|
2019-10-06 17:21:10 +03:00
|
|
|
if r.line.starts_with('print') {
|
|
|
|
source_code := r.functions.join('\n') + r.lines.join('\n') + '\n' + r.line
|
2019-08-18 22:50:38 +03:00
|
|
|
os.write_file(file, source_code)
|
2019-10-16 02:49:51 +03:00
|
|
|
s := os.exec('"$vexe" run $file -repl') or {
|
2019-11-05 15:10:15 +03:00
|
|
|
rerror(err)
|
2019-11-14 10:00:22 +03:00
|
|
|
return []
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
2019-11-13 20:14:30 +03:00
|
|
|
print_output(s)
|
2019-08-18 22:50:38 +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-01-07 14:26:49 +03:00
|
|
|
if !(
|
|
|
|
r.line.contains(' ') ||
|
|
|
|
r.line.contains(':') ||
|
|
|
|
r.line.contains('=') ||
|
|
|
|
r.line.contains(',') ||
|
|
|
|
r.line.ends_with('++') ||
|
|
|
|
r.line.ends_with('--') ||
|
|
|
|
r.line == '') && !func_call {
|
2019-10-06 17:21:10 +03:00
|
|
|
temp_line = 'println($r.line)'
|
2019-08-18 22:50:38 +03:00
|
|
|
temp_flag = true
|
|
|
|
}
|
2019-10-06 17:21:10 +03:00
|
|
|
temp_source_code := r.functions.join('\n') + r.lines.join('\n') + '\n' + r.temp_lines.join('\n') + '\n' + temp_line
|
2019-08-18 22:50:38 +03:00
|
|
|
os.write_file(temp_file, temp_source_code)
|
2019-10-16 02:49:51 +03:00
|
|
|
s := os.exec('"$vexe" run $temp_file -repl') or {
|
2019-11-13 20:14:30 +03:00
|
|
|
println("SDFSDF")
|
2019-11-05 15:10:15 +03:00
|
|
|
rerror(err)
|
2019-11-14 10:00:22 +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)
|
|
|
|
}
|
2019-10-06 17:21:10 +03:00
|
|
|
r.lines << r.line
|
2019-08-18 22:50:38 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.lines
|
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) {
|
|
|
|
lines := s.output.split('\n')
|
|
|
|
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-01-20 19:06:36 +03:00
|
|
|
println(sline[idx+1..])
|
|
|
|
} 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.
|
|
|
|
idx := line.index('.vrepl.v:') or { return }
|
|
|
|
println(line[idx..])
|
|
|
|
} else {
|
2019-11-13 20:14:30 +03:00
|
|
|
println(line)
|
|
|
|
}
|
|
|
|
}
|
2019-11-30 15:27:16 +03:00
|
|
|
|
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.
|
|
|
|
args := cmdline.after(os.args, ['repl'])
|
|
|
|
replfolder := os.realpath( cmdline.option(args, '-replfolder', '.') )
|
|
|
|
replprefix := cmdline.option(args, '-replprefix', 'noprefix.')
|
|
|
|
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-01-20 19:06:36 +03:00
|
|
|
run_repl( replfolder, replprefix )
|
2019-10-31 19:01:04 +03:00
|
|
|
}
|
2019-11-05 15:10:15 +03:00
|
|
|
|
|
|
|
pub fn rerror(s string) {
|
|
|
|
println('V repl error: $s')
|
|
|
|
os.flush_stdout()
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn v_version() string {
|
2019-12-23 13:02:50 +03:00
|
|
|
vexe := os.getenv('VEXE')
|
2019-11-05 15:10:15 +03:00
|
|
|
vversion_res := os.exec('$vexe --version') or { panic('"$vexe --version" is not working') }
|
|
|
|
return vversion_res.output
|
|
|
|
}
|