Exemple #1
0
        public override void OutASAssignmentExp(ASAssignmentExp node)
        {
            AAssignmentExp replacer = null;
            PAssignop      assignop = node.GetAssignop();
            Token          token    = null;
            PBinop         binop    = null;

            if (assignop is AAddAssignop)
            {
                token = ((AAddAssignop)assignop).GetToken();
                binop = new APlusBinop(new TPlus("+", token.Line, token.Pos));
            }
            else if (assignop is ASubAssignop)
            {
                token = ((ASubAssignop)assignop).GetToken();
                binop = new AMinusBinop(new TMinus("-", token.Line, token.Pos));
            }
            else if (assignop is AMulAssignop)
            {
                token = ((AMulAssignop)assignop).GetToken();
                binop = new ATimesBinop(new TStar("*", token.Line, token.Pos));
            }
            else if (assignop is ADivAssignop)
            {
                token = ((ADivAssignop)assignop).GetToken();
                binop = new ADivideBinop(new TDiv("/", token.Line, token.Pos));
            }
            else if (assignop is AModAssignop)
            {
                token = ((AModAssignop)assignop).GetToken();
                binop = new AModuloBinop(new TMod("%", token.Line, token.Pos));
            }
            else// if (assignop is AAssignAssignop)
            {
                token = ((AAssignAssignop)assignop).GetToken();
            }
            PExp rightSide;

            if (binop != null)
            {
                rightSide = new ABinopExp(new ALvalueExp((PLvalue)node.GetLvalue().Clone()),
                                          binop,
                                          (PExp)node.GetExp().Clone());
            }
            else
            {
                rightSide = (PExp)node.GetExp().Clone();
            }
            replacer = new AAssignmentExp(new TAssign("=", token.Line, token.Pos), (PLvalue)node.GetLvalue().Clone(), rightSide);
            node.ReplaceBy(replacer);
            replacer.Apply(this);
        }
 internal override void RemoveChild(Node child)
 {
     if (_left_ == child)
     {
         _left_ = null;
         return;
     }
     if (_binop_ == child)
     {
         _binop_ = null;
         return;
     }
     if (_right_ == child)
     {
         _right_ = null;
         return;
     }
 }
        public void SetBinop(PBinop node)
        {
            if (_binop_ != null)
            {
                _binop_.Parent(null);
            }

            if (node != null)
            {
                if (node.Parent() != null)
                {
                    node.Parent().RemoveChild(node);
                }

                node.Parent(this);
            }

            _binop_ = node;
        }
 public ABinopExp(
         PExp _left_,
         PBinop _binop_,
         PExp _right_
 )
 {
     SetLeft(_left_);
     SetBinop(_binop_);
     SetRight(_right_);
 }
 internal override void RemoveChild(Node child)
 {
     if (_visibility_modifier_ == child)
     {
         _visibility_modifier_ = null;
         return;
     }
     if (_static_ == child)
     {
         _static_ = null;
         return;
     }
     if (_return_type_ == child)
     {
         _return_type_ = null;
         return;
     }
     if (_token_ == child)
     {
         _token_ = null;
         return;
     }
     if (_operator_ == child)
     {
         _operator_ = null;
         return;
     }
     if (_formals_.Contains(child))
     {
         _formals_.Remove(child);
         return;
     }
     if (_block_ == child)
     {
         _block_ = null;
         return;
     }
 }
        public void SetOperator(PBinop node)
        {
            if (_operator_ != null)
            {
                _operator_.Parent(null);
            }

            if (node != null)
            {
                if (node.Parent() != null)
                {
                    node.Parent().RemoveChild(node);
                }

                node.Parent(this);
            }

            _operator_ = node;
        }
 public AOperatorDecl(
         PVisibilityModifier _visibility_modifier_,
         TStatic _static_,
         PType _return_type_,
         TOperator _token_,
         PBinop _operator_,
         IList _formals_,
         PBlock _block_
 )
 {
     SetVisibilityModifier(_visibility_modifier_);
     SetStatic(_static_);
     SetReturnType(_return_type_);
     SetToken(_token_);
     SetOperator(_operator_);
     this._formals_ = new TypedList(new Formals_Cast(this));
     this._formals_.Clear();
     this._formals_.AddAll(_formals_);
     SetBlock(_block_);
 }
        private int FoldInt(PExp exp, ref bool valid)
        {
            if (!valid)
            {
                return(-1);
            }
            if (exp is AIntConstExp)
            {
                return(int.Parse(((AIntConstExp)exp).GetIntegerLiteral().Text));
            }
            if (exp is ABinopExp)
            {
                ABinopExp aExp  = (ABinopExp)exp;
                int       left  = FoldInt(aExp.GetLeft(), ref valid);
                int       right = FoldInt(aExp.GetLeft(), ref valid);
                if (!valid)
                {
                    return(-1);
                }
                PBinop binop = aExp.GetBinop();
                if (binop is APlusBinop)
                {
                    return(left + right);
                }
                if (binop is AMinusBinop)
                {
                    return(left - right);
                }
                if (binop is ATimesBinop)
                {
                    return(left * right);
                }
                if ((binop is AModuloBinop || binop is ADivideBinop) && right == 0)
                {
                    Token token = binop is AModuloBinop
                                      ? (Token)((AModuloBinop)binop).GetToken()
                                      : ((ADivideBinop)binop).GetToken();
                    errors.Add(new ErrorCollection.Error(token, "Zero division during constant folding."));
                    throw new ParserException(null, null);
                }
                if (binop is AModuloBinop)
                {
                    return(left % right);
                }
                if (binop is ADivideBinop)
                {
                    return(left / right);
                }
                if (binop is AAndBinop)
                {
                    return(left & right);
                }
                if (binop is AOrBinop)
                {
                    return(left | right);
                }
                if (binop is AXorBinop)
                {
                    return(left ^ right);
                }
                if (binop is ALBitShiftBinop)
                {
                    return(left << right);
                }
                if (binop is ARBitShiftBinop)
                {
                    return(left >> right);
                }
            }
            if (exp is ALvalueExp)
            {
                return(FoldInt(((ALvalueExp)exp).GetLvalue(), ref valid));
            }


            valid = false;
            return(-1);
        }