2017-08-29 00:28:19 -07:00
|
|
|
use ::std::marker::PhantomData;
|
|
|
|
pub mod tokenizer;
|
|
|
|
pub mod parser;
|
|
|
|
pub mod eval;
|
|
|
|
pub mod compilation;
|
2017-02-03 11:34:26 -08:00
|
|
|
|
2017-08-30 19:15:04 -07:00
|
|
|
use language::{ProgrammingLanguageInterface, EvalOptions, ProgrammingLanguage, EvaluationMachine, ParseError, TokenError, 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-30 19:15:04 -07:00
|
|
|
pub struct NewMaaru<'a> {
|
|
|
|
evaluator: MaaruEvaluator<'a>
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> NewMaaru<'a> {
|
2017-08-30 22:58:57 -07:00
|
|
|
pub fn new() -> NewMaaru<'a> {
|
2017-08-30 19:15:04 -07:00
|
|
|
NewMaaru {
|
|
|
|
evaluator: MaaruEvaluator::new(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ProgrammingLanguageInterface for NewMaaru<'a> {
|
|
|
|
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-29 00:28:19 -07:00
|
|
|
pub struct Maaru<'a> { marker: PhantomData<&'a ()> }
|
|
|
|
impl<'a> Maaru<'a> {
|
|
|
|
pub fn new() -> Maaru <'a> {
|
|
|
|
Maaru { marker: PhantomData }
|
2017-02-02 01:21:40 -08:00
|
|
|
}
|
2017-01-25 20:09:51 -08:00
|
|
|
}
|
|
|
|
|
2017-08-29 00:28:19 -07:00
|
|
|
impl<'a> ProgrammingLanguage for Maaru<'a> {
|
|
|
|
type Token = tokenizer::Token;
|
|
|
|
type AST = parser::AST;
|
|
|
|
type Evaluator = MaaruEvaluator<'a>;
|
2017-01-25 20:09:51 -08:00
|
|
|
|
2017-02-02 01:04:15 -08:00
|
|
|
fn name() -> String {
|
|
|
|
"Maaru".to_string()
|
|
|
|
}
|
|
|
|
|
2017-02-03 11:34:26 -08:00
|
|
|
fn tokenize(input: &str) -> Result<Vec<Self::Token>, TokenError> {
|
2017-08-29 00:28:19 -07:00
|
|
|
tokenizer::tokenize(input)
|
2017-01-25 20:09:51 -08:00
|
|
|
}
|
2017-08-29 00:28:19 -07:00
|
|
|
fn parse(input: Vec<Self::Token>) -> Result<Self::AST, ParseError> {
|
|
|
|
parser::parse(&input, &[]).map_err(|x| ParseError { msg: x.msg })
|
2017-01-25 20:09:51 -08:00
|
|
|
}
|
2017-08-29 00:28:19 -07:00
|
|
|
fn evaluate(ast: Self::AST, evaluator: &mut Self::Evaluator) -> Vec<String> {
|
|
|
|
evaluator.run(ast)
|
2017-01-25 20:09:51 -08:00
|
|
|
}
|
2017-08-29 00:28:19 -07:00
|
|
|
fn compile(ast: Self::AST) -> LLVMCodeString {
|
|
|
|
compilation::compile_ast(ast)
|
2017-01-25 20:09:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-29 00:28:19 -07:00
|
|
|
impl<'a> EvaluationMachine for MaaruEvaluator<'a> {
|
2017-01-25 20:09:51 -08:00
|
|
|
fn set_option(&mut self, option: &str, value: bool) -> bool {
|
|
|
|
if option == "trace_evaluation" {
|
|
|
|
self.trace_evaluation = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2017-08-29 00:28:19 -07:00
|
|
|
fn new() -> MaaruEvaluator<'a> {
|
|
|
|
MaaruEvaluator::new(None)
|
2017-01-25 20:09:51 -08:00
|
|
|
}
|
|
|
|
}
|