From 9927a6b1fdba46a94ea8793672c09547d60a146b Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 15 Oct 2018 20:28:18 -0700 Subject: [PATCH] Implement custom interpreter directives - and a wtf? See the comment about &mut self vs &self --- schala-codegen/src/lib.rs | 13 ++++++++++++- schala-lang/src/lib.rs | 5 +++++ schala-repl/src/repl.rs | 4 +++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/schala-codegen/src/lib.rs b/schala-codegen/src/lib.rs index f33eb0c..942ec15 100644 --- a/schala-codegen/src/lib.rs +++ b/schala-codegen/src/lib.rs @@ -69,7 +69,7 @@ fn get_attribute_identifier(attr_name: &str, attrs: &Vec) -> Option

TokenStream { let ast: DeriveInput = syn::parse(input).unwrap(); let name = &ast.ident; @@ -89,6 +89,16 @@ pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream } }; + let handle_custom_interpreter_directives_impl = match get_attribute_identifier("HandleCustomInterpreterDirectives", attrs) { + None => quote! { }, + Some(method_name) => quote! { + fn handle_custom_interpreter_directives(&mut self, commands: &Vec<&str>) -> Option { + //println!("If #method_name is &self not &mut self, this runs forever"); + self.#method_name(commands) + } + } + }; + let pass_descriptors = passes.iter().map(|pass| { let name = pass.0.to_string(); let opts: Vec = match &pass.1 { @@ -121,6 +131,7 @@ pub fn derive_programming_language_interface(input: TokenStream) -> TokenStream vec![ #(#pass_descriptors),* ] } #get_doc_impl + #handle_custom_interpreter_directives_impl } }; diff --git a/schala-lang/src/lib.rs b/schala-lang/src/lib.rs index 2359a95..c01ed4c 100644 --- a/schala-lang/src/lib.rs +++ b/schala-lang/src/lib.rs @@ -37,6 +37,7 @@ mod eval; #[SourceFileExtension = "schala"] #[PipelineSteps(tokenizing, parsing(compact,expanded,trace), symbol_table, typechecking, ast_reducing, eval)] #[DocMethod = get_doc] +#[HandleCustomInterpreterDirectives = handle_custom_interpreter_directives] pub struct Schala { state: eval::State<'static>, symbol_table: Rc>, @@ -47,6 +48,10 @@ impl Schala { fn get_doc(&self, commands: &Vec<&str>) -> Option { Some(format!("Documentation on commands: {:?}", commands)) } + + fn handle_custom_interpreter_directives(&mut self, commands: &Vec<&str>) -> Option { + Some(format!("You typed a command but I can't handle it")) + } } impl Schala { diff --git a/schala-repl/src/repl.rs b/schala-repl/src/repl.rs index 0b3d841..a184c43 100644 --- a/schala-repl/src/repl.rs +++ b/schala-repl/src/repl.rs @@ -232,9 +232,11 @@ impl Repl { "doc" => self.languages[self.current_language_index] .get_doc(&commands) .or(Some(format!("No docs implemented"))), - e => self.languages[self.current_language_index] + e => { + self.languages[self.current_language_index] .handle_custom_interpreter_directives(&commands) .or(Some(format!("Unknown command: {}", e))) + } } } fn handle_debug(&mut self, commands: Vec<&str>) -> Option {