daisy/src/context.rs

133 lines
3.1 KiB
Rust
Raw Normal View History

2023-08-04 12:25:10 -07:00
use crate::parser::{Expression, Function, Constant};
use crate::quantity::freeunit_from_string;
2023-06-14 14:36:58 -07:00
use std::collections::HashMap;
#[derive(Debug)]
pub struct Context {
2023-06-16 12:58:06 -07:00
history: Vec<Expression>,
2023-08-04 12:25:10 -07:00
variables: HashMap<String, Expression>,
2023-08-04 16:06:43 -07:00
functions: HashMap<String, (Vec<String>, Expression)>,
// Shadow variables, for function evaluation.
shadow: HashMap<String, Option<Expression>>
2023-06-14 14:36:58 -07:00
}
2023-08-04 12:25:10 -07:00
// General functions
2023-06-14 14:36:58 -07:00
impl Context {
pub fn new() -> Context {
2023-08-04 12:25:10 -07:00
Context{
history: Vec::new(),
variables: HashMap::new(),
functions: HashMap::new(),
2023-08-04 16:06:43 -07:00
shadow: HashMap::new(),
2023-08-04 12:25:10 -07:00
}
2023-06-14 14:36:58 -07:00
}
2023-06-16 12:58:06 -07:00
pub fn push_hist(&mut self, t: Expression) { self.history.push(t); }
2023-08-04 12:25:10 -07:00
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<(), ()> {
2023-06-17 22:15:58 -07:00
if self.valid_varible(&s) {
2023-08-04 12:25:10 -07:00
self.functions.remove(&s);
2023-06-17 22:15:58 -07:00
self.variables.insert(s, t);
return Ok(());
} else { return Err(()); }
}
2023-06-14 14:36:58 -07:00
2023-08-04 16:06:43 -07:00
// Returns None if this is a "floating" variable
2023-06-16 12:58:06 -07:00
pub fn get_variable(&self, s: &String) -> Option<Expression> {
2023-08-04 16:06:43 -07:00
if self.shadow.contains_key(s) {
return self.shadow.get(s).unwrap().clone();
}
2023-06-16 12:58:06 -07:00
let v: Option<&Expression>;
2023-06-14 14:36:58 -07:00
if s == "ans" {
v = self.history.last();
} else {
2023-06-14 16:15:51 -07:00
v = self.variables.get(s);
2023-06-14 14:36:58 -07:00
}
2023-08-04 16:06:43 -07:00
if v.is_some() {
return Some(v.unwrap().clone());
} else { panic!() }
2023-06-14 14:36:58 -07:00
}
2023-06-17 22:15:58 -07:00
pub fn valid_varible(&self, s: &str) -> bool {
2023-08-03 14:28:22 -07:00
if {
Function::from_string(s).is_some() ||
2023-08-03 22:05:46 -07:00
Constant::from_string(s).is_some() ||
freeunit_from_string(s).is_some()
2023-08-03 14:28:22 -07:00
} { return false }
for c in s.to_lowercase().chars() {
if !"abcdefghijklmnopqrtstuvwxyz_".contains(c) {
return false;
}
}
2023-06-17 22:15:58 -07:00
return match s {
"ans" => false,
_ => true
}
}
2023-08-02 17:37:29 -07:00
pub fn is_varible(&self, s: &str) -> bool {
2023-08-04 16:06:43 -07:00
return self.valid_varible(s) && (
self.variables.contains_key(s) ||
self.shadow.contains_key(s)
);
2023-08-02 17:37:29 -07:00
}
2023-08-02 15:36:55 -07:00
pub fn get_variables(&self) -> &HashMap<String, Expression> {
return &self.variables
}
2023-08-04 16:06:43 -07:00
pub fn add_shadow(&mut self, s: String, v: Option<Expression>) {
if !self.valid_varible(&s) { panic!() }
self.shadow.insert(s, v);
}
pub fn clear_shadow(&mut self) {
self.shadow = HashMap::new();
}
2023-06-14 14:36:58 -07:00
}
2023-08-04 12:25:10 -07:00
// 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<String>, 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<String>, 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<String, (Vec<String>, Expression)> {
return &self.functions
}
}