2016-01-17 00:16:01 -08:00
|
|
|
#![feature(advanced_slice_patterns, slice_patterns, box_patterns)]
|
2016-12-28 15:56:02 -08:00
|
|
|
extern crate getopts;
|
2017-01-12 20:56:23 -08:00
|
|
|
extern crate linefeed;
|
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;
|
2017-01-12 20:56:23 -08:00
|
|
|
use std::process;
|
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-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 =
|
2017-01-09 22:56:29 -08:00
|
|
|
match program_options().parse(std::env::args()) {
|
|
|
|
Ok(o) => o,
|
|
|
|
Err(e) => {
|
|
|
|
println!("{:?}", e);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
};
|
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-12 20:56:23 -08:00
|
|
|
let mut repl = Repl::new(trace);
|
|
|
|
repl.run();
|
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) => {
|
2016-12-31 17:02:49 -08:00
|
|
|
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 {
|
2016-12-25 22:37:46 -08:00
|
|
|
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-12 22:43:26 -08:00
|
|
|
type LineReader = linefeed::Reader<linefeed::terminal::DefaultTerminal>;
|
2017-01-12 20:56:23 -08:00
|
|
|
struct Repl<'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,
|
2017-01-03 01:53:44 -08:00
|
|
|
evaluator: Evaluator<'a>,
|
2017-01-12 20:56:23 -08:00
|
|
|
interpreter_directive_sigil: char,
|
2017-01-12 22:43:26 -08:00
|
|
|
reader: LineReader,
|
2015-12-20 13:20:24 -08:00
|
|
|
}
|
|
|
|
|
2017-01-12 20:56:23 -08:00
|
|
|
impl<'a> Repl<'a> {
|
|
|
|
fn new(trace_evaluation: bool) -> Repl<'a> {
|
2017-01-13 12:10:02 -08:00
|
|
|
let mut reader: linefeed::Reader<_> = linefeed::Reader::new("Schala").unwrap();
|
2017-01-12 22:43:26 -08:00
|
|
|
reader.set_prompt(">> ");
|
2017-01-12 20:56:23 -08:00
|
|
|
Repl {
|
|
|
|
show_tokens: false,
|
|
|
|
show_parse: false,
|
|
|
|
show_eval: false,
|
|
|
|
evaluator: Evaluator::new_with_opts(None, trace_evaluation),
|
|
|
|
interpreter_directive_sigil: '.',
|
2017-01-12 22:43:26 -08:00
|
|
|
reader: reader,
|
2017-01-12 20:56:23 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
fn run(&mut self) {
|
|
|
|
use linefeed::ReadResult::*;
|
|
|
|
println!("Schala v 0.02");
|
|
|
|
loop {
|
2017-01-12 22:43:26 -08:00
|
|
|
match self.reader.read_line() {
|
2017-01-12 20:56:23 -08:00
|
|
|
Err(e) => {
|
2017-01-13 12:10:02 -08:00
|
|
|
println!("Terminal read error: {}", e);
|
2017-01-12 20:56:23 -08:00
|
|
|
},
|
|
|
|
Ok(Eof) => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(Input(ref input)) => {
|
2017-01-13 12:10:02 -08:00
|
|
|
self.reader.add_history(input.clone());
|
2017-01-12 21:51:00 -08:00
|
|
|
if self.handle_interpreter_directive(input) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-01-12 20:56:23 -08:00
|
|
|
let output = self.input_handler(input);
|
2017-01-13 12:10:02 -08:00
|
|
|
println!("=> {}", output);
|
2017-01-12 20:56:23 -08:00
|
|
|
}
|
2017-01-13 12:10:02 -08:00
|
|
|
_ => (),
|
2017-01-12 20:56:23 -08:00
|
|
|
}
|
|
|
|
}
|
2017-01-13 12:10:02 -08:00
|
|
|
println!("Exiting...");
|
2017-01-12 20:56:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
fn input_handler(&mut self, input: &str) -> String {
|
|
|
|
let mut result = String::new();
|
|
|
|
|
|
|
|
let tokens = match tokenize(input) {
|
|
|
|
Err(e) => return format!("Tokenization error: {}", e.msg),
|
|
|
|
Ok(t) => t,
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.show_tokens {
|
|
|
|
result.push_str(&format!("Tokens: {:?}\n", tokens));
|
|
|
|
}
|
|
|
|
|
|
|
|
let ast = match parse(&tokens, &[]) {
|
|
|
|
Ok(ast) => ast,
|
|
|
|
Err(err) => return format!("Parse error: {}", err.msg),
|
|
|
|
};
|
|
|
|
|
|
|
|
if self.show_parse {
|
|
|
|
result.push_str(&format!("AST: {:?}\n", ast));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut output: Vec<String> = self.evaluator.run(ast);
|
|
|
|
|
|
|
|
// for now only handle last output
|
|
|
|
let interpreter_result = output.pop().unwrap_or("".to_string());
|
|
|
|
result.push_str(&interpreter_result);
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
fn handle_interpreter_directive(&mut self, input: &str) -> bool {
|
|
|
|
match input.chars().nth(0) {
|
|
|
|
Some(ch) if ch == self.interpreter_directive_sigil => (),
|
|
|
|
_ => return false
|
|
|
|
}
|
|
|
|
|
|
|
|
let trimmed_sigil: String = input.chars()
|
|
|
|
.skip(1)
|
|
|
|
.collect();
|
|
|
|
let commands: Vec<&str> = trimmed_sigil
|
|
|
|
.split_whitespace()
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let cmd: &str = match commands.get(0).clone() { None => return true, Some(s) => s };
|
|
|
|
|
|
|
|
match cmd {
|
|
|
|
"exit" | "quit" => process::exit(0),
|
|
|
|
"history" => {
|
|
|
|
},
|
|
|
|
"output_history" => {
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
self.update_state(&commands);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|