Exemple #1
0
        public void Save(string path)
        {
            using (var stream = new FileStream(path, FileMode.Create, FileAccess.Write))
            {
                var writer = new DatBinaryWriter(stream);

                writeToStreamProgram(writer);
            }
        }
Exemple #2
0
        public byte[] getBinary()
        {
            using (var stream = new MemoryStream())
            {
                var writer = new DatBinaryWriter(stream);

                writeToStreamProgram(writer);

                return(stream.ToArray());
            }
        }
        private static void SaveDatTokens(DatBinaryWriter writer, List <DatToken> tokens)
        {
            int stackLength = tokens.Select(x => x.Size).Sum();

            writer.Write(stackLength);

            foreach (var token in tokens)
            {
                token.Save(writer);
            }
        }
Exemple #4
0
        private void SaveTokens(DatBinaryWriter writer, IEnumerable <DatToken> tokens)
        {
            var stackLength = Tokens.Select(x => x.Size).Sum();

            writer.Write(stackLength);

            foreach (var token in Tokens)
            {
                token.Save(writer);
            }
        }
Exemple #5
0
        public void Save(DatBinaryWriter writer)
        {
            writer.Write((byte)TokenType);

            if (IntParam.HasValue)
            {
                writer.Write(IntParam.Value);
            }

            if (ByteParam.HasValue)
            {
                writer.Write(ByteParam.Value);
            }
        }
Exemple #6
0
        private void SaveSymbols(DatBinaryWriter writer, IEnumerable <DatSymbol> symbols)
        {
            writer.Write(symbols.Count());

            var symbolsOrder = symbols
                               .Select((symbol, id) => new { Id = id, SymbolName = symbol.Name })
                               .OrderBy(s => s.SymbolName, StringComparer.OrdinalIgnoreCase)
                               .Select(s => s.Id)
                               .ToList();

            symbolsOrder.ForEach(id => writer.Write(id));

            foreach (var symbol in symbols)
            {
                symbol.Save(writer);
            }
        }
        private static void SaveDatSymbols(DatBinaryWriter writer, List <DatSymbol> symbols)
        {
            writer.Write(symbols.Count);

            List <int> nameOrderedSymbols = symbols
                                            .Select((symbol, id) => new { Id = id, SymbolName = symbol.Name })
                                            .OrderBy(s => s.SymbolName, StringComparer.OrdinalIgnoreCase)
                                            .Select(s => s.Id)
                                            .ToList();

            nameOrderedSymbols.ForEach(writer.Write);

            foreach (DatSymbol symbol in symbols)
            {
                symbol.Save(writer);
            }
        }
Exemple #8
0
        /// <summary>
        /// Saves DatSymbol to binary stream (in DAT format)
        /// </summary>
        public void Save(DatBinaryWriter writer)
        {
            writer.Write(Convert.ToUInt32(Name != null)); // TODO is it even possible for symbol to not have name?
            if (Name != null)
            {
                writer.Write(Name);
            }

            writer.Write(OffClsRet);

            uint bitField = Count;

            bitField |= (uint)BuiltinType << 12;
            bitField |= (uint)Flags << 16;
            bitField |= 0x400000;
            writer.Write(bitField);

            writer.Write(FileIndex);
            writer.Write(Line);
            writer.Write(LinesCount);
            writer.Write(Column);
            writer.Write(CharsCount);

            foreach (var obj in Content ?? Enumerable.Empty <object>())
            {
                switch (BuiltinType)
                {
                case SymbolType.String:
                    writer.Write((string)obj);
                    break;

                case SymbolType.Float:
                    writer.Write(Convert.ToSingle(obj));
                    break;

                default:
                    writer.Write(Convert.ToInt32(obj));
                    break;
                }
            }

            writer.Write(ParentIndex);
        }
Exemple #9
0
 private void writeToStreamProgram(DatBinaryWriter writer)
 {
     writer.Write(Version);
     SaveSymbols(writer, Symbols);
     SaveTokens(writer, Tokens);
 }
Exemple #10
0
        /// <summary>
        /// Saves DatSymbol to binary stream using DAT format
        /// </summary>
        public void Save(DatBinaryWriter writer)
        {
            // Save name
            writer.Write(Convert.ToUInt32(Name != null));
            if (Name != null)
            {
                writer.Write(Name);
            }

            // Save ReturnType / ClassSize / ClassVarOffset
            if (Type == DatSymbolType.Func && Flags.HasFlag(DatSymbolFlag.Return))
            {
                writer.Write((int)ReturnType);
            }
            else if (Flags.HasFlag(DatSymbolFlag.Classvar))
            {
                writer.Write(ClassVarOffset);
            }
            else if (Type == DatSymbolType.Class)
            {
                writer.Write(ClassSize);
            }
            else
            {
                writer.Write(0);
            }

            // Save ArrayLength & Type & Flags
            var bitField = 0u;

            if (Type == DatSymbolType.Func && !Flags.HasFlag(DatSymbolFlag.Classvar))
            {
                bitField |= ParametersCount;
            }
            else
            {
                bitField |= ArrayLength;
            }
            bitField |= ((uint)Type << 12);
            bitField |= ((uint)Flags << 16);
            bitField |= 0x400000;
            writer.Write(bitField);

            writer.Write(Location.FileNumber);
            writer.Write(Location.Line);
            writer.Write(Location.LinesCount);
            writer.Write(Location.Position);
            writer.Write(Location.PositionsCount);

            if (!Flags.Equals(0) && !Flags.HasFlag(DatSymbolFlag.Classvar))
            {
                switch (Type)
                {
                case DatSymbolType.Class:
                    writer.Write(ClassOffset);
                    break;

                case DatSymbolType.Func:
                case DatSymbolType.Instance:
                case DatSymbolType.Prototype:
                    writer.Write(FirstTokenAddress);
                    break;

                default:
                    foreach (var obj in Content ?? Enumerable.Empty <object>())
                    {
                        switch (Type)
                        {
                        case DatSymbolType.String:
                            writer.Write((string)obj);
                            break;

                        case DatSymbolType.Float:
                            writer.Write(Convert.ToSingle(obj));
                            break;

                        default:
                            writer.Write((int)obj);
                            break;
                        }
                    }
                    break;
                }
            }

            // Save parent
            writer.Write(ParentIndex);
        }