Ejemplo n.º 1
0
        public override String ToString()
        {
            String lhs;
            String rhs;

            if (isNull())
            {
                return("()");
            }

            if (value.getType() == typeof(RacketPair))
            {
                lhs = ((RacketPair)value.getObj()).printList();
            }
            else
            {
                lhs = value.getObj().ToString();
            }

            if (rest.getType() == typeof(RacketPair))
            {
                rhs = ((RacketPair)rest.getObj()).printList();
            }
            else
            {
                if (rest.getType() == typeof(voidObj))
                {
                    return(lhs);
                }
                rhs = rest.getObj().ToString();
            }

            return("(" + lhs + " " + rhs + ")");
        }
Ejemplo n.º 2
0
        public static ObjBox Apply(FunctionHolder function, RacketPair list)
        {
            int           param_num = function.param_num;
            List <Object> args      = new List <Object>();
            ObjBox        fun_call;

            if (list.length() != param_num)
            {
                throw new RuntimeException("The expected number of arguments does not match the given number");
            }
            int length = list.length();

            for (int i = 0; i < length; i++)
            {
                args.Add(list.car());
                ObjBox rest = list.cdr();

                if (rest.getType() == typeof(voidObj))
                {
                    break;
                }
                else
                {
                    list = (RacketPair)rest.getObj();
                }
            }
            fun_call = function.invoke(args);

            return(fun_call);
        }
Ejemplo n.º 3
0
 public override bool Equals(object obj)
 {
     if (obj.GetType() == typeof(ObjBox))
     {
         ObjBox other = (ObjBox)obj;
         return((this.type == other.type) && (this.getObj().Equals(other.getObj())));
     }
     return(base.Equals(obj));
 }
Ejemplo n.º 4
0
        private static void ConvertRacketNum(ObjBox o, List <Type> argTypes, List <object> objArray)
        {
            Type t = o.getType();

            if (t == typeof(RacketInt))
            {
                argTypes.Add(typeof(int));
                objArray.Add(((RacketInt)o.getObj()).value);
            }
            else if (t == typeof(RacketFloat))
            {
                argTypes.Add(typeof(double));
                objArray.Add(((RacketFloat)o.getObj()).value);
            }
            else if (t == typeof(RacketComplex))
            {
                argTypes.Add(typeof(Complex));
                objArray.Add(((RacketComplex)o.getObj()).value);
            }
        }
Ejemplo n.º 5
0
        public static ObjBox Foldl(FunctionHolder function, ObjBox init_param, List <RacketPair> lists)
        {
            ObjBox        init     = init_param;
            List <Object> args     = new List <Object>();
            bool          restNull = lists[0].isNull();
            int           listLength;

            if (function.param_num != lists.Count + 1)
            {
                throw new RuntimeException("Wrong number of arguments for given procedure");
            }

            while (!lists[0].isNull())
            {
                args.Clear();
                //    args.Add(init);
                listLength = -1;
                for (int i = 0; i < lists.Count; i++)
                {
                    if (listLength == -1)
                    {
                        listLength = lists[i].length();
                    }
                    if (lists[i].length() != listLength)
                    {
                        throw new RuntimeException("Lists must be of same length");
                    }
                    if (lists[i].isNull())
                    {
                        throw new RuntimeException("Lists must be of same length");
                    }

                    args.Add(lists[i].car());
                    ObjBox rest = lists[i].cdr();

                    /*
                     * if (rest.getType() == typeof(voidObj))
                     * {
                     *  restNull = true;
                     * }
                     * else
                     * { */
                    lists[i] = (RacketPair)rest.getObj();
                }
                args.Add(init);
                init = function.invoke(args); // should be okay if making copies
                //    returnedValues.Add(function.invoke(args));
            }

            return(init);
        }
Ejemplo n.º 6
0
        public static ObjBox Map(FunctionHolder function, List <RacketPair> lists)
        {
            List <ObjBox> returnedValues = new List <ObjBox>();
            List <Object> args           = new List <Object>();
            bool          restNull       = lists[0].isNull();
            int           listLength     = -1;

            while (!restNull)
            {
                args.Clear();
                for (int i = 0; i < lists.Count; i++)
                {
                    if (listLength == -1)
                    {
                        listLength = lists[i].length();
                    }
                    if (lists[i].length() != listLength)
                    {
                        throw new RuntimeException("Lists must be of same length");
                    }

                    args.Add(lists[i].car());
                    ObjBox rest = lists[i].cdr();

                    if (((RacketPair)rest.getObj()).isNull())
                    {
                        restNull = true;
                    }
                    else
                    {
                        lists[i] = (RacketPair)rest.getObj();
                    }
                }
                returnedValues.Add(function.invoke(args));
            }

            return(new ObjBox(new RacketPair(returnedValues), typeof(RacketPair)));
        }
Ejemplo n.º 7
0
        public static ObjBox call(ObjBox wrapper, String s, ObjBox[] args)
        {
            Object instance = null;
            Type   t;

            //if we have a static call the objBox will have a string that is the fully qualified type
            if (wrapper.getType() == (typeof(voidObj)))
            {
                String typename = (String)wrapper.getObj();
                t = typeResolver.resolve(typename);
            }
            else
            {
                t        = wrapper.getType();
                instance = wrapper.getObj();
            }

            if (t == null)
            {
                throw new RuntimeException("Could not resolve type: " + s);
            }

            // lets get all our object boxes into nice arrays
            List <Type>   argTypes = new List <Type>();
            List <Object> objArray = new List <Object>();

            unpackObjList(args, argTypes, objArray);

            MethodInfo   m = t.GetMethod(s, argTypes.ToArray());
            FieldInfo    f = t.GetField(s);
            PropertyInfo p = t.GetProperty(s);

            // Case where we are calling a method
            if (m != null)
            {
                return(callMethod(instance, m, argTypes, objArray));
            }

            // they may be trying to access a field
            if (f != null)
            {
                return(callField(instance, f, argTypes, objArray));
            }

            // lets check if we are trying to set a property
            if (p != null)
            {
                return(callProperty(instance, p, argTypes, objArray));
            }

            String exceptionMessage;

            if (argTypes.Count == 0)
            {
                exceptionMessage = "Type:" + t.ToString() + "does not contain matching method or property: " + s;
            }
            else
            {
                exceptionMessage = "Type:" + t.ToString() + "does not contain method matching signature: " + s + " with types: ";
                foreach (Type sig in argTypes)
                {
                    exceptionMessage += " " + sig.ToString() + " ";
                }
            }
            throw new RuntimeException(exceptionMessage);
        }
Ejemplo n.º 8
0
 static int getVal(ObjBox val)
 {
     RacketInt rint = (RacketInt) val.getObj();
     return rint.value;
 }
Ejemplo n.º 9
0
 static RacketPair getRP(ObjBox val)
 {
     return (RacketPair)val.getObj();
 }