Added variable delete command

pull/2/head
Mark 2023-08-02 17:37:29 -07:00
parent ef408ff181
commit da31fc43e5
Signed by: Mark
GPG Key ID: AD62BB059C2AAEE4
2 changed files with 60 additions and 3 deletions

View File

@ -1,6 +1,7 @@
use std::io::Write;
use crate::context::Context;
use crate::parser::Constant;
use crate::parser::substitute;
use termion::{
raw::RawTerminal,
@ -13,12 +14,16 @@ use termion::{
pub fn is_command(
s: &String
) -> bool {
match &s.trim()[..] {
let args: Vec<&str> = s.split(" ").collect();
let first = args[0];
match first {
"help" | "clear"
| "ops" | "operators"
| "fns" | "functions"
| "vars"
| "consts" | "constants"
| "del" | "delete"
=> true,
_ => false
}
@ -57,8 +62,10 @@ pub fn do_command(
s: &String,
context: &mut Context
) -> Result<(), std::io::Error> {
let args: Vec<&str> = s.split(" ").collect();
let first = args[0];
match &s[..] {
match first {
"help" => {
draw_greeter(stdout)?;
@ -81,6 +88,7 @@ pub fn do_command(
" {c}ops{r} List built-in operators\r\n",
" {c}fns{r} List built-in functions\r\n",
" {c}vars{r} List user-defined variables\r\n",
" {c}del{r} Delete a variable\r\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!")
};

View File

@ -19,7 +19,6 @@ impl Context {
return Ok(());
} else { return Err(()); }
}
pub fn del_var(&mut self, s: &String) { self.variables.remove(s); }
pub fn get_variable(&self, s: &String) -> Option<Expression> {
let v: Option<&Expression>;
@ -31,6 +30,12 @@ impl Context {
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 {
return match s {
"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> {
return &self.variables
}