155 lines
4.8 KiB
Rust
155 lines
4.8 KiB
Rust
use super::{Repl, InterpreterDirectiveOutput};
|
|
use crate::repl::command_tree::CommandTree;
|
|
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,
|
|
ShowImmediate,
|
|
Show,
|
|
Hide,
|
|
TotalTimeOff,
|
|
TotalTimeOn,
|
|
StageTimeOff,
|
|
StageTimeOn,
|
|
Doc,
|
|
}
|
|
|
|
impl DirectiveAction {
|
|
pub fn perform(&self, repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
|
|
use DirectiveAction::*;
|
|
match self {
|
|
Null => None,
|
|
Help => help(repl, 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)
|
|
},
|
|
ShowImmediate => {
|
|
let cur_state = repl.get_cur_language_state();
|
|
let stage_name = match arguments.get(1) {
|
|
Some(s) => s.to_string(),
|
|
None => return Some(format!("Must specify a thing to debug")),
|
|
};
|
|
let meta = LangMetaRequest::ImmediateDebug(DebugAsk::ByStage { stage_name: stage_name.clone() });
|
|
|
|
let response = match cur_state.request_meta(meta) {
|
|
LangMetaResponse::ImmediateDebug(DebugResponse { ask, value }) => {
|
|
if (ask != DebugAsk::ByStage { stage_name: stage_name }) {
|
|
return Some(format!("Didn't get debug stage requested"));
|
|
}
|
|
value
|
|
},
|
|
_ => return Some(format!("Invalid language meta response")),
|
|
};
|
|
Some(response)
|
|
},
|
|
Show => {
|
|
let stage_name = match arguments.get(0) {
|
|
Some(s) => s.to_string(),
|
|
None => return Some(format!("Must specify a stage to show")),
|
|
};
|
|
let ask = DebugAsk::ByStage { stage_name };
|
|
repl.options.debug_asks.insert(ask);
|
|
None
|
|
},
|
|
Hide => {
|
|
let stage_name = match arguments.get(0) {
|
|
Some(s) => s.to_string(),
|
|
None => return Some(format!("Must specify a stage to hide")),
|
|
};
|
|
let ask = DebugAsk::ByStage { stage_name };
|
|
repl.options.debug_asks.remove(&ask);
|
|
None
|
|
},
|
|
TotalTimeOff => total_time_off(repl, arguments),
|
|
TotalTimeOn => total_time_on(repl, arguments),
|
|
StageTimeOff => stage_time_off(repl, arguments),
|
|
StageTimeOn => stage_time_on(repl, arguments),
|
|
Doc => doc(repl, arguments),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn total_time_on(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
|
|
repl.options.show_total_time = true;
|
|
None
|
|
}
|
|
|
|
fn total_time_off(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
|
|
repl.options.show_total_time = false;
|
|
None
|
|
}
|
|
|
|
fn stage_time_on(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
|
|
repl.options.show_stage_times = true;
|
|
None
|
|
}
|
|
|
|
fn stage_time_off(repl: &mut Repl, _: &[&str]) -> InterpreterDirectiveOutput {
|
|
repl.options.show_stage_times = false;
|
|
None
|
|
}
|
|
|
|
fn doc(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
|
|
arguments.get(0).map(|cmd| {
|
|
let source = cmd.to_string();
|
|
let meta = LangMetaRequest::Docs { source };
|
|
let cur_state = repl.get_cur_language_state();
|
|
match cur_state.request_meta(meta) {
|
|
LangMetaResponse::Docs { doc_string } => Some(doc_string),
|
|
_ => Some(format!("Invalid doc response"))
|
|
}
|
|
}).unwrap_or(Some(format!(":docs needs an argument")))
|
|
}
|
|
|
|
fn help(repl: &mut Repl, arguments: &[&str]) -> InterpreterDirectiveOutput {
|
|
let mut buf = String::new();
|
|
let directives = match repl.get_directives() {
|
|
CommandTree::Top(children) => children,
|
|
_ => panic!("Top-level CommandTree not Top")
|
|
};
|
|
|
|
match arguments {
|
|
[] => {
|
|
writeln!(buf, "MetaInterpreter options").unwrap();
|
|
writeln!(buf, "-----------------------").unwrap();
|
|
|
|
for directive in directives {
|
|
let trailer = " ";
|
|
writeln!(buf, "{}{}- {}", directive.get_cmd(), trailer, directive.get_help()).unwrap();
|
|
}
|
|
|
|
let ref lang = repl.get_cur_language_state();
|
|
writeln!(buf, "").unwrap();
|
|
writeln!(buf, "Language-specific help for {}", lang.get_language_name()).unwrap();
|
|
writeln!(buf, "-----------------------").unwrap();
|
|
},
|
|
_ => {
|
|
writeln!(buf, "Command-specific help not available yet").unwrap();
|
|
}
|
|
};
|
|
Some(buf)
|
|
}
|