Ejemplo n.º 1
0
 /// <summary>
 /// (gui-inspect OBJECT)
 /// shows a Form with data on the item OBJECT
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static object GuiInspect(Cons args, Environment environment)
 {
     // TODO : Fix
     //string sname = (string) Functions.SymbolName(new Cons(args.First()), environment);
     InspectorForm i = new InspectorForm(args.Car(), environment);
     i.ShowDialog();
     return args.First();
 }
Ejemplo n.º 2
0
        public static Object Cdar(Cons args, Environment environment)
        {
            object o = args.First();

            if (o is Cons)
            {
                return(((Cons)o).Cdar());
            }

            throw new LSharpException(string.Format("Caar: {0} is not a List", o));
        }
Ejemplo n.º 3
0
		public static bool Eq(Cons args)
		{
			object last = args.First();

			foreach (object item in (Cons)args.Rest()) 
			{
				if (!(object.ReferenceEquals(last,item)))
					return false;
				last = item;
			}
			return true;
		}
Ejemplo n.º 4
0
        public static Object Map(Cons args, Environment environment)
        {
            Cons temp = null;

            foreach (object o in (IEnumerable)args.Second())
            {
                temp = new Cons(
                    Runtime.Apply(args.First(), new Cons(o), environment),
                    temp);
            }
            return(temp.Reverse());
        }
Ejemplo n.º 5
0
        public static Object While(Cons args, Environment environment)
        {
            object test;

            while ((Conversions.ObjectToBoolean(test = Runtime.Eval(args.First(), environment))))
            {
                foreach (object item in (Cons)args.Rest())
                {
                    Runtime.Eval(item, environment);
                }
            }
            return(test);
        }
Ejemplo n.º 6
0
        public static Object Exit(Cons args, Environment environment)
        {
            if (args == null)
            {
                System.Environment.Exit(0);
            }
            else
            {
                System.Environment.Exit((int)args.First());
            }

            return(null);
        }
Ejemplo n.º 7
0
        // TODO

        public static Object Append(Cons args, Environment environment)
        {
            if (args.Rest() == null)
            {
                return args.First();
            }
            else
            {
                Cons result;

                if (args.First() == null)
                {
                    result = (Cons)Append((Cons)args.Rest(), environment);
                }
                else
                {
                    result = ((Cons)args.First()).CopyList();
                    ((Cons)result.Last()).Rplacd(Append((Cons)args.Rest(), environment));
                }
                return result;
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Converts a list to a SortedList
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static SortedList ConsToSortedList(Object o)
        {
            SortedList sortedList = new SortedList();

            Object temp = o;

            while (temp != null)
            {
                Cons element = (Cons)((Cons)temp).First();
                sortedList.Add(element.First(), element.Second());
                temp = ((Cons)temp).Rest();
            }
            return(sortedList);
        }
Ejemplo n.º 9
0
        public static Object Cdr(Cons args, Environment environment)
        {
            object o = args.First();

            if (o is Cons)
            {
                return(((Cons)o).Rest());
            }
            if (o is string)
            {
                return(((string)o).Substring(1));
            }
            throw new LSharpException(string.Format("Cdr: {0} is not a String or a List", o));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// (add object*)
        /// Returns the sum of all the specified objects.
        /// Each object must be a numerical type such as System.In32 or System.Double.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Add(Cons args, Environment environment)
        {
            Type type = args.First().GetType();
            Double result = 0;
            foreach (Object item in args)
            {
                if (item is Double)
                    type = item.GetType();

                result += Convert.ToDouble(item);
            }
            return Convert.ChangeType(result,type);

        }
Ejemplo n.º 11
0
        public static Object Nconc(Cons args, Environment environment)
        {
            // With no argument, returns null
            if (args == null)
            {
                return(null);
            }

            // With one argument, returns that argument
            if (args.Length() < 2)
            {
                return(args.First());
            }

            for (int i = 0; i < args.Length() - 1; i++)
            {
                Cons cons = (Cons)args.Nth(i);
                cons = (Cons)cons.Last();
                cons.Rplacd(args.Nth(i + 1));
            }

            return(args.First());
        }
Ejemplo n.º 12
0
        public static Object Setq(Cons args, Environment environment)
        {
            object v = null;

            while (args != null)
            {
                Symbol s = (Symbol)args.First();
                v = Runtime.Eval(args.Second(), environment);
                environment.Assign(s, v);
                args = (Cons)args.Cddr();
            }

            return(v);
        }
Ejemplo n.º 13
0
        public static bool Eq(Cons args)
        {
            object last = args.First();

            foreach (object item in (Cons)args.Rest())
            {
                if (!(object.ReferenceEquals(last, item)))
                {
                    return(false);
                }
                last = item;
            }
            return(true);
        }
Ejemplo n.º 14
0
        public static Object LessThanEqual(Cons args, Environment environment)
        {
            Double last = Convert.ToDouble(args.First());

            foreach (object item in (Cons)args.Rest())
            {
                Double current = Convert.ToDouble(item);
                if (!(last <= current))
                {
                    return(false);
                }
                last = current;
            }
            return(true);
        }
Ejemplo n.º 15
0
        public static Object Read(Cons args, Environment environment)
        {
            ReadTable readTable = (ReadTable)environment.GetValue(Symbol.FromName("*readtable*"));

            TextReader textReader = args.First() as TextReader;

            object eofValue = Reader.EOFVALUE;

            if (args.Length() > 1)
            {
                eofValue = args.Second();
            }

            return(Reader.Read(textReader, readTable, eofValue));
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Converts a list to a Hashtable
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static Hashtable ConsToHashtable(Object o)
        {
            Hashtable hashtable = new Hashtable();

            Object temp = o;

            while (temp != null)
            {
                Cons element = (Cons)((Cons)temp).First();

                hashtable[element.First()] = element.Second();
                temp = ((Cons)temp).Rest();
            }
            return(hashtable);
        }
Ejemplo n.º 17
0
        public static Object Let(Cons args, Environment environment)
        {
            Environment localEnvironment = new Environment(environment);

            localEnvironment.AssignLocal((Symbol)args.First(), Runtime.Eval(args.Second(), environment));

            object result = null;

            foreach (object item in (Cons)args.Cddr())
            {
                result = Runtime.Eval(item, localEnvironment);
            }
            //return Runtime.Eval(args.Third(),localEnvironment);
            return(result);
        }
Ejemplo n.º 18
0
        public static Object Add(Cons args, Environment environment)
        {
            object car    = args.First();
            Type   type   = car == null ? typeof(double) : car.GetType();
            Double result = 0;

            foreach (Object item in args)
            {
                if (item is Double)
                {
                    type = typeof(double);
                }
                result += Convert.ToDouble(item);
            }
            return(Convert.ChangeType(result, type));
        }
Ejemplo n.º 19
0
        public static Object Multiply(Cons args, Environment environment)
        {
            Type   type   = args.First().GetType();
            Double result = 1;

            foreach (Object item in args)
            {
                if (item is Double)
                {
                    type = item.GetType();
                }

                result *= Convert.ToDouble(item);
            }
            return(Convert.ChangeType(result, type));
        }
Ejemplo n.º 20
0
        public static Object For(Cons args, Environment environment)
        {
            Environment localEnvironment = new Environment(environment);

            Runtime.Eval(args.First(), localEnvironment);
            object test;

            while ((Conversions.ObjectToBoolean(test = Runtime.Eval(args.Second(), localEnvironment))))
            {
                foreach (object item in (Cons)args.Cdddr())
                {
                    Runtime.Eval(item, localEnvironment);
                }
                Runtime.Eval(args.Third(), localEnvironment);
            }
            return(test);
        }
Ejemplo n.º 21
0
        public static object BackQuoteExpand(Object form, Environment environment)
        {
            if (!(form is Cons))
            {
                return(form);
            }

            Cons expression = (Cons)form;

            Cons result = null;

            foreach (object item in expression)
            {
                if (item is Cons)
                {
                    Cons   list = (Cons)item;
                    Symbol sym  = list.First() as Symbol;
                    if (sym == Symbol.BACKQUOTE)
                    {
                        result = new Cons(BackQuoteExpand(list.Second(), environment), result);
                    }
                    else if (sym == Symbol.UNQUOTE)
                    {
                        result = new Cons(Runtime.Eval(BackQuoteExpand(list.Second(), environment), environment), result);
                    }
                    else if (sym == Symbol.SPLICE)
                    {
                        Cons l = (Cons)Runtime.Eval(BackQuoteExpand(list.Second(), environment), environment);
                        foreach (object o in l)
                        {
                            result = new Cons(o, result);
                        }
                    }
                    else
                    {
                        result = new Cons(BackQuoteExpand(item, environment), result);
                    }
                }
                else
                {
                    result = new Cons(item, result);
                }
            }
            return(result.Reverse());
        }
Ejemplo n.º 22
0
        public static Object Car(Cons args, Environment environment)
        {
            object o = args.First();

            if (o is Cons)
            {
                return(((Cons)o).First());
            }

            if (o is IEnumerable)
            {
                IEnumerator e = ((IEnumerable)o).GetEnumerator();
                e.MoveNext();
                return(e.Current);
            }

            throw new LSharpException(string.Format("Car: {0} is not IEnumerable", o));
        }
Ejemplo n.º 23
0
 public static Object If(Cons args, Environment environment)
 {
     if (Conversions.ObjectToBoolean(Runtime.Eval(args.First(), environment)))
     {
         // Evaluate the then part
         return(Runtime.Eval(args.Second(), environment));
     }
     else
     if (args.Length() > 2)
     {
         // Evaluate the optional else part
         return(Runtime.Eval(args.Third(), environment));
     }
     else
     {
         return(null);
     }
 }
Ejemplo n.º 24
0
        public static Object ForEach(Cons args, Environment environment)
        {
            Environment localEnvironment = new Environment(environment);

            Symbol variable = (Symbol)args.First();
            Object list     = Runtime.Eval(args.Second(), localEnvironment);

            foreach (object o in (System.Collections.IEnumerable)list)
            {
                localEnvironment.AssignLocal(variable, o);
                //Runtime.Eval(args.Third(),localEnvironment);
                foreach (object item in (Cons)args.Cddr())
                {
                    Runtime.Eval(item, localEnvironment);
                }
            }

            return(null);
        }
Ejemplo n.º 25
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);
        }
Ejemplo n.º 26
0
        public static Object Nth(Cons args, Environment environment)
        {
            int    index = (int)args.First();
            object o     = args.Second();

            if (o is IEnumerable)
            {
                IEnumerator e = ((IEnumerable)o).GetEnumerator();
                for (int i = 0; i <= index; i++)
                {
                    if (!e.MoveNext())
                    {
                        throw new IndexOutOfRangeException();
                    }
                }
                return(e.Current);
            }
            else
            {
                throw new LSharpException(string.Format("Nth: {0} is not IEnumerable", o));
            }
        }
Ejemplo n.º 27
0
        public static Object Load(Cons args, Environment environment)
        {
            object filename = args.First();

            if (filename is string)
            {
                TextReader textReader = new StreamReader((string)filename);

                string buffer = textReader.ReadToEnd();

                textReader.Close();

                // We want to evaluate the whole file, so there is an implicit do
                // which we now make explicit
                string expression = string.Format("(do {0})", buffer);

                object input  = Reader.Read(new StringReader(expression), ReadTable.DefaultReadTable());
                object output = Runtime.Eval(input, environment);

                return(output);
            }
            throw new LSharpException(String.Format("Using: {0} is not a string", filename));
        }
Ejemplo n.º 28
0
        public static Object Trace(Cons args, Environment environment)
        {
            string filename = (String)Runtime.Eval(args.First(), environment);

            try
            {
                Runtime.Profiler = new XmlTracer(filename);

                object result = null;;

                foreach (object item in (Cons)args.Rest())
                {
                    result = Runtime.Eval(item, environment);
                }

                return(result);
            }
            finally
            {
                Runtime.Profiler.Close();
                Runtime.Profiler = new DefaultProfiler();
            }
        }
Ejemplo n.º 29
0
        /// <summary>
        /// (nconc list*)
        /// Returns a list whose elements are the elements of each list in
        /// order. Destructively modifies all but the last list, such that
        /// the cdr of the last cons in each list is the next list.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Nconc(Cons args, Environment environment)
        {
            // With no argument, returns null
            if (args == null)
                return null;

            // With one argument, returns that argument
            if (args.Length() < 2)
                return args.First();

            for (int i = 0; i < args.Length() -1; i ++)
            {
                Cons cons = (Cons)args.Nth(i);
                cons = (Cons)cons.Last();
                cons.Rplacd(args.Nth(i+1));
            }

            return args.First();
        }
Ejemplo n.º 30
0
        /// <summary>
        /// (member item list)
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Member(Cons args, Environment environment)
        {
            object value = args.First();
            object list = args.Second();

            // TODO potential speed ups if list is IList or IDictionary

            foreach (object o in (IEnumerable)list)
            {
                if (Primitives.Eql(o,value))
                    return o;
            }

            return null;
        }
Ejemplo n.º 31
0
        /// <summary>
        /// (map function list) Maps function to each element in list return a new
        /// list of return values.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Map(Cons args, Environment environment)
        {
            if (args.Second() == null)
                return null;

            Cons temp = null;
            foreach (object o in (IEnumerable)args.Second())
            {
                temp = new Cons(
                    Runtime.Apply( args.First(),new Cons(o),environment),
                    temp);
            }
            return temp.Reverse();
        }
Ejemplo n.º 32
0
 public static Object BackQuote(Cons args, Environment environment)
 {
     return(Runtime.BackQuoteExpand(args.First(), environment));
 }
Ejemplo n.º 33
0
 /// <summary>
 /// Evaluates an LSharp expression contained in a string within a given
 /// environment
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object EvalString(Cons args, Environment environment)
 {
     return Runtime.EvalString (args.First().ToString(), environment);
 }
Ejemplo n.º 34
0
        /// <summary>
        /// Creates a fresh cons, the car of which is object-1 and the cdr of which is object-2.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Cons(Cons args, Environment environment)
        {
            if (args.Length() == 1)
                return args.First();
            if (args.Length() == 2)
                return new Cons(args.First(),Cons((Cons)args.Rest(), environment));

            throw new LSharpException("Too many arguments given to cons");
        }
Ejemplo n.º 35
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)));
            }
        }
Ejemplo n.º 36
0
        public static Object The(Cons args, Environment environment)
        {
            Type o = TypeCache.FindType(args.First().ToString());

            return(Conversions.The(o, Runtime.Eval(args.Second(), environment)));
        }
Ejemplo n.º 37
0
        /// <summary>
        /// Calls a .NET method.
        /// The first argument is the object to which the method is attached.
        /// Passes the rest of the arguments to the appropriate constructor
        /// </summary>
        /// <param name="method"></param>
        /// <param name="arguments"></param>
        /// <returns></returns>
        public static object Call(String method, Cons arguments)
        {
            BindingFlags bindingFlags = BindingFlags.IgnoreCase
                                        | BindingFlags.Public
                                        | BindingFlags.NonPublic;

            string methname = method;
            string typename = string.Empty;
            Type   type     = null;

            int i = methname.LastIndexOf(".");

            if (i >= 0)
            {
                methname = methname.Substring(i + 1);
                typename = method.Substring(0, i);
                type     = TypeCache.FindType(typename);
            }

            // Is it a method on a static type or an object instance ?
            if (type == null)
            {
                if (arguments.First() is Symbol)
                {
                    bindingFlags = bindingFlags | BindingFlags.Static | BindingFlags.FlattenHierarchy;
                    // Find the type object from its name
                    type = TypeCache.FindType(arguments.First().ToString());
                }
                else
                {
                    bindingFlags = bindingFlags | BindingFlags.Instance;
                    type         = arguments.First().GetType();
                }
            }
            else
            {
                bindingFlags = bindingFlags | BindingFlags.Instance;
            }

            if (type == null)
            {
                throw new LSharpException(string.Format("Call: No such type '{0}'. Did you forget a 'using'?", arguments.First()));
            }

            Type[]   types      = new Type[arguments.Length() - 1];
            object[] parameters = new object[arguments.Length() - 1];
            int      loop       = 0;

            if (arguments.Rest() != null)
            {
                foreach (object argument in (Cons)arguments.Rest())
                {
                    types[loop]      = argument.GetType();
                    parameters[loop] = argument;
                    loop++;
                }
            }

            // Start by looking for a method call
            MethodInfo m = type.GetMethod(methname,
                                          bindingFlags | BindingFlags.InvokeMethod
                                          , null, types, null);

            if (m != null)
            {
                return(m.Invoke(arguments.First(), parameters));
            }

            // Now loook for a property get
            PropertyInfo p = type.GetProperty(methname, bindingFlags | BindingFlags.GetProperty,
                                              null, null, types, null);

            if (p != null)
            {
                return(p.GetGetMethod().Invoke(arguments.First(), parameters));
            }

            // Now look for a field get
            FieldInfo f = type.GetField(methname, bindingFlags | BindingFlags.GetField);

            if (f != null)
            {
                return(f.GetValue(arguments.First()));
            }


            // or an event ?

            throw new LSharpException(string.Format("Call: No such method, property or field '{1}.{0}({2})'",
                                                    method.ToString(), type, TypeString(types, parameters)));
        }
Ejemplo n.º 38
0
 public static Object Macro(Cons args, Environment environment)
 {
     return(new Macro((Cons)args.First(), (Cons)args.Cdr(), environment));
 }
Ejemplo n.º 39
0
        //converts args[0] to type of args[1]
        public static object Coerce(Cons args, Environment e)
        {
            object o = args.First();
            object t = args.Second();
            Type type;

            if (t is String)
                type =(Type) TypeOf(new Cons(t), e);
            else
                type = (Type)t;

            return Convert.ChangeType(o, type, System.Globalization.CultureInfo.InvariantCulture);
        }
Ejemplo n.º 40
0
 public static Object Quote(Cons args, Environment environment)
 {
     return(args.First());
 }
Ejemplo n.º 41
0
 /// <summary>
 /// Returns a shallow copy of the list given as its argument
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object CopyList(Cons args, Environment environment)
 {
     if (args.First() is Cons)
     {
         return ((Cons)args.First()).CopyList();
     }
     else
     {
         throw new LSharpException(args.First().ToString() + " is not a list.");
     }
 }
Ejemplo n.º 42
0
 /// <summary>
 /// (inspect object) Returns a description of the specified object, using reflection.
 /// Useful for debugging.
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Inspect(Cons args, Environment environment)
 {
     object o = args.First();
     string s = Inspector.Inspect(o);
     Console.WriteLine(s);
     return null;
 }
Ejemplo n.º 43
0
 /// <summary>
 /// Evaluates an LSharp expression in a given environment
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Eval(Cons args, Environment environment)
 {
     if (args.Length() == 1)
         return Runtime.Eval(args.First(), environment);
     else
         throw new LSharpException("Incorrect arguments given to eval");
 }
Ejemplo n.º 44
0
        /// <summary>
        /// (<= object1 object2 object*) Less than or equal
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object LessThanEqual(Cons args, Environment environment)
        {
            Double last = Convert.ToDouble(args.First());

            foreach (object item in (Cons)args.Rest())
            {
                Double current = Convert.ToDouble(item);
                if (!(last <= current))
                    return false;
                last = current;
            }
            return true;
        }
Ejemplo n.º 45
0
 /// <summary>
 /// (exit [exit-code])
 /// Terminates the current process and returns the specified exit-code to the
 /// calling process.
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Exit(Cons args, Environment environment)
 {
     if (args == null)
         System.Environment.Exit(0);
     else
         System.Environment.Exit((int)args.First());
     
     return null;
 }
Ejemplo n.º 46
0
        /// <summary>
        /// (^ expression*)
        /// Performs a bitwise logical exclusive or operation on its arguments
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object LogXor(Cons args, Environment environment)
        {
            Type type = args.First().GetType();
            object result = args.First();
            foreach (Object item in (Cons)args.Rest())
            {

                // The integral types dont define operator overload methods
                // for performace reasons, so we have to implement this
                // operator on each integral type

                if (type == typeof(sbyte))
                    result = (sbyte)result ^ (sbyte)(item);
                else if (type == typeof(byte))
                    result = (byte)result ^ (byte)(item);
                else if (type == typeof(char))
                    result = (char)result ^ (char)(item);
                else if (type == typeof(short))
                    result = (short)result ^ (short)(item);
                else if (type == typeof(ushort))
                    result = (ushort)result ^ (ushort)(item);
                else if (type == typeof(int))
                    result = (int)result ^ (int)(item);
                else if (type == typeof(uint))
                    result = (uint)result ^ (uint)(item);
                else if (type == typeof(long))
                    result = (long)result ^ (long)(item);
                else if (type == typeof(ulong))
                    result = (ulong)result ^ (ulong)(item);
                else
                    return Runtime.Call("op_ExclusiveOr",args);

            }

            return Convert.ChangeType(result,type);
        }
Ejemplo n.º 47
0
 /// <summary>
 /// (handle-event target eventName handler)
 /// Sets up a new event handler for events named eventName on target. The
 /// handler is an LSharp closure with two arguments, the sender and the
 /// event arguments (defun fn (sender args) (prl "Event Handled")).
 /// Experimental.
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object HandleEvent(Cons args, Environment environment)
 {
     return EventAdapter.AddEventHandler(args.First(), (string)args.Second(), (Closure)args.Third());
 }
Ejemplo n.º 48
0
        /// <summary>
        /// (read TextReader [eof])
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Read(Cons args, Environment environment)
        {
            ReadTable readTable = (ReadTable)environment.GetValue(Symbol.FromName("*readtable*"));

            TextReader textReader = (TextReader)args.First();
            object eofValue = null;
            if (args.Length() > 1)
                eofValue = args.Second();
            
            return Reader.Read(textReader, readTable, eofValue);
        }
Ejemplo n.º 49
0
        /// <summary>
        /// (is type expression)
        /// Used to check whether the run-time type of an object is
        /// compatible with a given type.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Is(Cons args, Environment environment)
        {
            object obj = args.Second();

            TypeCache typeCache = TypeCache.Instance();
            string typeName = args.First().ToString();
            Type type = typeCache.FindType(typeName);

            object result =  (((Type)type).IsInstanceOfType (obj));

            return result;

        }
Ejemplo n.º 50
0
 /// <summary>
 /// (symbol-name symbol) Returns the name of a symbol as a string
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object SymbolName(Cons args, Environment environment)
 {
     return ((Symbol)args.First()).Name;
 }
Ejemplo n.º 51
0
        /// <summary>
        /// (load filename) Loads and evaluates all statements in the given
        /// filename which must be a text file.
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Load(Cons args, Environment environment)
        {
            object filename = args.First();

            if (filename is string)
            {
                TextReader textReader;
                // 9/29/11: Enabled "short" filenames (adds .ls if needed)
                if (System.IO.File.Exists((string)filename))
                    textReader = new StreamReader((string)filename);
                else if (File.Exists(filename + ".ls")) // short file specified?
                    textReader = new StreamReader((string)filename + ".ls");
                else
                    throw new LSharpException("Cannot find file " + filename);
                string buffer = textReader.ReadToEnd();

                textReader.Close();

                // We want to evaluate the whole file, so there is an implicit do
                // which we now make explicit
                string expression = string.Format("(do {0}\n)",buffer);

                object input = Reader.Read(new StringReader(expression),ReadTable.DefaultReadTable());
                object output = Runtime.Eval(input, environment);

                return output;
            }
            throw new LSharpException(String.Format("Load: {0} is not a string", filename));
        }
Ejemplo n.º 52
0
 /// <summary>
 /// (throw exception) Throws a System.Exception
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Throw(Cons args, Environment environment)
 {
     throw ((Exception)args.First());
 }
Ejemplo n.º 53
0
        public static Object MacroExpand(Cons args, Environment environment)
        {
            Macro macro = (Macro)args.First();
            Cons arguments = (Cons)args.Rest();
            return macro.Expand(arguments);

        }
Ejemplo n.º 54
0
        /// <summary>
        /// (typeof symbol) Returns the type object of the same name as the given symbol.
        /// </summary>
        /// <example>(typeof Console)</example>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object TypeOf(Cons args, Environment environment)
        {
            Type type = TypeCache.Instance().FindType(args.First().ToString());

            return (type);
        }
Ejemplo n.º 55
0
 /// <summary>
 /// (apply function list)
 /// Applies function to a list of arguments. function may be a built-in lsharp function,
 /// a closure defined by fn a macro defined by macro or the name of a method in the
 /// .NET framework.
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Apply(Cons args, Environment environment)
 {
     return Runtime.Apply(args.First(), args.Second(),environment);
 }
Ejemplo n.º 56
0
        /// <summary>
        /// (car list)
        /// Returns the first element of a list or other IEnumerable data structure
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Car(Cons args, Environment environment)
        {
            object o = args.First();
            if (o is Cons)
                return ((Cons)o).First();

            if (o is IEnumerable)
            {
                IEnumerator e = ((IEnumerable) o).GetEnumerator();
                e.MoveNext();
                return e.Current;
            }

            throw new LSharpException(string.Format("Car: {0} is not IEnumerable",o));
        }
Ejemplo n.º 57
0
        public static Object Mod(Cons args, Environment environment)
        {
            Type type = args.First().GetType();
            Double result = Convert.ToDouble(args.First());
            foreach (object item in (Cons)args.Rest())
            {
                if (item is Double)
                    type = item.GetType();

                result %= Convert.ToDouble(item);
            }
            return Convert.ChangeType(result, type);
        }
Ejemplo n.º 58
0
        /// <summary>
        /// (cddar list)
        /// A shorthand for (cdr (cdr (car x)))
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object Cddar(Cons args, Environment environment)
        {
            object o = args.First();
            if (o is Cons)
                return ((Cons)o).Cddar();

            throw new LSharpException(string.Format("Cddar: {0} is not a List",o));
        }
Ejemplo n.º 59
0
        /// <summary>
        /// (new class) Creates a new object, an instance of type class
        /// </summary>
        /// <param name="args"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Object New(Cons args, Environment environment)
        {
            Type type = TypeCache.Instance().FindType(args.First().ToString());

            return Runtime.MakeInstance(type,args.Rest());
        }
Ejemplo n.º 60
0
 /// <summary>
 /// Returns the Cdr (second part) of a cons
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Cdr(Cons args, Environment environment)
 {
     object o = args.First();
     if (o is Cons)
         return ((Cons)o).Rest();
     if (o is string)
         return ((string)o).Substring(1);
     throw new LSharpException(string.Format("Cdr: {0} is not a String or a List",o));
 }