Exemple #1
0
        public void RejectConstVariable(ast.ExprNode node)
        {
            // Is the lhs a global?
            if (node.GetType() != typeof(ast.ExprNodeIdentifier))
            {
                return;
            }

            // Check it's not a member accessor
            var identifier = (ast.ExprNodeIdentifier)node;

            if (identifier.Lhs != null)
            {
                return;
            }

            // Find the symbol and mark it as not a const
            var s = currentScope.FindSymbol(identifier.Name);

            if (s != null)
            {
                s.ConstValue   = null;
                s.ConstAllowed = false;
            }
        }
Exemple #2
0
 // Constructor
 public ExprNodeAssignment(Bookmark bookmark, ExprNode lhs, ExprNode rhs, Token op)
     : base(bookmark)
 {
     Lhs = lhs;
     Rhs = rhs;
     Op = op;
 }
Exemple #3
0
 public override ExprNode Simplify()
 {
     Condition = Condition.Simplify();
     TrueResult = TrueResult.Simplify();
     FalseResult = FalseResult.Simplify();
     return this;
 }
Exemple #4
0
 public override ExprNode Simplify()
 {
     Lhs = Lhs.Simplify();
     Rhs = Rhs.Simplify();
     return this;
 }
Exemple #5
0
        public override ExprNode Simplify()
        {
            // Simplify the RHS
            Rhs = Rhs.Simplify();

            // Redundant positive operator?
            if (Op == Token.add)
            {
                return Rhs;
            }

            // Negatives?
            if (Op == Token.subtract)
            {
                // Double negative
                var rhsUnary = Rhs as ExprNodeUnary;
                if (rhsUnary!=null && rhsUnary.Op == Token.subtract)
                {
                    return rhsUnary.Rhs;
                }

                // Negative Add/Subtract
                var rhsLtr = Rhs as ExprNodeLtr;
                if (rhsLtr != null && rhsLtr.GetPrecedence() == OperatorPrecedence.add)
                {
                    //eg: convert -(a+b) to -a-b

                    // Swap all operators

                    // Wrap the LHS in a unary negative, then simplify it again
                    rhsLtr.Lhs = new ast.ExprNodeUnary(Bookmark, rhsLtr.Lhs, Token.subtract);
                    rhsLtr.Lhs = rhsLtr.Lhs.Simplify();

                    // Swap the add/subtract on all other terms
                    foreach (var t in rhsLtr.Terms)
                    {
                        switch (t.Op)
                        {
                            case Token.add:
                                t.Op = Token.subtract;
                                break;

                            case Token.subtract:
                                t.Op = Token.add;
                                break;

                            default:
                                System.Diagnostics.Debug.Assert(false);
                                break;
                        }
                    }

                    // Return the simplified LTR expression
                    return rhsLtr;
                }

                // Negative of multiply terms
                if (rhsLtr != null && rhsLtr.GetPrecedence() == OperatorPrecedence.multiply)
                {
                    // eg: convert -(a*b) to -a*b

                    // Wrap the first term in a unary negative, then simplify it again
                    rhsLtr.Lhs = new ast.ExprNodeUnary(Bookmark, rhsLtr.Lhs, Token.subtract);
                    return rhsLtr.Simplify();
                }
            }

            if (Op == Token.bitwiseNot || Op == Token.logicalNot)
            {
                /*
                // Double negative  eg: !!x   or ~~x
                var rhsUnary = Rhs as ExprNodeUnary;
                if (rhsUnary != null && rhsUnary.Op == Op)
                {
                    return rhsUnary.Rhs;
                }
                 */

                // Actually, don't do the above cause
                //		!! converts to bool
                //		~~ converts bool to integer

            }

            return this;
        }
Exemple #6
0
 // Render an child node, wrapping it in parentheses if necessary
 public void WrapAndRender(RenderContext dest, ExprNode other, bool bWrapEqualPrecedence)
 {
     var precOther=other.GetPrecedence();
     var precThis = this.GetPrecedence();
     if (precOther < precThis || (precOther==precThis && bWrapEqualPrecedence))
     {
         dest.Append("(");
         other.Render(dest);
         dest.Append(")");
     }
     else
     {
         other.Render(dest);
     }
 }
Exemple #7
0
 public void AddTerm(Token Op, ExprNode Rhs)
 {
     Terms.Add(new Term(Rhs, Op));
 }
Exemple #8
0
 // Constructor
 public ExprNodeCall(Bookmark bookmark, ExprNode lhs) : base(bookmark)
 {
     Lhs = lhs;
 }
Exemple #9
0
 // Constructor
 public ExprNodeAssignment(Bookmark bookmark, ExprNode lhs, ExprNode rhs, Token op) : base(bookmark)
 {
     Lhs = lhs;
     Rhs = rhs;
     Op  = op;
 }
Exemple #10
0
 public override ExprNode Simplify()
 {
     Lhs = Lhs.Simplify();
     SimplifyList(Arguments);
     return this;
 }
Exemple #11
0
 // Constructor
 public ExprNodeNew(Bookmark bookmark, ExprNode objectType) : base(bookmark)
 {
     ObjectType = objectType;
 }
Exemple #12
0
 public override ExprNode Simplify()
 {
     Lhs = Lhs.Simplify();
     Rhs = Rhs.Simplify();
     return(this);
 }
Exemple #13
0
 public override ExprNode Simplify()
 {
     ObjectType = ObjectType.Simplify();
     SimplifyList(Arguments);
     return(this);
 }
Exemple #14
0
 public Term(ExprNode rhs, Token op)
 {
     Rhs = rhs;
     Op = op;
 }
Exemple #15
0
        public override ExprNode Simplify()
        {
            // First, simplify all our terms
            Lhs = Lhs.Simplify();
            foreach (var t in Terms)
            {
                t.Rhs = t.Rhs.Simplify();
            }

            // Combine add/subtract subterms
            if (GetPrecedence() == OperatorPrecedence.add)
            {
                for (int i=0; i<Terms.Count; i++)
                {
                    var t = Terms[i];

                    // Negative term, swap our own operator
                    // eg: a - -b -> a+b
                    var unaryTerm = t.Rhs as ExprNodeUnary;
                    if (unaryTerm != null)
                    {
                        if (unaryTerm.Op == Token.subtract)
                        {
                            t.Op = InverseOp(t.Op);
                            t.Rhs = unaryTerm.Rhs;
                        }
                    }

                    /*
                    // Nested LTR add operation
                    // eg: x-(a+b) => x-a-b
                    var ltrTerm = t.Rhs as ExprNodeLtr;
                    if (ltrTerm != null && ltrTerm.GetPrecedence()==OperatorPrecedence.add)
                    {
                        // Move the first child term to self
                        t.Rhs=ltrTerm.Lhs;

                        // Negate the other child terms
                        if (t.Op == Token.subtract)
                        {
                            // Swap the operator of other terms
                            foreach (var nestedTerm in ltrTerm.Terms)
                            {
                                nestedTerm.Op = InverseOp(nestedTerm.Op);
                            }
                        }

                        // Insert the inner terms
                        Terms.InsertRange(i + 1, ltrTerm.Terms);

                        // Continue with self again to catch new subtract of negative
                        //  eg: we've now done this: x-(-a+b) => x- -a + b
                        //      need to reprocess this term to get		x+a+b
                        i--;
                    }
                     */
                }
            }

            // Combine multiply subterms
            if (GetPrecedence() == OperatorPrecedence.multiply)
            {
                // Remove negatives on any modulus ops  eg: a%-b => a%b
                foreach (var t in Terms)
                {
                    if (t.Op == Token.modulus)
                    {
                        var unaryTerm = t.Rhs as ExprNodeUnary;
                        if (unaryTerm != null && unaryTerm.Op == Token.subtract)
                        {
                            t.Rhs = unaryTerm.Rhs;
                        }
                    }
                }

                // Nested LTR multiply operation
                // eg: x*(a*b) => x*a*b
                /*
                for (int i = 0; i < Terms.Count; i++)
                {
                    var t = Terms[i];

                    // nb: we don't do x/(a*b)=>x/a/b on the (possibly flawed) assumption div is slower
                    if (t.Op != Token.multiply)
                        continue;

                    var ltrTerm = t.Rhs as ExprNodeLtr;
                    if (ltrTerm != null && ltrTerm.GetPrecedence() == OperatorPrecedence.multiply)
                    {
                        int iLastMod = ltrTerm.IndexOfLastModulusOp();

                        if (iLastMod < 0)
                        {
                            // Move the first child term to self
                            t.Rhs = ltrTerm.Lhs;

                            // Insert the inner terms
                            Terms.InsertRange(i + 1, ltrTerm.Terms);
                        }
                        else
                        {
                            // Move the trailing multiply/divs to self
                            // ie: a*(b%c*d) => a*(b%c)*d
                            int iInsertPos = i + 1;
                            while (iLastMod + 1 < ltrTerm.Terms.Count)
                            {
                                Terms.Insert(iInsertPos++, ltrTerm.Terms[iLastMod+1]);
                                ltrTerm.Terms.RemoveAt(iLastMod + 1);
                            }
                        }
                    }
                }
                 */

                // Remove -ve * -ve    eg: -a * -b => a*b

                // Step 1 - make all negated terms, positive.
                //			and count how many
                int negateCount = 0;
                foreach (var t in Terms)
                {
                    var unaryTerm = t.Rhs as ExprNodeUnary;
                    if (unaryTerm != null && unaryTerm.Op == Token.subtract)
                    {
                        // Remove the negate
                        t.Rhs = unaryTerm.Rhs;
                        negateCount++;
                    }
                }

                // Step 2 - if there was an odd number of negates in the
                //			other terms, negate the Lhs term
                if ((negateCount % 2) == 1)
                {
                    var temp = new ExprNodeUnary(Lhs.Bookmark, Lhs, Token.subtract);
                    Lhs = temp.Simplify();
                }

            }

            return this;
        }
Exemple #16
0
 public void AddTerm(Token Op, ExprNode Rhs)
 {
     Terms.Add(new Term(Rhs, Op));
 }
Exemple #17
0
 // Constructor
 public ExprNodeIndexer(Bookmark bookmark, ExprNode lhs, ExprNode index)
     : base(bookmark)
 {
     Lhs = lhs;
     Index = index;
 }
Exemple #18
0
 public override ExprNode Simplify()
 {
     ObjectType = ObjectType.Simplify();
     SimplifyList(Arguments);
     return this;
 }
Exemple #19
0
 public override ExprNode Simplify()
 {
     Lhs = Lhs.Simplify();
     Index = Index.Simplify();
     return this;
 }
Exemple #20
0
 public Term(ExprNode rhs, Token op)
 {
     Rhs = rhs;
     Op  = op;
 }
Exemple #21
0
 // Constructor
 public ExprNodeParens(Bookmark bookmark, ExprNode inner) : base(bookmark)
 {
     Inner = inner;
 }
Exemple #22
0
        public override ExprNode Simplify()
        {
            // First, simplify all our terms
            Lhs = Lhs.Simplify();
            foreach (var t in Terms)
            {
                t.Rhs = t.Rhs.Simplify();
            }

            // Combine add/subtract subterms
            if (GetPrecedence() == OperatorPrecedence.add)
            {
                for (int i = 0; i < Terms.Count; i++)
                {
                    var t = Terms[i];

                    // Negative term, swap our own operator
                    // eg: a - -b -> a+b
                    var unaryTerm = t.Rhs as ExprNodeUnary;
                    if (unaryTerm != null)
                    {
                        if (unaryTerm.Op == Token.subtract)
                        {
                            t.Op  = InverseOp(t.Op);
                            t.Rhs = unaryTerm.Rhs;
                        }
                    }

                    /*
                     * // Nested LTR add operation
                     * // eg: x-(a+b) => x-a-b
                     * var ltrTerm = t.Rhs as ExprNodeLtr;
                     * if (ltrTerm != null && ltrTerm.GetPrecedence()==OperatorPrecedence.add)
                     * {
                     *      // Move the first child term to self
                     *      t.Rhs=ltrTerm.Lhs;
                     *
                     *      // Negate the other child terms
                     *      if (t.Op == Token.subtract)
                     *      {
                     *              // Swap the operator of other terms
                     *              foreach (var nestedTerm in ltrTerm.Terms)
                     *              {
                     *                      nestedTerm.Op = InverseOp(nestedTerm.Op);
                     *              }
                     *      }
                     *
                     *      // Insert the inner terms
                     *      Terms.InsertRange(i + 1, ltrTerm.Terms);
                     *
                     *      // Continue with self again to catch new subtract of negative
                     *      //  eg: we've now done this: x-(-a+b) => x- -a + b
                     *      //      need to reprocess this term to get		x+a+b
                     *      i--;
                     * }
                     */
                }
            }

            // Combine multiply subterms
            if (GetPrecedence() == OperatorPrecedence.multiply)
            {
                // Remove negatives on any modulus ops  eg: a%-b => a%b
                foreach (var t in Terms)
                {
                    if (t.Op == Token.modulus)
                    {
                        var unaryTerm = t.Rhs as ExprNodeUnary;
                        if (unaryTerm != null && unaryTerm.Op == Token.subtract)
                        {
                            t.Rhs = unaryTerm.Rhs;
                        }
                    }
                }

                // Nested LTR multiply operation
                // eg: x*(a*b) => x*a*b

                /*
                 * for (int i = 0; i < Terms.Count; i++)
                 * {
                 *      var t = Terms[i];
                 *
                 *      // nb: we don't do x/(a*b)=>x/a/b on the (possibly flawed) assumption div is slower
                 *      if (t.Op != Token.multiply)
                 *              continue;
                 *
                 *      var ltrTerm = t.Rhs as ExprNodeLtr;
                 *      if (ltrTerm != null && ltrTerm.GetPrecedence() == OperatorPrecedence.multiply)
                 *      {
                 *              int iLastMod = ltrTerm.IndexOfLastModulusOp();
                 *
                 *              if (iLastMod < 0)
                 *              {
                 *                      // Move the first child term to self
                 *                      t.Rhs = ltrTerm.Lhs;
                 *
                 *                      // Insert the inner terms
                 *                      Terms.InsertRange(i + 1, ltrTerm.Terms);
                 *              }
                 *              else
                 *              {
                 *                      // Move the trailing multiply/divs to self
                 *                      // ie: a*(b%c*d) => a*(b%c)*d
                 *                      int iInsertPos = i + 1;
                 *                      while (iLastMod + 1 < ltrTerm.Terms.Count)
                 *                      {
                 *                              Terms.Insert(iInsertPos++, ltrTerm.Terms[iLastMod+1]);
                 *                              ltrTerm.Terms.RemoveAt(iLastMod + 1);
                 *                      }
                 *              }
                 *      }
                 * }
                 */

                // Remove -ve * -ve    eg: -a * -b => a*b

                // Step 1 - make all negated terms, positive.
                //			and count how many
                int negateCount = 0;
                foreach (var t in Terms)
                {
                    var unaryTerm = t.Rhs as ExprNodeUnary;
                    if (unaryTerm != null && unaryTerm.Op == Token.subtract)
                    {
                        // Remove the negate
                        t.Rhs = unaryTerm.Rhs;
                        negateCount++;
                    }
                }

                // Step 2 - if there was an odd number of negates in the
                //			other terms, negate the Lhs term
                if ((negateCount % 2) == 1)
                {
                    var temp = new ExprNodeUnary(Lhs.Bookmark, Lhs, Token.subtract);
                    Lhs = temp.Simplify();
                }
            }

            return(this);
        }
Exemple #23
0
 // Constructor
 public ExprNodeTernary(Bookmark bookmark, ExprNode condition) : base(bookmark)
 {
     Condition = condition;
 }
Exemple #24
0
 public override ExprNode Simplify()
 {
     Lhs = Lhs.Simplify();
     SimplifyList(Arguments);
     return(this);
 }
Exemple #25
0
        public override ExprNode Simplify()
        {
            // Simplify the RHS
            Rhs = Rhs.Simplify();

            // Redundant positive operator?
            if (Op == Token.add)
            {
                return(Rhs);
            }

            // Negatives?
            if (Op == Token.subtract)
            {
                // Double negative
                var rhsUnary = Rhs as ExprNodeUnary;
                if (rhsUnary != null && rhsUnary.Op == Token.subtract)
                {
                    return(rhsUnary.Rhs);
                }

                // Negative Add/Subtract
                var rhsLtr = Rhs as ExprNodeLtr;
                if (rhsLtr != null && rhsLtr.GetPrecedence() == OperatorPrecedence.add)
                {
                    //eg: convert -(a+b) to -a-b

                    // Swap all operators

                    // Wrap the LHS in a unary negative, then simplify it again
                    rhsLtr.Lhs = new ast.ExprNodeUnary(Bookmark, rhsLtr.Lhs, Token.subtract);
                    rhsLtr.Lhs = rhsLtr.Lhs.Simplify();

                    // Swap the add/subtract on all other terms
                    foreach (var t in rhsLtr.Terms)
                    {
                        switch (t.Op)
                        {
                        case Token.add:
                            t.Op = Token.subtract;
                            break;

                        case Token.subtract:
                            t.Op = Token.add;
                            break;

                        default:
                            System.Diagnostics.Debug.Assert(false);
                            break;
                        }
                    }

                    // Return the simplified LTR expression
                    return(rhsLtr);
                }

                // Negative of multiply terms
                if (rhsLtr != null && rhsLtr.GetPrecedence() == OperatorPrecedence.multiply)
                {
                    // eg: convert -(a*b) to -a*b

                    // Wrap the first term in a unary negative, then simplify it again
                    rhsLtr.Lhs = new ast.ExprNodeUnary(Bookmark, rhsLtr.Lhs, Token.subtract);
                    return(rhsLtr.Simplify());
                }
            }

            if (Op == Token.bitwiseNot || Op == Token.logicalNot)
            {
                /*
                 * // Double negative  eg: !!x   or ~~x
                 * var rhsUnary = Rhs as ExprNodeUnary;
                 * if (rhsUnary != null && rhsUnary.Op == Op)
                 * {
                 *      return rhsUnary.Rhs;
                 * }
                 */

                // Actually, don't do the above cause
                //		!! converts to bool
                //		~~ converts bool to integer
            }

            return(this);
        }
Exemple #26
0
 // Constrctor
 public ExprNodeUnary(Bookmark bookmark, ExprNode rhs, Token op)
     : base(bookmark)
 {
     Rhs = rhs;
     Op = op;
 }
Exemple #27
0
 // Constrctor
 public ExprNodeUnary(Bookmark bookmark, ExprNode rhs, Token op) : base(bookmark)
 {
     Rhs = rhs;
     Op  = op;
 }
Exemple #28
0
 // Constructor
 public ExprNodeTernary(Bookmark bookmark, ExprNode condition)
     : base(bookmark)
 {
     Condition = condition;
 }
Exemple #29
0
 public override ExprNode Simplify()
 {
     if (Lhs != null)
     {
         Lhs = Lhs.Simplify();
     }
     return this;
 }
Exemple #30
0
 // Constructor
 public Expression(ExprNode rootNode)
     : base(rootNode.Bookmark)
 {
     RootNode = rootNode;
 }
Exemple #31
0
 public KeyExpressionPair(object key, ExprNode value)
 {
     Key = key;
     Value = value;
 }
Exemple #32
0
 // Constructor
 public ExprNodePostfix(Bookmark bookmark, ExprNode lhs, Token op)
     : base(bookmark)
 {
     Lhs = lhs;
     Op = op;
 }
Exemple #33
0
 // Constructor
 public ExprNodeNew(Bookmark bookmark, ExprNode objectType)
     : base(bookmark)
 {
     ObjectType= objectType;
 }
Exemple #34
0
 // Constructor
 public ExprNodeIdentifier(Bookmark bookmark, string name, ExprNode lhs)
     : base(bookmark)
 {
     Name = name;
     Lhs = lhs;
 }
Exemple #35
0
 // Constructor
 public ExprNodeCall(Bookmark bookmark, ExprNode lhs)
     : base(bookmark)
 {
     Lhs = lhs;
 }
Exemple #36
0
 // Constructor
 public ExprNodeParens(Bookmark bookmark, ExprNode inner)
     : base(bookmark)
 {
     Inner = inner;
 }
Exemple #37
0
 // Constructor
 public Expression(ExprNode rootNode) : base(rootNode.Bookmark)
 {
     RootNode = rootNode;
 }