78 lines
2.2 KiB
Rust
78 lines
2.2 KiB
Rust
use std::fmt::Write as FmtWrite;
|
|
|
|
use crate::{
|
|
help::help,
|
|
language::{LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface},
|
|
InterpreterDirectiveOutput, Repl,
|
|
};
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum DirectiveAction {
|
|
Null,
|
|
Help,
|
|
QuitProgram,
|
|
ListPasses,
|
|
TotalTime(bool),
|
|
StageTime(bool),
|
|
Doc,
|
|
}
|
|
|
|
impl DirectiveAction {
|
|
pub fn perform<L: ProgrammingLanguageInterface>(
|
|
&self,
|
|
repl: &mut Repl<L>,
|
|
arguments: &[&str],
|
|
) -> InterpreterDirectiveOutput {
|
|
use DirectiveAction::*;
|
|
match self {
|
|
Null => None,
|
|
Help => help(repl, arguments),
|
|
QuitProgram => {
|
|
repl.save_before_exit();
|
|
::std::process::exit(0)
|
|
}
|
|
ListPasses => {
|
|
let pass_names = match repl.language_state.request_meta(LangMetaRequest::StageNames) {
|
|
LangMetaResponse::StageNames(names) => names,
|
|
_ => vec![],
|
|
};
|
|
|
|
let mut buf = String::new();
|
|
for pass in pass_names.iter().map(Some).intersperse(None) {
|
|
match pass {
|
|
Some(pass) => write!(buf, "{}", pass).unwrap(),
|
|
None => write!(buf, " -> ").unwrap(),
|
|
}
|
|
}
|
|
Some(buf)
|
|
}
|
|
TotalTime(value) => {
|
|
repl.options.show_total_time = *value;
|
|
None
|
|
}
|
|
StageTime(value) => {
|
|
repl.options.show_stage_times = *value;
|
|
None
|
|
}
|
|
Doc => doc(repl, arguments),
|
|
}
|
|
}
|
|
}
|
|
|
|
fn doc<L: ProgrammingLanguageInterface>(
|
|
repl: &mut Repl<L>,
|
|
arguments: &[&str],
|
|
) -> InterpreterDirectiveOutput {
|
|
arguments
|
|
.get(0)
|
|
.map(|cmd| {
|
|
let source = cmd.to_string();
|
|
let meta = LangMetaRequest::Docs { source };
|
|
match repl.language_state.request_meta(meta) {
|
|
LangMetaResponse::Docs { doc_string } => Some(doc_string),
|
|
_ => Some("Invalid doc response".to_owned()),
|
|
}
|
|
})
|
|
.unwrap_or_else(|| Some(":docs needs an argument".to_owned()))
|
|
}
|