Added support for interpreter directives

This commit is contained in:
greg 2015-07-24 03:08:54 -07:00
parent 05238bced3
commit 1342a76786
1 changed files with 30 additions and 1 deletions

View File

@ -1,6 +1,7 @@
use std::io; use std::io;
use std::io::Write; use std::io::Write;
use std::io::BufRead; use std::io::BufRead;
use std::process;
use tokenizer::tokenize; use tokenizer::tokenize;
use parser::{parse, ParseResult}; use parser::{parse, ParseResult};
@ -19,16 +20,22 @@ fn repl() {
let mut stdout = io::stdout(); let mut stdout = io::stdout();
let mut buf = String::with_capacity(20); let mut buf = String::with_capacity(20);
loop { loop {
buf.clear();
print!(">> "); print!(">> ");
stdout.flush().ok(); stdout.flush().ok();
let line = stdin.lock().read_line(&mut buf); let line = stdin.lock().read_line(&mut buf);
match line { match line {
Ok(_) => { Ok(_) => {
if buf.is_empty() { if buf.is_empty() {
break; break;
} }
if handle_interpreter_directive(&buf) {
continue;
}
let tokens = tokenize(&buf); let tokens = tokenize(&buf);
buf.clear();
println!("Tokens: {:?}", tokens); println!("Tokens: {:?}", tokens);
match parse(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;
}