More DirectiveAction conversion work
This commit is contained in:
parent
fd517351de
commit
fe08e64860
@ -1,5 +1,5 @@
|
|||||||
use super::{Repl, InterpreterDirectiveOutput};
|
use super::{Repl, InterpreterDirectiveOutput};
|
||||||
|
use crate::repl::directive_actions::DirectiveAction;
|
||||||
use colored::*;
|
use colored::*;
|
||||||
|
|
||||||
pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option<String>)>;
|
pub type BoxedCommandFunction = Box<(fn(&mut Repl, &[&str]) -> Option<String>)>;
|
||||||
@ -27,27 +27,6 @@ pub enum CommandTree {
|
|||||||
Top(Vec<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 {
|
impl CommandTree {
|
||||||
pub fn nonterm_no_further_tab_completions(s: &str, help: Option<&str>) -> 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 }
|
CommandTree::NonTerminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), children: vec![], action: DirectiveAction::Null }
|
||||||
|
43
schala-repl/src/repl/directive_actions.rs
Normal file
43
schala-repl/src/repl/directive_actions.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,38 +2,26 @@ 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, DirectiveAction};
|
use crate::repl::command_tree::CommandTree;
|
||||||
|
use crate::repl::directive_actions::DirectiveAction;
|
||||||
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>) -> CommandTree {
|
||||||
|
use DirectiveAction::*;
|
||||||
|
|
||||||
let passes_directives: Vec<CommandTree> = pass_names.iter()
|
let passes_directives: Vec<CommandTree> = pass_names.iter()
|
||||||
.map(|pass_name| { CommandTree::nonterm_no_further_tab_completions(pass_name, None) })
|
.map(|pass_name| { CommandTree::nonterm_no_further_tab_completions(pass_name, None) })
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
CommandTree::Top(vec![
|
CommandTree::Top(vec![
|
||||||
CommandTree::terminal_act("exit", Some("exit the REPL"), vec![], DirectiveAction::QuitProgram),
|
CommandTree::terminal_act("exit", Some("exit the REPL"), vec![], QuitProgram),
|
||||||
CommandTree::terminal_act("quit", Some("exit the REPL"), vec![], DirectiveAction::QuitProgram),
|
CommandTree::terminal_act("quit", Some("exit the REPL"), vec![], QuitProgram),
|
||||||
CommandTree::terminal_act("help", Some("Print this help message"), vec![], DirectiveAction::Help),
|
CommandTree::terminal_act("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("list-passes", Some("List all registered compiler passes"), vec![], Box::new(|repl: &mut Repl, _cmds: &[&str]| {
|
CommandTree::terminal_act("list-passes", Some("List all registered compiler passes"), vec![], 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)
|
|
||||||
})),
|
|
||||||
CommandTree::terminal("show-immediate", None, passes_directives.clone(),
|
CommandTree::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();
|
||||||
|
@ -11,6 +11,7 @@ mod command_tree;
|
|||||||
use self::command_tree::{CommandTree, BoxedCommandFunction};
|
use self::command_tree::{CommandTree, BoxedCommandFunction};
|
||||||
mod repl_options;
|
mod repl_options;
|
||||||
use repl_options::ReplOptions;
|
use repl_options::ReplOptions;
|
||||||
|
mod directive_actions;
|
||||||
mod directives;
|
mod directives;
|
||||||
use directives::directives_from_pass_names;
|
use directives::directives_from_pass_names;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user