Making pipeline macro nicer

This commit is contained in:
greg 2018-04-29 00:55:39 -07:00
parent a305610a39
commit fd89de77cc
2 changed files with 22 additions and 16 deletions

View File

@ -60,9 +60,7 @@ impl ProgrammingLanguageInterface for Schala {
fn execute_pipeline(&mut self, input: &str, options: &EvalOptions) -> FinishedComputation {
//let chain = pass_chain![tokenizing::tokenize, parsing::parse];
let chain = pass_chain![tokenizing_stage, parsing_stage];
let output = Ok(format!("{:?}", chain(input)));
let mut evaluation = UnfinishedComputation::default();
evaluation.output(output) //TODO rename this method it's confusing
chain(input)
}
fn execute(&mut self, input: &str, options: &EvalOptions) -> FinishedComputation {

View File

@ -183,26 +183,34 @@ pub trait ProgrammingLanguageInterface {
#[macro_export]
macro_rules! pass_chain {
($($pass:path), *) => {
|begin| pass_chain_helper! { begin $(, $pass)* }
|text_input| { pass_chain_helper! { text_input $(, $pass)* } }
};
}
//generates a function resulting in (for now) the return type of the last one
//but should in the future return a FinishedComputation
#[macro_export]
macro_rules! pass_chain_helper {
($e:expr, $next:path $(, $rest:path)*) => {
pass_chain_helper! {
($input:expr, $pass:path $(, $rest:path)*) => {
{
let output = $next({
let input = $e;
println!("About to run: {}", stringify!($next));
input
});
println!("Finished running {}", stringify!($next));
output.unwrap()
let pass_name = stringify!($pass);
println!("Running pass {}", pass_name);
let output = $pass($input);
match output {
Ok(result) => pass_chain_helper! { result $(, $rest)* },
Err(err) => {
let comp = UnfinishedComputation::default();
comp.output(Err(format!("Pass {} failed with {:?}", pass_name, err)))
}
}
$(, $rest)*
}
};
// Done
($e:expr) => { $e };
($final_output:expr) => {
{
let comp = UnfinishedComputation::default();
let final_output: FinishedComputation = comp.output(Ok(format!("{:?}", $final_output)));
final_output
}
};
}