예제 #1
0
 public static Object Append(Cons args, Environment environment)
 {
     if (args.Rest() == null)
     {
         return(args.First());
     }
     else
     {
         Cons result = ((Cons)args.First()).CopyList();
         ((Cons)result.Last()).Rplacd(Append((Cons)args.Rest(), environment));
         return(result);
     }
 }
예제 #2
0
        public static Object MacroExpand(Cons args, Environment environment)
        {
            Macro macro     = (Macro)args.First();
            Cons  arguments = (Cons)args.Rest();

            return(macro.Expand(arguments));
        }
예제 #3
0
        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));
        }
예제 #4
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;
		}
예제 #5
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;
            }
        }
예제 #6
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);
        }
예제 #7
0
        public static Object Cons(Cons args, Environment environment)
        {
            int l = args.Length();

            if (l == 0)
            {
                return(null);
            }
            if (l == 1)
            {
                return(args.First());
            }
            return(new Cons(args.First(), Cons((Cons)args.Rest(), environment)));
        }
예제 #8
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);
        }
예제 #9
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);
        }
예제 #10
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));
        }
예제 #11
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();
            }
        }
예제 #12
0
        public static Object MacroExpand(Cons args, Environment environment)
        {
            Macro macro = (Macro)args.First();
            Cons arguments = (Cons)args.Rest();
            return macro.Expand(arguments);

        }
예제 #13
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);
        }
예제 #14
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;
        }
예제 #15
0
    /// <summary>
    /// (while test expression*)
    /// </summary>
    public static Object While(Cons args, LSharp.Environment environment)
    {
        string v = "";//"//(while " + Printer.ConsToString(args) + ")" + NewLine;
        v += Generate(args.First(),environment);
        v += @"
while (LSharp.Conversions.ObjectToBoolean(retval))
{
";
        foreach (object item in (Cons)args.Rest())
        {
            v += Generate(item, environment);
        }

        v += Generate(args.First(),environment);

        v += "}" + NewLine;
        return v;
    }
예제 #16
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");
        }
예제 #17
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; 

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

			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(method.ToString(), 
						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(method.ToString(),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(method.ToString(),bindingFlags | BindingFlags.GetField);
			if (f != null)
				return f.GetValue(arguments.First());
			

			// FIXME: or an event ?
			EventInfo e  = type.GetEvent(method.ToString(), bindingFlags); // | BindingFlags.Event)
			if (e != null) // attempt to call the click event
			    return e.GetRaiseMethod().Invoke(arguments.First(), parameters);
			
			throw new LSharpException(string.Format("Call: No such method, property, field, or event '{0}' on '{1}'", method.ToString(),type));

			
		}
예제 #18
0
		/// <summary>
		/// (call method object argument*)
		/// Calls a .NET method on a given object with given arguments. 
		/// This is useful if the method name clashes with a variable which is already 
		/// bound in the current L Sharp lexical environment. 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		public static Object Call(Cons args, Environment environment) 
		{
			return Runtime.Call(args.Car().ToString(),
				(Cons)Runtime.EvalList(args.Rest(),environment));
		}
예제 #19
0
		/// <summary>
		/// (while test expression*) 
		/// The while special form corresponds to the while construct found 
		/// in most algebraic programming languages. First test is evauated, 
		/// if true then expression* is evaluated. The process continues until 
		/// the evaluation of test is false. 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		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;
		}
예제 #20
0
		/// <summary>
		/// (trace filename expression*) 
		/// Traces an evaluation of expression* (as if in an implicit do), 
		/// documenting all call and return steps; writes the output as an 
		/// XML file in filename. 
		/// </summary>
		/// <param name="args"></param>
		/// <param name="environment"></param>
		/// <returns></returns>
		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;
			}
			catch (Exception e) 
			{
				throw;
			}
			finally
			{
				Runtime.Profiler.Close();
				Runtime.Profiler = new DefaultProfiler();
			}
		
		}
예제 #21
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)));
        }
예제 #22
0
        public static string GenerateCons(Cons args, LSharp.Environment environment)
    {
        // ananlysi cons
        if (args == null)
        {
            return @"//retval = null; // dont null mite need retval
";
        }
        else
        {
            Symbol sym = args.Car() as Symbol;

            object e = Runtime.Eval(sym, environment);

            if (e is Function)
            {
                Function f = e as Function;
                
                string v = "{" + NewLine;
                Cons rest = args.Rest() as Cons;
                v += GenerateFuncCall(f.Method.DeclaringType.ToString(), f.Method.Name, rest, environment);
                return v + "}" + NewLine;
            }

            else if (e is SpecialForm)
            {
                SpecialForm f = e as SpecialForm;

                Cons rest = args.Rest() as Cons;

                string r = Printer.ConsToString(rest);
                string lFName = f.Method.Name.ToLower();
                if (lFName == "while")
                    return While(rest, environment) as string;
                if (lFName == "for")
                    return For(rest, environment) as string;
                if (lFName == "and")
                    return And(rest, environment) as string;
                if (lFName == "call")
                    return Call(rest, environment) as string;
                if (lFName == "cond")
                    return Cond(rest, environment) as string;
                if (lFName == "do")
                    return Do(rest, environment) as string;
                if (lFName == "foreach")
                    return ForEach(rest, environment) as string;
                if (lFName == "if")
                    return If(rest, environment) as string;
                if (lFName == "let")
                    return Let(rest, environment) as string;
                if (lFName == "or")
                    return Or(rest, environment) as string;
                if (lFName == "quote")
                    return Quote(rest, environment) as string;
                if (lFName == "setq")
                    return Setq(rest, environment) as string;
                if (lFName == "the")
                    return The(rest, environment) as string;
                if (lFName == "to")
                    return To(rest, environment) as string;
                if (lFName == "try")
                    return Try(rest, environment) as string;
                if (lFName == "when")
                    return When(rest, environment) as string;
                if (lFName == "with")
                    return With(rest, environment) as string;
                
                return Runtime.EvalString("(" + f.Method.Name + " " + r + ")", environment) as string;
            }

            else if (e is Macro)
            {
                Macro m = e as Macro;
                Cons rest = args.Rest() as Cons;
                Cons em = m.Expand(rest) as Cons;

                return Generate(em, environment);
            }
            else if (e is Closure)
            {
                extracode += Closure(new Cons(sym), environment) as string;
                
                string v = "{" + NewLine;
                Cons rest = args.Rest() as Cons;
                v += GenerateFuncCall(null, environment.GetValue(sym) as string, rest, environment);
                return v + "}" + NewLine;
                
            }
            else if (currsymbols.ContainsKey(sym))
            {
                string v = "{" + NewLine;
                Cons rest = args.Rest() as Cons;
                v += GenerateFuncCall(null, environment.GetValue(sym) as string, rest, environment);
                return v + "}" + NewLine;
            }
            else
            {
                // not good, lets not support this for now: .NET method call
                try
                {
                    // try: LSharp.Runtime.Appy(<args>);
                    Cons rest = args.Rest() as Cons;
                    string ret = GenerateList(rest, environment, "temporarytableforspecialfunctioncall");
                    ret += "\nLSharp.Runtime.Apply(retval, LSharp.Cons.FromList(temporarytableforspecialfunctioncall), environment);";
                    //return ret; //"// call not supported";// Call(args, environment).ToString();
                    return Call(args, environment) as string;
                }
                catch
                {
                    string margs = GetArgs();
                    string v = GenerateList(args.Rest() as Cons, environment, margs);
                    return string.Format("retval = LSharp.Cons.FromList({0});{1}",
                                         margs, NewLine);
                }
            }
        }
    }
예제 #23
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);
        }
예제 #24
0
        public static Object New(Cons args, Environment environment)
        {
            Type type = TypeCache.FindType(args.First().ToString());

            return(Runtime.MakeInstance(type, args.Rest()));
        }
예제 #25
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());
        }
예제 #26
0
 public static Object Call(Cons args, Environment environment)
 {
     return(Runtime.Call(args.Car().ToString(),
                         (Cons)Runtime.EvalList(args.Rest(), environment)));
 }