From 5ccc6e5ec8a433f68bfeb17e8dcedec5b62a36a9 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Wed, 17 Jan 2024 18:24:21 +0100 Subject: First sketch of matheval --- src/context.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/context.rs (limited to 'src/context.rs') diff --git a/src/context.rs b/src/context.rs new file mode 100644 index 0000000..12bea6b --- /dev/null +++ b/src/context.rs @@ -0,0 +1,51 @@ +use crate::function::Function; +use crate::operation::Operation; +use std::collections::HashMap; + +#[derive(Default)] +pub struct Context { + ops: Vec, + vars: HashMap, + funcs: HashMap>, +} + +impl Context { + pub fn new() -> Self { + Self::default() + } + + pub fn with_operations(mut self, ops: Vec) -> Self { + self.ops = ops; + self + } + + pub fn with_variables(mut self, vars: HashMap) -> Self { + self.vars = vars; + self + } + + pub fn with_functions(mut self, funcs: HashMap>) -> Self { + self.funcs = funcs; + self + } + + pub fn operations(&self) -> &Vec { + &self.ops + } + + pub fn variable_mut(&mut self, name: &str) -> Option<&mut f64> { + self.vars.get_mut(name) + } + + pub fn function_mut(&mut self, name: &str) -> Option<&mut Box> { + self.funcs.get_mut(name) + } + + pub fn variable(&self, name: &str) -> Option<&f64> { + self.vars.get(name) + } + + pub fn function(&self, name: &str) -> Option<&Box> { + self.funcs.get(name) + } +} -- cgit v1.2.3-70-g09d2