예제 #1
0
        public static Primitive [] LoadPrimitives(BinaryReader binaryReader, uint sectionBase, UInt32 count, UInt32 nPrims)
        {
            EncodedObject [] sectionContents = new EncodedObject [count];
            for (int i = 0; i < count; i++)
            {
                sectionContents [i] = new EncodedObject(binaryReader.ReadUInt32());
            }

            Primitive [] primitives = new Primitive [nPrims];
            uint         offset     = sectionBase;

            for (uint i = 0; i < primitives.Length; i++)
            {
                EncodedObject arity = sectionContents [offset / 4];
                offset += 4;
                uint    stringLength = sectionContents [(offset / 4) + 1].Datum;
                char [] result       = new char [stringLength];
                for (uint j = 0; j < stringLength; j++)
                {
                    EncodedObject encoded = sectionContents [(offset + 8 + j) / 4];
                    result [j] = (char)encoded.GetByte((byte)((offset + 8 + j) % 4));
                }
                string name = new string (result);

                offset        += (sectionContents [offset / 4].Datum) * 4;
                offset        += 4;
                primitives [i] = Primitive.Find(name, arity.Datum == 0x03ffffff ? -1 : (int)arity.Datum);
            }
            return(primitives);
        }
예제 #2
0
 public static FaslSection Load(BinaryReader binaryReader, int sectionBase, int sectionLimit, UInt32 count)
 {
     EncodedObject [] contents = new EncodedObject [count];
     for (int i = 0; i < count; i++)
     {
         contents [i] = new EncodedObject(binaryReader.ReadUInt32());
     }
     return(new FaslSection(sectionBase, sectionBase + (sectionLimit * 4), contents));
 }
예제 #3
0
        public SCode ReadExtendedLambda(uint location)
        {
            Symbol name;

            Symbol [] formals;
            ReadFormals(location + 4, out name, out formals);
            object        third    = ReadObject(location + 8);
            EncodedObject argcount = new EncodedObject((uint)(int)third);
            uint          optional = (argcount.Datum & 0x00FF);
            uint          required = (argcount.Datum & 0xFF00) >> 8;
            bool          rest     = ((argcount.Datum & 0x10000) == 0x10000);
            SCode         body     = SCode.EnsureSCode(ReadObject(location));

            return(ExtendedLambda.Make(name, formals, body, required, optional, rest));
        }
예제 #4
0
        public object ReadBignum(FaslFile file, uint offset)
        {
            //EncodedObject header = this [arg0Offset];
            EncodedObject h1 = this [offset + 4];

            if (h1.Datum == 1)
            {
                EncodedObject w0    = this [offset + 8];
                long          total = w0.ToLong();
                return(total);
            }
            if (h1.Datum == 2)
            {
                EncodedObject w0    = this [offset + 8];
                EncodedObject w1    = this [offset + 12];
                long          total = (w1.ToLong() << 30) + w0.ToLong();
                return(total);
            }
            throw new NotImplementedException();
        }
예제 #5
0
 public static FaslSection Load(BinaryReader binaryReader, int sectionBase, int sectionLimit, UInt32 count)
 {
     EncodedObject [] contents = new EncodedObject [count];
     for (int i = 0; i < count; i++)
         contents [i] = new EncodedObject (binaryReader.ReadUInt32 ());
     return new FaslSection (sectionBase, sectionBase + (sectionLimit * 4), contents);
 }
예제 #6
0
 FaslSection(int sectionBase, int sectionLimit, EncodedObject [] contents)
 {
     this.sectionBase = sectionBase;
     this.sectionLimit = sectionLimit;
     this.contents = contents;
 }
예제 #7
0
 public SCode ReadExtendedLambda(uint location)
 {
     Symbol name;
     Symbol [] formals;
     ReadFormals (location + 4, out name, out formals);
     object third = ReadObject (location + 8);
     EncodedObject argcount = new EncodedObject ((uint) (int) third);
     uint optional = (argcount.Datum & 0x00FF);
     uint required = (argcount.Datum & 0xFF00) >> 8;
     bool rest = ((argcount.Datum & 0x10000) == 0x10000);
     SCode body = SCode.EnsureSCode (ReadObject (location));
     return ExtendedLambda.Make (name, formals, body, required, optional, rest);
 }
예제 #8
0
        public static Primitive[] LoadPrimitives(BinaryReader binaryReader, uint sectionBase, UInt32 count, UInt32 nPrims)
        {
            EncodedObject [] sectionContents = new EncodedObject [count];
            for (int i = 0; i < count; i++)
                sectionContents [i] = new EncodedObject (binaryReader.ReadUInt32 ());

            Primitive [] primitives = new Primitive [nPrims];
            uint offset = sectionBase;
            for (uint i = 0; i < primitives.Length; i++)
            {
                EncodedObject arity = sectionContents [offset / 4];
                offset += 4;
                uint stringLength = sectionContents [(offset / 4) + 1].Datum;
                char [] result = new char [stringLength];
                for (uint j = 0; j < stringLength; j++)
                {
                    EncodedObject encoded = sectionContents [(offset + 8 + j) / 4];
                    result [j] = (char) encoded.GetByte ((byte) ((offset + 8 + j) % 4));
                }
                string name = new string (result);

                offset += (sectionContents [offset / 4].Datum) * 4;
                offset += 4;
                primitives [i] = Primitive.Find (name, arity.Datum == 0x03ffffff ? -1 : (int) arity.Datum);
            }
            return primitives;
        }
예제 #9
0
        internal object ReadObject(uint location)
        {
            object probe = null;

            if (this.sharingTable.TryGetValue(location, out probe) == true)
            {
                return(probe);
            }

            EncodedObject encoded =
                heapSection.Contains(location) ? heapSection [location]
                : constSection.Contains(location) ? constSection [location]
                : new EncodedObject(0);
            // Console.WriteLine ("{0}", encoded.TypeCode);
            object first = null;

            switch (encoded.TypeCode)
            {
            case TC.ACCESS:
                return(Access.Make(ReadObject(encoded.Datum),
                                   (Symbol)ReadObject(encoded.Datum + 4)));

            case TC.ASSIGNMENT:
                return(ReadAssignment(encoded.Datum));

            case TC.BIG_FIXNUM:
                return(ReadBignum(encoded.Datum));

            case TC.BIG_FLONUM:
                return(ReadBigFlonum(encoded.Datum));

            case TC.CHARACTER:
                return((char)(encoded.Datum));

            case TC.CHARACTER_STRING:
                return(heapSection.ReadString(encoded.Datum));

            case TC.COMBINATION:
                return(Combination.Make(ReadVector(encoded.Datum)));

            case TC.COMBINATION_1:
                return(Combination1.Make(ReadObject(encoded.Datum),
                                         ReadObject(encoded.Datum + 4)));

            case TC.COMBINATION_2:
                return(Combination2.Make(ReadObject(encoded.Datum),
                                         ReadObject(encoded.Datum + 4),
                                         ReadObject(encoded.Datum + 8)));

            case TC.COMMENT:
                return(Comment.Make(ReadObject(encoded.Datum),
                                    ReadObject(encoded.Datum + 4)));

            case TC.COMPLEX:
                return(new Complex(ReadObject(encoded.Datum),
                                   ReadObject(encoded.Datum + 4)));

            case TC.CONDITIONAL:
                return(Conditional.Make(ReadObject(encoded.Datum),
                                        ReadObject(encoded.Datum + 4),
                                        ReadObject(encoded.Datum + 8)));

            case TC.CONSTANT:
                return(Constant.Decode(encoded.Datum));

            case TC.DEFINITION:
                return(Definition.Make((Symbol)ReadObject(encoded.Datum),
                                       ReadObject(encoded.Datum + 4)));

            case TC.DELAY:
                return(Delay.Make(ReadObject(encoded.Datum)));

            case TC.DISJUNCTION:
                return(Disjunction.Make(ReadObject(encoded.Datum),
                                        ReadObject(encoded.Datum + 4)));

            case TC.EXTENDED_LAMBDA:
                return(ReadExtendedLambda(encoded.Datum));

            case TC.FIXNUM:
                return(encoded.Datum > 0x02000000
                           ? (int)-(0x04000000 - encoded.Datum)
                           : (int)encoded.Datum);

            case TC.INTERNED_SYMBOL:
                return(Symbol.Make(new String((char [])ReadObject(encoded.Datum))));

            case TC.LAMBDA:
                Symbol    name;
                Symbol [] formals;
                ReadFormals(encoded.Datum + 4, out name, out formals);
                return(Lambda.Make(name, formals, SCode.EnsureSCode(ReadObject(encoded.Datum))));

            case TC.LIST:
                object second = ReadObject(encoded.Datum + 4);
                return(new Cons(ReadObject(encoded.Datum),
                                second == sharpF ? null : second));

            case TC.NULL:
                if (encoded.Datum != 0)
                {
                    throw new NotImplementedException();
                }
                return(sharpF);

            case TC.PCOMB0:
                return(PrimitiveCombination0.Make((Primitive0)primSection[encoded.Datum]));

            case TC.PCOMB1:
                return(PrimitiveCombination1.Make((Primitive1)ReadObject(encoded.Datum),
                                                  ReadObject(encoded.Datum + 4)));

            case TC.PCOMB2:
                return(PrimitiveCombination2.Make((Primitive2)ReadObject(encoded.Datum),
                                                  ReadObject(encoded.Datum + 4),
                                                  ReadObject(encoded.Datum + 8)));

            case TC.PCOMB3:
                return(PrimitiveCombination3.Make((Primitive3)ReadObject(encoded.Datum + 4),
                                                  ReadObject(encoded.Datum + 8),
                                                  ReadObject(encoded.Datum + 12),
                                                  ReadObject(encoded.Datum + 16)));

            case TC.PRIMITIVE:
                return(primSection [encoded.Datum]);

            case TC.REFERENCE_TRAP:
                if (encoded.Datum == 0)
                {
                    return(ReferenceTrap.Unassigned);
                }
                else
                {
                    throw new NotImplementedException();
                }
            // return ReferenceTrap.Make (encoded.Datum);

            case TC.RATNUM:
                return(new Ratnum(ReadObject(encoded.Datum),
                                  ReadObject(encoded.Datum + 4)));

            case TC.RETURN_CODE:
                return((ReturnCode)(encoded.Datum));

            case TC.SEQUENCE_2:
                return(Sequence2.Make(ReadObject(encoded.Datum),
                                      ReadObject(encoded.Datum + 4)));

            case TC.SEQUENCE_3:
                // Chains of sequence_3 can be arbitrarily long.
                // Unfortunately, the CLR puts a strict limit on
                // the stack, so we have to do this funky thing.
                Cons sequenceStack = null;
                while (true)
                {
                    // read the first two elements
                    object s1 = ReadObject(encoded.Datum);
                    sequenceStack = new Cons(s1, sequenceStack);
                    object s2 = ReadObject(encoded.Datum + 4);
                    sequenceStack = new Cons(s2, sequenceStack);

                    // peek at the third

                    EncodedObject sencoded =
                        heapSection.Contains(encoded.Datum + 8) ? heapSection [encoded.Datum + 8]
                           : constSection.Contains(encoded.Datum + 8) ? constSection [encoded.Datum + 8]
                           : new EncodedObject(0);

                    if (sencoded.TypeCode == TC.SEQUENCE_3)
                    {
                        encoded = sencoded;
                    }
                    else
                    {
                        // found the end of the chain!
                        object tail = ReadObject(encoded.Datum + 8);
                        while (sequenceStack != null)
                        {
                            object ob2 = sequenceStack.Car;
                            sequenceStack = (Cons)sequenceStack.Cdr;
                            object ob1 = sequenceStack.Car;
                            sequenceStack = (Cons)sequenceStack.Cdr;
                            tail          = Sequence3.Make(ob1, ob2, tail);
                        }
                        return(tail);
                    }
                }


            case TC.THE_ENVIRONMENT:
                return(TheEnvironment.Make());

            case TC.UNINTERNED_SYMBOL:
                // KLUDGE!!  Make sure that all uninterned strings within a file
                // keep their identity when read.
                // Also, postpend a unique number so we can tell these apart.
                first = ReadObject(encoded.Datum);
                if (first is Symbol)
                {
                    return(first);
                }
                else
                {
                    Symbol result = Symbol.MakeUninterned("#:" + new String((char [])first) + "-" + (gensymCounter++).ToString(CultureInfo.InvariantCulture));
                    this.sharingTable.Add(encoded.Datum, result);
                    return(result);
                }

            case TC.VARIABLE:
                return(Variable.Make((Symbol)ReadObject(encoded.Datum)));

            case TC.VECTOR:
                return(ReadVector(encoded.Datum));

            default:
                throw new NotImplementedException();
            }
        }