Ejemplo n.º 1
0
        // Return the value of a target symbol in this or an outer scope.
        public JKLVal Get(JKLSym keySymbol)
        {
            Env e = Find(keySymbol);

            if (e == null)
            {
                switch (keySymbol.ToString(true))
                {
                // Some symbols are only valid when used in a particular context.

                case "unquote":
                    throw new JKLEvalError("'unquote' used incorrectly (missing macro?)");

                case "quasiquote":
                    throw new JKLEvalError("'quasiquote' used incorrectly (missing macro?)");

                case "splice-unquote":
                    throw new JKLEvalError("'splice-unquote' used incorrectly (missing macro?)");
                }
                //// If here the symbol simply hasn't been defined or is mis-spelt.
                throw new JKLLookupError("Get - Symbol not found '" + keySymbol.ToString(true) + "'");
            }
            else
            {
                if (e.data.TryGetValue(keySymbol.getName(), out JKLVal value))
                {
                    return(value);
                }
                else
                {
                    throw new JKLInternalError("Get - Find successful but symbol retrieval failed '" + keySymbol.ToString(true) + "'");
                }
            }
        }
Ejemplo n.º 2
0
 public void Set(JKLSym keySymbol, JKLVal value)
 {
     // Takes a symbol key and a JKL value and adds them to the environment.
     if (!data.TryAdd(keySymbol.getName(), value))
     {
         // Symbol can shadow an equivalent in an outer scope but cannot be duped.
         throw new JKLEvalError("Attempt to redefine '" + keySymbol.ToString(true) + "'");
     }
 }
Ejemplo n.º 3
0
 // Search for and return the environment (scope) that contains a target symbol.
 public Env Find(JKLSym keySymbol)
 {
     if (data.ContainsKey(keySymbol.getName()))
     {
         // Symbol exists in the current scope.
         return(this);
     }
     else if (outer != null)
     {
         // Recurse to search for the symbol in an outer scope.
         return(outer.Find(keySymbol));
     }
     else
     {
         // Symbol is not defined.
         return(null);
     }
 }