2017-08-29 00:28:19 -07:00
|
|
|
pub mod tokenizer;
|
|
|
|
pub mod parser;
|
|
|
|
pub mod eval;
|
|
|
|
pub mod compilation;
|
2017-02-03 11:34:26 -08:00
|
|
|
|
2017-08-31 00:02:17 -07:00
|
|
|
use language::{ProgrammingLanguageInterface, EvalOptions, LLVMCodeString};
|
2017-01-25 20:09:51 -08:00
|
|
|
|
2017-08-29 00:28:19 -07:00
|
|
|
pub use self::eval::Evaluator as MaaruEvaluator;
|
2017-01-25 20:09:51 -08:00
|
|
|
|
2017-08-31 00:02:17 -07:00
|
|
|
pub struct Maaru<'a> {
|
2017-08-30 19:15:04 -07:00
|
|
|
evaluator: MaaruEvaluator<'a>
|
|
|
|
}
|
|
|
|
|
2017-08-31 00:02:17 -07:00
|
|
|
impl<'a> Maaru<'a> {
|
|
|
|
pub fn new() -> Maaru<'a> {
|
|
|
|
Maaru {
|
2017-08-30 19:15:04 -07:00
|
|
|
evaluator: MaaruEvaluator::new(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 00:02:17 -07:00
|
|
|
impl<'a> ProgrammingLanguageInterface for Maaru<'a> {
|
2017-08-30 19:15:04 -07:00
|
|
|
fn get_language_name(&self) -> String {
|
|
|
|
"Maaru".to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn evaluate_in_repl(&mut self, input: &str, options: EvalOptions) -> Vec<String> {
|
|
|
|
let mut output = vec![];
|
|
|
|
let tokens = match tokenizer::tokenize(input) {
|
|
|
|
Ok(tokens) => {
|
|
|
|
if options.debug_tokens {
|
|
|
|
output.push(format!("{:?}", tokens));
|
|
|
|
}
|
|
|
|
tokens
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
let msg = format!("Tokenization error: {:?}\n", err.msg);
|
|
|
|
output.push(msg);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let ast = match parser::parse(&tokens, &[]) {
|
|
|
|
Ok(ast) => {
|
|
|
|
if options.debug_parse {
|
|
|
|
output.push(format!("{:?}", ast));
|
|
|
|
}
|
|
|
|
ast
|
|
|
|
},
|
|
|
|
Err(err) => {
|
|
|
|
let msg = format!("Parse error: {:?}\n", err.msg);
|
|
|
|
output.push(msg);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let evaluation_output = self.evaluator.run(ast);
|
|
|
|
output.extend(evaluation_output);
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
2017-08-31 19:15:32 -07:00
|
|
|
|
|
|
|
fn can_compile(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compile(&mut self, input: &str) -> LLVMCodeString {
|
|
|
|
let tokens = match tokenizer::tokenize(input) {
|
|
|
|
Ok(tokens) => tokens,
|
|
|
|
Err(err) => {
|
|
|
|
let msg = format!("Tokenization error: {:?}\n", err.msg);
|
|
|
|
panic!("{}", msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let ast = match parser::parse(&tokens, &[]) {
|
|
|
|
Ok(ast) => ast,
|
|
|
|
Err(err) => {
|
|
|
|
let msg = format!("Parse error: {:?}\n", err.msg);
|
|
|
|
panic!("{}", msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
compilation::compile_ast(ast)
|
|
|
|
}
|
2017-08-30 19:15:04 -07:00
|
|
|
}
|