From b0ca955de6ed100e10b75523e4ec76e8c26efe73 Mon Sep 17 00:00:00 2001 From: greg Date: Sat, 1 Jun 2019 18:34:13 -0700 Subject: [PATCH] Rename CommandTree to OldCommandTree --- schala-repl/src/repl/command_tree.rs | 73 ------------------------ schala-repl/src/repl/directives.rs | 48 ++++++++-------- schala-repl/src/repl/mod.rs | 36 ++++++------ schala-repl/src/repl/old_command_tree.rs | 73 ++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 115 deletions(-) delete mode 100644 schala-repl/src/repl/command_tree.rs create mode 100644 schala-repl/src/repl/old_command_tree.rs diff --git a/schala-repl/src/repl/command_tree.rs b/schala-repl/src/repl/command_tree.rs deleted file mode 100644 index c8cd05a..0000000 --- a/schala-repl/src/repl/command_tree.rs +++ /dev/null @@ -1,73 +0,0 @@ -use super::Repl; - -pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option)>; - -/// 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, - help_msg: Option, - function: BoxedCommandFunction, - }, - NonTerminal { - name: String, - children: Vec, - help_msg: Option, - }, - Top(Vec), -} - -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, 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::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, 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() - } - } -} diff --git a/schala-repl/src/repl/directives.rs b/schala-repl/src/repl/directives.rs index 191b252..d0fe149 100644 --- a/schala-repl/src/repl/directives.rs +++ b/schala-repl/src/repl/directives.rs @@ -2,30 +2,30 @@ use std::fmt::Write as FmtWrite; use itertools::Itertools; use crate::repl::Repl; -use crate::repl::command_tree::CommandTree; +use crate::repl::old_command_tree::OldCommandTree; use crate::language::{LangMetaRequest, LangMetaResponse, DebugAsk, DebugResponse}; -pub fn directives_from_pass_names(pass_names: &Vec) -> CommandTree { - let passes_directives: Vec = pass_names.iter() - .map(|pass_name| { CommandTree::nonterm_no_further_tab_completions(pass_name, None) }) +pub fn directives_from_pass_names(pass_names: &Vec) -> OldCommandTree { + let passes_directives: Vec = pass_names.iter() + .map(|pass_name| { OldCommandTree::nonterm_no_further_tab_completions(pass_name, None) }) .collect(); - CommandTree::Top(vec![ - CommandTree::terminal("exit", Some("exit the REPL"), vec![], Box::new(|repl: &mut Repl, _cmds: &[&str]| { + OldCommandTree::Top(vec![ + OldCommandTree::terminal("exit", Some("exit the REPL"), vec![], Box::new(|repl: &mut Repl, _cmds: &[&str]| { repl.save_before_exit(); ::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(); ::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)) })), - CommandTree::nonterm("debug", + OldCommandTree::nonterm("debug", Some("Configure debug information"), 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 pass_names = match language_state.request_meta(LangMetaRequest::StageNames) { LangMetaResponse::StageNames(names) => names, @@ -41,7 +41,7 @@ pub fn directives_from_pass_names(pass_names: &Vec) -> CommandTree { } 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]| { let cur_state = repl.get_cur_language_state(); let stage_name = match cmds.get(1) { @@ -61,7 +61,7 @@ pub fn directives_from_pass_names(pass_names: &Vec) -> CommandTree { }; 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) { Some(s) => s.to_string(), None => return Some(format!("Must specify a stage to show")), @@ -70,7 +70,7 @@ pub fn directives_from_pass_names(pass_names: &Vec) -> CommandTree { repl.options.debug_asks.insert(ask); 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) { Some(s) => s.to_string(), None => return Some(format!("Must specify a stage to hide")), @@ -79,34 +79,34 @@ pub fn directives_from_pass_names(pass_names: &Vec) -> CommandTree { repl.options.debug_asks.remove(&ask); None })), - CommandTree::nonterm("total-time", None, vec![ - CommandTree::terminal("on", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| { + OldCommandTree::nonterm("total-time", None, vec![ + OldCommandTree::terminal("on", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| { repl.options.show_total_time = true; 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![ - CommandTree::terminal("on", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| { + OldCommandTree::nonterm("stage-times", Some("Computation time per-stage"), vec![ + OldCommandTree::terminal("on", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| { repl.options.show_stage_times = true; 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; None })), ]) ] ), - CommandTree::nonterm("lang", + OldCommandTree::nonterm("lang", Some("switch between languages, or go directly to a langauge by name"), vec![ - CommandTree::nonterm_no_further_tab_completions("next", None), - CommandTree::nonterm_no_further_tab_completions("prev", None), - CommandTree::nonterm("go", None, vec![]), + OldCommandTree::nonterm_no_further_tab_completions("next", None), + OldCommandTree::nonterm_no_further_tab_completions("prev", None), + 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| { let source = cmd.to_string(); let meta = LangMetaRequest::Docs { source }; diff --git a/schala-repl/src/repl/mod.rs b/schala-repl/src/repl/mod.rs index f3a1bc8..2d9d5ff 100644 --- a/schala-repl/src/repl/mod.rs +++ b/schala-repl/src/repl/mod.rs @@ -7,8 +7,8 @@ use crate::language::{ProgrammingLanguageInterface, ComputationRequest, ComputationResponse, DebugAsk, LangMetaResponse, LangMetaRequest}; -mod command_tree; -use self::command_tree::{CommandTree, BoxedCommandFunction}; +mod old_command_tree; +use self::old_command_tree::{OldCommandTree, BoxedCommandFunction}; mod repl_options; use repl_options::ReplOptions; mod directives; @@ -22,7 +22,7 @@ pub struct Repl { line_reader: ::linefeed::interface::Interface<::linefeed::terminal::DefaultTerminal>, language_states: Vec>, options: ReplOptions, - directives: CommandTree, + directives: OldCommandTree, } impl Repl { @@ -101,13 +101,13 @@ impl Repl { 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> { - let mut dir_pointer: &CommandTree = &directives; + fn get_function_from_directives<'a>(directives: &'a OldCommandTree, commands: &Vec<&str>) -> Result<(&'a BoxedCommandFunction, usize), String> { + let mut dir_pointer: &OldCommandTree = &directives; let mut idx = 0; loop { match dir_pointer { - CommandTree::Top(subcommands) | CommandTree::NonTerminal { children: subcommands, .. } => { + OldCommandTree::Top(subcommands) | OldCommandTree::NonTerminal { children: subcommands, .. } => { let next_command = match commands.get(idx) { Some(cmd) => cmd, None => break Err(format!("Command requires arguments")) @@ -120,7 +120,7 @@ impl Repl { None => break Err(format!("Command {} not found", next_command)) }; }, - CommandTree::Terminal { function, .. } => { + OldCommandTree::Terminal { function, .. } => { break Ok((function, idx)); }, } @@ -150,8 +150,8 @@ impl Repl { fn print_help_message(&mut self, commands_passed_to_help: &[&str] ) -> String { let mut buf = String::new(); let directives = match self.get_directives() { - CommandTree::Top(children) => children, - _ => panic!("Top-level CommandTree not Top") + OldCommandTree::Top(children) => children, + _ => panic!("Top-level OldCommandTree not Top") }; match commands_passed_to_help { @@ -227,7 +227,7 @@ impl Repl { buf } - fn get_directives(&mut self) -> CommandTree { + fn get_directives(&mut self) -> OldCommandTree { let language_state = self.get_cur_language_state(); let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) { LangMetaResponse::StageNames(names) => names, @@ -241,14 +241,14 @@ impl Repl { struct TabCompleteHandler { sigil: char, - top_level_commands: CommandTree, + top_level_commands: OldCommandTree, } use linefeed::complete::{Completion, Completer}; use linefeed::terminal::Terminal; impl TabCompleteHandler { - fn new(sigil: char, top_level_commands: CommandTree) -> TabCompleteHandler { + fn new(sigil: char, top_level_commands: OldCommandTree) -> TabCompleteHandler { TabCompleteHandler { top_level_commands, sigil, @@ -266,13 +266,13 @@ impl Completer for TabCompleteHandler { let mut words = line[1..(if start == 0 { 1 } else { start })].split_whitespace(); 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 { match words.next() { None => { let top = match command_tree { - Some(CommandTree::Top(_)) => true, + Some(OldCommandTree::Top(_)) => true, _ => false }; let word = if top { word.get(1..).unwrap() } else { word }; @@ -288,10 +288,10 @@ impl Completer for TabCompleteHandler { break; }, Some(s) => { - let new_ptr: Option<&CommandTree> = command_tree.and_then(|cm| match cm { - CommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s), - CommandTree::NonTerminal { children, .. } => children.iter().find(|c| c.get_cmd() == s), - CommandTree::Terminal { children, .. } => children.iter().find(|c| c.get_cmd() == s), + let new_ptr: Option<&OldCommandTree> = command_tree.and_then(|cm| match cm { + OldCommandTree::Top(children) => children.iter().find(|c| c.get_cmd() == s), + OldCommandTree::NonTerminal { children, .. } => children.iter().find(|c| c.get_cmd() == s), + OldCommandTree::Terminal { children, .. } => children.iter().find(|c| c.get_cmd() == s), }); command_tree = new_ptr; } diff --git a/schala-repl/src/repl/old_command_tree.rs b/schala-repl/src/repl/old_command_tree.rs new file mode 100644 index 0000000..5cb2dde --- /dev/null +++ b/schala-repl/src/repl/old_command_tree.rs @@ -0,0 +1,73 @@ +use super::Repl; + +pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option)>; + +/// 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, + help_msg: Option, + function: BoxedCommandFunction, + }, + NonTerminal { + name: String, + children: Vec, + help_msg: Option, + }, + Top(Vec), +} + +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, 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::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, 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() + } + } +}