Contains() public method

Determines whether the environment contains a definition for a variable with the given symbol
public Contains ( LSharp.Symbol symbol ) : bool
symbol LSharp.Symbol
return bool
Esempio n. 1
0
        /// <summary>
        /// Determines whether the environment contains a definition for
        /// a variable with the given symbol
        /// </summary>
        /// <param name="symbol"></param>
        /// <returns>True or false</returns>
        public bool Contains(Symbol symbol)
        {
            if (hashtable.ContainsKey(symbol))
            {
                return(true);
            }

            if (previousEnvironment != null)
            {
                return(previousEnvironment.Contains(symbol));
            }

            return(false);
        }
Esempio n. 2
0
        /// <summary>
        /// Evaluates an expression in a given lexical environment
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Eval(Object expression, Environment environment)
        {
            profiler.TraceCall(expression);

            if (expression == null)
                return profiler.TraceReturn(null);

            // The expression is either an atom or a list
            if (Primitives.IsAtom(expression))
            {
                // Number
                if (expression.GetType() == typeof(Double))
                    return profiler.TraceReturn(expression);

                if (expression.GetType() == typeof(int))
                    return profiler.TraceReturn(expression);

                // Character
                if (expression.GetType() == typeof(char))
                    return profiler.TraceReturn(expression);

                // String
                if (expression.GetType() == typeof(string))
                    return profiler.TraceReturn(expression);

                if (((Symbol)expression) == Symbol.TRUE)
                    return profiler.TraceReturn(true);

                if (((Symbol)expression) == Symbol.FALSE)
                    return profiler.TraceReturn(false);

                if (((Symbol)expression) == Symbol.NULL)
                    return profiler.TraceReturn(null);

                // If the symbol is bound to a value in this lexical environment
                if (environment.Contains((Symbol)expression))
                    // Then it's a variable so return it's value
                    return profiler.TraceReturn(environment.GetValue((Symbol)expression));
                else
                {
                    // Otherwise symbols evaluate to themselves
                    return profiler.TraceReturn(expression);
                }
            }
            else
            {
                // The expression must be a list
                Cons cons = (Cons)expression;

                // Lists are assumed to be of the form (function arguments)
                object function = null;

                // However, we need to check to see if the function is actually a cons
                // that evaluates to a function first, as in:
                // (= foo (fn () "bar"))
                // (= dispatch (new system.collections.hashtable))
                // (set_item dispatch "foo" foo)
                // ((item dispatch "foo"))
                if (cons.First() is Cons)
                {
                    // if the first arg is a Cons, try to evaluate it to a function
                    function = Eval(cons.First(), environment);
                }
                else if (cons.First() is Symbol)
                {
                    // See if there is a binding to a function, clsoure, macro or special form
                    // in this lexical environment
                    function = environment.GetValue((Symbol)cons.First());

                    if (function == null)
                        function = cons.First();
                }
                else if (cons.First() == null)
                {
                    string msg = "Expected a function as the first argument in a list but received null";
                    throw new System.ArgumentNullException(msg);
                }
                else
                {
                    string msg = "Expected a function as the first argument in a list but received type '";
                    msg += cons.First().GetType().ToString() + "'";
                    throw new System.ArgumentException(msg);
                }

                // If it's a special form
                if (function.GetType() == typeof(SpecialForm))
                {
                    return profiler.TraceReturn(((SpecialForm)function)((Cons)cons.Cdr(), environment));
                }

                // If its a macro application
                if (function.GetType() == typeof(Macro))
                {
                    object expansion = ((Macro)function).Expand((Cons)cons.Cdr());
                    return profiler.TraceReturn(Runtime.Eval(expansion, environment));
                }

                // It must be a function, closure or method invocation,
                // so call apply
                Object arguments = EvalList((Cons)cons.Cdr(), environment);
                return profiler.TraceReturn(Runtime.Apply(function, arguments, environment));

            }
        }
Esempio n. 3
0
 public static bool Bound(Symbol symbol, Environment environment)
 {
     if (environment.Contains(symbol))
         return true;
     else
         return false;
 }
Esempio n. 4
0
        public static Expression Compile1(Object s, Environment environment)
        {
            // Literals are constants
            if (Runtime.IsLiteral(s))
                return Expression.Constant(s);

            // Special Syntax gets expanded and compiled
            if (Runtime.IsSsyntax(s))
                return Compile1(Runtime.ExpandSSyntax(s),environment);

            // Symbols are variable references
            if (s is Symbol)
                return CompileVarRef((Symbol)s);

            // Special Syntax gets expanded and compiled
            if (Runtime.IsSsyntax(Runtime.Car(s)))
            {
                object expansion = Runtime.ExpandSSyntax(((Pair)s).First());
                return Compile1(new Pair(expansion, (Pair)s),environment);
            }

            if (s is Pair)
            {
                object f = ((Pair)s).First();

                // Special Forms

                if (f == Symbol.FromName("if"))
                    return Compiler.CompileIf(((Pair)s).Rest(), environment);

                if (f == Symbol.FromName("quote"))
                    return Compiler.CompileQuote(((Pair)s).Rest(), environment);

                if (f == Symbol.FromName("call-clr"))
                    return Compiler.CompileCall((Pair)((Pair)s).Rest(), environment);

                if (f == Symbol.FromName("call-instance"))
                    return Compiler.CompileInstanceCall((Pair)((Pair)s).Rest(), environment);

                if (f == Symbol.FromName("fn"))
                    return Compiler.CompileFn((Pair)((Pair)s).Rest(), environment);

                if (f == Symbol.FromName("macro"))
                    return Compiler.CompileMac((Pair)((Pair)s).Rest(), environment);

                if (f == Symbol.FromName("quasiquote"))
                    return Compiler.CompileQuasiQuote((Pair)((Pair)s).Rest(), environment);

                if ((f is Symbol) && (environment.Contains((Symbol)f)))
                {
                    object value = environment.GetValue((Symbol)f);

                    // Macros get invoked at compile time, then their results are themeselves compiled
                    if (value is Macro)
                    {
                        object expansion = ((Macro)value).Call(Runtime.AsArray(((Pair)s).Rest()));
                        return Compile1(expansion,environment);
                    }

                }

                // It must be a function call
                return CompileFunctionCall(f, ((Pair)s).Rest(),environment);

            }

            throw new LSharpException(string.Format("Bad object in expression: ", s),environment);
        }
Esempio n. 5
0
        public static Expression Compile1(Object s, Environment environment)
        {
            // Literals are constants
            if (Runtime.IsLiteral(s))
            {
                return(Expression.Constant(s));
            }

            // Special Syntax gets expanded and compiled
            if (Runtime.IsSsyntax(s))
            {
                return(Compile1(Runtime.ExpandSSyntax(s), environment));
            }

            // Symbols are variable references
            if (s is Symbol)
            {
                return(CompileVarRef((Symbol)s));
            }

            // Special Syntax gets expanded and compiled
            if (Runtime.IsSsyntax(Runtime.Car(s)))
            {
                object expansion = Runtime.ExpandSSyntax(((Pair)s).First());
                return(Compile1(new Pair(expansion, (Pair)s), environment));
            }

            if (s is Pair)
            {
                object f = ((Pair)s).First();

                // Special Forms

                if (f == Symbol.FromName("if"))
                {
                    return(Compiler.CompileIf(((Pair)s).Rest(), environment));
                }

                if (f == Symbol.FromName("quote"))
                {
                    return(Compiler.CompileQuote(((Pair)s).Rest(), environment));
                }

                if (f == Symbol.FromName("call-clr"))
                {
                    return(Compiler.CompileCall((Pair)((Pair)s).Rest(), environment));
                }

                if (f == Symbol.FromName("call-instance"))
                {
                    return(Compiler.CompileInstanceCall((Pair)((Pair)s).Rest(), environment));
                }

                if (f == Symbol.FromName("fn"))
                {
                    return(Compiler.CompileFn((Pair)((Pair)s).Rest(), environment));
                }

                if (f == Symbol.FromName("macro"))
                {
                    return(Compiler.CompileMac((Pair)((Pair)s).Rest(), environment));
                }

                if (f == Symbol.FromName("quasiquote"))
                {
                    return(Compiler.CompileQuasiQuote((Pair)((Pair)s).Rest(), environment));
                }



                if ((f is Symbol) && (environment.Contains((Symbol)f)))
                {
                    object value = environment.GetValue((Symbol)f);


                    // Macros get invoked at compile time, then their results are themeselves compiled
                    if (value is Macro)
                    {
                        object expansion = ((Macro)value).Call(Runtime.AsArray(((Pair)s).Rest()));
                        return(Compile1(expansion, environment));
                    }
                }

                // It must be a function call
                return(CompileFunctionCall(f, ((Pair)s).Rest(), environment));
            }

            throw new LSharpException(string.Format("Bad object in expression: ", s), environment);
        }
Esempio n. 6
0
        /// <summary>
        /// Evaluates an expression in a given lexical environment
        /// </summary>
        /// <param name="form"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Eval(Object expression, Environment environment)
        {
            profiler.TraceCall(expression);

            if (expression == Reader.EOFVALUE)
            {
                return(profiler.TraceReturn(expression));
            }

            if (expression == null)
            {
                return(profiler.TraceReturn(null));
            }

            // The expression is either an atom or a list
            if (Primitives.IsAtom(expression))
            {
                // Number
                if (expression is double)
                {
                    return(profiler.TraceReturn(expression));
                }

                if (expression is int)
                {
                    return(profiler.TraceReturn(expression));
                }

                // Character
                if (expression is char)
                {
                    return(profiler.TraceReturn(expression));
                }

                // String
                if (expression is string)
                {
                    return(profiler.TraceReturn(expression));
                }

                Symbol sym = expression as Symbol;

                if (sym == Symbol.TRUE)
                {
                    return(profiler.TraceReturn(true));
                }

                if (sym == Symbol.FALSE)
                {
                    return(profiler.TraceReturn(false));
                }

                if (sym == Symbol.NULL)
                {
                    return(profiler.TraceReturn(null));
                }

                // If the symbol is bound to a value in this lexical environment
                if (environment.Contains(sym))
                {
                    // Then it's a variable so return it's value
                    return(profiler.TraceReturn(environment.GetValue(sym)));
                }
                else
                {
                    // Otherwise symbols evaluate to themselves
                    return(profiler.TraceReturn(expression));
                }
            }
            else
            {
                // The expression must be a list
                Cons cons = (Cons)expression;

                // Lists are assumed to be of the form (function arguments)

                // See if there is a binding to a function, clsoure, macro or special form
                // in this lexical environment
                object function = environment.GetValue((Symbol)cons.First());

                // If there is no binding, then use the function name directly - it's probably
                // the name of a .NET method
                if (function == null)
                {
                    function = cons.First();
                }

                // If it's a special form
                if (function is SpecialForm)
                {
                    return(profiler.TraceReturn(((SpecialForm)function)((Cons)cons.Cdr(), environment)));
                }

                // If its a macro application
                if (function is Macro)
                {
                    object expansion = ((Macro)function).Expand((Cons)cons.Cdr());
                    return(profiler.TraceReturn(Runtime.Eval(expansion, environment)));
                }

                // It must be a function, closure or method invocation,
                // so call apply
                Object arguments = EvalList((Cons)cons.Cdr(), environment);
                return(profiler.TraceReturn(Runtime.Apply(function, arguments, environment)));
            }
        }
Esempio n. 7
0
        private static void ProcessKeyArguments(Cons argumentNameList, Cons argumentList,
                                                Environment localEnvironment)
        {
            // Make sure that all of the defined key arguments are inserted to the local enviroment with their
            // defaults.

            while (argumentNameList != null)
            {
                Symbol argumentName = null;
                object argumentValue = null;


                // We need to get the name of the argument, it can either be just the name or, it can be
                // it's own Cons with the name and an expression for the default value.

                if (argumentNameList.Car().GetType() == typeof(Cons))
                {
                    // It is a Cons, so extract the name and the default value.  Because the default can be
                    // any expression, we need to evaluate the value every time the function is called.

                    argumentName = (Symbol)argumentNameList.Caar();
                    argumentValue = Runtime.Eval(argumentNameList.Cadar(), localEnvironment);
                }
                else
                {
                    argumentName = (Symbol)argumentNameList.Car();
                }


                // Add this variable to the closure's environment, then advance to the next parameter.

                localEnvironment.AssignLocal(argumentName, argumentValue);

                argumentNameList = (Cons)argumentNameList.Cdr();
            }


            // Now that the parameters and their defaults have been added to the environment we can now
            // process the supplied arguments.

            while (argumentList != null)
            {
                // Because these are keyed parameters, the caller needs to specify the name of each
                // parameter.

                if (argumentList.Car().GetType() != typeof(Symbol))
                {
                    throw new LSharpException("Key parameters must be specified by name.");
                }


                // Grab the current parameter and the value associated with it.  Then make sure that this
                // is a keyword.

                Symbol keywordName = (Symbol)argumentList.Car();
                object argumentValue = argumentList.Cadr();

                if (keywordName.Name[0] != ':')
                {
                    throw new LSharpException(keywordName + " is not a valid keyword.");
                }


                // Now that we know they supplied a keyword, create a symbol out of it and make sure that
                // it exists.

                //keywordName = new Symbol(keywordName.Name.Substring(1));
                keywordName = Symbol.FromName(keywordName.Name.Substring(1));

                if (localEnvironment.Contains(keywordName) == false)
                {
                    throw new LSharpException(keywordName + " is not a recognised keyword.");
                }


                // Update the parameter with the value that the user specified and then move onto the next
                // argument in the list.

                localEnvironment.AssignLocal(keywordName, argumentValue);
                argumentList = (Cons)argumentList.Cddr();
            }

        }
Esempio n. 8
0
        private static void ProcessKeyArguments(Cons argumentNameList, Cons argumentList,
                                                Environment localEnvironment)
        {
            // Make sure that all of the defined key arguments are inserted to the local enviroment with their
            // defaults.

            while (argumentNameList != null)
            {
                Symbol argumentName  = null;
                object argumentValue = null;


                // We need to get the name of the argument, it can either be just the name or, it can be
                // it's own Cons with the name and an expression for the default value.

                if (argumentNameList.Car() is Cons)
                {
                    // It is a Cons, so extract the name and the default value.  Because the default can be
                    // any expression, we need to evaluate the value every time the function is called.

                    argumentName  = (Symbol)argumentNameList.Caar();
                    argumentValue = Runtime.Eval(argumentNameList.Cadar(), localEnvironment);
                }
                else
                {
                    argumentName = (Symbol)argumentNameList.Car();
                }


                // Add this variable to the closure's environment, then advance to the next parameter.

                localEnvironment.AssignLocal(argumentName, argumentValue);

                argumentNameList = (Cons)argumentNameList.Cdr();
            }


            // Now that the parameters and their defaults have been added to the environment we can now
            // process the supplied arguments.

            while (argumentList != null)
            {
                // Because these are keyed parameters, the caller needs to specify the name of each
                // parameter.

                if (argumentList.Car().GetType() != typeof(Symbol))
                {
                    throw new LSharpException("Key parameters must be specified by name.");
                }


                // Grab the current parameter and the value associated with it.  Then make sure that this
                // is a keyword.

                Symbol keywordName   = (Symbol)argumentList.Car();
                object argumentValue = argumentList.Cadr();

                if (keywordName.Name[0] != ':')
                {
                    throw new LSharpException(keywordName + " is not a valid keyword.");
                }


                // Now that we know they supplied a keyword, create a symbol out of it and make sure that
                // it exists.

                //keywordName = new Symbol(keywordName.Name.Substring(1));
                keywordName = Symbol.FromName(keywordName.Name.Substring(1));

                if (localEnvironment.Contains(keywordName) == false)
                {
                    throw new LSharpException(keywordName + " is not a recognised keyword.");
                }


                // Update the parameter with the value that the user specified and then move onto the next
                // argument in the list.

                localEnvironment.AssignLocal(keywordName, argumentValue);
                argumentList = (Cons)argumentList.Cddr();
            }
        }