Implement custom interpreter directives - and a wtf?

See the comment about &mut self vs &self
This commit is contained in:
greg 2018-10-15 20:28:18 -07:00
parent e8dfc2be34
commit 9927a6b1fd
3 changed files with 20 additions and 2 deletions

View File

@ -69,7 +69,7 @@ fn get_attribute_identifier(attr_name: &str, attrs: &Vec<Attribute>) -> Option<p
}
#[proc_macro_derive(ProgrammingLanguageInterface,
attributes(LanguageName, SourceFileExtension, PipelineSteps, DocMethod))]
attributes(LanguageName, SourceFileExtension, PipelineSteps, DocMethod, HandleCustomInterpreterDirectives))]
pub fn derive_programming_language_interface(input: TokenStream) -> 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<String> {
//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<String> = 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
}
};

View File

@ -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<RefCell<symbol_table::SymbolTable>>,
@ -47,6 +48,10 @@ impl Schala {
fn get_doc(&self, commands: &Vec<&str>) -> Option<String> {
Some(format!("Documentation on commands: {:?}", commands))
}
fn handle_custom_interpreter_directives(&mut self, commands: &Vec<&str>) -> Option<String> {
Some(format!("You typed a command but I can't handle it"))
}
}
impl Schala {

View File

@ -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<String> {