Skip to content

Commit

Permalink
Merge pull request #2 from amiremohamadi/feature/repl-history
Browse files Browse the repository at this point in the history
Support History on REPL
  • Loading branch information
ali77gh committed Oct 6, 2023
2 parents bce9771 + 1638660 commit 389ff85
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ path = "src/main.rs"

[dependencies]
rand = "0.8.5"
rustyline = "12.0.0"

[profile.release]
opt-level = 3
lto = true
codegen-units = 1
overflow-checks=false
panic = "abort"
panic = "abort"
27 changes: 14 additions & 13 deletions src/repl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::{io::{self, Write}, process::exit};
use std::process::exit;

use crate::{
parser::Parser,
preprocessor::Preprocessor,
runtime::Runtime, common::errors::ErrorType
};

use rustyline::DefaultEditor;


pub fn start_rpel(){

Expand All @@ -14,16 +16,23 @@ pub fn start_rpel(){

let parser = Parser;

let mut reader = DefaultEditor::new().unwrap(); // TODO: handle error

let mut runtime = Runtime::new(|msg|{
println!("{}", msg);
},||{
input()
String::from("")
});

loop {
print!("-> ");
let _ = io::stdout().flush();
let source = input();
// let source: String;
let source = match reader.readline("-> ") {
Ok(line) => {
let _ = reader.add_history_entry(line.clone());
line
},
Err(_) => break,
};
let lines = preprocessor.on_new_line(source);
for line in lines{
let el = parser.on_new_line(line);
Expand All @@ -49,11 +58,3 @@ pub fn start_rpel(){
}
}
}

fn input() -> String{
let mut buffer = String::new();
let stdin = io::stdin(); // We get `Stdin` here.
stdin.read_line(&mut buffer).unwrap();
buffer = buffer.replace("\n", "").trim().to_string();
buffer
}

0 comments on commit 389ff85

Please sign in to comment.