2015-07-16 02:55:03 -07:00
|
|
|
use std::io;
|
|
|
|
use std::io::Write;
|
|
|
|
use std::io::BufRead;
|
2015-07-24 03:08:54 -07:00
|
|
|
use std::process;
|
2015-08-06 00:53:50 -07:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::collections::HashMap;
|
2015-07-22 02:26:46 -07:00
|
|
|
|
2015-07-22 03:12:01 -07:00
|
|
|
use tokenizer::tokenize;
|
|
|
|
use parser::{parse, ParseResult};
|
2015-07-30 02:44:52 -07:00
|
|
|
use evaluate::{evaluate, Environment};
|
2015-07-22 03:02:55 -07:00
|
|
|
|
|
|
|
mod tokenizer;
|
2015-07-22 03:12:01 -07:00
|
|
|
mod parser;
|
2015-07-29 00:52:17 -07:00
|
|
|
mod evaluate;
|
2015-07-22 02:26:46 -07:00
|
|
|
|
2015-08-06 00:53:50 -07:00
|
|
|
type BinopTable = HashMap<&'static str, i32>;
|
|
|
|
|
|
|
|
thread_local!(static BINOP_TABLE: RefCell<BinopTable> = RefCell::new(HashMap::new()));
|
2015-07-16 02:55:03 -07:00
|
|
|
|
2015-07-16 01:40:37 -07:00
|
|
|
fn main() {
|
2015-07-16 02:55:03 -07:00
|
|
|
println!("Unnamed language 0.01");
|
2015-08-06 00:53:50 -07:00
|
|
|
init_binop_table();
|
2015-07-16 02:55:03 -07:00
|
|
|
repl();
|
|
|
|
}
|
|
|
|
|
2015-08-06 00:53:50 -07:00
|
|
|
fn init_binop_table() {
|
|
|
|
BINOP_TABLE.with(|hm| {
|
|
|
|
macro_rules! insert_precedence {
|
|
|
|
($op:expr, $prec:expr) => { hm.borrow_mut().insert($op, $prec) }
|
|
|
|
}
|
|
|
|
insert_precedence!("+", 20);
|
|
|
|
insert_precedence!("-", 20);
|
|
|
|
insert_precedence!("*", 40);
|
|
|
|
insert_precedence!("/", 40);
|
|
|
|
insert_precedence!("==", 10);
|
|
|
|
insert_precedence!(">", 15);
|
|
|
|
insert_precedence!("<", 15);
|
|
|
|
insert_precedence!("<=>", 15);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-07-16 02:55:03 -07:00
|
|
|
fn repl() {
|
2015-07-19 16:53:37 -07:00
|
|
|
let stdin = io::stdin();
|
2015-07-16 02:55:03 -07:00
|
|
|
let mut stdout = io::stdout();
|
|
|
|
let mut buf = String::with_capacity(20);
|
2015-07-31 01:35:39 -07:00
|
|
|
let mut env = Environment::new();
|
2015-07-16 02:55:03 -07:00
|
|
|
loop {
|
2015-07-24 03:08:54 -07:00
|
|
|
buf.clear();
|
2015-07-16 02:55:03 -07:00
|
|
|
print!(">> ");
|
|
|
|
stdout.flush().ok();
|
|
|
|
let line = stdin.lock().read_line(&mut buf);
|
2015-07-24 03:08:54 -07:00
|
|
|
|
2015-07-16 02:55:03 -07:00
|
|
|
match line {
|
2015-07-19 16:53:37 -07:00
|
|
|
Ok(_) => {
|
2015-07-19 17:11:22 -07:00
|
|
|
if buf.is_empty() {
|
|
|
|
break;
|
|
|
|
}
|
2015-07-24 03:08:54 -07:00
|
|
|
|
2015-07-31 01:38:35 -07:00
|
|
|
if handle_interpreter_directive(&buf, &env) {
|
2015-07-24 03:08:54 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-07-18 14:50:26 -07:00
|
|
|
let tokens = tokenize(&buf);
|
|
|
|
println!("Tokens: {:?}", tokens);
|
|
|
|
|
2015-07-20 01:46:02 -07:00
|
|
|
match parse(tokens) {
|
2015-07-29 00:52:17 -07:00
|
|
|
ParseResult::Ok(ast) => {
|
|
|
|
println!("AST: {:?}", ast);
|
2015-07-30 02:44:52 -07:00
|
|
|
|
2015-07-31 01:35:39 -07:00
|
|
|
let (eval, new_env) = evaluate(ast, env);
|
2015-07-29 00:52:17 -07:00
|
|
|
println!("{}", eval);
|
2015-07-31 01:35:39 -07:00
|
|
|
env = new_env;
|
2015-07-29 00:52:17 -07:00
|
|
|
},
|
2015-07-22 02:48:27 -07:00
|
|
|
ParseResult::Err(err) => println!("Error: {}", err)
|
2015-07-20 01:46:02 -07:00
|
|
|
}
|
2015-07-16 02:55:03 -07:00
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
println!("Error: {}", err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-24 03:08:54 -07:00
|
|
|
|
2015-07-31 01:38:35 -07:00
|
|
|
fn handle_interpreter_directive(input: &str, env: &Environment) -> bool {
|
2015-07-24 03:08:54 -07:00
|
|
|
|
|
|
|
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);
|
|
|
|
},
|
2015-07-31 01:38:35 -07:00
|
|
|
Some(s) if *s == ".env" => {
|
|
|
|
env.display();
|
|
|
|
},
|
2015-08-06 02:16:15 -07:00
|
|
|
Some(s) if *s == ".prec" => {
|
|
|
|
BINOP_TABLE.with(|hm| {
|
|
|
|
|
|
|
|
println!("{0: <10} | {1: <10}", "operator", "precedence");
|
|
|
|
let prec_table = hm.borrow();
|
|
|
|
for (op, prec) in prec_table.iter() {
|
|
|
|
println!("{0: <10} | {1: <10}", op, prec);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2015-07-24 03:08:54 -07:00
|
|
|
Some(s) => {
|
|
|
|
println!("Unknown directive: {}", s);
|
|
|
|
},
|
|
|
|
None => () //should never happen
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|