2019-08-18 21:50:38 +02:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
module main
|
|
|
|
|
|
|
|
import os
|
2019-08-22 04:36:24 +02:00
|
|
|
import term
|
2019-08-18 21:50:38 +02:00
|
|
|
|
|
|
|
struct Repl {
|
|
|
|
mut:
|
|
|
|
indent int
|
|
|
|
in_func bool
|
2019-10-06 16:21:10 +02:00
|
|
|
line string
|
2019-08-18 21:50:38 +02:00
|
|
|
lines []string
|
|
|
|
temp_lines []string
|
|
|
|
functions_name []string
|
|
|
|
functions []string
|
|
|
|
}
|
|
|
|
|
2019-10-06 16:21:10 +02:00
|
|
|
fn (r mut Repl) checks() bool {
|
2019-08-18 21:50:38 +02:00
|
|
|
mut in_string := false
|
2019-10-06 16:21:10 +02:00
|
|
|
mut is_cut := false
|
2019-08-18 21:50:38 +02:00
|
|
|
was_indent := r.indent > 0
|
|
|
|
|
2019-10-06 16:21:10 +02:00
|
|
|
for i := 0; i < r.line.len; i++ {
|
|
|
|
if r.line[i] == `\'` && (i == 0 || r.line[i - 1] != `\\`) {
|
2019-08-18 21:50:38 +02:00
|
|
|
in_string = !in_string
|
|
|
|
}
|
2019-10-06 16:21:10 +02:00
|
|
|
if r.line[i] == `{` && !in_string {
|
|
|
|
r.line = r.line.left(i + 1) + '\n' + r.line.right(i + 1)
|
|
|
|
is_cut = true
|
|
|
|
i++
|
2019-08-18 21:50:38 +02:00
|
|
|
r.indent++
|
|
|
|
}
|
2019-10-06 16:21:10 +02:00
|
|
|
if r.line[i] == `}` && !in_string {
|
|
|
|
r.line = r.line.left(i) + '\n' + r.line.right(i)
|
|
|
|
is_cut = true
|
|
|
|
i++
|
2019-08-18 21:50:38 +02:00
|
|
|
r.indent--
|
|
|
|
if r.indent == 0 {
|
|
|
|
r.in_func = false
|
|
|
|
}
|
|
|
|
}
|
2019-10-06 16:21:10 +02:00
|
|
|
if i + 2 < r.line.len && r.indent == 0 && r.line[i + 1] == `f` && r.line[i + 2] == `n` {
|
2019-08-18 21:50:38 +02:00
|
|
|
r.in_func = true
|
|
|
|
}
|
|
|
|
}
|
2019-10-06 16:21:10 +02:00
|
|
|
return r.in_func || (was_indent && r.indent <= 0) || r.indent > 0 || is_cut
|
2019-08-18 21:50:38 +02:00
|
|
|
}
|
|
|
|
|
2019-09-17 12:09:58 +02:00
|
|
|
fn (r &Repl) function_call(line string) bool {
|
2019-08-18 21:50:38 +02:00
|
|
|
for function in r.functions_name {
|
|
|
|
if line.starts_with(function) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-08-22 04:36:24 +02:00
|
|
|
fn repl_help() {
|
2019-09-11 00:12:46 +02:00
|
|
|
version_hash := vhash()
|
2019-08-22 04:36:24 +02:00
|
|
|
println('
|
2019-09-11 00:12:46 +02:00
|
|
|
V $Version $version_hash
|
2019-08-22 04:36:24 +02:00
|
|
|
help Displays this information.
|
|
|
|
Ctrl-C, Ctrl-D, exit Exits the REPL.
|
|
|
|
clear Clears the screen.
|
|
|
|
')
|
|
|
|
}
|
|
|
|
|
2019-08-18 21:50:38 +02:00
|
|
|
fn run_repl() []string {
|
2019-09-11 00:12:46 +02:00
|
|
|
version_hash := vhash()
|
|
|
|
println('V $Version $version_hash')
|
2019-08-18 21:50:38 +02:00
|
|
|
println('Use Ctrl-C or `exit` to exit')
|
|
|
|
file := '.vrepl.v'
|
|
|
|
temp_file := '.vrepl_temp.v'
|
|
|
|
defer {
|
|
|
|
os.rm(file)
|
|
|
|
os.rm(temp_file)
|
|
|
|
os.rm(file.left(file.len - 2))
|
|
|
|
os.rm(temp_file.left(temp_file.len - 2))
|
|
|
|
}
|
|
|
|
mut r := Repl{}
|
|
|
|
vexe := os.args[0]
|
|
|
|
for {
|
|
|
|
if r.indent == 0 {
|
|
|
|
print('>>> ')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
print('... ')
|
|
|
|
}
|
2019-10-06 16:21:10 +02:00
|
|
|
r.line = os.get_raw_line()
|
|
|
|
if r.line.trim_space() == '' && r.line.ends_with('\n') {
|
2019-08-18 21:50:38 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-10-06 16:21:10 +02:00
|
|
|
r.line = r.line.trim_space()
|
|
|
|
if r.line.len == -1 || r.line == '' || r.line == 'exit' {
|
2019-08-18 21:50:38 +02:00
|
|
|
break
|
|
|
|
}
|
2019-10-06 16:21:10 +02:00
|
|
|
if r.line == '\n' {
|
2019-08-18 21:50:38 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-10-06 16:21:10 +02:00
|
|
|
if r.line == 'clear' {
|
2019-08-22 04:36:24 +02:00
|
|
|
term.erase_display('2')
|
|
|
|
continue
|
|
|
|
}
|
2019-10-06 16:21:10 +02:00
|
|
|
if r.line == 'help' {
|
2019-08-22 04:36:24 +02:00
|
|
|
repl_help()
|
|
|
|
continue
|
|
|
|
}
|
2019-10-06 16:21:10 +02:00
|
|
|
if r.line.starts_with('fn') {
|
2019-08-18 21:50:38 +02:00
|
|
|
r.in_func = true
|
2019-10-06 16:21:10 +02:00
|
|
|
r.functions_name << r.line.all_after('fn').all_before('(').trim_space()
|
2019-08-18 21:50:38 +02:00
|
|
|
}
|
|
|
|
was_func := r.in_func
|
2019-10-06 16:21:10 +02:00
|
|
|
if r.checks() {
|
|
|
|
for line in r.line.split('\n') {
|
|
|
|
if r.in_func || was_func {
|
|
|
|
r.functions << line
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
r.temp_lines << line
|
|
|
|
}
|
2019-08-18 21:50:38 +02:00
|
|
|
}
|
|
|
|
if r.indent > 0 {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-06 16:21:10 +02:00
|
|
|
r.line = ''
|
2019-08-18 21:50:38 +02: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 16:21:10 +02:00
|
|
|
if r.line.starts_with('print') {
|
|
|
|
source_code := r.functions.join('\n') + r.lines.join('\n') + '\n' + r.line
|
2019-08-18 21:50:38 +02:00
|
|
|
os.write_file(file, source_code)
|
|
|
|
s := os.exec('$vexe run $file -repl') or {
|
2019-09-23 23:40:34 +02:00
|
|
|
verror(err)
|
2019-08-29 02:30:17 +02:00
|
|
|
return []string
|
2019-08-18 21:50:38 +02:00
|
|
|
}
|
|
|
|
vals := s.output.split('\n')
|
|
|
|
for i:=0; i < vals.len; i++ {
|
|
|
|
println(vals[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2019-10-06 16:21:10 +02:00
|
|
|
mut temp_line := r.line
|
2019-08-18 21:50:38 +02:00
|
|
|
mut temp_flag := false
|
2019-10-06 16:21:10 +02:00
|
|
|
func_call := r.function_call(r.line)
|
|
|
|
if !(r.line.contains(' ') || r.line.contains(':') || r.line.contains('=') || r.line.contains(',') || r.line == '') && !func_call {
|
|
|
|
temp_line = 'println($r.line)'
|
2019-08-18 21:50:38 +02:00
|
|
|
temp_flag = true
|
|
|
|
}
|
2019-10-06 16:21:10 +02:00
|
|
|
temp_source_code := r.functions.join('\n') + r.lines.join('\n') + '\n' + r.temp_lines.join('\n') + '\n' + temp_line
|
2019-08-18 21:50:38 +02:00
|
|
|
os.write_file(temp_file, temp_source_code)
|
|
|
|
s := os.exec('$vexe run $temp_file -repl') or {
|
2019-09-23 23:40:34 +02:00
|
|
|
verror(err)
|
2019-08-29 02:30:17 +02:00
|
|
|
return []string
|
2019-08-18 21:50:38 +02:00
|
|
|
}
|
2019-10-06 16:21:10 +02:00
|
|
|
if !func_call && s.exit_code == 0 && !temp_flag {
|
2019-08-18 21:50:38 +02: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 16:21:10 +02:00
|
|
|
r.lines << r.line
|
2019-08-18 21:50:38 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
for r.temp_lines.len > 0 {
|
|
|
|
r.temp_lines.delete(0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vals := s.output.split('\n')
|
|
|
|
for i:=0; i<vals.len; i++ {
|
|
|
|
println(vals[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r.lines
|
2019-08-29 02:30:17 +02:00
|
|
|
}
|