diff options
author | Freya Murphy <freya@freyacat.org> | 2024-02-29 17:04:28 -0500 |
---|---|---|
committer | Freya Murphy <freya@freyacat.org> | 2024-02-29 17:04:28 -0500 |
commit | 5d2747e26f51cc2344a6bd95f93457248fdfebd8 (patch) | |
tree | 8755b4068166c3854d26817683ce438a771ab319 /matrix-bin/src/repl.rs | |
parent | more mat, sys, and os stdlib functions, better matrix printing, other fixes (diff) | |
download | matrix-5d2747e26f51cc2344a6bd95f93457248fdfebd8.tar.gz matrix-5d2747e26f51cc2344a6bd95f93457248fdfebd8.tar.bz2 matrix-5d2747e26f51cc2344a6bd95f93457248fdfebd8.zip |
fin prob
Diffstat (limited to 'matrix-bin/src/repl.rs')
-rw-r--r-- | matrix-bin/src/repl.rs | 53 |
1 files changed, 35 insertions, 18 deletions
diff --git a/matrix-bin/src/repl.rs b/matrix-bin/src/repl.rs index f2964d4..fe9975f 100644 --- a/matrix-bin/src/repl.rs +++ b/matrix-bin/src/repl.rs @@ -1,21 +1,26 @@ use std::{io::Write, sync::atomic::Ordering}; - -use matrix::{value::Value, vm::Interupt}; -use rustyline::{Config, EditMode, ColorMode, Editor, CompletionType}; +use rustyline::{Config, EditMode, ColorMode, Editor, CompletionType, error::ReadlineError}; +use matrix_lang::prelude::*; use crate::{State, helper::MatrixHelper}; -pub struct Repl<'a> { - state: State<'a> +pub struct Repl<'s, 'a> { + state: &'s mut State<'a> } -impl<'a> Repl<'a> { +impl<'s, 'a> Repl<'s, 'a> { - pub fn new(state: State<'a>) -> Self { + pub fn new(state: &'s mut State<'a>) -> Self { Self { state } } - pub fn run(&mut self) { + fn execute(&mut self, line: String) -> Result<Value> { + let fun = self.state.compile(line)?; + let val = self.state.execute(fun)?; + Ok(val) + } + + pub fn run(&mut self) { let interupt = self.state.vm.borrow().interupt(); ctrlc::set_handler(move || { @@ -23,26 +28,33 @@ impl<'a> Repl<'a> { }).unwrap(); let config = Config::builder() + .indent_size(4) + .edit_mode(EditMode::Emacs) .check_cursor_position(true) .completion_type(CompletionType::List) - .edit_mode(EditMode::Emacs) .color_mode(if self.state.color { ColorMode::Enabled } else { ColorMode::Disabled }) .build(); let helper = MatrixHelper::new(self.state.vm.clone()); + let histfile = std::env::var("MATRIX_HISTORY").ok(); + let mut rl = Editor::with_config(config).unwrap(); rl.set_helper(Some(helper)); + if let Some(hf) = &histfile { + rl.load_history(hf).ok(); + } loop { - let Ok(line) = rl.readline(">> ") else { - break; - }; - if let Err(_) = rl.add_history_entry(&line) { - break; + let line = match rl.readline(">> ") { + Ok(line) => line, + Err(ReadlineError::Eof) => break, + Err(_) => continue, }; - match self.state.execute(line) { - Err(err) => crate::error(err, &self.state), + + rl.add_history_entry(&line).ok(); + + match self.execute(line) { Ok(val) => { if val != Value::Nil { if self.state.color { @@ -52,8 +64,13 @@ impl<'a> Repl<'a> { } } } - } - let _ = std::io::stdout().flush(); + Err(err) => crate::error(err, &self.state), + }; + std::io::stdout().flush().ok(); + } + + if let Some(hf) = &histfile { + rl.save_history(hf).ok(); } } |