From 10bfeab7e457fec2111b193fe0faba74799da921 Mon Sep 17 00:00:00 2001 From: greg Date: Sun, 2 Jun 2019 00:43:55 -0700 Subject: [PATCH] Switch over everything to new directive paradigm --- schala-repl/src/repl/directive_actions.rs | 83 ++++++++++++++++++++++- schala-repl/src/repl/directives.rs | 75 +++----------------- 2 files changed, 90 insertions(+), 68 deletions(-) diff --git a/schala-repl/src/repl/directive_actions.rs b/schala-repl/src/repl/directive_actions.rs index 8cd1948..19054a1 100644 --- a/schala-repl/src/repl/directive_actions.rs +++ b/schala-repl/src/repl/directive_actions.rs @@ -9,6 +9,14 @@ pub enum DirectiveAction { Help, QuitProgram, ListPasses, + ShowImmediate, + Show, + Hide, + TotalTimeOff, + TotalTimeOn, + StageTimeOff, + StageTimeOn, + Doc, } impl DirectiveAction { @@ -36,8 +44,81 @@ impl DirectiveAction { } } 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"))) +} diff --git a/schala-repl/src/repl/directives.rs b/schala-repl/src/repl/directives.rs index 6bc30cd..7a7b9e5 100644 --- a/schala-repl/src/repl/directives.rs +++ b/schala-repl/src/repl/directives.rs @@ -1,5 +1,4 @@ use std::fmt::Write as FmtWrite; -use itertools::Itertools; use crate::repl::Repl; use crate::repl::command_tree::CommandTree; @@ -22,60 +21,16 @@ pub fn directives_from_pass_names(pass_names: &Vec) -> CommandTree { Some("Configure debug information"), vec![ CommandTree::terminal_act("list-passes", Some("List all registered compiler passes"), vec![], ListPasses), - CommandTree::terminal("show-immediate", None, passes_directives.clone(), - Box::new(|repl: &mut Repl, cmds: &[&str]| { - let cur_state = repl.get_cur_language_state(); - let stage_name = match cmds.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) - })), - CommandTree::terminal("show", None, passes_directives.clone(), Box::new(|repl: &mut Repl, cmds: &[&str]| { - let stage_name = match cmds.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 - })), - CommandTree::terminal("hide", None, passes_directives.clone(), Box::new(|repl: &mut Repl, cmds: &[&str]| { - let stage_name = match cmds.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 - })), + CommandTree::terminal_act("show-immediate", None, passes_directives.clone(), ShowImmediate), + CommandTree::terminal_act("show", None, passes_directives.clone(), Show), + CommandTree::terminal_act("hide", None, passes_directives.clone(), Hide), CommandTree::nonterm("total-time", None, vec![ - CommandTree::terminal("on", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| { - repl.options.show_total_time = true; - None - })), - CommandTree::terminal("off", None, vec![], Box::new(turn_off)), + CommandTree::terminal_act("on", None, vec![], TotalTimeOn), + CommandTree::terminal_act("off", None, vec![], TotalTimeOff), ]), CommandTree::nonterm("stage-times", Some("Computation time per-stage"), vec![ - CommandTree::terminal("on", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| { - repl.options.show_stage_times = true; - None - })), - CommandTree::terminal("off", None, vec![], Box::new(|repl: &mut Repl, _: &[&str]| { - repl.options.show_stage_times = false; - None - })), + CommandTree::terminal_act("on", None, vec![], StageTimeOn), + CommandTree::terminal_act("off", None, vec![], StageTimeOff), ]) ] ), @@ -87,21 +42,7 @@ pub fn directives_from_pass_names(pass_names: &Vec) -> CommandTree { CommandTree::nonterm("go", None, vec![]), ] ), - CommandTree::terminal("doc", Some("Get language-specific help for an item"), vec![], Box::new(|repl: &mut Repl, cmds: &[&str]| { - cmds.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"))) - })) + CommandTree::terminal_act("doc", Some("Get language-specific help for an item"), vec![], Doc), ]) } -fn turn_off(repl: &mut Repl, _cmds: &[&str]) -> Option { - repl.options.show_total_time = false; - None -}