Esempio n. 1
0
        protected StatementList ReadBytecode()
        {
            var s          = new MemoryStream(_bytecode);
            var reader     = new BinaryReader(s);
            var bcReader   = new BytecodeReader(_self.Package, reader);
            var statements = new StatementList((Statement)null);

            while (s.Position < s.Length)
            {
                int           startOffset = (int)s.Position;
                BytecodeToken bc;
                try
                {
                    bc = bcReader.ReadNext();
                }
                catch (EndOfStreamException)
                {
                    break;
                }
                if (bc == null || bc is EndOfScriptToken)
                {
                    break;
                }
                statements.Add(new Statement(startOffset, (int)s.Position, bc));
                if (bc is ErrorBytecodeToken)
                {
                    var errorToken = (ErrorBytecodeToken)bc;
                    int bytecode   = errorToken.UnknownBytecode;
                    if (bytecode >= 0)
                    {
                        ProblemRegistry.RegisterUnknownBytecode((byte)bytecode, this, errorToken.SubsequentBytes);
                    }
                    else
                    {
                        ProblemRegistry.RegisterBytecodeError(this, errorToken.ToString());
                    }
                    break;
                }
            }
            return(statements);
        }
Esempio n. 2
0
 private void DecompileReplicationBlock(TextBuilder result, StatementList statementList)
 {
     result.Append("replication\n{\n").PushIndent();
     for (int i = 0; i < statementList.Count; i++)
     {
         List <String> names = FindReplicatedProperties(statementList [i].StartOffset);
         if (names.Count > 0)
         {
             result.Indent().Append("if (").Append(statementList[i].Token.ToString()).Append(")").NewLine();
             result.Indent().Append("    ");
             foreach (string name in names)
             {
                 result.Append(name);
                 if (name != names.Last())
                 {
                     result.Append(", ");
                 }
             }
             result.Append(";").NewLine().NewLine();
         }
     }
     result.Append("}").NewLine().NewLine().PopIndent();
 }
Esempio n. 3
0
 protected void DecompileBytecode(StatementList statements, TextBuilder result, bool createControlStatements)
 {
     var labelTableStatement = statements.Find(s => s.Token is LabelTableToken);
     var labelTable = labelTableStatement != null ? (LabelTableToken) labelTableStatement.Token : null;
     result.HasErrors = statements.HasErrors();
     if (createControlStatements)
     {
         try
         {
             statements.CreateControlStatements();
         }
         catch (Exception)
         {
             ProblemRegistry.RegisterIncompleteControlFlow(this);
         }
         if (statements.IsIncompleteControlFlow())
         {
             ProblemRegistry.RegisterIncompleteControlFlow(this);
         }
         statements.RemoveRedundantReturns();
     }
     statements.Print(result, labelTable, !createControlStatements);
 }
Esempio n. 4
0
 protected StatementList ReadBytecode()
 {
     var s = new MemoryStream(_bytecode);
     var reader = new BinaryReader(s);
     var bcReader = new BytecodeReader(_self.Package, reader);
     var statements = new StatementList((Statement) null);
     while(s.Position < s.Length)
     {
         int startOffset = (int) s.Position;
         BytecodeToken bc;
         try
         {
             bc = bcReader.ReadNext();
         }
         catch (EndOfStreamException)
         {
             break;
         }
         if (bc == null || bc is EndOfScriptToken) break;
         statements.Add(new Statement(startOffset, (int) s.Position, bc));
         if (bc is ErrorBytecodeToken)
         {
             var errorToken = (ErrorBytecodeToken) bc;
             int bytecode = errorToken.UnknownBytecode;
             if (bytecode >= 0)
             {
                 ProblemRegistry.RegisterUnknownBytecode((byte) bytecode, this, errorToken.SubsequentBytes);
             }
             else
             {
                 ProblemRegistry.RegisterBytecodeError(this, errorToken.ToString());
             }
             break;
         }
     }
     return statements;
 }
Esempio n. 5
0
        protected void DecompileBytecode(StatementList statements, TextBuilder result, bool createControlStatements)
        {
            var labelTableStatement = statements.Find(s => s.Token is LabelTableToken);
            var labelTable          = labelTableStatement != null ? (LabelTableToken)labelTableStatement.Token : null;

            result.HasErrors = statements.HasErrors();
            if (createControlStatements)
            {
                try
                {
                    statements.CreateControlStatements();
                }
                catch (Exception)
                {
                    ProblemRegistry.RegisterIncompleteControlFlow(this);
                }
                if (statements.IsIncompleteControlFlow())
                {
                    ProblemRegistry.RegisterIncompleteControlFlow(this);
                }
                statements.RemoveRedundantReturns();
            }
            statements.Print(result, labelTable, !createControlStatements);
        }
Esempio n. 6
0
 public WhileStatement(int startOffset, BytecodeToken token, StatementList children, int endOffset)
     : base(startOffset, token, children, endOffset)
 {
 }
Esempio n. 7
0
 private static void RemoveRedundantPop(StatementList children)
 {
     for(int i=0; i<children.Count; i++)
     {
         if (children [i] is CompositeStatement)
         {
             RemoveRedundantPop(((CompositeStatement) children [i]).Children);
         }
         else if (i < children.Count-1 && children [i].Token is IteratorPopToken && children [i+1].Token is ReturnToken)
         {
             children.RemoveRange(i, 1);
         }
     }
 }
Esempio n. 8
0
 protected CompositeStatement(int startOffset, BytecodeToken token, StatementList children, int endOffset)
     : base(startOffset, token)
 {
     _children = children;
     _children.Parent = this;
     EndOffset = endOffset;
 }
Esempio n. 9
0
 internal void ReplaceRange(int index, StatementList oldStatements, Statement newStatement)
 {
     oldStatements._statements.ForEach(c => _statements.Remove(c));
     _statements.Insert(index, newStatement);
     newStatement.Parent = _parent;
 }
Esempio n. 10
0
 private void DecompileReplicationBlock(TextBuilder result, StatementList statementList)
 {
     result.Append("replication\n{\n").PushIndent();
     for(int i=0; i<statementList.Count; i++)
     {
         List<String> names = FindReplicatedProperties(statementList [i].StartOffset);
         if (names.Count > 0)
         {
             result.Indent().Append("if (").Append(statementList[i].Token.ToString()).Append(")").NewLine();
             result.Indent().Append("    ");
             foreach (string name in names)
             {
                 result.Append(name);
                 if (name != names.Last()) result.Append(", ");
             }
             result.Append(";").NewLine().NewLine();
         }
     }
     result.Append("}").NewLine().NewLine().PopIndent();
 }
Esempio n. 11
0
 internal void ReplaceRange(int index, StatementList oldStatements, Statement newStatement)
 {
     oldStatements._statements.ForEach(c => _statements.Remove(c));
     _statements.Insert(index, newStatement);
     newStatement.Parent = _parent;
 }
Esempio n. 12
0
 public SwitchStatement(int startOffset, BytecodeToken token, StatementList children, int endOffset)
     : base(startOffset, token, children, endOffset)
 {
 }