From e676ab53ddbab367179ee2ab214bb41ff2ee0c11 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Thu, 1 Oct 2020 15:13:04 +0200 Subject: =?UTF-8?q?Rename=20compute=20to=20eval=20=E2=9C=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/eval/scope.rs | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/eval/scope.rs (limited to 'src/eval/scope.rs') 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, +} + +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, 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() + } +} -- cgit v1.2.3