schala/src/main.rs

154 lines
4.1 KiB
Rust
Raw Normal View History

2016-01-17 00:16:01 -08:00
#![feature(advanced_slice_patterns, slice_patterns, box_patterns)]
extern crate simplerepl;
2016-12-28 15:56:02 -08:00
extern crate getopts;
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-12-29 02:04:03 -08:00
use parser::parse;
2015-12-24 22:01:59 -08:00
mod parser;
2016-12-29 02:04:03 -08:00
use eval::Evaluator;
2016-01-18 02:24:14 -08:00
mod eval;
2016-12-29 02:04:03 -08:00
use compilation::compilation_sequence;
2016-03-04 14:32:22 -08:00
mod compilation;
2016-12-26 23:29:24 -08:00
mod llvm_wrap;
2016-02-11 10:49:45 -08:00
2015-07-16 01:40:37 -07:00
fn main() {
2016-12-29 02:04:03 -08:00
let option_matches =
program_options().parse(std::env::args()).expect("Could not parse options");
2017-01-09 04:26:42 -08:00
let trace = option_matches.opt_present("t");
2016-12-28 15:56:02 -08:00
match option_matches.free[..] {
[] | [_] => {
2017-01-09 04:26:42 -08:00
run_repl(trace);
2016-12-29 02:04:03 -08:00
}
[_, ref filename, _..] => {
2017-01-09 04:26:42 -08:00
run_noninteractive(filename, !option_matches.opt_present("i"), trace);
2016-12-28 15:56:02 -08:00
}
};
}
fn program_options() -> getopts::Options {
let mut options = getopts::Options::new();
2016-12-29 02:04:03 -08:00
options.optflag("i",
"interpret",
"Interpret source file instead of compiling");
2017-01-09 04:26:42 -08:00
options.optflag("t",
"trace-evaluation",
"Print out trace of evaluation");
2016-12-28 15:56:02 -08:00
options
2016-03-03 22:18:16 -08:00
}
2016-01-24 22:25:12 -08:00
2017-01-09 04:26:42 -08:00
fn run_noninteractive(filename: &str, compile: bool, trace_evaluation: bool) {
2016-03-03 22:18:16 -08:00
let mut source_file = File::open(&Path::new(filename)).unwrap();
let mut buffer = String::new();
source_file.read_to_string(&mut buffer).unwrap();
2016-01-24 22:25:12 -08:00
2016-03-03 22:18:16 -08:00
let tokens = match tokenize(&buffer) {
2016-12-28 22:52:23 -08:00
Ok(t) => t,
Err(e) => {
2016-12-28 23:55:13 -08:00
println!("Tokenization error: {}", e.msg);
std::process::exit(1)
2016-12-28 22:52:23 -08:00
}
2016-03-03 22:18:16 -08:00
};
2016-01-24 22:25:12 -08:00
2016-03-03 22:18:16 -08:00
let ast = match parse(&tokens, &[]) {
Ok(ast) => ast,
2016-12-28 23:55:13 -08:00
Err(err) => {
println!("Parse error: {:?}", err.msg);
println!("Remaining tokens: {:?}", err.remaining_tokens);
2016-12-28 23:55:13 -08:00
std::process::exit(1)
}
2016-03-03 22:18:16 -08:00
};
2016-01-24 22:25:12 -08:00
2016-03-04 14:32:22 -08:00
if compile {
compilation_sequence(ast, filename);
2016-03-04 14:32:22 -08:00
} else {
2017-01-09 04:26:42 -08:00
let mut evaluator = Evaluator::new_with_opts(None, trace_evaluation);
2016-03-04 14:32:22 -08:00
let results = evaluator.run(ast);
for result in results.iter() {
println!("{}", result);
}
2015-08-14 17:07:02 -07:00
}
2015-07-16 02:55:03 -07:00
}
2017-01-09 04:26:42 -08:00
fn run_repl(trace_evaluation: bool) {
2016-03-03 22:18:16 -08:00
println!("Schala v 0.02");
let initial_state = InterpreterState {
show_tokens: false,
show_parse: false,
2017-01-09 04:26:42 -08:00
show_eval: trace_evaluation,
evaluator: Evaluator::new_with_opts(None, trace_evaluation),
2016-03-03 22:18:16 -08:00
};
2016-12-29 02:04:03 -08:00
REPL::with_prompt_and_state(Box::new(repl_handler), ">> ", initial_state).run();
2016-03-03 22:18:16 -08:00
}
struct InterpreterState<'a> {
2015-12-20 13:20:24 -08:00
show_tokens: bool,
show_parse: bool,
2017-01-09 04:26:42 -08:00
show_eval: bool,
evaluator: Evaluator<'a>,
2015-12-20 13:20:24 -08:00
}
impl<'a> ReplState for InterpreterState<'a> {
2015-12-20 13:20:24 -08:00
fn update_state(&mut self, input: &Vec<&str>) {
2016-12-18 22:04:36 -08:00
match input[..] {
2015-12-20 13:20:24 -08:00
["set", "show", "tokens", "true"] => {
self.show_tokens = true;
2016-12-29 02:04:03 -08:00
}
2015-12-20 13:20:24 -08:00
["set", "show", "tokens", "false"] => {
self.show_tokens = false;
2016-12-29 02:04:03 -08:00
}
2015-12-20 13:20:24 -08:00
["set", "show", "parse", "true"] => {
self.show_parse = true;
2016-12-29 02:04:03 -08:00
}
2015-12-20 13:20:24 -08:00
["set", "show", "parse", "false"] => {
self.show_parse = false;
2016-12-29 02:04:03 -08:00
}
2017-01-09 04:26:42 -08:00
["set", "show", "eval", "true"] => {
self.evaluator.trace_evaluation = true;
}
["set", "show", "eval", "false"] => {
self.evaluator.trace_evaluation = false;
}
2016-12-29 02:04:03 -08:00
_ => (),
2015-12-20 13:20:24 -08:00
}
}
}
fn repl_handler(input: &str, state: &mut InterpreterState) -> String {
let mut result = String::new();
2016-01-06 23:48:53 -08:00
let tokens = match tokenize(input) {
2016-12-29 02:09:44 -08:00
Err(e) => return format!("Tokenization error: {}", e.msg),
2016-12-29 02:04:03 -08:00
Ok(t) => t,
2016-01-06 23:48:53 -08:00
};
2015-12-20 13:20:24 -08:00
if state.show_tokens {
result.push_str(&format!("Tokens: {:?}\n", 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,
2016-03-02 23:55:21 -08:00
Err(err) => return format!("Parse error: {}", err.msg),
2016-01-10 01:15:34 -08:00
};
2015-12-25 02:03:11 -08:00
if state.show_parse {
result.push_str(&format!("AST: {:?}\n", 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
2016-12-29 02:04:03 -08:00
// for now only handle last output
let interpreter_result = output.pop().unwrap_or("".to_string());
result.push_str(&interpreter_result);
result
}