mirror of https://github.com/rm-dr/daisy
Added variable delete command
parent
ef408ff181
commit
da31fc43e5
|
@ -1,6 +1,7 @@
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
use crate::parser::Constant;
|
use crate::parser::Constant;
|
||||||
|
use crate::parser::substitute;
|
||||||
|
|
||||||
use termion::{
|
use termion::{
|
||||||
raw::RawTerminal,
|
raw::RawTerminal,
|
||||||
|
@ -13,12 +14,16 @@ use termion::{
|
||||||
pub fn is_command(
|
pub fn is_command(
|
||||||
s: &String
|
s: &String
|
||||||
) -> bool {
|
) -> bool {
|
||||||
match &s.trim()[..] {
|
let args: Vec<&str> = s.split(" ").collect();
|
||||||
|
let first = args[0];
|
||||||
|
|
||||||
|
match first {
|
||||||
"help" | "clear"
|
"help" | "clear"
|
||||||
| "ops" | "operators"
|
| "ops" | "operators"
|
||||||
| "fns" | "functions"
|
| "fns" | "functions"
|
||||||
| "vars"
|
| "vars"
|
||||||
| "consts" | "constants"
|
| "consts" | "constants"
|
||||||
|
| "del" | "delete"
|
||||||
=> true,
|
=> true,
|
||||||
_ => false
|
_ => false
|
||||||
}
|
}
|
||||||
|
@ -57,8 +62,10 @@ pub fn do_command(
|
||||||
s: &String,
|
s: &String,
|
||||||
context: &mut Context
|
context: &mut Context
|
||||||
) -> Result<(), std::io::Error> {
|
) -> Result<(), std::io::Error> {
|
||||||
|
let args: Vec<&str> = s.split(" ").collect();
|
||||||
|
let first = args[0];
|
||||||
|
|
||||||
match &s[..] {
|
match first {
|
||||||
"help" => {
|
"help" => {
|
||||||
draw_greeter(stdout)?;
|
draw_greeter(stdout)?;
|
||||||
|
|
||||||
|
@ -81,6 +88,7 @@ pub fn do_command(
|
||||||
" {c}ops{r} List built-in operators\r\n",
|
" {c}ops{r} List built-in operators\r\n",
|
||||||
" {c}fns{r} List built-in functions\r\n",
|
" {c}fns{r} List built-in functions\r\n",
|
||||||
" {c}vars{r} List user-defined variables\r\n",
|
" {c}vars{r} List user-defined variables\r\n",
|
||||||
|
" {c}del{r} Delete a variable\r\n",
|
||||||
"\n\n",
|
"\n\n",
|
||||||
),
|
),
|
||||||
|
|
||||||
|
@ -236,6 +244,46 @@ pub fn do_command(
|
||||||
)?;
|
)?;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
"del" | "delete" => {
|
||||||
|
if args.len() != 2 {
|
||||||
|
write!(stdout,
|
||||||
|
"{c}{cmd}{r} {t}takes exactly two arguments.{r}\r\n\n",
|
||||||
|
cmd = first,
|
||||||
|
r = format!("{}{}", color::Fg(color::Reset), style::Reset),
|
||||||
|
t = format!("{}{}", color::Fg(color::Red), style::Bold),
|
||||||
|
c = format!("{}{}", color::Fg(color::LightBlack), style::Italic)
|
||||||
|
)?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let v = args[1].to_string();
|
||||||
|
let (_, v) = substitute(&v, v.len());
|
||||||
|
let r = context.delete_variable(&v);
|
||||||
|
|
||||||
|
match r {
|
||||||
|
Ok(()) => {
|
||||||
|
/*write!(stdout,
|
||||||
|
"Deleted variable {c}{v}{r}\r\n\n",
|
||||||
|
v = v,
|
||||||
|
r = format!("{}{}", color::Fg(color::Reset), style::Reset),
|
||||||
|
c = format!("{}{}", color::Fg(color::LightBlack), style::Italic)
|
||||||
|
)?;*/
|
||||||
|
},
|
||||||
|
|
||||||
|
Err(()) => {
|
||||||
|
write!(stdout,
|
||||||
|
"{c}{v}{r} {t}isn't a variable.{r}\r\n\n",
|
||||||
|
v = v,
|
||||||
|
r = format!("{}{}", color::Fg(color::Reset), style::Reset),
|
||||||
|
t = format!("{}{}", color::Fg(color::Red), style::Bold),
|
||||||
|
c = format!("{}{}", color::Fg(color::LightBlack), style::Italic)
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(());
|
||||||
|
},
|
||||||
|
|
||||||
_ => unreachable!("Bad command!")
|
_ => unreachable!("Bad command!")
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@ impl Context {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
} else { return Err(()); }
|
} else { return Err(()); }
|
||||||
}
|
}
|
||||||
pub fn del_var(&mut self, s: &String) { self.variables.remove(s); }
|
|
||||||
|
|
||||||
pub fn get_variable(&self, s: &String) -> Option<Expression> {
|
pub fn get_variable(&self, s: &String) -> Option<Expression> {
|
||||||
let v: Option<&Expression>;
|
let v: Option<&Expression>;
|
||||||
|
@ -31,6 +30,12 @@ impl Context {
|
||||||
if v.is_some() { Some(v.unwrap().clone()) } else { None }
|
if v.is_some() { Some(v.unwrap().clone()) } else { None }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn delete_variable(&mut self, s: &String) -> Result<(), ()> {
|
||||||
|
if !self.is_varible(s) { return Err(()) };
|
||||||
|
self.variables.remove(s);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
pub fn valid_varible(&self, s: &str) -> bool {
|
pub fn valid_varible(&self, s: &str) -> bool {
|
||||||
return match s {
|
return match s {
|
||||||
"ans" => false,
|
"ans" => false,
|
||||||
|
@ -38,6 +43,10 @@ impl Context {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn is_varible(&self, s: &str) -> bool {
|
||||||
|
return self.valid_varible(s) && self.variables.contains_key(s);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_variables(&self) -> &HashMap<String, Expression> {
|
pub fn get_variables(&self) -> &HashMap<String, Expression> {
|
||||||
return &self.variables
|
return &self.variables
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue