Exemplo n.º 1
0
        /// <summary>
        /// Process the arguments passed to a closure, and add them to the given enviroment.
        /// </summary>
        /// <param name="argumentNameList">The list of names and kewords the closure was created with.</param>
        /// <param name="argumentList">The arguments passed to the closure.</param>
        /// <param name="localEnvironment">The closure's local variables.</param>
        /// <returns>Nothing.</returns>
        public static void ProcessArguments(Cons argumentNameList, Cons argumentList, Environment localEnvironment)
        {
            while (argumentNameList != null)
            {
                // Get the name for the closure's parameter.  Then check to see if it's a keyword, if it is then
                // process the keyword.  Otherwise set up that parameter in the closure's enviroment with the
                // caller specified value.

                Symbol argumentName = (Symbol)argumentNameList.Car();

                switch (argumentName.ToString())
                {
                case "&rest":
                    argumentName = (Symbol)argumentNameList.Cadr();
                    localEnvironment.AssignLocal(argumentName, argumentList);
                    argumentNameList = null;
                    argumentList     = null;
                    break;

                case "&optional":
                    ProcessOptionalArguments((Cons)argumentNameList.Cdr(), argumentList, localEnvironment);
                    argumentNameList = null;
                    argumentList     = null;
                    break;

                case "&key":
                    ProcessKeyArguments((Cons)argumentNameList.Cdr(), argumentList, localEnvironment);
                    argumentNameList = null;
                    argumentList     = null;
                    break;

                default:
                    if (argumentList == null)
                    {
                        throw new LSharpException("Not enough parameters given.");
                    }

                    localEnvironment.AssignLocal(argumentName, argumentList.Car());
                    argumentList     = (Cons)argumentList.Cdr();
                    argumentNameList = (Cons)argumentNameList.Cdr();
                    break;
                }
            }


            // Looks like the caller has supplied more parameters than the closure can use.

            if (argumentList != null)
            {
                throw new LSharpException("Too many parameters given.");
            }
        }
Exemplo n.º 2
0
        private static void ProcessOptionalArguments(Cons argumentNameList, Cons argumentList,
                                                     Environment localEnvironment)
        {
            // We need to add all the arguments to the closure's environment.

            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.

                    argumentName  = (Symbol)argumentNameList.Caar();
                    argumentValue = argumentNameList.Cadar();
                }
                else
                {
                    argumentName = (Symbol)argumentNameList.Car();
                }


                // Now, if the caller has specified a value for this argument, get it now.

                if (argumentList != null)
                {
                    argumentValue = argumentList.Car();
                    argumentList  = (Cons)argumentList.Cdr();
                }


                // Finally add the parameter to the closure's list and then move onto the next argument.
                // Because the default can be any expression, we need to evaluate the value every time the
                // function is called.

                localEnvironment.AssignLocal(argumentName, Runtime.Eval(argumentValue, localEnvironment));

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


            // Looks like the caller has supplied more parameters than the closure can use.

            if (argumentList != null)
            {
                throw new LSharpException("Too many parameters given.");
            }
        }
Exemplo n.º 3
0
        public override bool Equals(object obj)
        {
            if (obj is Cons)
            {
                Cons that = (Cons)obj;

                bool carsEqual = Primitives.Eql(this.Car(), that.Car());
                bool cdrsEqual = Primitives.Eql(this.Cdr(), that.Cdr());

                return(carsEqual && cdrsEqual);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 4
0
        public bool MoveNext()
        {
            object o = list.Cdr();

            if (o == null)
            {
                return(false);
            }

            if (o is Cons)
            {
                list = (Cons)o;
            }
            else
            {
                list = new Cons(o);
            }

            return(true);
        }
Exemplo n.º 5
0
        public static Object With(Cons args, Environment environment)
        {
            Environment localEnvironment = new Environment(environment);

            Cons bindings = (Cons)args.First();

            while ((bindings != null) && (bindings.Length() > 1))
            {
                localEnvironment.AssignLocal((Symbol)bindings.First(), Runtime.Eval(bindings.Second(), environment));
                bindings = (Cons)bindings.Cddr();
            }

            object result = null;

            foreach (object item in (Cons)args.Cdr())
            {
                result = Runtime.Eval(item, localEnvironment);
            }
            return(result);
        }
Exemplo n.º 6
0
		/// <summary>
		/// (with ((symbol value)* ) expression*) 
		/// Binds new local variables symbols to values in a new local 
		/// lexical environment, before evaluating expressions. Similar to 
		/// let, but allows multiple local variables to be bound.
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object With(Cons args, Environment environment) 
		{
			Environment localEnvironment = new Environment(environment);

			Cons bindings = (Cons)args.First();

			while ((bindings != null) && (bindings.Length() > 1))
			{
                localEnvironment.AssignLocal((Symbol)bindings.First(), Runtime.Eval(bindings.Second(), localEnvironment));
				bindings = (Cons)bindings.Cddr();
			}	
			
			object result = null;
			foreach (object item in (Cons)args.Cdr()) 
			{
				result = Runtime.Eval(item, localEnvironment);
			}
			return result;
		}	
Exemplo n.º 7
0
		/// <summary>
		/// (macro arguments expression*)
		/// Defines a macro. Similar in some respects to defining a clsoure using fn 
		/// except that the expressions are expanded using macroexpand in the current 
		/// environment before being evaluated. 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object Macro(Cons args, Environment environment) 
		{
			return new Macro((Cons)args.First(),(Cons)args.Cdr(),environment);
		}
Exemplo n.º 8
0
		/// <summary>
		/// (fn arguments body)
		/// Defines a closure with the specified list of arguments and the specified body, 
		/// an L Sharp expression. Not unlike a lambda expression in Common Lisp.NB arguments 
		/// is a simple list, we dont yet support keyword or optional arguments. 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object Fn(Cons args, Environment environment) 
		{
			return new Closure((Cons)args.First(),(Cons)args.Cdr(),environment);
		}
Exemplo n.º 9
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)));
            }
        }
Exemplo n.º 10
0
 public static Object Fn(Cons args, Environment environment)
 {
     return(new Closure(args.First() as Cons, args.Cdr() as Cons, environment));
 }
Exemplo n.º 11
0
    /// <summary>
    /// (with ((symbol value)* ) expression*)
    /// </summary>
    public static Object With(Cons args, LSharp.Environment environment)
    {
        string v = //"//(with " + Printer.ConsToString(args) + ")" + NewLine +
            "{"  + NewLine;
        Cons bindings = (Cons)args.First();
        LSharp.Environment localEnvironment = new LSharp.Environment(environment);

        while ((bindings != null) && (bindings.Length() > 1))
        {
            v += GenerateAssignLocal((Symbol) bindings.First(), Generate(bindings.Second(),environment), localEnvironment);
            bindings = (Cons)bindings.Cddr();
        }
        
        foreach (object item in (Cons)args.Cdr())
        {
            v += Generate(item, localEnvironment);
        }
        return v + "}";
    }
Exemplo n.º 12
0
		/// <summary>
        /// Process the arguments passed to a closure, and add them to the given enviroment.
		/// </summary>
        /// <param name="argumentNameList">The list of names and kewords the closure was created with.</param>
        /// <param name="argumentList">The arguments passed to the closure.</param>
        /// <param name="localEnvironment">The closure's local variables.</param>
		/// <returns>Nothing.</returns>
        public static void ProcessArguments(Cons argumentNameList, Cons argumentList, Environment localEnvironment) 
		{
			while (argumentNameList != null) 
			{
                // Get the name for the closure's parameter.  Then check to see if it's a keyword, if it is then
                // process the keyword.  Otherwise set up that parameter in the closure's enviroment with the
                // caller specified value.

				Symbol argumentName = (Symbol)argumentNameList.Car();

                switch (argumentName.ToString())
                {
                    case "&rest":
                        argumentName = (Symbol)argumentNameList.Cadr();
                        localEnvironment.AssignLocal(argumentName, argumentList);
                        argumentNameList = null;
                        argumentList = null;
                        break;

                    case "&optional":
                        ProcessOptionalArguments((Cons)argumentNameList.Cdr(), argumentList, localEnvironment);
                        argumentNameList = null;
                        argumentList = null;
                        break;

                    case "&key":
                        ProcessKeyArguments((Cons)argumentNameList.Cdr(), argumentList, localEnvironment);
                        argumentNameList = null;
                        argumentList = null;
                        break;

                    default:
                        if (argumentList == null)
                        {
                            throw new LSharpException("Not enough parameters given.");
                        }

                        localEnvironment.AssignLocal(argumentName, argumentList.Car());
                        argumentList = (Cons)argumentList.Cdr();
                        argumentNameList = (Cons)argumentNameList.Cdr();
                        break;
                }
			}


            // Looks like the caller has supplied more parameters than the closure can use.

            if (argumentList != null)
            {
                throw new LSharpException("Too many parameters given.");
            }
		}
Exemplo n.º 13
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();
            }

        }
Exemplo n.º 14
0
        private static void ProcessOptionalArguments(Cons argumentNameList, Cons argumentList,
                                                     Environment localEnvironment)
        {
            // We need to add all the arguments to the closure's environment.

            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.

                    argumentName = (Symbol)argumentNameList.Caar();
                    argumentValue = argumentNameList.Cadar();
                }
                else
                {
                    argumentName = (Symbol)argumentNameList.Car();
                }


                // Now, if the caller has specified a value for this argument, get it now.

                if (argumentList != null)
                {
                    argumentValue = argumentList.Car();
                    argumentList = (Cons)argumentList.Cdr();
                }


                // Finally add the parameter to the closure's list and then move onto the next argument.
                // Because the default can be any expression, we need to evaluate the value every time the
                // function is called.

                localEnvironment.AssignLocal(argumentName, Runtime.Eval(argumentValue, localEnvironment));

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


            // Looks like the caller has supplied more parameters than the closure can use.

            if (argumentList != null)
            {
                throw new LSharpException("Too many parameters given.");
            }
        }
Exemplo n.º 15
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();
            }
        }
Exemplo n.º 16
0
        public static string WriteToString(Object x)
        {
            if (x == null)
            {
                return("null");
            }

            if (x == Reader.EOFVALUE)
            {
                return("EOF");
            }

            Type type = x.GetType();

            if (x is string)
            {
                return(string.Format("\"{0}\"", (string)x));
            }

            if (x is bool)
            {
                return(x.ToString().ToLower());
            }

            if (x is char)
            {
                return(string.Format("#\\{0}", x));
            }

            if (x is Symbol)
            {
                return(string.Format("{0}", x));
            }

            if (x is Cons)
            {
                bool          wasquote      = true;
                Cons          cons          = (Cons)x;
                StringBuilder stringBuilder = new StringBuilder();
                Symbol        car           = cons.Car() as Symbol;

                if (car == Symbol.QUOTE)
                {
                    stringBuilder.Append("'");
                }
                else if (car == Symbol.BACKQUOTE)
                {
                    stringBuilder.Append("`");
                }
                else if (car == Symbol.SPLICE)
                {
                    stringBuilder.Append(",@");
                }
                else if (car == Symbol.UNQUOTE)
                {
                    stringBuilder.Append(",");
                }
                else
                {
                    wasquote = false;
                    stringBuilder.Append("(");
                    stringBuilder.Append(WriteToString(cons.Car()));
                    stringBuilder.Append(" ");
                }

                Object o;
                o = cons.Cdr();
                while (o != null)
                {
                    if (o is Cons)
                    {
                        cons = (Cons)o;
                        stringBuilder.Append(WriteToString(cons.Car()));

                        o = cons.Cdr();

                        if (o != null)
                        {
                            stringBuilder.Append(" ");
                        }
                    }
                    else
                    {
                        stringBuilder.Append(". ");
                        stringBuilder.Append(WriteToString(o));
                        o = null;
                    }
                }
                string op = stringBuilder.ToString().Trim();

                if (wasquote)
                {
                    return(op);
                }
                else
                {
                    return(op + ")");
                }
            }

            return(x.ToString().Trim());
        }
Exemplo n.º 17
0
 public static Object Macro(Cons args, Environment environment)
 {
     return(new Macro((Cons)args.First(), (Cons)args.Cdr(), environment));
 }