schala/src/main.rs

89 lines
2.2 KiB
Rust
Raw Normal View History

2016-01-17 00:16:01 -08:00
#![feature(advanced_slice_patterns, slice_patterns, box_patterns)]
2015-12-20 13:20:24 -08:00
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
2016-01-17 00:16:01 -08:00
use parser::{parse};
2015-12-24 22:01:59 -08:00
mod parser;
2016-01-18 02:24:14 -08:00
use eval::{Evaluator};
mod eval;
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 {
let initial_state = InterpreterState {
show_tokens: false,
show_parse: false,
evaluator: Evaluator::new(),
};
2015-12-20 13:20:24 -08:00
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,
evaluator: Evaluator,
2015-12-20 13:20:24 -08:00
}
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 {
2016-01-06 23:48:53 -08:00
let tokens = match tokenize(input) {
2016-01-11 00:49:29 -08:00
None => return format!("Tokenization error"),
2016-01-06 23:48:53 -08:00
Some(t) => t
};
2015-12-20 13:20:24 -08:00
if state.show_tokens {
2016-01-06 23:48:53 -08:00
println!("Tokens: {:?}", tokens);
2015-12-25 02:03:11 -08:00
}
2016-01-10 01:15:34 -08:00
let ast = match parse(&tokens, &[]) {
Ok(ast) => ast,
Err(err) => return err.msg
};
2015-12-25 02:03:11 -08:00
if state.show_parse {
2016-01-10 01:15:34 -08:00
println!("AST: {:?}", ast);
2015-12-20 13:20:24 -08:00
}
let mut output: Vec<String> = state.evaluator.run(ast);
2016-01-18 02:24:14 -08:00
//for now only handle last output
output.pop().unwrap_or("".to_string())
}