From caa331ecdce4782bcc8f696d9ca9acc7675e91d5 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 14 Aug 2015 17:07:02 -0700 Subject: [PATCH] Read from file as well as repl --- src/main.rs | 20 +++++++++++++++++++- test.lang | 4 ++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test.lang diff --git a/src/main.rs b/src/main.rs index 020a55e..73ad14b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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,9 +35,24 @@ type BinopTable = HashMap<&'static str, i32>; thread_local!(static BINOP_TABLE: RefCell = RefCell::new(HashMap::new())); fn main() { + let args: Vec = std::env::args().collect(); println!("Unnamed language 0.01"); init_binop_table(); - repl(); + 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() { diff --git a/test.lang b/test.lang new file mode 100644 index 0000000..37075bf --- /dev/null +++ b/test.lang @@ -0,0 +1,4 @@ +let a = 1 +let b = 2 +a + b +