From d452f764559d624641f427f590c34836af8e0d80 Mon Sep 17 00:00:00 2001 From: Mark Date: Fri, 4 Aug 2023 12:25:10 -0700 Subject: [PATCH] Added functions to context --- src/context.rs | 64 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 11 deletions(-) diff --git a/src/context.rs b/src/context.rs index c685589..6ef855c 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1,20 +1,42 @@ -use crate::parser::Expression; +use crate::parser::{Expression, Function, Constant}; +use crate::quantity::freeunit_from_string; use std::collections::HashMap; #[derive(Debug)] pub struct Context { history: Vec, - variables: HashMap + variables: HashMap, + functions: HashMap, Expression)> } +// General functions impl Context { pub fn new() -> Context { - Context{ history: Vec::new(), variables: HashMap::new() } + Context{ + history: Vec::new(), + variables: HashMap::new(), + functions: HashMap::new(), + } } pub fn push_hist(&mut self, t: Expression) { self.history.push(t); } - pub fn push_var(&mut self, s: String, t: Expression) -> Result<(), ()> { + + + pub fn delete(&mut self, s: &String) -> Result<(), ()> { + if !(self.is_varible(s) || self.is_function(s)) { return Err(()) }; + if self.is_varible(s) { self.variables.remove(s); } + if self.is_function(s) { self.functions.remove(s); } + return Ok(()); + } +} + + + +// Variable manipulation +impl Context { + pub fn push_variable(&mut self, s: String, t: Expression) -> Result<(), ()> { if self.valid_varible(&s) { + self.functions.remove(&s); self.variables.insert(s, t); return Ok(()); } else { return Err(()); } @@ -30,14 +52,7 @@ 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 { - if { Function::from_string(s).is_some() || Constant::from_string(s).is_some() || @@ -65,3 +80,30 @@ impl Context { } } + + +// Function manipulation +impl Context { + pub fn valid_function(&self, s: &str) -> bool { + return self.valid_varible(s); + } + + pub fn push_function(&mut self, s: String, a: Vec, t: Expression) -> Result<(), ()> { + if self.valid_function(&s) { + self.variables.remove(&s); + self.functions.insert(s, (a, t)); + return Ok(()); + } else { return Err(()); } + } + + pub fn get_function(&self, s: &String) -> Option<(Vec, Expression)> { + return Some(self.functions.get(s).unwrap().clone()); + } + + pub fn is_function(&self, s: &str) -> bool { + return self.valid_function(s) && self.functions.contains_key(s); + } + pub fn get_functions(&self) -> &HashMap, Expression)> { + return &self.functions + } +} \ No newline at end of file