schala/src/main.rs

257 lines
7.6 KiB
Rust
Raw Normal View History

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;
extern crate linefeed;
2015-08-14 17:07:02 -07:00
use std::path::Path;
use std::fs::File;
use std::io::Read;
use std::process;
2015-07-22 03:02:55 -07:00
mod tokenizer;
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;
2017-01-21 01:49:45 -08:00
use language::{ProgrammingLanguage, ParseError, TokenError};
mod language;
2017-01-16 02:38:58 -08:00
use compilation::{compilation_sequence, compile_ast};
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");
2017-01-16 02:47:05 -08:00
let show_llvm = option_matches.opt_present("l");
2016-12-28 15:56:02 -08:00
match option_matches.free[..] {
[] | [_] => {
2017-01-16 02:47:05 -08:00
let mut repl = Repl::new(trace, show_llvm);
repl.run();
2016-12-29 02:04:03 -08:00
}
[_, ref filename, _..] => {
2017-01-23 01:32:57 -08:00
let language = Schala { };
run_noninteractive(filename, !option_matches.opt_present("i"), trace, &language);
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");
2017-01-16 02:47:05 -08:00
options.optflag("l",
"llvm-in-repl",
"Show LLVM IR in REPL");
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-23 01:32:57 -08:00
fn run_noninteractive<'a, T: ProgrammingLanguage<eval::Evaluator<'a>>>(filename: &str, compile: bool, trace_evaluation: bool, language: &T) {
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
2017-01-23 01:32:57 -08:00
let tokens = match T::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
2017-01-23 01:32:57 -08:00
let ast = match T::parse(tokens) {
2016-03-03 22:18:16 -08:00
Ok(ast) => ast,
2016-12-28 23:55:13 -08:00
Err(err) => {
println!("Parse error: {:?}", err.msg);
2017-01-23 01:32:57 -08:00
/*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 {
2017-01-23 01:32:57 -08:00
compilation_sequence(T::compile(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);
2017-01-23 01:32:57 -08:00
let results = T::evaluate(ast, &mut evaluator);
2016-03-04 14:32:22 -08:00
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>;
struct Repl<'a> {
2015-12-20 13:20:24 -08:00
show_tokens: bool,
show_parse: bool,
2017-01-16 02:38:58 -08:00
show_llvm_ir: bool,
evaluator: Evaluator<'a>,
interpreter_directive_sigil: char,
2017-01-12 22:43:26 -08:00
reader: LineReader,
2015-12-20 13:20:24 -08:00
}
impl<'a> Repl<'a> {
2017-01-16 02:47:05 -08:00
fn new(trace_evaluation: bool, show_llvm: 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(">> ");
Repl {
show_tokens: false,
show_parse: false,
2017-01-16 02:47:05 -08:00
show_llvm_ir: show_llvm,
evaluator: Evaluator::new_with_opts(None, trace_evaluation),
interpreter_directive_sigil: '.',
2017-01-12 22:43:26 -08:00
reader: reader,
}
}
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() {
Err(e) => {
2017-01-13 12:10:02 -08:00
println!("Terminal read error: {}", e);
},
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;
}
let output = self.input_handler(input);
2017-01-13 12:10:02 -08:00
println!("=> {}", output);
}
2017-01-13 12:10:02 -08:00
_ => (),
}
}
2017-01-13 12:10:02 -08:00
println!("Exiting...");
}
fn input_handler(&mut self, input: &str) -> String {
let schala = Schala { };
self.input_handler_new(input, schala)
}
fn input_handler_new<T: ProgrammingLanguage<eval::Evaluator<'a>>>(&mut self, input: &str, language: T) -> String {
2017-01-16 02:38:58 -08:00
let mut output = String::new();
let tokens = match T::tokenize(input) {
2017-01-16 02:38:58 -08:00
Ok(tokens) => tokens,
Err(err) => {
output.push_str(&format!("Tokenization error: {}\n", err.msg));
return output;
}
};
2017-01-16 02:38:58 -08:00
if self.show_tokens {
output.push_str(&format!("Tokens: {:?}\n", tokens));
}
let ast = match T::parse(tokens) {
2017-01-16 02:38:58 -08:00
Ok(ast) => ast,
Err(err) => {
output.push_str(&format!("Parse error: {:?}\n", err.msg));
return output;
}
};
if self.show_parse {
output.push_str(&format!("AST: {:?}\n", ast));
}
if self.show_llvm_ir {
2017-01-23 01:32:57 -08:00
let s = T::compile(ast);
2017-01-16 02:38:58 -08:00
output.push_str(&s);
} else {
// for now only handle last output
let mut full_output: Vec<String> = T::evaluate(ast, &mut self.evaluator);
2017-01-16 02:38:58 -08:00
output.push_str(&full_output.pop().unwrap_or("".to_string()));
}
output
}
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 mut iter = input.chars();
iter.next();
let trimmed_sigil: &str = iter.as_str();
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" => {
for item in self.reader.history() {
println!("{}", item);
}
},
"set" => {
let show = match commands[1] {
"show" => true,
"hide" => false,
e => {
println!("Bad `set` argument: {}", e);
return true;
}
};
match commands[2] {
"tokens" => self.show_tokens = show,
"parse" => self.show_parse = show,
"eval" => self.evaluator.trace_evaluation = show,
2017-01-16 02:38:58 -08:00
"llvm" => self.show_llvm_ir = show,
e => {
println!("Bad `show`/`hide` argument: {}", e);
return true;
}
}
},
2017-01-13 18:59:34 -08:00
e => println!("Unknown command: {}", e)
2015-12-20 13:20:24 -08:00
}
return true;
2015-12-20 13:20:24 -08:00
}
}
2017-01-21 01:49:45 -08:00
struct Schala { }
impl<'a> ProgrammingLanguage<eval::Evaluator<'a>> for Schala {
2017-01-21 01:49:45 -08:00
type Token = tokenizer::Token;
type AST = parser::AST;
fn tokenize(input: &str) -> Result<Vec<Self::Token>, TokenError> {
tokenizer::tokenize(input).map_err(|x| TokenError { msg: x.msg })
}
fn parse(input: Vec<Self::Token>) -> Result<Self::AST, ParseError> {
2017-01-21 14:32:03 -08:00
parser::parse(&input, &[]).map_err(|x| ParseError { msg: x.msg })
2017-01-21 01:49:45 -08:00
}
2017-01-23 01:32:57 -08:00
fn evaluate(ast: Self::AST, evaluator: &mut Evaluator) -> Vec<String> {
evaluator.run(ast)
2017-01-21 01:49:45 -08:00
}
2017-01-23 01:32:57 -08:00
fn compile(ast: Self::AST) -> String {
compile_ast(ast)
2017-01-21 01:49:45 -08:00
}
}