Start refactoring how interpreter options are split up
This commit is contained in:
parent
1c0365529d
commit
66b6ddcf93
2
TODO.md
2
TODO.md
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
|
|
||||||
* change 'trait' to 'interface'
|
* 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
|
* Share state between programming languages
|
||||||
|
|
||||||
|
@ -170,4 +170,10 @@ pub trait ProgrammingLanguageInterface {
|
|||||||
}
|
}
|
||||||
fn get_language_name(&self) -> String;
|
fn get_language_name(&self) -> String;
|
||||||
fn get_source_file_suffix(&self) -> String;
|
fn get_source_file_suffix(&self) -> String;
|
||||||
|
fn handle_custom_interpreter_directives(&mut self, commands: &Vec<&str>) -> Option<String> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
fn custom_interpreter_directives_help(&self) -> String {
|
||||||
|
format!(">> No custom interpreter directives specified <<")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -214,6 +214,74 @@ impl Repl {
|
|||||||
interpreter_output.to_repl()
|
interpreter_output.to_repl()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handle_interpreter_directive(&mut self, input: &str) -> Option<String> {
|
||||||
|
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 <name> |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<String> {
|
fn handle_interpreter_directive(&mut self, input: &str) -> Option<String> {
|
||||||
let mut iter = input.chars();
|
let mut iter = input.chars();
|
||||||
iter.next();
|
iter.next();
|
||||||
@ -309,6 +377,7 @@ tokens: {}, parse: {}, ast: {}, symbols: {}"#, tokens, parse_tree, ast, symbol_t
|
|||||||
e => Some(format!("Unknown command: {}", e))
|
e => Some(format!("Unknown command: {}", e))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user