From fd89de77cc2bfc75c26081f0092fee02f0abe183 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 29 Apr 2018 00:55:39 -0700 Subject: [PATCH] Making pipeline macro nicer --- schala-lang/src/lib.rs | 4 +--- schala-repl/src/language.rs | 34 +++++++++++++++++++++------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/schala-lang/src/lib.rs b/schala-lang/src/lib.rs index 23083fe..a497974 100644 --- a/schala-lang/src/lib.rs +++ b/schala-lang/src/lib.rs @@ -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 { diff --git a/schala-repl/src/language.rs b/schala-repl/src/language.rs index 8fde299..74b81b3 100644 --- a/schala-repl/src/language.rs +++ b/schala-repl/src/language.rs @@ -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! { - { - let output = $next({ - let input = $e; - println!("About to run: {}", stringify!($next)); - input - }); - println!("Finished running {}", stringify!($next)); - output.unwrap() + ($input:expr, $pass:path $(, $rest:path)*) => { + { + 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 + } + }; }