Exemplo n.º 1
0
        /// <summary>
        /// References a variable. Searches the enclosing environment if not found.
        /// Signals an error if the variable is unbound.
        /// </summary>
        /// <param name="key">The name of the variable.</param>
        /// <returns></returns>
        public IValue LookupBinding(string key)
        {
            if (this == TheEmptyEnvironment)
            {
                throw new RuntimeErrorException("environment", $"Attempting to reference an unbound variable '{key}");
            }

            if (bindings.ContainsKey(key))
            {
                return(bindings[key]);
            }
            else
            {
                return(EnclosingEnvironment.LookupBinding(key));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Assigns to a variable. Searches the enclosing environment if not found.
        /// Signals an error if the variable is unbound.
        /// </summary>
        /// <param name="key">The name of the variable.</param>
        /// <param name="newValue">The new value of the variable</param>
        public void SetBinding(string key, IValue newValue)
        {
            if (this == TheEmptyEnvironment)
            {
                throw new RuntimeErrorException("environment", $" Attempting to assign to an unbound variable '{key}");
            }

            if (bindings.ContainsKey(key))
            {
                bindings[key] = newValue;
            }
            else
            {
                EnclosingEnvironment.SetBinding(key, newValue);
            }
        }