From abf15e863cf8adb8f141f1f9673c373a935c97d0 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 2 Apr 2020 19:21:12 +0300 Subject: [PATCH] v2: compile vrepl, 'fn main(){println(2+2)}' runs now It works if you give it complete V programs like fn main(){println(2+2)} but not for single expressions like 2+2 or statements like a:='abc' --- vlib/builtin/string.v | 6 +++++- vlib/readline/readline_linux.v | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index 196a279bc5..fb77d9cf30 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -916,6 +916,10 @@ pub fn (s mut []string) sort_by_len() { s.sort_with_compare(compare_strings_by_len) } +pub fn (s ustring) str() string { + return s.s +} + pub fn (s string) ustring() ustring { mut res := ustring{ s: s @@ -985,7 +989,7 @@ fn (u ustring) ge(a ustring) bool { return !u.lt(a) } -fn (u ustring) add(a ustring) ustring { +pub fn (u ustring) add(a ustring) ustring { mut res := ustring{ s: u.s + a.s runes: new_array(0, u.s.len + a.s.len, sizeof(int)) diff --git a/vlib/readline/readline_linux.v b/vlib/readline/readline_linux.v index 9314c25034..327cb794f5 100644 --- a/vlib/readline/readline_linux.v +++ b/vlib/readline/readline_linux.v @@ -313,7 +313,8 @@ fn shift_cursor(xpos int, yoffset int) { print('\x1b[${xpos + 1}G') } -fn calculate_screen_position(x_in int, y_in int, screen_columns int, char_count int, out mut []int) { +fn calculate_screen_position(x_in int, y_in int, screen_columns int, char_count int, inp []int) []int { + mut out := inp mut x := x_in mut y := y_in out[0] = x @@ -330,15 +331,16 @@ fn calculate_screen_position(x_in int, y_in int, screen_columns int, char_count out[0] = 0 out[1]++ } + return out } // Will redraw the line fn (r mut Readline) refresh_line() { mut end_of_input := [0, 0] - calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.current.len, mut end_of_input) + end_of_input = calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.current.len, end_of_input) end_of_input[1] += r.current.count('\n'.ustring()) mut cursor_pos := [0, 0] - calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.cursor, mut cursor_pos) + cursor_pos = calculate_screen_position(r.prompt.len, 0, get_screen_columns(), r.cursor, cursor_pos) shift_cursor(0, -r.cursor_row_offset) term.erase_toend() @@ -363,9 +365,9 @@ fn (r mut Readline) eof() bool { fn (r mut Readline) insert_character(c int) { if !r.overwrite || r.cursor == r.current.len { - r.current = r.current.left(r.cursor).ustring() + utf32_to_str(u32(c)).ustring() + r.current.right(r.cursor).ustring() + r.current = r.current.left(r.cursor).ustring().add( utf32_to_str(u32(c)).ustring() ).add( r.current.right(r.cursor).ustring() ) } else { - r.current = r.current.left(r.cursor).ustring() + utf32_to_str(u32(c)).ustring() + r.current.right(r.cursor + 1).ustring() + r.current = r.current.left(r.cursor).ustring().add( utf32_to_str(u32(c)).ustring() ).add( r.current.right(r.cursor + 1).ustring() ) } r.cursor++ // Refresh the line to add the new character @@ -380,7 +382,7 @@ fn (r mut Readline) delete_character() { return } r.cursor-- - r.current = r.current.left(r.cursor).ustring() + r.current.right(r.cursor + 1).ustring() + r.current = r.current.left(r.cursor).ustring().add( r.current.right(r.cursor + 1).ustring() ) r.refresh_line() } @@ -389,7 +391,7 @@ fn (r mut Readline) suppr_character() { if r.cursor > r.current.len { return } - r.current = r.current.left(r.cursor).ustring() + r.current.right(r.cursor + 1).ustring() + r.current = r.current.left(r.cursor).ustring().add( r.current.right(r.cursor + 1).ustring() ) r.refresh_line() } @@ -397,7 +399,7 @@ fn (r mut Readline) suppr_character() { fn (r mut Readline) commit_line() bool { r.previous_lines.insert(1, r.current) a := '\n'.ustring() - r.current = r.current + a + r.current = r.current.add( a ) r.cursor = r.current.len if r.is_tty { r.refresh_line()