Read from file as well as repl
This commit is contained in:
parent
ae3a030ad8
commit
caa331ecdc
18
src/main.rs
18
src/main.rs
@ -4,6 +4,9 @@ use std::io::BufRead;
|
|||||||
use std::process;
|
use std::process;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::path::Path;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
use tokenizer::tokenize;
|
use tokenizer::tokenize;
|
||||||
use parser::{parse};
|
use parser::{parse};
|
||||||
@ -32,10 +35,25 @@ type BinopTable = HashMap<&'static str, i32>;
|
|||||||
thread_local!(static BINOP_TABLE: RefCell<BinopTable> = RefCell::new(HashMap::new()));
|
thread_local!(static BINOP_TABLE: RefCell<BinopTable> = RefCell::new(HashMap::new()));
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let args: Vec<String> = std::env::args().collect();
|
||||||
println!("Unnamed language 0.01");
|
println!("Unnamed language 0.01");
|
||||||
init_binop_table();
|
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();
|
repl();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn init_binop_table() {
|
fn init_binop_table() {
|
||||||
BINOP_TABLE.with(|hm| {
|
BINOP_TABLE.with(|hm| {
|
||||||
|
Loading…
Reference in New Issue
Block a user