示例#1
0
        public BList <T> ParseList(Stream stream, BHeader header)
        {
            var length = header.Integer.Parse(stream, header);
            var values = new List <T>();

            length.Iterate(() => {
                values.Add(Parse(stream, header));
            });

            return(new BList <T>(length, values));
        }
示例#2
0
        public override BInteger Parse(Stream stream, BHeader header)
        {
            var value = RawParse(stream, header);

            if (header.Debug)
            {
                Console.WriteLine("-- parsed <integer> " + value.AsInteger());
            }

            return(value);
        }
示例#3
0
        public override BSizeT Parse(Stream stream, BHeader header)
        {
            var value = new BSizeT(m_integerType.RawParse(stream, header));

            if (header.Debug)
            {
                Console.WriteLine("-- parsed <size_t> " + value.AsInteger());
            }

            return(value);
        }
示例#4
0
        protected override void ParseMain(Stream stream, BHeader header, LFunctionParseState s)
        {
            s.LineBegin = header.Integer.Parse(stream, header).AsInteger();
            s.LineEnd   = header.Integer.Parse(stream, header).AsInteger();

            s.LenParameter     = stream.ReadByte();
            s.VarArg           = stream.ReadByte();
            s.MaximumStackSize = stream.ReadByte();

            ParseCode(stream, header, s);
            ParseConstants(stream, header, s);
            ParseUpvalues(stream, header, s);
            ParseDebug(stream, header, s);
        }
示例#5
0
 public LFunction(BHeader header, int[] code, LLocal[] locals,
                  LObject[] constants, LUpvalue[] upvalues, LFunction[] functions,
                  int maximumStackSize, int numUpValues, int numParams, int vararg)
 {
     Header       = header;
     Code         = code;
     Locals       = locals;
     Constants    = constants;
     UpValues     = upvalues;
     Functions    = functions;
     MaxStackSize = maximumStackSize;
     NumUpValues  = numUpValues;
     NumParams    = numParams;
     VarArg       = vararg;
 }
示例#6
0
        public override LLocal Parse(Stream stream, BHeader header)
        {
            var name = header.String.Parse(stream, header);

            var start = header.Integer.Parse(stream, header);
            var end   = header.Integer.Parse(stream, header);

            if (header.Debug)
            {
                Console.WriteLine("-- parsing local, name: {0} from {1} to {2}",
                                  name, start.AsInteger(), end.AsInteger());
            }

            return(new LLocal(name, start, end));
        }
示例#7
0
        protected virtual void ParseConstants(Stream stream, BHeader header, LFunctionParseState s)
        {
            if (header.Debug)
            {
                Console.WriteLine("-- beginning to parse constants list");
            }

            s.Constants = header.Constant.ParseList(stream, header);

            if (header.Debug)
            {
                Console.WriteLine("-- beginning to parse functions list");
            }

            s.Functions = header.Function.ParseList(stream, header);
        }
示例#8
0
        protected internal BInteger RawParse(Stream stream, BHeader header)
        {
            // HACK HACK HACK
            var bigEndian = header.BigEndian;

            BInteger value = null;

            switch (IntSize)
            {
            case 0:
                value = new BInteger(0);
                break;

            case 1:
                value = new BInteger(stream.ReadByte());
                break;

            case 2:
                value = new BInteger(stream.ReadInt16(bigEndian));
                break;

            case 4:
                value = new BInteger(stream.ReadInt32(bigEndian));
                break;

            case 8:
                value = new BInteger(stream.ReadInt64(bigEndian));
                break;

            default:
                throw new InvalidOperationException("Bad IntSize, cannot parse data");
                //default:
                //    {
                //        var bytes = new byte[IntSize];
                //
                //        var start = 0;
                //        var delta = 1;
                //
                //        for (int i = start; i >= 0 && i < IntSize; i += delta)
                //            bytes[i] = (byte)stream.ReadByte();
                //
                //        value = new BInteger(BitConverter.ToInt64(bytes, 0));
                //    } break;
            }

            return(value);
        }
示例#9
0
        public override LNumber Parse(Stream stream, BHeader header)
        {
            // HACK HACK HACK
            var bigEndian = header.BigEndian;

            LNumber value = null;

            if (Integral)
            {
                switch (Size)
                {
                case 4:
                    value = new LIntNumber(stream.ReadInt32(bigEndian));
                    break;

                case 8:
                    value = new LLongNumber(stream.ReadInt64(bigEndian));
                    break;
                }
            }
            else
            {
                switch (Size)
                {
                case 4:
                    value = new LFloatNumber(stream.ReadSingle(bigEndian));
                    break;

                case 8:
                    value = new LDoubleNumber(stream.ReadDouble(bigEndian));
                    break;
                }
            }

            if (value == null)
            {
                throw new InvalidOperationException("The input chunk has an unsupported Lua number format");
            }

            if (header.Debug)
            {
                Console.WriteLine("-- parsed <number> " + value);
            }

            return(value);
        }
示例#10
0
        public override LString Parse(Stream stream, BHeader header)
        {
            var sizeT = header.SizeT.Parse(stream, header);

            var sb = new StringBuilder();

            sizeT.Iterate(() => {
                sb.Append(stream.ReadChar());
            });

            var str = sb.ToString();

            if (header.Debug)
            {
                Console.WriteLine("-- parsed <string> \"" + str + "\"");
            }

            return(new LString(sizeT, str));
        }
示例#11
0
        public override LBoolean Parse(Stream stream, BHeader header)
        {
            var value = stream.ReadByte();

            if ((value & 0xFFFFFFFE) != 0)
            {
                throw new InvalidOperationException();
            }
            else
            {
                var boolean = (value == 0) ? LBoolean.LFALSE : LBoolean.LTRUE;

                if (header.Debug)
                {
                    Console.WriteLine("-- parsed <boolean> " + boolean);
                }

                return(boolean);
            }
        }
示例#12
0
        public override LString Parse(Stream stream, BHeader header)
        {
            var sizeT = header.SizeT.Parse(stream, header);
            var xx    = sizeT.AsInteger();
            var sb    = new StringBuilder();
            //stream.ReadChars();
            Decoder dec       = Encoding.Default.GetDecoder();
            var     charData  = new char[xx];
            var     tempChars = stream.ReadBytes(xx);

            dec.GetChars(tempChars, 0, tempChars.Length, charData, 0);
            String tempSring = new String(charData);
            var    str       = tempSring.Replace("\0", "").Split('?')[0] + "\0";

            if (header.Debug)
            {
                Console.WriteLine("-- parsed <string> \"" + str + "\"");
            }

            return(new LString(sizeT, str));
        }
示例#13
0
        protected virtual void ParseCode(Stream stream, BHeader header, LFunctionParseState s)
        {
            if (header.Debug)
            {
                Console.WriteLine("-- beginning to parse bytecode list");
            }

            // HACK HACK HACK
            var bigEndian = header.BigEndian;

            s.Length = header.Integer.Parse(stream, header).AsInteger();
            s.Code   = new int[s.Length];

            for (int i = 0; i < s.Length; i++)
            {
                s.Code[i] = stream.ReadInt32(bigEndian);

                if (header.Debug)
                {
                    Console.WriteLine("-- parsed codepoint 0x{0:X}" + s.Code[i]);
                }
            }
        }
示例#14
0
        public override LFunction Parse(Stream stream, BHeader header)
        {
            if (header.Debug)
            {
                Console.WriteLine("-- beginning to parse function");
                Console.WriteLine("-- parsing name...start...end...upvalues...params...varargs...stack");
            }

            var s = new LFunctionParseState();

            ParseMain(stream, header, s);

            return(new LFunction(header,
                                 s.Code,
                                 s.Locals.AsArray(),
                                 s.Constants.AsArray(),
                                 s.Upvalues,
                                 s.Functions.AsArray(),
                                 s.MaximumStackSize,
                                 s.LenUpvalues,
                                 s.LenParameter,
                                 s.VarArg));
        }
示例#15
0
        protected override void ParseDebug(Stream stream, BHeader header, LFunctionParseState s)
        {
            s.Name = header.String.Parse(stream, header);

            base.ParseDebug(stream, header, s);
        }
示例#16
0
 public abstract T Parse(Stream stream, BHeader header);