2016-01-17 00:16:01 -08:00
|
|
|
#![feature(advanced_slice_patterns, slice_patterns, box_patterns)]
|
2015-12-18 23:40:30 -08:00
|
|
|
extern crate simplerepl;
|
2016-12-28 15:56:02 -08:00
|
|
|
extern crate getopts;
|
2015-12-18 23:40:30 -08:00
|
|
|
|
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
|
|
|
|
2015-12-18 23:40:30 -08: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;
|
|
|
|
|
2016-12-25 22:37:46 -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-28 15:56:02 -08:00
|
|
|
let option_matches = program_options().parse(std::env::args()).expect("Could not parse options");
|
|
|
|
match option_matches.free[..] {
|
|
|
|
[] | [_] => {
|
|
|
|
run_repl();
|
|
|
|
},
|
|
|
|
[_, ref filename, ..] => {
|
|
|
|
run_noninteractive(filename);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
fn program_options() -> getopts::Options {
|
|
|
|
let mut options = getopts::Options::new();
|
|
|
|
options.optflag("i", "interpret", "Interpret source file instead of compiling");
|
|
|
|
options
|
2016-03-03 22:18:16 -08:00
|
|
|
}
|
2016-01-24 22:25:12 -08:00
|
|
|
|
2016-03-03 22:18:16 -08:00
|
|
|
fn run_noninteractive(filename: &String) {
|
|
|
|
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) {
|
|
|
|
Some(t) => t,
|
|
|
|
None => { println!("Tokenization error"); return; }
|
|
|
|
};
|
2016-01-24 22:25:12 -08:00
|
|
|
|
2016-03-03 22:18:16 -08:00
|
|
|
let ast = match parse(&tokens, &[]) {
|
|
|
|
Ok(ast) => ast,
|
|
|
|
Err(err) => { println!("Parse error: {:?}", err); return; }
|
|
|
|
};
|
2016-01-24 22:25:12 -08:00
|
|
|
|
2016-03-04 14:32:22 -08:00
|
|
|
let compile = true;
|
|
|
|
|
|
|
|
if compile {
|
2016-12-25 22:37:46 -08:00
|
|
|
compilation_sequence(ast, filename);
|
2016-03-04 14:32:22 -08:00
|
|
|
} else {
|
|
|
|
let mut evaluator = Evaluator::new();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-03-03 22:18:16 -08:00
|
|
|
fn run_repl() {
|
|
|
|
println!("Schala v 0.02");
|
|
|
|
let initial_state = InterpreterState {
|
|
|
|
show_tokens: false,
|
|
|
|
show_parse: false,
|
|
|
|
evaluator: Evaluator::new(),
|
|
|
|
};
|
|
|
|
REPL::with_prompt_and_state(Box::new(repl_handler), ">> ", initial_state)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
2015-12-20 13:20:24 -08:00
|
|
|
struct InterpreterState {
|
|
|
|
show_tokens: bool,
|
|
|
|
show_parse: bool,
|
2016-01-22 02:20:12 -08:00
|
|
|
evaluator: Evaluator,
|
2015-12-20 13:20:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ReplState for InterpreterState {
|
|
|
|
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;
|
|
|
|
},
|
|
|
|
["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-12-21 23:29:14 -08:00
|
|
|
let mut result = String::new();
|
|
|
|
|
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-12-21 23:29:14 -08:00
|
|
|
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 {
|
2016-12-21 23:29:14 -08:00
|
|
|
result.push_str(&format!("AST: {:?}\n", ast));
|
2015-12-20 13:20:24 -08:00
|
|
|
}
|
2015-12-30 15:09:31 -08:00
|
|
|
|
2016-01-22 02:20:12 -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
|
2016-12-21 23:29:14 -08:00
|
|
|
let interpreter_result = output.pop().unwrap_or("".to_string());
|
|
|
|
result.push_str(&interpreter_result);
|
|
|
|
result
|
2015-07-24 03:08:54 -07:00
|
|
|
}
|