diff --git a/schala-repl/src/repl/command_tree.rs b/schala-repl/src/repl/command_tree.rs index ae8fa21..0b70c8f 100644 --- a/schala-repl/src/repl/command_tree.rs +++ b/schala-repl/src/repl/command_tree.rs @@ -1,5 +1,5 @@ use super::{Repl, InterpreterDirectiveOutput}; - +use crate::repl::directive_actions::DirectiveAction; use colored::*; pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option)>; @@ -27,27 +27,6 @@ pub enum CommandTree { Top(Vec), } -#[derive(Debug, Clone)] -pub enum DirectiveAction { - Null, - Help, - QuitProgram -} - -impl DirectiveAction { - fn perform(&self, repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput { - use DirectiveAction::*; - match self { - Null => None, - Help => Some(repl.print_help_message(arguments)), - QuitProgram => { - repl.save_before_exit(); - ::std::process::exit(0) - } - } - } -} - 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![], action: DirectiveAction::Null } diff --git a/schala-repl/src/repl/directive_actions.rs b/schala-repl/src/repl/directive_actions.rs new file mode 100644 index 0000000..8cd1948 --- /dev/null +++ b/schala-repl/src/repl/directive_actions.rs @@ -0,0 +1,43 @@ +use super::{Repl, InterpreterDirectiveOutput}; +use crate::language::{LangMetaRequest, LangMetaResponse, DebugAsk, DebugResponse}; +use itertools::Itertools; +use std::fmt::Write as FmtWrite; + +#[derive(Debug, Clone)] +pub enum DirectiveAction { + Null, + Help, + QuitProgram, + ListPasses, +} + +impl DirectiveAction { + pub fn perform(&self, repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput { + use DirectiveAction::*; + match self { + Null => None, + Help => Some(repl.print_help_message(arguments)), + QuitProgram => { + repl.save_before_exit(); + ::std::process::exit(0) + }, + ListPasses => { + let language_state = repl.get_cur_language_state(); + let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) { + LangMetaResponse::StageNames(names) => names, + _ => vec![], + }; + + let mut buf = String::new(); + for pass in pass_names.iter().map(|name| Some(name)).intersperse(None) { + match pass { + Some(pass) => write!(buf, "{}", pass).unwrap(), + None => write!(buf, " -> ").unwrap(), + } + } + Some(buf) + } + } + } +} + diff --git a/schala-repl/src/repl/directives.rs b/schala-repl/src/repl/directives.rs index d9101b7..6bc30cd 100644 --- a/schala-repl/src/repl/directives.rs +++ b/schala-repl/src/repl/directives.rs @@ -2,38 +2,26 @@ use std::fmt::Write as FmtWrite; use itertools::Itertools; use crate::repl::Repl; -use crate::repl::command_tree::{CommandTree, DirectiveAction}; +use crate::repl::command_tree::CommandTree; +use crate::repl::directive_actions::DirectiveAction; use crate::language::{LangMetaRequest, LangMetaResponse, DebugAsk, DebugResponse}; pub fn directives_from_pass_names(pass_names: &Vec) -> CommandTree { + use DirectiveAction::*; + let passes_directives: Vec = pass_names.iter() .map(|pass_name| { CommandTree::nonterm_no_further_tab_completions(pass_name, None) }) .collect(); CommandTree::Top(vec![ - CommandTree::terminal_act("exit", Some("exit the REPL"), vec![], DirectiveAction::QuitProgram), - CommandTree::terminal_act("quit", Some("exit the REPL"), vec![], DirectiveAction::QuitProgram), - CommandTree::terminal_act("help", Some("Print this help message"), vec![], DirectiveAction::Help), + CommandTree::terminal_act("exit", Some("exit the REPL"), vec![], QuitProgram), + CommandTree::terminal_act("quit", Some("exit the REPL"), vec![], QuitProgram), + CommandTree::terminal_act("help", Some("Print this help message"), vec![], Help), CommandTree::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]| { - let language_state = repl.get_cur_language_state(); - let pass_names = match language_state.request_meta(LangMetaRequest::StageNames) { - LangMetaResponse::StageNames(names) => names, - _ => vec![], - }; - - let mut buf = String::new(); - for pass in pass_names.iter().map(|name| Some(name)).intersperse(None) { - match pass { - Some(pass) => write!(buf, "{}", pass).unwrap(), - None => write!(buf, " -> ").unwrap(), - } - } - Some(buf) - })), + CommandTree::terminal_act("list-passes", Some("List all registered compiler passes"), vec![], ListPasses), CommandTree::terminal("show-immediate", None, passes_directives.clone(), Box::new(|repl: &mut Repl, cmds: &[&str]| { let cur_state = repl.get_cur_language_state(); diff --git a/schala-repl/src/repl/mod.rs b/schala-repl/src/repl/mod.rs index d1fb4ae..48a03d4 100644 --- a/schala-repl/src/repl/mod.rs +++ b/schala-repl/src/repl/mod.rs @@ -11,6 +11,7 @@ mod command_tree; use self::command_tree::{CommandTree, BoxedCommandFunction}; mod repl_options; use repl_options::ReplOptions; +mod directive_actions; mod directives; use directives::directives_from_pass_names;