More DirectiveAction conversion work

This commit is contained in:
greg 2019-06-02 00:27:12 -07:00
parent fd517351de
commit fe08e64860
4 changed files with 53 additions and 42 deletions

View File

@ -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<String>)>;
@ -27,27 +27,6 @@ pub enum CommandTree {
Top(Vec<CommandTree>),
}
#[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 }

View File

@ -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)
}
}
}
}

View File

@ -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<String>) -> CommandTree {
use DirectiveAction::*;
let passes_directives: Vec<CommandTree> = 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();

View File

@ -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;