directive-related cleanup

This commit is contained in:
greg 2019-06-02 00:48:59 -07:00
parent 10bfeab7e4
commit bb39c59db2
3 changed files with 15 additions and 28 deletions

View File

@ -2,19 +2,15 @@ use super::{Repl, InterpreterDirectiveOutput};
use crate::repl::directive_actions::DirectiveAction; use crate::repl::directive_actions::DirectiveAction;
use colored::*; use colored::*;
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 /// A CommandTree is either a `Terminal` or a `NonTerminal`. When command parsing reaches the first
/// Terminal, it will use the `DirectiveAction` found there to find an appropriate function to execute, /// Terminal, it will use the `DirectiveAction` found there to find an appropriate function to execute,
/// and then execute it with any remaining arguments /// and then execute it with any remaining arguments
//TODO remove function: BoxedCommandFunction
#[derive(Clone)] #[derive(Clone)]
pub enum CommandTree { pub enum CommandTree {
Terminal { Terminal {
name: String, name: String,
children: Vec<CommandTree>, children: Vec<CommandTree>,
help_msg: Option<String>, help_msg: Option<String>,
function: BoxedCommandFunction,
action: DirectiveAction, action: DirectiveAction,
}, },
NonTerminal { NonTerminal {
@ -32,12 +28,8 @@ impl CommandTree {
CommandTree::NonTerminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children: vec![], action: DirectiveAction::Null } CommandTree::NonTerminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children: vec![], action: DirectiveAction::Null }
} }
pub fn terminal(s: &str, help: Option<&str>, children: Vec<CommandTree>, function: BoxedCommandFunction) -> CommandTree { pub fn terminal(s: &str, help: Option<&str>, children: Vec<CommandTree>, action: DirectiveAction) -> CommandTree {
CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function, children, action: DirectiveAction::Null } CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children, action}
}
pub fn terminal_act(s: &str, help: Option<&str>, children: Vec<CommandTree>, action: DirectiveAction) -> CommandTree {
CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function: Box::new(|_,_| { None }), children, action}
} }
pub fn nonterm(s: &str, help: Option<&str>, children: Vec<CommandTree>) -> CommandTree { pub fn nonterm(s: &str, help: Option<&str>, children: Vec<CommandTree>) -> CommandTree {

View File

@ -1,10 +1,5 @@
use std::fmt::Write as FmtWrite;
use crate::repl::Repl;
use crate::repl::command_tree::CommandTree; use crate::repl::command_tree::CommandTree;
use crate::repl::directive_actions::DirectiveAction; use crate::repl::directive_actions::DirectiveAction;
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>) -> CommandTree {
use DirectiveAction::*; use DirectiveAction::*;
@ -14,23 +9,23 @@ pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree {
.collect(); .collect();
CommandTree::Top(vec![ CommandTree::Top(vec![
CommandTree::terminal_act("exit", Some("exit the REPL"), vec![], QuitProgram), CommandTree::terminal("exit", Some("exit the REPL"), vec![], QuitProgram),
CommandTree::terminal_act("quit", Some("exit the REPL"), vec![], QuitProgram), CommandTree::terminal("quit", Some("exit the REPL"), vec![], QuitProgram),
CommandTree::terminal_act("help", Some("Print this help message"), vec![], Help), CommandTree::terminal("help", Some("Print this help message"), vec![], Help),
CommandTree::nonterm("debug", CommandTree::nonterm("debug",
Some("Configure debug information"), Some("Configure debug information"),
vec![ vec![
CommandTree::terminal_act("list-passes", Some("List all registered compiler passes"), vec![], ListPasses), CommandTree::terminal("list-passes", Some("List all registered compiler passes"), vec![], ListPasses),
CommandTree::terminal_act("show-immediate", None, passes_directives.clone(), ShowImmediate), CommandTree::terminal("show-immediate", None, passes_directives.clone(), ShowImmediate),
CommandTree::terminal_act("show", None, passes_directives.clone(), Show), CommandTree::terminal("show", None, passes_directives.clone(), Show),
CommandTree::terminal_act("hide", None, passes_directives.clone(), Hide), CommandTree::terminal("hide", None, passes_directives.clone(), Hide),
CommandTree::nonterm("total-time", None, vec![ CommandTree::nonterm("total-time", None, vec![
CommandTree::terminal_act("on", None, vec![], TotalTimeOn), CommandTree::terminal("on", None, vec![], TotalTimeOn),
CommandTree::terminal_act("off", None, vec![], TotalTimeOff), CommandTree::terminal("off", None, vec![], TotalTimeOff),
]), ]),
CommandTree::nonterm("stage-times", Some("Computation time per-stage"), vec![ CommandTree::nonterm("stage-times", Some("Computation time per-stage"), vec![
CommandTree::terminal_act("on", None, vec![], StageTimeOn), CommandTree::terminal("on", None, vec![], StageTimeOn),
CommandTree::terminal_act("off", None, vec![], StageTimeOff), CommandTree::terminal("off", None, vec![], StageTimeOff),
]) ])
] ]
), ),
@ -42,7 +37,7 @@ pub fn directives_from_pass_names(pass_names: &Vec<String>) -> CommandTree {
CommandTree::nonterm("go", None, vec![]), CommandTree::nonterm("go", None, vec![]),
] ]
), ),
CommandTree::terminal_act("doc", Some("Get language-specific help for an item"), vec![], Doc), CommandTree::terminal("doc", Some("Get language-specific help for an item"), vec![], Doc),
]) ])
} }

View File

@ -8,7 +8,7 @@ ComputationRequest, ComputationResponse,
DebugAsk, LangMetaResponse, LangMetaRequest}; DebugAsk, LangMetaResponse, LangMetaRequest};
mod command_tree; mod command_tree;
use self::command_tree::{CommandTree, BoxedCommandFunction}; use self::command_tree::CommandTree;
mod repl_options; mod repl_options;
use repl_options::ReplOptions; use repl_options::ReplOptions;
mod directive_actions; mod directive_actions;