repl: fix `reset`; make `echo "print(2.0 * 3.14159)" | ./v` print only the result

pull/6299/head
Delyan Angelov 2020-10-11 10:16:49 +03:00
parent c7e0a27e0d
commit 62f6e65509
3 changed files with 77 additions and 33 deletions

View File

@ -1,7 +1,6 @@
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved. // Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license // Use of this source code is governed by an MIT license
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module main module main
import os import os
@ -24,10 +23,19 @@ mut:
temp_lines []string // all the temporary expressions/printlns temp_lines []string // all the temporary expressions/printlns
} }
const (
is_stdin_a_pipe = (is_atty(0) == 0)
)
fn new_repl() &Repl {
return &Repl{
modules: ['os', 'time', 'math']
}
}
fn (mut r Repl) checks() bool { fn (mut r Repl) checks() bool {
mut in_string := false mut in_string := false
was_indent := r.indent > 0 was_indent := r.indent > 0
for i := 0; i < r.line.len; i++ { for i := 0; i < r.line.len; i++ {
if r.line[i] == `\'` && (i == 0 || r.line[i - 1] != `\\`) { if r.line[i] == `\'` && (i == 0 || r.line[i - 1] != `\\`) {
in_string = !in_string in_string = !in_string
@ -54,7 +62,7 @@ fn (mut r Repl) checks() bool {
fn (r &Repl) function_call(line string) bool { fn (r &Repl) function_call(line string) bool {
for function in r.functions_name { for function in r.functions_name {
is_function_definition := line.replace(' ', '').starts_with("$function:=") is_function_definition := line.replace(' ', '').starts_with('$function:=')
if line.starts_with(function) && !is_function_definition { if line.starts_with(function) && !is_function_definition {
return true return true
} }
@ -87,15 +95,18 @@ fn repl_help() {
') ')
} }
fn run_repl(workdir string, vrepl_prefix string) { fn run_repl(workdir, vrepl_prefix string) {
if !is_stdin_a_pipe {
println(util.full_v_version(false)) println(util.full_v_version(false))
println('Use Ctrl-C or `exit` to exit') println('Use Ctrl-C or `exit` to exit, or `help` to see other available commands')
}
file := os.join_path(workdir, '.${vrepl_prefix}vrepl.v') file := os.join_path(workdir, '.${vrepl_prefix}vrepl.v')
temp_file := os.join_path(workdir, '.${vrepl_prefix}vrepl_temp.v') temp_file := os.join_path(workdir, '.${vrepl_prefix}vrepl_temp.v')
mut prompt := '>>> ' mut prompt := '>>> '
defer { defer {
if !is_stdin_a_pipe {
println('') println('')
}
os.rm(file) os.rm(file)
os.rm(temp_file) os.rm(temp_file)
$if windows { $if windows {
@ -112,10 +123,7 @@ fn run_repl(workdir string, vrepl_prefix string) {
os.rm(temp_file[..temp_file.len - 2]) os.rm(temp_file[..temp_file.len - 2])
} }
} }
mut r := Repl{ mut r := new_repl()
modules: ['os', 'time', 'math']
}
mut readline := readline.Readline{}
vexe := os.getenv('VEXE') vexe := os.getenv('VEXE')
for { for {
if r.indent == 0 { if r.indent == 0 {
@ -123,7 +131,7 @@ fn run_repl(workdir string, vrepl_prefix string) {
} else { } else {
prompt = '... ' prompt = '... '
} }
oline := readline.read_line(prompt) or { oline := r.get_one_line(prompt) or {
break break
} }
line := oline.trim_space() line := oline.trim_space()
@ -172,7 +180,7 @@ fn run_repl(workdir string, vrepl_prefix string) {
continue continue
} }
if r.line == 'reset' { if r.line == 'reset' {
r = Repl{} r = new_repl()
continue continue
} }
if r.line == 'list' { if r.line == 'list' {
@ -189,7 +197,7 @@ fn run_repl(workdir string, vrepl_prefix string) {
r.line = 'println(' + r.line[1..] + ')' r.line = 'println(' + r.line[1..] + ')'
} }
if r.line.starts_with('print') { if r.line.starts_with('print') {
source_code := r.current_source_code(false) + '\n${r.line}\n' source_code := r.current_source_code(false) + '\n$r.line\n'
os.write_file(file, source_code) os.write_file(file, source_code)
s := os.exec('"$vexe" -repl run "$file"') or { s := os.exec('"$vexe" -repl run "$file"') or {
rerror(err) rerror(err)
@ -200,13 +208,33 @@ fn run_repl(workdir string, vrepl_prefix string) {
mut temp_line := r.line mut temp_line := r.line
mut temp_flag := false mut temp_flag := false
func_call := r.function_call(r.line) func_call := r.function_call(r.line)
filter_line := r.line.replace(r.line.find_between('\'', '\''), '').replace(r.line.find_between('"', '"'), '') filter_line := r.line.replace(r.line.find_between("\'", "\'"), '').replace(r.line.find_between('"',
'"'), '')
possible_statement_patterns := [ possible_statement_patterns := [
'=', '++', '--', '<<', '=',
'//', '/*', '++',
'fn ', 'pub ', 'mut ', 'enum ', 'const ', 'struct ', 'interface ', 'import ', '--',
'#include ', ':=', 'for ', 'or ', 'insert', 'delete', 'prepend', '<<',
'sort', 'clear', 'trim', '//',
'/*',
'fn ',
'pub ',
'mut ',
'enum ',
'const ',
'struct ',
'interface ',
'import ',
'#include ',
':=',
'for ',
'or ',
'insert',
'delete',
'prepend',
'sort',
'clear',
'trim',
] ]
mut is_statement := false mut is_statement := false
for pattern in possible_statement_patterns { for pattern in possible_statement_patterns {
@ -227,10 +255,10 @@ fn run_repl(workdir string, vrepl_prefix string) {
if temp_line.starts_with('import ') { if temp_line.starts_with('import ') {
mod := r.line.fields()[1] mod := r.line.fields()[1]
if mod !in r.modules { if mod !in r.modules {
temp_source_code = '${temp_line}\n' + r.current_source_code(false) temp_source_code = '$temp_line\n' + r.current_source_code(false)
} }
} else if temp_line.starts_with('#include ') { } else if temp_line.starts_with('#include ') {
temp_source_code = '${temp_line}\n' + r.current_source_code(false) temp_source_code = '$temp_line\n' + r.current_source_code(false)
} else { } else {
for i, l in r.lines { for i, l in r.lines {
if (l.starts_with('for ') || l.starts_with('if ')) && l.contains('println') { if (l.starts_with('for ') || l.starts_with('if ')) && l.contains('println') {
@ -238,8 +266,7 @@ fn run_repl(workdir string, vrepl_prefix string) {
break break
} }
} }
temp_source_code = r.current_source_code(true) + '\n$temp_line\n'
temp_source_code = r.current_source_code(true) + '\n${temp_line}\n'
} }
os.write_file(temp_file, temp_source_code) os.write_file(temp_file, temp_source_code)
s := os.exec('"$vexe" -repl run "$temp_file"') or { s := os.exec('"$vexe" -repl run "$temp_file"') or {
@ -287,7 +314,9 @@ fn print_output(s os.Result) {
} else if line.contains('.vrepl.v:') { } else if line.contains('.vrepl.v:') {
// Ensure that .vrepl.v: is at the start, ignore the path // Ensure that .vrepl.v: is at the start, ignore the path
// This is needed to have stable .repl tests. // This is needed to have stable .repl tests.
idx := line.index('.vrepl.v:') or { return } idx := line.index('.vrepl.v:') or {
return
}
println(line[idx..]) println(line[idx..])
} else { } else {
println(line) println(line)
@ -316,3 +345,18 @@ fn rerror(s string) {
println('V repl error: $s') println('V repl error: $s')
os.flush() os.flush()
} }
fn (mut r Repl) get_one_line(prompt string) ?string {
mut readline := readline.Readline{}
if is_stdin_a_pipe {
iline := os.get_raw_line()
if iline.len == 0 {
return none
}
return iline
}
rline := readline.read_line(prompt) or {
return none
}
return rline
}

View File

@ -27,7 +27,7 @@ fn main() {
// args = 123 // args = 123
if args.len == 0 || args[0] in ['-', 'repl'] { if args.len == 0 || args[0] in ['-', 'repl'] {
// Running `./v` without args launches repl // Running `./v` without args launches repl
if args.len == 0 { if args.len == 0 && is_atty(0) != 0 {
println('For usage information, quit V REPL and run `v help`') println('For usage information, quit V REPL and run `v help`')
} }
util.launch_tool(false, 'vrepl', os.args[1..]) util.launch_tool(false, 'vrepl', os.args[1..])

View File

@ -60,7 +60,7 @@ pub fn run_repl_file(wd, vexec, file string) ?string {
} }
os.rm(input_temporary_filename) os.rm(input_temporary_filename)
result := r.output.replace('\r', '').replace('>>> ', '').replace('>>>', '').replace('... ', result := r.output.replace('\r', '').replace('>>> ', '').replace('>>>', '').replace('... ',
'').all_after('Use Ctrl-C or `exit` to exit\n').replace(wd + os.path_separator, '').replace(vexec_folder, '').replace(wd + os.path_separator, '').replace(vexec_folder,
'').replace('\\', '/').trim_right('\n\r') '').replace('\\', '/').trim_right('\n\r')
if result != output { if result != output {
file_result := '${file}.result.txt' file_result := '${file}.result.txt'