Rename CommandTree to OldCommandTree

This commit is contained in:
greg 2019-06-01 18:34:13 -07:00
parent 3987360f8e
commit b0ca955de6
4 changed files with 115 additions and 115 deletions

View File

@ -1,73 +0,0 @@
use super::Repl;
pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option<String>)>;
/// A CommandTree is either a `Terminal` or a `NonTerminal`. When command parsing reaches the first
/// Terminal, it will execute the `BoxedCommandFunction` found there with any remaining arguments
#[derive(Clone)]
pub enum CommandTree {
Terminal {
name: String,
children: Vec<CommandTree>,
help_msg: Option<String>,
function: BoxedCommandFunction,
},
NonTerminal {
name: String,
children: Vec<CommandTree>,
help_msg: Option<String>,
},
Top(Vec<CommandTree>),
}
impl CommandTree {
pub fn nonterm_no_further_tab_completions(s: &str, help: Option<&str>) -> CommandTree {
CommandTree::NonTerminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children: vec![] }
}
pub fn terminal(s: &str, help: Option<&str>, children: Vec<CommandTree>, function: BoxedCommandFunction) -> CommandTree {
CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function, children }
}
pub fn nonterm(s: &str, help: Option<&str>, children: Vec<CommandTree>) -> CommandTree {
CommandTree::NonTerminal {
name: s.to_string(),
help_msg: help.map(|x| x.to_string()),
children,
}
}
/*
pub fn nonterm_with_function(s: &str, help: Option<&str>, children: Vec<CommandTree>, func: BoxedCommandFunction) -> CommandTree {
CommandTree::NonTerminal {
name: s.to_string(),
help_msg: help.map(|x| x.to_string()),
children,
function: Some(func),
}
}
*/
pub fn get_cmd(&self) -> &str {
match self {
CommandTree::Terminal { name, .. } => name.as_str(),
CommandTree::NonTerminal {name, ..} => name.as_str(),
CommandTree::Top(_) => "",
}
}
pub fn get_help(&self) -> &str {
match self {
CommandTree::Terminal { help_msg, ..} => help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""),
CommandTree::NonTerminal { help_msg, .. } => help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""),
CommandTree::Top(_) => ""
}
}
pub fn get_children(&self) -> Vec<&str> {
use CommandTree::*;
match self {
Terminal { children, .. } |
NonTerminal { children, .. } |
Top(children) => children.iter().map(|x| x.get_cmd()).collect()
}
}
}

View File

@ -2,30 +2,30 @@ use std::fmt::Write as FmtWrite;
use itertools::Itertools; use itertools::Itertools;
use crate::repl::Repl; use crate::repl::Repl;
use crate::repl::command_tree::CommandTree; use crate::repl::old_command_tree::OldCommandTree;
use crate::language::{LangMetaRequest, LangMetaResponse, DebugAsk, DebugResponse}; use crate::language::{LangMetaRequest, LangMetaResponse, DebugAsk, DebugResponse};
pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree { pub fn directives_from_pass_names(pass_names: &Vec<String>) -> OldCommandTree {
let passes_directives: Vec<CommandTree> = pass_names.iter() let passes_directives: Vec<OldCommandTree> = pass_names.iter()
.map(|pass_name| { CommandTree::nonterm_no_further_tab_completions(pass_name, None) }) .map(|pass_name| { OldCommandTree::nonterm_no_further_tab_completions(pass_name, None) })
.collect(); .collect();
CommandTree::Top(vec![ OldCommandTree::Top(vec![
CommandTree::terminal("exit", Some("exit the REPL"), vec![], Box::new(|repl: &mut Repl, _cmds: &[&str]| { OldCommandTree::terminal("exit", Some("exit the REPL"), vec![], Box::new(|repl: &mut Repl, _cmds: &[&str]| {
repl.save_before_exit(); repl.save_before_exit();
::std::process::exit(0) ::std::process::exit(0)
})), })),
CommandTree::terminal("quit", Some("exit the REPL"), vec![], Box::new(|repl: &mut Repl, _cmds: &[&str]| { OldCommandTree::terminal("quit", Some("exit the REPL"), vec![], Box::new(|repl: &mut Repl, _cmds: &[&str]| {
repl.save_before_exit(); repl.save_before_exit();
::std::process::exit(0) ::std::process::exit(0)
})), })),
CommandTree::terminal("help", Some("Print this help message"), vec![], Box::new(|repl: &mut Repl, cmds: &[&str]| { OldCommandTree::terminal("help", Some("Print this help message"), vec![], Box::new(|repl: &mut Repl, cmds: &[&str]| {
Some(repl.print_help_message(cmds)) Some(repl.print_help_message(cmds))
})), })),
CommandTree::nonterm("debug", OldCommandTree::nonterm("debug",
Some("Configure debug information"), Some("Configure debug information"),
vec![ vec![
CommandTree::terminal("list-passes", Some("List all registered compiler passes"), vec![], Box::new(|repl: &mut Repl, _cmds: &[&str]| { OldCommandTree::terminal("list-passes", Some("List all registered compiler passes"), vec![], Box::new(|repl: &mut Repl, _cmds: &[&str]| {
let language_state = repl.get_cur_language_state(); let language_state = repl.get_cur_language_state();
let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) { let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) {
LangMetaResponse::StageNames(names) => names, LangMetaResponse::StageNames(names) => names,
@ -41,7 +41,7 @@ pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree {
} }
Some(buf) Some(buf)
})), })),
CommandTree::terminal("show-immediate", None, passes_directives.clone(), OldCommandTree::terminal("show-immediate", None, passes_directives.clone(),
Box::new(|repl: &mut Repl, cmds: &[&str]| { Box::new(|repl: &mut Repl, cmds: &[&str]| {
let cur_state = repl.get_cur_language_state(); let cur_state = repl.get_cur_language_state();
let stage_name = match cmds.get(1) { let stage_name = match cmds.get(1) {
@ -61,7 +61,7 @@ pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree {
}; };
Some(response) Some(response)
})), })),
CommandTree::terminal("show", None, passes_directives.clone(), Box::new(|repl: &mut Repl, cmds: &[&str]| { OldCommandTree::terminal("show", None, passes_directives.clone(), Box::new(|repl: &mut Repl, cmds: &[&str]| {
let stage_name = match cmds.get(0) { let stage_name = match cmds.get(0) {
Some(s) => s.to_string(), Some(s) => s.to_string(),
None => return Some(format!("Must specify a stage to show")), None => return Some(format!("Must specify a stage to show")),
@ -70,7 +70,7 @@ pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree {
repl.options.debug_asks.insert(ask); repl.options.debug_asks.insert(ask);
None None
})), })),
CommandTree::terminal("hide", None, passes_directives.clone(), Box::new(|repl: &mut Repl, cmds: &[&str]| { OldCommandTree::terminal("hide", None, passes_directives.clone(), Box::new(|repl: &mut Repl, cmds: &[&str]| {
let stage_name = match cmds.get(0) { let stage_name = match cmds.get(0) {
Some(s) => s.to_string(), Some(s) => s.to_string(),
None => return Some(format!("Must specify a stage to hide")), None => return Some(format!("Must specify a stage to hide")),
@ -79,34 +79,34 @@ pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree {
repl.options.debug_asks.remove(&ask); repl.options.debug_asks.remove(&ask);
None None
})), })),
CommandTree::nonterm("total-time", None, vec![ OldCommandTree::nonterm("total-time", None, vec![
CommandTree::terminal("on", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| { OldCommandTree::terminal("on", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| {
repl.options.show_total_time = true; repl.options.show_total_time = true;
None None
})), })),
CommandTree::terminal("off", None, vec![], Box::new(turn_off)), OldCommandTree::terminal("off", None, vec![], Box::new(turn_off)),
]), ]),
CommandTree::nonterm("stage-times", Some("Computation time per-stage"), vec![ OldCommandTree::nonterm("stage-times", Some("Computation time per-stage"), vec![
CommandTree::terminal("on", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| { OldCommandTree::terminal("on", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| {
repl.options.show_stage_times = true; repl.options.show_stage_times = true;
None None
})), })),
CommandTree::terminal("off", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| { OldCommandTree::terminal("off", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| {
repl.options.show_stage_times = false; repl.options.show_stage_times = false;
None None
})), })),
]) ])
] ]
), ),
CommandTree::nonterm("lang", OldCommandTree::nonterm("lang",
Some("switch between languages, or go directly to a langauge by name"), Some("switch between languages, or go directly to a langauge by name"),
vec![ vec![
CommandTree::nonterm_no_further_tab_completions("next", None), OldCommandTree::nonterm_no_further_tab_completions("next", None),
CommandTree::nonterm_no_further_tab_completions("prev", None), OldCommandTree::nonterm_no_further_tab_completions("prev", None),
CommandTree::nonterm("go", None, vec![]), OldCommandTree::nonterm("go", None, vec![]),
] ]
), ),
CommandTree::terminal("doc", Some("Get language-specific help for an item"), vec![], Box::new(|repl: &mut Repl, cmds: &[&str]| { OldCommandTree::terminal("doc", Some("Get language-specific help for an item"), vec![], Box::new(|repl: &mut Repl, cmds: &[&str]| {
cmds.get(0).map(|cmd| { cmds.get(0).map(|cmd| {
let source = cmd.to_string(); let source = cmd.to_string();
let meta = LangMetaRequest::Docs { source }; let meta = LangMetaRequest::Docs { source };

View File

@ -7,8 +7,8 @@ use crate::language::{ProgrammingLanguageInterface,
ComputationRequest, ComputationResponse, ComputationRequest, ComputationResponse,
DebugAsk, LangMetaResponse, LangMetaRequest}; DebugAsk, LangMetaResponse, LangMetaRequest};
mod command_tree; mod old_command_tree;
use self::command_tree::{CommandTree, BoxedCommandFunction}; use self::old_command_tree::{OldCommandTree, BoxedCommandFunction};
mod repl_options; mod repl_options;
use repl_options::ReplOptions; use repl_options::ReplOptions;
mod directives; mod directives;
@ -22,7 +22,7 @@ pub struct Repl {
line_reader: ::linefeed::interface::Interface<::linefeed::terminal::DefaultTerminal>, line_reader: ::linefeed::interface::Interface<::linefeed::terminal::DefaultTerminal>,
language_states: Vec<Box<ProgrammingLanguageInterface>>, language_states: Vec<Box<ProgrammingLanguageInterface>>,
options: ReplOptions, options: ReplOptions,
directives: CommandTree, directives: OldCommandTree,
} }
impl Repl { impl Repl {
@ -101,13 +101,13 @@ impl Repl {
self.options.save_to_file(OPTIONS_SAVE_FILE); self.options.save_to_file(OPTIONS_SAVE_FILE);
} }
fn get_function_from_directives<'a>(directives: &'a CommandTree, commands: &Vec<&str>) -> Result<(&'a BoxedCommandFunction, usize), String> { fn get_function_from_directives<'a>(directives: &'a OldCommandTree, commands: &Vec<&str>) -> Result<(&'a BoxedCommandFunction, usize), String> {
let mut dir_pointer: &CommandTree = &directives; let mut dir_pointer: &OldCommandTree = &directives;
let mut idx = 0; let mut idx = 0;
loop { loop {
match dir_pointer { match dir_pointer {
CommandTree::Top(subcommands) | CommandTree::NonTerminal { children: subcommands, .. } => { OldCommandTree::Top(subcommands) | OldCommandTree::NonTerminal { children: subcommands, .. } => {
let next_command = match commands.get(idx) { let next_command = match commands.get(idx) {
Some(cmd) => cmd, Some(cmd) => cmd,
None => break Err(format!("Command requires arguments")) None => break Err(format!("Command requires arguments"))
@ -120,7 +120,7 @@ impl Repl {
None => break Err(format!("Command {} not found", next_command)) None => break Err(format!("Command {} not found", next_command))
}; };
}, },
CommandTree::Terminal { function, .. } => { OldCommandTree::Terminal { function, .. } => {
break Ok((function, idx)); break Ok((function, idx));
}, },
} }
@ -150,8 +150,8 @@ impl Repl {
fn print_help_message(&mut self, commands_passed_to_help: &[&str] ) -> String { fn print_help_message(&mut self, commands_passed_to_help: &[&str] ) -> String {
let mut buf = String::new(); let mut buf = String::new();
let directives = match self.get_directives() { let directives = match self.get_directives() {
CommandTree::Top(children) => children, OldCommandTree::Top(children) => children,
_ => panic!("Top-level CommandTree not Top") _ => panic!("Top-level OldCommandTree not Top")
}; };
match commands_passed_to_help { match commands_passed_to_help {
@ -227,7 +227,7 @@ impl Repl {
buf buf
} }
fn get_directives(&mut self) -> CommandTree { fn get_directives(&mut self) -> OldCommandTree {
let language_state = self.get_cur_language_state(); let language_state = self.get_cur_language_state();
let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) { let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) {
LangMetaResponse::StageNames(names) => names, LangMetaResponse::StageNames(names) => names,
@ -241,14 +241,14 @@ impl Repl {
struct TabCompleteHandler { struct TabCompleteHandler {
sigil: char, sigil: char,
top_level_commands: CommandTree, top_level_commands: OldCommandTree,
} }
use linefeed::complete::{Completion, Completer}; use linefeed::complete::{Completion, Completer};
use linefeed::terminal::Terminal; use linefeed::terminal::Terminal;
impl TabCompleteHandler { impl TabCompleteHandler {
fn new(sigil: char, top_level_commands: CommandTree) -> TabCompleteHandler { fn new(sigil: char, top_level_commands: OldCommandTree) -> TabCompleteHandler {
TabCompleteHandler { TabCompleteHandler {
top_level_commands, top_level_commands,
sigil, sigil,
@ -266,13 +266,13 @@ impl<T: Terminal> Completer<T> for TabCompleteHandler {
let mut words = line[1..(if start == 0 { 1 } else { start })].split_whitespace(); let mut words = line[1..(if start == 0 { 1 } else { start })].split_whitespace();
let mut completions = Vec::new(); let mut completions = Vec::new();
let mut command_tree: Option<&CommandTree> = Some(&self.top_level_commands); let mut command_tree: Option<&OldCommandTree> = Some(&self.top_level_commands);
loop { loop {
match words.next() { match words.next() {
None => { None => {
let top = match command_tree { let top = match command_tree {
Some(CommandTree::Top(_)) => true, Some(OldCommandTree::Top(_)) => true,
_ => false _ => false
}; };
let word = if top { word.get(1..).unwrap() } else { word }; let word = if top { word.get(1..).unwrap() } else { word };
@ -288,10 +288,10 @@ impl<T: Terminal> Completer<T> for TabCompleteHandler {
break; break;
}, },
Some(s) => { Some(s) => {
let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm { let new_ptr: Option<&OldCommandTree> = command_tree.and_then(|cm| match cm {
CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s), OldCommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s),
CommandTree::NonTerminal { children, .. } => children.iter().find(|c| c.get_cmd() == s), OldCommandTree::NonTerminal { children, .. } => children.iter().find(|c| c.get_cmd() == s),
CommandTree::Terminal { children, .. } => children.iter().find(|c| c.get_cmd() == s), OldCommandTree::Terminal { children, .. } => children.iter().find(|c| c.get_cmd() == s),
}); });
command_tree = new_ptr; command_tree = new_ptr;
} }

View File

@ -0,0 +1,73 @@
use super::Repl;
pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option<String>)>;
/// A OldCommandTree is either a `Terminal` or a `NonTerminal`. When command parsing reaches the first
/// Terminal, it will execute the `BoxedCommandFunction` found there with any remaining arguments
#[derive(Clone)]
pub enum OldCommandTree {
Terminal {
name: String,
children: Vec<OldCommandTree>,
help_msg: Option<String>,
function: BoxedCommandFunction,
},
NonTerminal {
name: String,
children: Vec<OldCommandTree>,
help_msg: Option<String>,
},
Top(Vec<OldCommandTree>),
}
impl OldCommandTree {
pub fn nonterm_no_further_tab_completions(s: &str, help: Option<&str>) -> OldCommandTree {
OldCommandTree::NonTerminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children: vec![] }
}
pub fn terminal(s: &str, help: Option<&str>, children: Vec<OldCommandTree>, function: BoxedCommandFunction) -> OldCommandTree {
OldCommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function, children }
}
pub fn nonterm(s: &str, help: Option<&str>, children: Vec<OldCommandTree>) -> OldCommandTree {
OldCommandTree::NonTerminal {
name: s.to_string(),
help_msg: help.map(|x| x.to_string()),
children,
}
}
/*
pub fn nonterm_with_function(s: &str, help: Option<&str>, children: Vec<OldCommandTree>, func: BoxedCommandFunction) -> OldCommandTree {
OldCommandTree::NonTerminal {
name: s.to_string(),
help_msg: help.map(|x| x.to_string()),
children,
function: Some(func),
}
}
*/
pub fn get_cmd(&self) -> &str {
match self {
OldCommandTree::Terminal { name, .. } => name.as_str(),
OldCommandTree::NonTerminal {name, ..} => name.as_str(),
OldCommandTree::Top(_) => "",
}
}
pub fn get_help(&self) -> &str {
match self {
OldCommandTree::Terminal { help_msg, ..} => help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""),
OldCommandTree::NonTerminal { help_msg, .. } => help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""),
OldCommandTree::Top(_) => ""
}
}
pub fn get_children(&self) -> Vec<&str> {
use OldCommandTree::*;
match self {
Terminal { children, .. } |
NonTerminal { children, .. } |
Top(children) => children.iter().map(|x| x.get_cmd()).collect()
}
}
}