From 66b6ddcf937e64ccc9e78ce62f985a04d67d30bd Mon Sep 17 00:00:00 2001 From: greg Date: Mon, 23 Apr 2018 21:33:15 -0700 Subject: [PATCH] Start refactoring how interpreter options are split up --- TODO.md | 2 +- schala-repl/src/language.rs | 6 ++++ schala-repl/src/lib.rs | 69 +++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/TODO.md b/TODO.md index eb9b9c3..fc6c559 100644 --- a/TODO.md +++ b/TODO.md @@ -7,7 +7,7 @@ * change 'trait' to 'interface' - -think about idris-related ideas of multiple implementations of a type for an interface (+ vs * impl for monoids) + -think about idris-related ideas of multiple implementations of a type for an interface (+ vs * impl for monoids, for preorder/inorder/postorder for Foldable) * Share state between programming languages diff --git a/schala-repl/src/language.rs b/schala-repl/src/language.rs index 24cd104..4008afc 100644 --- a/schala-repl/src/language.rs +++ b/schala-repl/src/language.rs @@ -170,4 +170,10 @@ pub trait ProgrammingLanguageInterface { } fn get_language_name(&self) -> String; fn get_source_file_suffix(&self) -> String; + fn handle_custom_interpreter_directives(&mut self, commands: &Vec<&str>) -> Option { + None + } + fn custom_interpreter_directives_help(&self) -> String { + format!(">> No custom interpreter directives specified <<") + } } diff --git a/schala-repl/src/lib.rs b/schala-repl/src/lib.rs index 633a38c..e8be90b 100644 --- a/schala-repl/src/lib.rs +++ b/schala-repl/src/lib.rs @@ -214,6 +214,74 @@ impl Repl { interpreter_output.to_repl() } + fn handle_interpreter_directive(&mut self, input: &str) -> Option { + let mut iter = input.chars(); + iter.next(); + let commands: Vec<&str> = iter + .as_str() + .split_whitespace() + .collect(); + + let cmd: &str = match commands.get(0).clone() { + None => return None, + Some(s) => s + }; + + match cmd { + "exit" | "quit" => { + self.save_options(); + exit(0) + }, + "lang" | "language" => match commands.get(1) { + Some(&"show") => { + let mut buf = String::new(); + for (i, lang) in self.languages.iter().enumerate() { + write!(buf, "{}{}\n", if i == self.current_language_index { "* "} else { "" }, lang.get_language_name()).unwrap(); + } + Some(buf) + }, + Some(&"go") => match commands.get(2) { + None => Some(format!("Must specify a language name")), + Some(&desired_name) => { + for (i, _) in self.languages.iter().enumerate() { + let lang_name = self.languages[i].get_language_name(); + if lang_name.to_lowercase() == desired_name.to_lowercase() { + self.current_language_index = i; + return Some(format!("Switching to {}", self.languages[self.current_language_index].get_language_name())); + } + } + Some(format!("Language {} not found", desired_name)) + } + }, + Some(&"next") | Some(&"n") => { + self.current_language_index = (self.current_language_index + 1) % self.languages.len(); + Some(format!("Switching to {}", self.languages[self.current_language_index].get_language_name())) + }, + Some(&"previous") | Some(&"p") | Some(&"prev") => { + self.current_language_index = if self.current_language_index == 0 { self.languages.len() - 1 } else { self.current_language_index - 1 }; + Some(format!("Switching to {}", self.languages[self.current_language_index].get_language_name())) + }, + Some(e) => Some(format!("Bad `lang(uage)` argument: {}", e)), + None => Some(format!("Valid arguments for `lang(uage)` are `show`, `next`|`n`, `previous`|`prev`|`n`")) + }, + "help" => { + let mut buf = String::new(); + let ref lang = self.languages[self.current_language_index]; + + writeln!(buf, "MetaInterpreter options").unwrap(); + writeln!(buf, "-----------------------").unwrap(); + writeln!(buf, "exit | quit - exit the REPL").unwrap(); + writeln!(buf, "lang [prev|next|go |show] - toggle to previous or next language, go to a specific language by name, or show all languages").unwrap(); + writeln!(buf, "Language-specific help for {}", lang.get_language_name()).unwrap(); + writeln!(buf, "-----------------------").unwrap(); + writeln!(buf, "{}", lang.custom_interpreter_directives_help()).unwrap(); + Some(buf) + }, + _ => self.languages[self.current_language_index].handle_custom_interpreter_directives(&commands), + } + } + + /* fn handle_interpreter_directive(&mut self, input: &str) -> Option { let mut iter = input.chars(); iter.next(); @@ -309,6 +377,7 @@ tokens: {}, parse: {}, ast: {}, symbols: {}"#, tokens, parse_tree, ast, symbol_t e => Some(format!("Unknown command: {}", e)) } } + */ } /*