schala/src/main.rs

80 lines
2.0 KiB
Rust
Raw Normal View History

2015-12-20 13:20:24 -08:00
#![feature(advanced_slice_patterns, slice_patterns)]
extern crate simplerepl;
2015-08-14 17:07:02 -07:00
use std::path::Path;
use std::fs::File;
use std::io::Read;
2015-07-22 02:26:46 -07:00
2015-12-20 13:20:24 -08:00
use simplerepl::{REPL, ReplState};
2015-07-22 03:02:55 -07:00
use tokenizer::tokenize;
2015-07-22 03:02:55 -07:00
mod tokenizer;
2015-07-16 02:55:03 -07:00
2015-12-24 22:01:59 -08:00
use parser::{ParseResult, parse};
mod parser;
use evaluator::{evaluate};
mod evaluator;
2015-12-20 13:20:24 -08:00
2015-07-16 01:40:37 -07:00
fn main() {
2015-08-14 17:07:02 -07:00
let args: Vec<String> = std::env::args().collect();
2015-08-30 02:22:19 -07:00
println!("Schala v 0.02");
2015-08-14 17:07:02 -07:00
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();
panic!("Not implemented yet");
2015-08-14 17:07:02 -07:00
} else {
2015-12-20 13:20:24 -08:00
let initial_state = InterpreterState { show_tokens: false, show_parse: false };
REPL::with_prompt_and_state(Box::new(repl_handler), ">> ", initial_state)
.run();
2015-08-14 17:07:02 -07:00
}
2015-07-16 02:55:03 -07:00
}
2015-12-20 13:20:24 -08:00
struct InterpreterState {
show_tokens: bool,
show_parse: bool,
}
impl ReplState for InterpreterState {
fn update_state(&mut self, input: &Vec<&str>) {
match &input[..] {
["set", "show", "tokens", "true"] => {
self.show_tokens = true;
},
["set", "show", "tokens", "false"] => {
self.show_tokens = false;
},
["set", "show", "parse", "true"] => {
self.show_parse = true;
},
["set", "show", "parse", "false"] => {
self.show_parse = false;
},
_ => ()
}
}
}
fn repl_handler(input: &str, state: &mut InterpreterState) -> String {
if state.show_tokens {
2015-12-25 02:03:11 -08:00
println!("Tokens: {:?}", tokenize(input))
}
if state.show_parse {
println!("Parse: {:?}", parse(tokenize(input)))
2015-12-20 13:20:24 -08:00
}
let parse_result = parse(tokenize(input));
match parse_result {
Ok(ast) => {
format!("{}", evaluate(ast))
},
Err(err) => {
format!("Parse error: {:?}", err)
}
}
}