Beispiel #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();
 }
Beispiel #2
0
		/// <summary>
		/// Returns true if all arguments are true, false otherwise.
		/// Performs short circuit evaluation on its arguments.
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object And(Cons args, Environment environment) 
		{
			foreach (Object item in args) 
			{
				if (Conversions.ObjectToBoolean(Runtime.Eval(item,environment)) == false)
					return false;
			}
			return true;
		}
Beispiel #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;
		}
Beispiel #4
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);

        }
Beispiel #5
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);
        }
Beispiel #6
0
		public Object Expand(Cons arguments) 
		{
			// Create a new lexical environment
			Environment localEnvironment = new Environment(environment);

			Primitives.ProcessArguments(argumentNames, arguments, localEnvironment);

			object result = null;
			foreach (object o in body) 
			{
				result = Runtime.Eval(o,localEnvironment);
			}
			return result;

		}
Beispiel #7
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);
        }
Beispiel #8
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);
        }
Beispiel #9
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));
        }
Beispiel #10
0
        /// <summary>
        /// Creates a list given a DataTable
        /// </summary>
        /// <param name="dataTable"></param>
        /// <returns></returns>
        public static Cons FromDataTable(DataTable dataTable)
        {
            object list = null;

            foreach (DataRow row in dataTable.Rows)
            {
                object subList = null;
                foreach (DataColumn column in dataTable.Columns)
                {
                    subList = new Cons(row[column], subList);
                }
                list = new Cons(((Cons)subList).Reverse(), list);
            }
            return((Cons)list);
        }
Beispiel #11
0
		/// <summary>
		/// Maps eval to a list of expressions
		/// </summary>
		/// <param name="list"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Cons EvalList(object list, Environment environment) 
		{
			if (list == null)
				return null;

			object result = null;

			foreach (object item in (Cons)list ) 
			{
				result = new Cons(Eval(item,environment) ,result);
			}

			return  ((Cons)result).Reverse();

		}
Beispiel #12
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);
        }
Beispiel #13
0
        public static Object Divide(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));
        }
Beispiel #14
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));
        }
Beispiel #15
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));
        }
Beispiel #16
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);
            }
        }
Beispiel #17
0
        /// <summary>
        /// Maps eval to a list of expressions
        /// </summary>
        /// <param name="list"></param>
        /// <param name="environment"></param>
        /// <returns></returns>
        public static Cons EvalList(object list, Environment environment)
        {
            if (list == null)
            {
                return(null);
            }

            object result = null;

            foreach (object item in (Cons)list)
            {
                result = new Cons(Eval(item, environment), result);
            }

            return(((Cons)result).Reverse());
        }
Beispiel #18
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;
		}
Beispiel #19
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);
        }
Beispiel #20
0
        public static Object To(Cons args, Environment environment)
        {
            Environment localEnvironment = new Environment(environment);

            localEnvironment.AssignLocal((Symbol)args.First(), 0);
            int endStop = int.Parse(Runtime.Eval(args.Second(), localEnvironment).ToString());

            while ((int)localEnvironment.GetValue((Symbol)args.First()) < endStop)
            {
                foreach (object item in (Cons)args.Cddr())
                {
                    Runtime.Eval(item, localEnvironment);
                }
                localEnvironment.AssignLocal((Symbol)args.First(), ((int)Runtime.Eval(args.First(), localEnvironment)) + 1);
            }
            return(null);
        }
Beispiel #21
0
        public static Object Reference(Cons args, Environment environment)
        {
            object result = null;

            foreach (object module in args)
            {
                if (module is string)
                {
                    result = AssemblyCache.LoadAssembly((string)module);
                }
                else
                {
                    throw new LSharpException(String.Format("Reference: {0} is not a string", module));
                }
            }
            return(result);
        }
Beispiel #22
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());
        }
Beispiel #23
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));
        }
Beispiel #24
0
        public Object Expand(Cons arguments)
        {
            // Create a new lexical environment
            Environment localEnvironment = new Environment(environment);

            Primitives.ProcessArguments(argumentNames, arguments, localEnvironment);

            object result = null;

            if (body != null)
            {
                foreach (object o in body)
                {
                    result = Runtime.Eval(o, localEnvironment);
                }
            }
            return(result);
        }
Beispiel #25
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);
     }
 }
Beispiel #26
0
        public static Object Using(Cons args, Environment environment)
        {
            string result = null;

            foreach (object name in args)
            {
                if (name is String)
                {
                    TypeCache.Using((string)name);
                    result = (string)name;
                }
                else
                {
                    throw new LSharpException(String.Format("Using: {0} is not a string", name));
                }
            }

            return(result);
        }
Beispiel #27
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);
        }
Beispiel #28
0
        /// <summary>
        /// Invokes the closure with arguments bound to the values specified in the
        /// argument list
        /// </summary>
        /// <param name="arguments"></param>
        /// <returns></returns>
        public Object Invoke(Cons arguments)
        {
            // Create a new lexical environment
            Environment localEnvironment = new Environment(environment);

            Primitives.ProcessArguments(argumentNames, arguments, localEnvironment);

            // Evaluate the body within this lexixal environment
            object result = null;

            if (body != null)
            {
                foreach (object o in body)
                {
                    result = Runtime.Eval(o, localEnvironment);
                }
            }
            return(result);
        }
Beispiel #29
0
		/// <summary>
		/// Invokes the closure with arguments bound to the values specified in the 
		/// argument list
		/// </summary>
		/// <param name="arguments"></param>
		/// <returns></returns>
		public Object Invoke(Cons arguments) 
		{
			// Create a new lexical environment
			Environment localEnvironment = new Environment(environment);

			Primitives.ProcessArguments(argumentNames, arguments, localEnvironment);

			// Evaluate the body within this lexical environment
			object result = null;
            if (body != null)
            {
                foreach (object o in body)
                {
                    result = Runtime.Eval(o, localEnvironment);
                }
            }
			return result;

		}
Beispiel #30
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);
        }
Beispiel #31
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);
        }
Beispiel #32
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));
            }
        }
Beispiel #33
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;
            }
        }
Beispiel #34
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();
            }
        }
Beispiel #35
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));
        }
Beispiel #36
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());
        }
Beispiel #37
0
 public static Object Pr(Cons args, Environment environment)
 {
     if (args == null)
     {
         return(null);
     }
     else
     {
         object last = null;
         foreach (object o in args)
         {
             last = o;
             if (o is System.String)
             {
                 Console.Write(o);
             }
             else
             {
                 Console.Write(Printer.WriteToString(o));
             }
         }
         return(last);
     }
 }
Beispiel #38
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));
        }
Beispiel #39
0
 /// <summary>
 /// (list object*)
 /// Creates a new cons, an ordered list with each object as a member.
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object List(Cons args, Environment environment)
 {
     return args;
 }
Beispiel #40
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;
        }
Beispiel #41
0
 /// <summary>
 /// (length expression)
 /// Returns the length of expression. If expression is null, length returns 0,
 /// otherwise the length is calculated by calling the length method on the object,
 /// ensuring that length works for strings, lists and most collection-like objects.
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Length(Cons args, Environment environment)
 {
     object o = args.Car();
     if (o == null)
         return 0;
     else
         return Runtime.Call("length", args);
 }
Beispiel #42
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();
        }
Beispiel #43
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;
        }
Beispiel #44
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();
        }
Beispiel #45
0
 /// <summary>
 /// Generates a new symbol
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Gensym(Cons args, Environment environment)
 {
     return new Symbol("#:G" + Guid.NewGuid());
 }
Beispiel #46
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);
 }
Beispiel #47
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;
 }
Beispiel #48
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");
 }
Beispiel #49
0
 /// <summary>
 /// (eql expression*) Returns true if all expressions are equal, that is
 /// their implementations of equal return true. As a special case, null
 /// is eql to null.
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Eql(Cons args, Environment environment)
 {
     return Primitives.Eql(args);
 }
Beispiel #50
0
 public Macro(Cons argumentNames, Cons body, Environment environment)
 {
     this.body          = body;
     this.argumentNames = argumentNames;
     this.environment   = environment;
 }
Beispiel #51
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);
        }
Beispiel #52
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());
 }
Beispiel #53
0
        public static Object MacroExpand(Cons args, Environment environment)
        {
            Macro macro = (Macro)args.First();
            Cons arguments = (Cons)args.Rest();
            return macro.Expand(arguments);

        }
Beispiel #54
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;
 }
Beispiel #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);
 }
Beispiel #56
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;

        }
Beispiel #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);
        }
Beispiel #58
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());
        }
Beispiel #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());
        }
Beispiel #60
0
 /// <summary>
 /// Returns an object representing the curent lexical environment
 /// </summary>
 /// <param name="args"></param>
 /// <param name="environment"></param>
 /// <returns></returns>
 public static Object Environment(Cons args, Environment environment)
 {
     return environment;
 }