1use crate::{ast::NumericLiteral, constants::CustomFn};
2use std::{collections::HashMap, convert::Into};
3
4pub enum Variable {
5 Number(NumericLiteral),
6 Function(CustomFn),
7}
8
9macro_rules! num_into_var {
11 ($($ty:ty)*) => {
12 $(impl From<$ty> for Variable {
13 fn from(val: $ty) -> Variable {
14 Variable::Number(val as NumericLiteral)
15 }
16 })*
17 }
18}
19
20num_into_var!(i8 i16 i32 i64 u8 u16 u32 u64 isize usize f32);
21
22impl From<CustomFn> for Variable {
23 fn from(val: CustomFn) -> Variable {
24 Variable::Function(val)
25 }
26}
27
28#[derive(Default)]
29pub struct Scope {
30 variables: HashMap<String, Variable>,
31}
32
33impl Scope {
34 pub fn new() -> Self {
35 Scope {
36 variables: HashMap::new(),
37 }
38 }
39
40 pub fn with_capacity(cap: usize) -> Self {
41 Scope {
42 variables: HashMap::with_capacity(cap),
43 }
44 }
45
46 pub fn set_var<T: Into<Variable>>(&mut self, var_name: &str, value: T) {
47 self.variables.insert(var_name.to_string(), value.into());
48 }
49
50 pub fn get_var(&self, var_name: &str) -> Option<&Variable> {
51 self.variables.get(var_name)
52 }
53}