summaryrefslogtreecommitdiff
path: root/src/eval/scope.rs
blob: c9ce1423e0c28887a7d9fe582179fc6a81bcf128 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//! Mapping from identifiers to functions.

use std::collections::HashMap;
use std::fmt::{self, Debug, Formatter};

use super::Value;

/// A map from identifiers to functions.
#[derive(Default, Clone, PartialEq)]
pub struct Scope {
    values: HashMap<String, Value>,
}

impl Scope {
    // Create a new empty scope with a fallback function that is invoked when no
    // match is found.
    pub fn new() -> Self {
        Self::default()
    }

    /// Return the value of the given variable.
    pub fn get(&self, var: &str) -> Option<&Value> {
        self.values.get(var)
    }

    /// Store the value for the given variable.
    pub fn set(&mut self, var: impl Into<String>, value: impl Into<Value>) {
        self.values.insert(var.into(), value.into());
    }
}

impl Debug for Scope {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        self.values.fmt(f)
    }
}