1use std::collections::{HashMap, HashSet};
3use crate::token::Function;
5
6#[derive(Debug, PartialEq, PartialOrd, Clone)]
8#[non_exhaustive]
9pub enum Symbol {
10 Variable(f64),
12 Function(Function),
14}
15
16impl From<f64> for Symbol {
17 #[inline]
18 fn from(value: f64) -> Self {
19 Self::Variable(value)
20 }
21}
22
23impl From<Function> for Symbol {
24 #[inline]
25 fn from(value: Function) -> Self {
26 Self::Function(value)
27 }
28}
29
30#[derive(Debug, Default, PartialEq, Clone)]
54pub struct Context<'names> {
55 symbols: HashMap<&'names str, Symbol>,
57 expected_vars: Option<HashSet<&'names str>>,
59}
60
61impl<'names> Context<'names> {
62 #[inline]
64 pub fn set_var<T: Into<f64>>(&mut self, name: &'names str, value: T) {
65 self.symbols.insert(name, value.into().into());
66 }
67
68 #[inline]
70 #[must_use]
71 pub fn with_var<T: Into<f64>>(
72 mut self,
73 name: &'names str,
74 value: T,
75 ) -> Self {
76 self.symbols.insert(name, value.into().into());
77 self
78 }
79
80 #[inline]
82 pub fn set_fn(&mut self, func: Function) {
83 self.symbols.insert(func.name, func.into());
84 }
85
86 #[inline]
88 #[must_use]
89 pub fn with_fn(mut self, func: Function) -> Self {
90 self.symbols.insert(func.name, func.into());
91 self
92 }
93
94 #[inline]
96 pub fn set_expected_vars(&mut self, expected_vars: HashSet<&'names str>) {
97 self.expected_vars = Some(expected_vars);
98 }
99
100 #[inline]
102 #[must_use]
103 pub fn with_expected_vars(
104 mut self,
105 expected_vars: HashSet<&'names str>,
106 ) -> Self {
107 self.expected_vars = Some(expected_vars);
108 self
109 }
110
111 #[inline]
113 #[must_use]
114 pub fn with_symbols(
115 mut self,
116 symbols: HashMap<&'names str, Symbol>,
117 ) -> Self {
118 self.symbols = symbols;
119 self
120 }
121
122 #[inline]
124 #[must_use]
125 pub fn get(&self, name: &str) -> Option<&Symbol> {
126 self.symbols.get(name)
127 }
128
129 #[inline]
131 #[must_use]
132 pub const fn get_expected_vars(&self) -> Option<&HashSet<&str>> {
133 self.expected_vars.as_ref()
134 }
135}