예제 #1
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);
        }
예제 #2
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);
        }
예제 #3
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));
        }
예제 #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     = 0xFF & stream.ReadByte();
            s.VarArg           = 0xFF & stream.ReadByte();
            s.MaximumStackSize = 0xFF & 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)
        {
            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());
                break;

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

            case 8:
                value = new BInteger(stream.ReadInt64());
                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)
        {
            LNumber value = null;

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

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

                case 8:
                    value = new LDoubleNumber(stream.ReadDouble());
                    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
        protected virtual void ParseCode(Stream stream, BHeader header, LFunctionParseState s)
        {
            if (header.Debug)
            {
                Console.WriteLine("-- beginning to parse bytecode list");
            }

            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();

                if (header.Debug)
                {
                    Console.WriteLine("-- parsed codepoint 0x{0:X}" + s.Code[i]);
                }
            }
        }
예제 #12
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);
            }
        }
예제 #13
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));
        }
예제 #14
0
        protected override void ParseDebug(Stream stream, BHeader header, LFunctionParseState s)
        {
            s.Name = header.String.Parse(stream, header);

            base.ParseDebug(stream, header, s);
        }
예제 #15
0
 public abstract T Parse(Stream stream, BHeader header);