2021-10-14 01:33:46 -07:00
|
|
|
use crate::help::help;
|
2021-10-14 00:56:01 -07:00
|
|
|
use crate::language::{
|
2021-10-14 02:08:32 -07:00
|
|
|
LangMetaRequest, LangMetaResponse, ProgrammingLanguageInterface,
|
2021-10-14 00:56:01 -07:00
|
|
|
};
|
2021-10-14 01:33:46 -07:00
|
|
|
use crate::{InterpreterDirectiveOutput, Repl};
|
2019-06-02 00:27:12 -07:00
|
|
|
use std::fmt::Write as FmtWrite;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub enum DirectiveAction {
|
2021-10-07 01:19:35 -07:00
|
|
|
Null,
|
|
|
|
Help,
|
|
|
|
QuitProgram,
|
|
|
|
ListPasses,
|
2021-10-14 02:20:11 -07:00
|
|
|
TotalTime(bool),
|
|
|
|
StageTime(bool),
|
2021-10-07 01:19:35 -07:00
|
|
|
Doc,
|
2019-06-02 00:27:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DirectiveAction {
|
2021-10-14 00:56:01 -07:00
|
|
|
pub fn perform<L: ProgrammingLanguageInterface>(
|
|
|
|
&self,
|
|
|
|
repl: &mut Repl<L>,
|
|
|
|
arguments: &[&str],
|
|
|
|
) -> InterpreterDirectiveOutput {
|
2021-10-07 01:19:35 -07:00
|
|
|
use DirectiveAction::*;
|
|
|
|
match self {
|
|
|
|
Null => None,
|
|
|
|
Help => help(repl, arguments),
|
|
|
|
QuitProgram => {
|
|
|
|
repl.save_before_exit();
|
|
|
|
::std::process::exit(0)
|
|
|
|
}
|
|
|
|
ListPasses => {
|
2021-10-14 00:56:01 -07:00
|
|
|
let pass_names = match repl
|
|
|
|
.language_state
|
|
|
|
.request_meta(LangMetaRequest::StageNames)
|
|
|
|
{
|
2021-10-07 01:19:35 -07:00
|
|
|
LangMetaResponse::StageNames(names) => names,
|
|
|
|
_ => vec![],
|
|
|
|
};
|
2019-06-02 00:27:12 -07:00
|
|
|
|
2021-10-07 01:19:35 -07:00
|
|
|
let mut buf = String::new();
|
2021-10-14 02:08:32 -07:00
|
|
|
for pass in pass_names.iter().map(Some).intersperse(None) {
|
2021-10-07 01:19:35 -07:00
|
|
|
match pass {
|
|
|
|
Some(pass) => write!(buf, "{}", pass).unwrap(),
|
|
|
|
None => write!(buf, " -> ").unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some(buf)
|
|
|
|
}
|
2021-10-14 02:20:11 -07:00
|
|
|
TotalTime(value) => {
|
|
|
|
repl.options.show_total_time = *value;
|
2021-10-14 00:56:01 -07:00
|
|
|
None
|
|
|
|
}
|
2021-10-14 02:20:11 -07:00
|
|
|
StageTime(value) => {
|
|
|
|
repl.options.show_stage_times = *value;
|
2021-10-14 00:56:01 -07:00
|
|
|
None
|
|
|
|
}
|
2021-10-07 01:19:35 -07:00
|
|
|
Doc => doc(repl, arguments),
|
|
|
|
}
|
2019-06-02 00:27:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-14 00:56:01 -07:00
|
|
|
fn doc<L: ProgrammingLanguageInterface>(
|
|
|
|
repl: &mut Repl<L>,
|
|
|
|
arguments: &[&str],
|
|
|
|
) -> InterpreterDirectiveOutput {
|
2021-10-07 01:19:35 -07:00
|
|
|
arguments
|
|
|
|
.get(0)
|
|
|
|
.map(|cmd| {
|
|
|
|
let source = cmd.to_string();
|
|
|
|
let meta = LangMetaRequest::Docs { source };
|
2021-10-14 00:56:01 -07:00
|
|
|
match repl.language_state.request_meta(meta) {
|
2021-10-07 01:19:35 -07:00
|
|
|
LangMetaResponse::Docs { doc_string } => Some(doc_string),
|
2021-10-14 02:08:32 -07:00
|
|
|
_ => Some("Invalid doc response".to_owned()),
|
2021-10-07 01:19:35 -07:00
|
|
|
}
|
|
|
|
})
|
2021-10-14 02:08:32 -07:00
|
|
|
.unwrap_or_else(|| Some(":docs needs an argument".to_owned()))
|
2019-06-02 00:43:55 -07:00
|
|
|
}
|