Added support for interpreter directives
This commit is contained in:
parent
05238bced3
commit
1342a76786
31
src/main.rs
31
src/main.rs
@ -1,6 +1,7 @@
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use std::io::BufRead;
|
||||
use std::process;
|
||||
|
||||
use tokenizer::tokenize;
|
||||
use parser::{parse, ParseResult};
|
||||
@ -19,16 +20,22 @@ fn repl() {
|
||||
let mut stdout = io::stdout();
|
||||
let mut buf = String::with_capacity(20);
|
||||
loop {
|
||||
buf.clear();
|
||||
print!(">> ");
|
||||
stdout.flush().ok();
|
||||
let line = stdin.lock().read_line(&mut buf);
|
||||
|
||||
match line {
|
||||
Ok(_) => {
|
||||
if buf.is_empty() {
|
||||
break;
|
||||
}
|
||||
|
||||
if handle_interpreter_directive(&buf) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let tokens = tokenize(&buf);
|
||||
buf.clear();
|
||||
println!("Tokens: {:?}", tokens);
|
||||
|
||||
match parse(tokens) {
|
||||
@ -47,3 +54,25 @@ fn repl() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_interpreter_directive(input: &str) -> bool {
|
||||
|
||||
match input.chars().nth(0) {
|
||||
Some('.') => (),
|
||||
_ => return false
|
||||
}
|
||||
|
||||
let commands: Vec<&str> = input.split(|c: char| c.is_whitespace()).collect();
|
||||
match commands.get(0) {
|
||||
Some(s) if *s == ".quit" => {
|
||||
println!("Siturei simasu");
|
||||
process::exit(0);
|
||||
},
|
||||
Some(s) => {
|
||||
println!("Unknown directive: {}", s);
|
||||
},
|
||||
None => () //should never happen
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user