80 lines
2.3 KiB
Rust
80 lines
2.3 KiB
Rust
use crate::help::help;
|
|
use crate::language::{
|
|
LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface,
|
|
};
|
|
use crate::{InterpreterDirectiveOutput, Repl};
|
|
use std::fmt::Write as FmtWrite;
|
|
|
|
#[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()))
|
|
}
|