Use state features from simplerepl
This commit is contained in:
parent
3af7e6a409
commit
16dfbb27d5
42
src/main.rs
42
src/main.rs
@ -1,14 +1,17 @@
|
|||||||
|
#![feature(advanced_slice_patterns, slice_patterns)]
|
||||||
|
|
||||||
extern crate simplerepl;
|
extern crate simplerepl;
|
||||||
|
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
|
||||||
use simplerepl::REPL;
|
use simplerepl::{REPL, ReplState};
|
||||||
|
|
||||||
use tokenizer::tokenize;
|
use tokenizer::tokenize;
|
||||||
mod tokenizer;
|
mod tokenizer;
|
||||||
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args: Vec<String> = std::env::args().collect();
|
let args: Vec<String> = std::env::args().collect();
|
||||||
println!("Schala v 0.02");
|
println!("Schala v 0.02");
|
||||||
@ -18,10 +21,41 @@ fn main() {
|
|||||||
source_file.read_to_string(&mut buffer).unwrap();
|
source_file.read_to_string(&mut buffer).unwrap();
|
||||||
panic!("Not implemented yet");
|
panic!("Not implemented yet");
|
||||||
} else {
|
} else {
|
||||||
REPL::default(repl_handler).run();
|
let initial_state = InterpreterState { show_tokens: false, show_parse: false };
|
||||||
|
REPL::with_prompt_and_state(Box::new(repl_handler), ">> ", initial_state)
|
||||||
|
.run();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn repl_handler(input: &str) -> String {
|
struct InterpreterState {
|
||||||
format!("{:?}", tokenize(input))
|
show_tokens: bool,
|
||||||
|
show_parse: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ReplState for InterpreterState {
|
||||||
|
fn update_state(&mut self, input: &Vec<&str>) {
|
||||||
|
match &input[..] {
|
||||||
|
["set", "show", "tokens", "true"] => {
|
||||||
|
self.show_tokens = true;
|
||||||
|
},
|
||||||
|
["set", "show", "tokens", "false"] => {
|
||||||
|
self.show_tokens = false;
|
||||||
|
},
|
||||||
|
["set", "show", "parse", "true"] => {
|
||||||
|
self.show_parse = true;
|
||||||
|
},
|
||||||
|
["set", "show", "parse", "false"] => {
|
||||||
|
self.show_parse = false;
|
||||||
|
},
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn repl_handler(input: &str, state: &mut InterpreterState) -> String {
|
||||||
|
if state.show_tokens {
|
||||||
|
format!("Tokens: {:?}", tokenize(input))
|
||||||
|
} else {
|
||||||
|
format!("{:?}", tokenize(input))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user