summaryrefslogtreecommitdiff
path: root/src/eval/scope.rs
blob: 1ed34f8667ca1435c4d24d191f1efce9ccf68bd8 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use std::collections::HashMap;
use std::fmt::{self, Debug, Formatter};
use std::iter;

use super::Value;

/// A stack of scopes.
#[derive(Debug, Clone, PartialEq)]
pub struct Scopes<'a> {
    /// The active scope.
    top: Scope,
    /// The stack of lower scopes.
    scopes: Vec<Scope>,
    /// The base scope.
    base: &'a Scope,
}

impl<'a> Scopes<'a> {
    /// Create a new hierarchy of scopes.
    pub fn new(base: &'a Scope) -> Self {
        Self { top: Scope::new(), scopes: vec![], base }
    }

    /// Push a new scope.
    pub fn push(&mut self) {
        self.scopes.push(std::mem::take(&mut self.top));
    }

    /// Pop the topmost scope.
    ///
    /// # Panics
    /// Panics if no scope was pushed.
    pub fn pop(&mut self) {
        self.top = self.scopes.pop().expect("no pushed scope");
    }

    /// Define a variable in the active scope.
    pub fn define(&mut self, var: impl Into<String>, value: impl Into<Value>) {
        self.top.define(var, value);
    }

    /// Look up the value of a variable.
    pub fn get(&self, var: &str) -> Option<&Value> {
        iter::once(&self.top)
            .chain(self.scopes.iter().rev())
            .chain(iter::once(self.base))
            .find_map(|scope| scope.get(var))
    }

    /// Get a mutable reference to a variable.
    pub fn get_mut(&mut self, var: &str) -> Option<&mut Value> {
        iter::once(&mut self.top)
            .chain(self.scopes.iter_mut().rev())
            .find_map(|scope| scope.get_mut(var))
    }

    /// Return whether the variable is constant (not writable).
    ///
    /// Defaults to `false` if the variable does not exist.
    pub fn is_const(&self, var: &str) -> bool {
        self.base.get(var).is_some()
    }
}

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

impl Scope {
    /// Create a new empty scope.
    pub fn new() -> Self {
        Self::default()
    }

    /// Define a new variable.
    pub fn define(&mut self, var: impl Into<String>, value: impl Into<Value>) {
        self.values.insert(var.into(), value.into());
    }

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

    /// Get a mutable reference to a variable.
    pub fn get_mut(&mut self, var: &str) -> Option<&mut Value> {
        self.values.get_mut(var)
    }
}

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