From da31fc43e599aa6bb20a4cd1a642295a2349314e Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 2 Aug 2023 17:37:29 -0700 Subject: [PATCH] Added variable delete command --- src/command/mod.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++-- src/context.rs | 11 +++++++++- 2 files changed, 60 insertions(+), 3 deletions(-) diff --git a/src/command/mod.rs b/src/command/mod.rs index b376f71..ccabf7f 100644 --- a/src/command/mod.rs +++ b/src/command/mod.rs @@ -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!") }; diff --git a/src/context.rs b/src/context.rs index 70b9066..1d8305e 100644 --- a/src/context.rs +++ b/src/context.rs @@ -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 { 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 { return &self.variables }