Read from file as well as repl

This commit is contained in:
greg 2015-08-14 17:07:02 -07:00
parent ae3a030ad8
commit caa331ecdc
2 changed files with 23 additions and 1 deletions

View File

@ -4,6 +4,9 @@ use std::io::BufRead;
use std::process;
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::Path;
use std::fs::File;
use std::io::Read;
use tokenizer::tokenize;
use parser::{parse};
@ -32,10 +35,25 @@ type BinopTable = HashMap<&'static str, i32>;
thread_local!(static BINOP_TABLE: RefCell<BinopTable> = RefCell::new(HashMap::new()));
fn main() {
let args: Vec<String> = std::env::args().collect();
println!("Unnamed language 0.01");
init_binop_table();
if let Some(filename) = args.get(1) {
let mut source_file = File::open(&Path::new(filename)).unwrap();
let mut buffer = String::new();
source_file.read_to_string(&mut buffer).unwrap();
match parse(tokenize(&buffer)) {
Ok(ast) => {
let (result, env) = evaluate(ast, Environment::new());
println!("{}", result);
},
Err(err) => println!("{}", err)
}
} else {
repl();
}
}
fn init_binop_table() {
BINOP_TABLE.with(|hm| {

4
test.lang Normal file
View File

@ -0,0 +1,4 @@
let a = 1
let b = 2
a + b