mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
repl: moved as a tool and using readline
This commit is contained in:
parent
114953f28d
commit
55f6e810cf
@ -2,10 +2,14 @@
|
|||||||
// 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 compiler
|
module main
|
||||||
|
|
||||||
import os
|
import (
|
||||||
import term
|
compiler
|
||||||
|
os
|
||||||
|
term
|
||||||
|
readline
|
||||||
|
)
|
||||||
|
|
||||||
struct Repl {
|
struct Repl {
|
||||||
mut:
|
mut:
|
||||||
@ -59,9 +63,9 @@ fn (r &Repl) function_call(line string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn repl_help() {
|
pub fn repl_help() {
|
||||||
version_hash := vhash()
|
version_hash := compiler.vhash()
|
||||||
println('
|
println('
|
||||||
V $Version $version_hash
|
V ${compiler.Version} $version_hash
|
||||||
help Displays this information.
|
help Displays this information.
|
||||||
Ctrl-C, Ctrl-D, exit Exits the REPL.
|
Ctrl-C, Ctrl-D, exit Exits the REPL.
|
||||||
clear Clears the screen.
|
clear Clears the screen.
|
||||||
@ -69,11 +73,12 @@ V $Version $version_hash
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn run_repl() []string {
|
pub fn run_repl() []string {
|
||||||
version_hash := vhash()
|
version_hash := compiler.vhash()
|
||||||
println('V $Version $version_hash')
|
println('V ${compiler.Version} $version_hash')
|
||||||
println('Use Ctrl-C or `exit` to exit')
|
println('Use Ctrl-C or `exit` to exit')
|
||||||
file := '.vrepl.v'
|
file := '.vrepl.v'
|
||||||
temp_file := '.vrepl_temp.v'
|
temp_file := '.vrepl_temp.v'
|
||||||
|
mut prompt := '>>> '
|
||||||
defer {
|
defer {
|
||||||
os.rm(file)
|
os.rm(file)
|
||||||
os.rm(temp_file)
|
os.rm(temp_file)
|
||||||
@ -81,22 +86,26 @@ pub fn run_repl() []string {
|
|||||||
os.rm(temp_file[..temp_file.len - 2])
|
os.rm(temp_file[..temp_file.len - 2])
|
||||||
}
|
}
|
||||||
mut r := Repl{}
|
mut r := Repl{}
|
||||||
vexe := os.args[0]
|
mut readline := readline.Readline{}
|
||||||
|
vexe := os.args[1]
|
||||||
for {
|
for {
|
||||||
if r.indent == 0 {
|
if r.indent == 0 {
|
||||||
print('>>> ')
|
prompt = '>>> '
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
print('... ')
|
prompt = '... '
|
||||||
}
|
}
|
||||||
r.line = os.get_raw_line()
|
mut line := readline.read_line(prompt) or {
|
||||||
if r.line.trim_space() == '' && r.line.ends_with('\n') {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
r.line = r.line.trim_space()
|
|
||||||
if r.line.len == -1 || r.line == '' || r.line == 'exit' {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
if line.trim_space() == '' && line.ends_with('\n') {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
line = line.trim_space()
|
||||||
|
if line.len <= -1 || line == '' || line == 'exit' {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
r.line = line
|
||||||
if r.line == '\n' {
|
if r.line == '\n' {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -134,7 +143,7 @@ pub fn run_repl() []string {
|
|||||||
source_code := r.functions.join('\n') + r.lines.join('\n') + '\n' + r.line
|
source_code := r.functions.join('\n') + r.lines.join('\n') + '\n' + r.line
|
||||||
os.write_file(file, source_code)
|
os.write_file(file, source_code)
|
||||||
s := os.exec('"$vexe" run $file -repl') or {
|
s := os.exec('"$vexe" run $file -repl') or {
|
||||||
verror(err)
|
compiler.verror(err)
|
||||||
return []string
|
return []string
|
||||||
}
|
}
|
||||||
vals := s.output.split('\n')
|
vals := s.output.split('\n')
|
||||||
@ -153,7 +162,7 @@ pub fn run_repl() []string {
|
|||||||
temp_source_code := r.functions.join('\n') + r.lines.join('\n') + '\n' + r.temp_lines.join('\n') + '\n' + temp_line
|
temp_source_code := r.functions.join('\n') + r.lines.join('\n') + '\n' + r.temp_lines.join('\n') + '\n' + temp_line
|
||||||
os.write_file(temp_file, temp_source_code)
|
os.write_file(temp_file, temp_source_code)
|
||||||
s := os.exec('"$vexe" run $temp_file -repl') or {
|
s := os.exec('"$vexe" run $temp_file -repl') or {
|
||||||
verror(err)
|
compiler.verror(err)
|
||||||
return []string
|
return []string
|
||||||
}
|
}
|
||||||
if !func_call && s.exit_code == 0 && !temp_flag {
|
if !func_call && s.exit_code == 0 && !temp_flag {
|
||||||
@ -178,3 +187,12 @@ pub fn run_repl() []string {
|
|||||||
}
|
}
|
||||||
return r.lines
|
return r.lines
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
if os.args.len != 2 || !os.file_exists(os.args[1]) {
|
||||||
|
println('Usage: vrepl [vexe]\n')
|
||||||
|
println('vexe: v binary file')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
run_repl()
|
||||||
|
}
|
@ -1048,6 +1048,23 @@ pub fn install_v(args[]string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn run_repl() {
|
||||||
|
vexec := vexe_path()
|
||||||
|
vroot := os.dir(vexec)
|
||||||
|
vrepl := '$vroot/tools/vrepl'
|
||||||
|
|
||||||
|
os.chdir(vroot + '/tools')
|
||||||
|
vrepl_compilation := os.exec('"$vexec" -o $vrepl vrepl.v') or {
|
||||||
|
verror(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if vrepl_compilation.exit_code != 0 {
|
||||||
|
verror(vrepl_compilation.output)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
vreplresult := os.system('$vrepl "$vexec"')
|
||||||
|
}
|
||||||
|
|
||||||
pub fn create_symlink() {
|
pub fn create_symlink() {
|
||||||
vexe := vexe_path()
|
vexe := vexe_path()
|
||||||
link_path := '/usr/local/bin/v'
|
link_path := '/usr/local/bin/v'
|
||||||
|
@ -27,7 +27,7 @@ struct Winsize {
|
|||||||
ws_ypixel u16
|
ws_ypixel u16
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Readline {
|
pub struct Readline {
|
||||||
mut:
|
mut:
|
||||||
is_raw bool
|
is_raw bool
|
||||||
orig_termios Termios // Linux
|
orig_termios Termios // Linux
|
||||||
|
@ -164,7 +164,7 @@ fn (r Readline) analyse(c int) Action {
|
|||||||
1 { return .move_cursor_begining } // ^A
|
1 { return .move_cursor_begining } // ^A
|
||||||
5 { return .move_cursor_end } // ^E
|
5 { return .move_cursor_end } // ^E
|
||||||
26 { return .suspend } // CTRL + Z, SUB
|
26 { return .suspend } // CTRL + Z, SUB
|
||||||
else { return if c >= ` ` { .insert_character } else { Action.nothing } }
|
else { return if c >= ` ` { Action.insert_character } else { Action.nothing } }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -174,17 +174,17 @@ fn (r Readline) analyse_control() Action {
|
|||||||
`[` {
|
`[` {
|
||||||
sequence := r.read_char()
|
sequence := r.read_char()
|
||||||
match sequence {
|
match sequence {
|
||||||
`C` { return Action.move_cursor_right }
|
`C` { return .move_cursor_right }
|
||||||
`D` { return Action.move_cursor_left }
|
`D` { return .move_cursor_left }
|
||||||
`B` { return Action.history_next }
|
`B` { return .history_next }
|
||||||
`A` { return Action.history_previous }
|
`A` { return .history_previous }
|
||||||
`1` { return r.analyse_extended_control() }
|
`1` { return r.analyse_extended_control() }
|
||||||
`2` { return r.analyse_extended_control_no_eat(sequence) }
|
`2` { return r.analyse_extended_control_no_eat(sequence) }
|
||||||
`3` { return r.analyse_extended_control_no_eat(sequence) }
|
`3` { return r.analyse_extended_control_no_eat(sequence) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Action.nothing
|
return .nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (r Readline) analyse_extended_control() Action {
|
fn (r Readline) analyse_extended_control() Action {
|
||||||
@ -194,12 +194,12 @@ fn (r Readline) analyse_extended_control() Action {
|
|||||||
`5` {
|
`5` {
|
||||||
direction := r.read_char()
|
direction := r.read_char()
|
||||||
match direction {
|
match direction {
|
||||||
`C` { return Action.move_cursor_word_right }
|
`C` { return .move_cursor_word_right }
|
||||||
`D` { return Action.move_cursor_word_left }
|
`D` { return .move_cursor_word_left }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Action.nothing
|
return .nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (r Readline) analyse_extended_control_no_eat(last_c byte) Action {
|
fn (r Readline) analyse_extended_control_no_eat(last_c byte) Action {
|
||||||
@ -207,32 +207,32 @@ fn (r Readline) analyse_extended_control_no_eat(last_c byte) Action {
|
|||||||
match c {
|
match c {
|
||||||
`~` {
|
`~` {
|
||||||
match last_c {
|
match last_c {
|
||||||
`3` { return Action.delete_right } // Suppr key
|
`3` { return .delete_right } // Suppr key
|
||||||
`2` { return Action.overwrite }
|
`2` { return .overwrite }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Action.nothing
|
return .nothing
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (r mut Readline) execute(a Action, c int) bool {
|
fn (r mut Readline) execute(a Action, c int) bool {
|
||||||
match a {
|
match a {
|
||||||
Action.eof { return r.eof() }
|
.eof { return r.eof() }
|
||||||
Action.insert_character { r.insert_character(c) }
|
.insert_character { r.insert_character(c) }
|
||||||
Action.commit_line { return r.commit_line() }
|
.commit_line { return r.commit_line() }
|
||||||
Action.delete_left { r.delete_character() }
|
.delete_left { r.delete_character() }
|
||||||
Action.delete_right { r.suppr_character() }
|
.delete_right { r.suppr_character() }
|
||||||
Action.move_cursor_left { r.move_cursor_left() }
|
.move_cursor_left { r.move_cursor_left() }
|
||||||
Action.move_cursor_right { r.move_cursor_right() }
|
.move_cursor_right { r.move_cursor_right() }
|
||||||
Action.move_cursor_begining { r.move_cursor_begining() }
|
.move_cursor_begining { r.move_cursor_begining() }
|
||||||
Action.move_cursor_end { r.move_cursor_end() }
|
.move_cursor_end { r.move_cursor_end() }
|
||||||
Action.move_cursor_word_left { r.move_cursor_word_left() }
|
.move_cursor_word_left { r.move_cursor_word_left() }
|
||||||
Action.move_cursor_word_right { r.move_cursor_word_right() }
|
.move_cursor_word_right { r.move_cursor_word_right() }
|
||||||
Action.history_previous { r.history_previous() }
|
.history_previous { r.history_previous() }
|
||||||
Action.history_next { r.history_next() }
|
.history_next { r.history_next() }
|
||||||
Action.overwrite { r.switch_overwrite() }
|
.overwrite { r.switch_overwrite() }
|
||||||
Action.clear_screen { r.clear_screen() }
|
.clear_screen { r.clear_screen() }
|
||||||
Action.suspend { r.suspend() }
|
.suspend { r.suspend() }
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user