summaryrefslogtreecommitdiff
path: root/src/eval/scope.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2020-10-01 15:13:04 +0200
committerLaurenz <laurmaedje@gmail.com>2020-10-01 15:13:04 +0200
commite676ab53ddbab367179ee2ab214bb41ff2ee0c11 (patch)
treef003f6eb6698494310da5771249bbb61a8da27a0 /src/eval/scope.rs
parent7c12f0c07f9d4ed473027dbd00cbbc00e4a3050c (diff)
Rename compute to eval ✏
Diffstat (limited to 'src/eval/scope.rs')
-rw-r--r--src/eval/scope.rs35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/eval/scope.rs b/src/eval/scope.rs
new file mode 100644
index 00000000..8e6576d1
--- /dev/null
+++ b/src/eval/scope.rs
@@ -0,0 +1,35 @@
+//! Mapping from identifiers to functions.
+
+use std::collections::HashMap;
+use std::fmt::{self, Debug, Formatter};
+
+use super::value::FuncValue;
+
+/// A map from identifiers to functions.
+pub struct Scope {
+ functions: HashMap<String, FuncValue>,
+}
+
+impl Scope {
+ // Create a new empty scope with a fallback function that is invoked when no
+ // match is found.
+ pub fn new() -> Self {
+ Self { functions: HashMap::new() }
+ }
+
+ /// Associate the given name with the function.
+ pub fn insert(&mut self, name: impl Into<String>, function: FuncValue) {
+ self.functions.insert(name.into(), function);
+ }
+
+ /// Return the function with the given name if there is one.
+ pub fn func(&self, name: &str) -> Option<&FuncValue> {
+ self.functions.get(name)
+ }
+}
+
+impl Debug for Scope {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ f.debug_set().entries(self.functions.keys()).finish()
+ }
+}