Make repl mod structure more complex
This commit is contained in:
parent
9927a6b1fd
commit
77e0d639c2
43
schala-repl/src/repl/command_tree.rs
Normal file
43
schala-repl/src/repl/command_tree.rs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub enum CommandTree {
|
||||||
|
Terminal {
|
||||||
|
name: String,
|
||||||
|
help_msg: Option<String>,
|
||||||
|
function: Option<Box<(fn() -> Option<String>)>>,
|
||||||
|
},
|
||||||
|
NonTerminal {
|
||||||
|
name: String,
|
||||||
|
children: Vec<CommandTree>,
|
||||||
|
help_msg: Option<String>,
|
||||||
|
function: Option<Box<(fn() -> Option<String>)>>,
|
||||||
|
},
|
||||||
|
Top(Vec<CommandTree>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CommandTree {
|
||||||
|
pub fn term(s: &str, help: Option<&str>) -> CommandTree {
|
||||||
|
CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function: None }
|
||||||
|
}
|
||||||
|
pub fn get_cmd(&self) -> &str {
|
||||||
|
match self {
|
||||||
|
CommandTree::Terminal { name, .. } => name.as_str(),
|
||||||
|
CommandTree::NonTerminal {name, ..} => name.as_str(),
|
||||||
|
CommandTree::Top(_) => "",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn get_help(&self) -> &str {
|
||||||
|
match self {
|
||||||
|
CommandTree::Terminal { help_msg, ..} => help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""),
|
||||||
|
CommandTree::NonTerminal { help_msg, .. } => help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""),
|
||||||
|
CommandTree::Top(_) => ""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn get_children(&self) -> Vec<&str> {
|
||||||
|
match self {
|
||||||
|
CommandTree::Terminal { .. } => vec![],
|
||||||
|
CommandTree::NonTerminal { children, .. } => children.iter().map(|x| x.get_cmd()).collect(),
|
||||||
|
CommandTree::Top(children) => children.iter().map(|x| x.get_cmd()).collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -7,6 +7,8 @@ use colored::*;
|
|||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use language::{ProgrammingLanguageInterface, EvalOptions,
|
use language::{ProgrammingLanguageInterface, EvalOptions,
|
||||||
PassDebugOptionsDescriptor};
|
PassDebugOptionsDescriptor};
|
||||||
|
mod command_tree;
|
||||||
|
use self::command_tree::CommandTree;
|
||||||
|
|
||||||
const HISTORY_SAVE_FILE: &'static str = ".schala_history";
|
const HISTORY_SAVE_FILE: &'static str = ".schala_history";
|
||||||
const OPTIONS_SAVE_FILE: &'static str = ".schala_repl";
|
const OPTIONS_SAVE_FILE: &'static str = ".schala_repl";
|
||||||
@ -164,12 +166,12 @@ impl Repl {
|
|||||||
.split_whitespace()
|
.split_whitespace()
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let cmd: &str = match commands.get(0).clone() {
|
let initial_cmd: &str = match commands.get(0).clone() {
|
||||||
None => return None,
|
None => return None,
|
||||||
Some(s) => s
|
Some(s) => s
|
||||||
};
|
};
|
||||||
|
|
||||||
match cmd {
|
match initial_cmd {
|
||||||
"exit" | "quit" => {
|
"exit" | "quit" => {
|
||||||
self.save_options();
|
self.save_options();
|
||||||
::std::process::exit(0)
|
::std::process::exit(0)
|
||||||
@ -348,46 +350,3 @@ impl<T: Terminal> Completer<T> for TabCompleteHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
|
||||||
enum CommandTree {
|
|
||||||
Terminal {
|
|
||||||
name: String,
|
|
||||||
help_msg: Option<String>,
|
|
||||||
function: Option<Box<(fn() -> Option<String>)>>,
|
|
||||||
},
|
|
||||||
NonTerminal {
|
|
||||||
name: String,
|
|
||||||
children: Vec<CommandTree>,
|
|
||||||
help_msg: Option<String>,
|
|
||||||
function: Option<Box<(fn() -> Option<String>)>>,
|
|
||||||
},
|
|
||||||
Top(Vec<CommandTree>),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CommandTree {
|
|
||||||
fn term(s: &str, help: Option<&str>) -> CommandTree {
|
|
||||||
CommandTree::Terminal {name: s.to_string(), help_msg: help.map(|x| x.to_string()), function: None }
|
|
||||||
}
|
|
||||||
fn get_cmd(&self) -> &str {
|
|
||||||
match self {
|
|
||||||
CommandTree::Terminal { name, .. } => name.as_str(),
|
|
||||||
CommandTree::NonTerminal {name, ..} => name.as_str(),
|
|
||||||
CommandTree::Top(_) => "",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn get_help(&self) -> &str {
|
|
||||||
match self {
|
|
||||||
CommandTree::Terminal { help_msg, ..} => help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""),
|
|
||||||
CommandTree::NonTerminal { help_msg, .. } => help_msg.as_ref().map(|s| s.as_str()).unwrap_or(""),
|
|
||||||
CommandTree::Top(_) => ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn get_children(&self) -> Vec<&str> {
|
|
||||||
match self {
|
|
||||||
CommandTree::Terminal { .. } => vec![],
|
|
||||||
CommandTree::NonTerminal { children, .. } => children.iter().map(|x| x.get_cmd()).collect(),
|
|
||||||
CommandTree::Top(children) => children.iter().map(|x| x.get_cmd()).collect(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user