cl_interpret/
env.rs

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//! Lexical and non-lexical scoping for variables

use crate::builtin::Builtin;

use super::{
    builtin::{Builtins, Math},
    convalue::ConValue,
    error::{Error, IResult},
    function::Function,
    Callable, Interpret,
};
use cl_ast::{Function as FnDecl, Sym};
use std::{
    collections::HashMap,
    fmt::Display,
    ops::{Deref, DerefMut},
    rc::Rc,
};

type StackFrame = HashMap<Sym, Option<ConValue>>;

/// Implements a nested lexical scope
#[derive(Clone, Debug)]
pub struct Environment {
    builtin: StackFrame,
    global: Vec<(StackFrame, &'static str)>,
    frames: Vec<(StackFrame, &'static str)>,
}

impl Display for Environment {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for (frame, name) in self
            .global
            .iter()
            .rev()
            .take(2)
            .rev()
            .chain(self.frames.iter())
        {
            writeln!(f, "--- {name} ---")?;
            for (var, val) in frame {
                write!(f, "{var}: ")?;
                match val {
                    Some(value) => writeln!(f, "\t{value}"),
                    None => writeln!(f, "<undefined>"),
                }?
            }
        }
        Ok(())
    }
}
impl Default for Environment {
    fn default() -> Self {
        Self {
            builtin: to_hashmap(Builtins.iter().chain(Math.iter())),
            global: vec![(HashMap::new(), "globals")],
            frames: vec![],
        }
    }
}

fn to_hashmap(from: impl IntoIterator<Item = &'static Builtin>) -> HashMap<Sym, Option<ConValue>> {
    from.into_iter()
        .map(|v| (v.name(), Some(v.into())))
        .collect()
}

impl Environment {
    pub fn new() -> Self {
        Self::default()
    }
    /// Creates an [Environment] with no [builtins](super::builtin)
    pub fn no_builtins() -> Self {
        Self {
            builtin: HashMap::new(),
            global: vec![(Default::default(), "globals")],
            frames: vec![],
        }
    }

    pub fn builtins(&self) -> &StackFrame {
        &self.builtin
    }

    pub fn add_builtin(&mut self, builtin: &'static Builtin) -> &mut Self {
        self.builtin.insert(builtin.name(), Some(builtin.into()));
        self
    }

    pub fn add_builtins(&mut self, builtins: &'static [Builtin]) {
        for builtin in builtins {
            self.add_builtin(builtin);
        }
    }

    pub fn push_frame(&mut self, name: &'static str, frame: StackFrame) {
        self.frames.push((frame, name));
    }

    pub fn pop_frame(&mut self) -> Option<(StackFrame, &'static str)> {
        self.frames.pop()
    }

    pub fn eval(&mut self, node: &impl Interpret) -> IResult<ConValue> {
        node.interpret(self)
    }

    /// Calls a function inside the interpreter's scope,
    /// and returns the result
    pub fn call(&mut self, name: Sym, args: &[ConValue]) -> IResult<ConValue> {
        // FIXME: Clone to satisfy the borrow checker
        let function = self.get(name)?.clone();
        function.call(self, args)
    }
    /// Enters a nested scope, returning a [`Frame`] stack-guard.
    ///
    /// [`Frame`] implements Deref/DerefMut for [`Environment`].
    pub fn frame(&mut self, name: &'static str) -> Frame {
        Frame::new(self, name)
    }
    /// Resolves a variable mutably.
    ///
    /// Returns a mutable reference to the variable's record, if it exists.
    pub fn get_mut(&mut self, id: Sym) -> IResult<&mut Option<ConValue>> {
        for (frame, _) in self.frames.iter_mut().rev() {
            if let Some(var) = frame.get_mut(&id) {
                return Ok(var);
            }
        }
        for (frame, _) in self.global.iter_mut().rev() {
            if let Some(var) = frame.get_mut(&id) {
                return Ok(var);
            }
        }
        self.builtin.get_mut(&id).ok_or(Error::NotDefined(id))
    }
    /// Resolves a variable immutably.
    ///
    /// Returns a reference to the variable's contents, if it is defined and initialized.
    pub fn get(&self, id: Sym) -> IResult<ConValue> {
        for (frame, _) in self.frames.iter().rev() {
            match frame.get(&id) {
                Some(Some(var)) => return Ok(var.clone()),
                Some(None) => return Err(Error::NotInitialized(id)),
                _ => (),
            }
        }
        for (frame, _) in self.global.iter().rev() {
            match frame.get(&id) {
                Some(Some(var)) => return Ok(var.clone()),
                Some(None) => return Err(Error::NotInitialized(id)),
                _ => (),
            }
        }
        self.builtin
            .get(&id)
            .cloned()
            .flatten()
            .ok_or(Error::NotDefined(id))
    }

    pub(crate) fn get_local(&self, id: Sym) -> IResult<ConValue> {
        for (frame, _) in self.frames.iter().rev() {
            match frame.get(&id) {
                Some(Some(var)) => return Ok(var.clone()),
                Some(None) => return Err(Error::NotInitialized(id)),
                _ => (),
            }
        }
        Err(Error::NotInitialized(id))
    }

    /// Inserts a new [ConValue] into this [Environment]
    pub fn insert(&mut self, id: Sym, value: Option<ConValue>) {
        if let Some((frame, _)) = self.frames.last_mut() {
            frame.insert(id, value);
        } else if let Some((frame, _)) = self.global.last_mut() {
            frame.insert(id, value);
        }
    }
    /// A convenience function for registering a [FnDecl] as a [Function]
    pub fn insert_fn(&mut self, decl: &FnDecl) {
        let FnDecl { name, .. } = decl;
        let (name, function) = (name, Rc::new(Function::new(decl)));
        if let Some((frame, _)) = self.frames.last_mut() {
            frame.insert(*name, Some(ConValue::Function(function.clone())));
        } else if let Some((frame, _)) = self.global.last_mut() {
            frame.insert(*name, Some(ConValue::Function(function.clone())));
        }
        // Tell the function to lift its upvars now, after it's been declared
        function.lift_upvars(self);
    }
}

/// Functions which aid in the implementation of [`Frame`]
impl Environment {
    /// Enters a scope, creating a new namespace for variables
    fn enter(&mut self, name: &'static str) -> &mut Self {
        self.frames.push((Default::default(), name));
        self
    }

    /// Exits the scope, destroying all local variables and
    /// returning the outer scope, if there is one
    fn exit(&mut self) -> &mut Self {
        self.frames.pop();
        self
    }
}

/// Represents a stack frame
#[derive(Debug)]
pub struct Frame<'scope> {
    scope: &'scope mut Environment,
}
impl<'scope> Frame<'scope> {
    fn new(scope: &'scope mut Environment, name: &'static str) -> Self {
        Self { scope: scope.enter(name) }
    }
}
impl Deref for Frame<'_> {
    type Target = Environment;
    fn deref(&self) -> &Self::Target {
        self.scope
    }
}
impl DerefMut for Frame<'_> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.scope
    }
}
impl Drop for Frame<'_> {
    fn drop(&mut self) {
        self.scope.exit();
    }
}