private List <PStm> MakeStatements(PExp exp, int line, int pos)
        {
            List <PStm> list = new List <PStm>();

            if (exp is ASimpleInvokeExp)
            {
                list.Add(new AExpStm(new TSemicolon(";", line, pos), exp));
                return(list);
            }
            if (exp is AAssignmentExp)
            {
                list.Add(new AExpStm(new TSemicolon(";", line, pos), exp));
                return(list);
            }
            if (exp is ANonstaticInvokeExp)
            {
                list.Add(new AExpStm(new TSemicolon(";", line, pos), exp));
                return(list);
            }
            if (exp is ABinopExp)
            {
                ABinopExp aExp = (ABinopExp)exp;
                list.AddRange(MakeStatements(aExp.GetLeft(), line, pos));
                list.AddRange(MakeStatements(aExp.GetRight(), line, pos));
                return(list);
            }
            if (exp is AUnopExp)
            {
                AUnopExp aExp = (AUnopExp)exp;
                list.AddRange(MakeStatements(aExp.GetExp(), line, pos));
                return(list);
            }
            if (exp is AParenExp)
            {
                AParenExp aExp = (AParenExp)exp;
                list.AddRange(MakeStatements(aExp.GetExp(), line, pos));
                return(list);
            }
            if (exp is ALvalueExp)
            {
                ALvalueExp aExp   = (ALvalueExp)exp;
                PLvalue    lvalue = aExp.GetLvalue();
                if (lvalue is AStructLvalue)
                {
                    AStructLvalue aLvalue = (AStructLvalue)lvalue;
                    list.AddRange(MakeStatements(aLvalue.GetReceiver(), line, pos));
                    return(list);
                }
                if (lvalue is AArrayLvalue)
                {
                    AArrayLvalue aLvalue = (AArrayLvalue)lvalue;
                    list.AddRange(MakeStatements(aLvalue.GetBase(), line, pos));
                    list.AddRange(MakeStatements(aLvalue.GetIndex(), line, pos));
                    return(list);
                }
            }
            return(list);
        }
예제 #2
0
 bool IsConstant(PExp exp)
 {
     if (exp is ABinopExp)
     {
         ABinopExp aExp = (ABinopExp)exp;
         return(IsConstant(aExp.GetLeft()) && IsConstant(aExp.GetRight()));
     }
     if (exp is AUnopExp)
     {
         AUnopExp aExp = (AUnopExp)exp;
         return(IsConstant(aExp.GetExp()));
     }
     if (exp is AIncDecExp)
     {
         AIncDecExp aExp = (AIncDecExp)exp;
         return(IsConstant(aExp.GetLvalue()));
     }
     if (exp is AIntConstExp || exp is AHexConstExp ||
         exp is AOctalConstExp || exp is AFixedConstExp ||
         exp is AStringConstExp || exp is ACharConstExp ||
         exp is ABooleanConstExp || exp is ANullExp ||
         exp is AAssignmentExp || exp is ADelegateExp)
     {
         return(true);
     }
     if (exp is ASimpleInvokeExp || exp is ANonstaticInvokeExp ||
         exp is ASyncInvokeExp || exp is ANewExp ||
         exp is ADelegateInvokeExp)
     {
         return(false);
     }
     if (exp is ALvalueExp)
     {
         ALvalueExp aExp = (ALvalueExp)exp;
         return(IsConstant(aExp.GetLvalue()));
     }
     if (exp is AParenExp)
     {
         AParenExp aExp = (AParenExp)exp;
         return(IsConstant(aExp.GetExp()));
     }
     if (exp is ACastExp)
     {
         ACastExp aExp = (ACastExp)exp;
         return(IsConstant(aExp.GetExp()));
     }
     if (exp is AIfExp)
     {
         AIfExp aExp = (AIfExp)exp;
         return(IsConstant(aExp.GetCond()) && IsConstant(aExp.GetThen()) && IsConstant(aExp.GetElse()));
     }
     if (exp == null)
     {
         return(false);
     }
     throw new Exception("Unexpected exp. Got " + exp);
 }
예제 #3
0
        public override void OutABinopExp(ABinopExp node)
        {
            if (node.Parent() is ABinopExp || node.Parent() is AUnopExp)
            {
                AParenExp paren = new AParenExp();
                node.ReplaceBy(paren);
                paren.SetExp(node);

                finalTrans.data.ExpTypes[paren] = finalTrans.data.ExpTypes[node];
            }
            base.OutABinopExp(node);
        }
예제 #4
0
        public override void CaseADelegateExp(ADelegateExp node)
        {
            /* Replace delegate<Type>(method)
             * With
             *
             * "method"
             *
             * or
             *
             * "method:reciever"
             */
            AMethodDecl    method = finalTrans.data.DelegateCreationMethod[node];
            string         d      = GetName(method);
            PExp           replacer;
            APointerLvalue reciever = finalTrans.data.DelegateRecieveres[node];

            if (reciever != null)
            {
                d += ":";
                AStringConstExp leftSide = new AStringConstExp(new TStringLiteral("\"" + d + "\""));
                replacer = new ABinopExp(leftSide, new APlusBinop(new TPlus("+")), reciever.GetBase());
                finalTrans.data.ExpTypes[leftSide]     =
                    finalTrans.data.ExpTypes[replacer] = new ANamedType(new TIdentifier("string"), null);

                if (Util.IsIntPointer(reciever, data.LvalueTypes[reciever], data))
                {
                    ASimpleInvokeExp intToStringInvoke = new ASimpleInvokeExp(new TIdentifier("IntToString"),
                                                                              new ArrayList()
                    {
                        ((ABinopExp)replacer).GetRight()
                    });
                    ((ABinopExp)replacer).SetRight(intToStringInvoke);

                    finalTrans.data.SimpleMethodLinks[intToStringInvoke] =
                        data.Libraries.Methods.First(m => m.GetName().Text == intToStringInvoke.GetName().Text);
                    data.ExpTypes[intToStringInvoke] = new ANamedType(new TIdentifier("string"), null);
                }
            }
            else
            {
                replacer = new AStringConstExp(new TStringLiteral("\"" + d + "\""));
                finalTrans.data.ExpTypes[replacer] = new ANamedType(new TIdentifier("string"), null);
            }
            MoveMethodDeclsOut mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data);

            node.Apply(mover);
            node.ReplaceBy(replacer);
            foreach (PStm stm in mover.NewStatements)
            {
                stm.Apply(this);
            }
        }
예제 #5
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);
        }
예제 #6
0
        private PExp Combine(PExp left, List <string> strings)
        {
            AStringConstExp right = new AStringConstExp(new TStringLiteral("\"" + strings[0] + "\""));

            strings.RemoveAt(0);
            ABinopExp binop = new ABinopExp(left, new APlusBinop(new TPlus("+")), right);

            data.StringsDontJoinRight.Add(right);
            data.ExpTypes[right]     =
                data.ExpTypes[binop] = new ANamedType(new TIdentifier("string"), null);
            if (strings.Count > 0)
            {
                return(Combine(binop, strings));
            }
            return(binop);
        }
예제 #7
0
        public override void OutABinopExp(ABinopExp node)
        {
            if (data.ExpTypes[node.GetLeft()] is ANamedType && data.StructTypeLinks.ContainsKey((ANamedType)data.ExpTypes[node.GetLeft()]))
            {
                AStructDecl str            = data.StructTypeLinks[(ANamedType)data.ExpTypes[node.GetLeft()]];
                PExp        replacementExp = new ABooleanConstExp(new ATrueBool());
                data.ExpTypes[replacementExp] = new ANamedType(new TIdentifier("bool"), null);
                foreach (AALocalDecl local in str.GetLocals().OfType <AALocalDecl>())
                {
                    AStructLvalue leftSide = new AStructLvalue(Util.MakeClone(node.GetLeft(), data),
                                                               new ADotDotType(new TDot(".")),
                                                               new TIdentifier(local.GetName().Text));
                    ALvalueExp    leftSideExp = new ALvalueExp(leftSide);
                    AStructLvalue rightSide   = new AStructLvalue(Util.MakeClone(node.GetRight(), data),
                                                                  new ADotDotType(new TDot(".")),
                                                                  new TIdentifier(local.GetName().Text));
                    ALvalueExp rightSideExp = new ALvalueExp(rightSide);


                    data.StructFieldLinks[leftSide]         =
                        data.StructFieldLinks[rightSide]    = local;
                    data.LvalueTypes[leftSide]              =
                        data.ExpTypes[leftSideExp]          =
                            data.LvalueTypes[rightSide]     =
                                data.ExpTypes[rightSideExp] = local.GetType();


                    ABinopExp binop = new ABinopExp(leftSideExp, (PBinop)node.GetBinop().Clone(), rightSideExp);
                    data.ExpTypes[binop] = data.ExpTypes[node];

                    if (replacementExp is ABooleanConstExp)
                    {
                        replacementExp = binop;
                    }
                    else
                    {
                        replacementExp = new ABinopExp(replacementExp, new ALazyAndBinop(new TAndAnd("&&")), binop);
                        data.ExpTypes[replacementExp] = new ANamedType(new TIdentifier("bool"), null);
                    }
                }
                node.ReplaceBy(replacementExp);
                replacementExp.Apply(this);
            }

            base.OutABinopExp(node);
        }
예제 #8
0
        public override void DefaultIn(Node node)
        {
            if (node is PExp)
            {
                PExp exp = (PExp)node;
                if (finalTrans.data.ExpTypes[exp] is ANamedType)
                {
                    ANamedType type = (ANamedType)finalTrans.data.ExpTypes[exp];
                    if (finalTrans.data.StructTypeLinks.ContainsKey(type))
                    {
                        AStructDecl strDecl = finalTrans.data.StructTypeLinks[type];
                        if (strDecl.GetLocals().Cast <PLocalDecl>().Select(decl => decl is AALocalDecl).Count() == 0)
                        {
                            if (node.Parent() is AAssignmentExp)
                            {
                                node = node.Parent().Parent();
                            }
                            MoveMethodDeclsOut mover = new MoveMethodDeclsOut("removedStructVar", finalTrans.data);
                            node.Apply(mover);
                            foreach (PStm pStm in mover.NewStatements)
                            {
                                pStm.Apply(this);
                            }
                            node.Parent().RemoveChild(node);

                            if (node.Parent() is ABinopExp)
                            {
                                ABinopExp        parent = (ABinopExp)node.Parent();
                                ABooleanConstExp replacer;
                                if (parent.GetBinop() is ANeBinop || parent.GetBinop() is AGtBinop || parent.GetBinop() is ALtBinop)
                                {
                                    replacer = new ABooleanConstExp(new AFalseBool());
                                }
                                else
                                {
                                    replacer = new ABooleanConstExp(new ATrueBool());
                                }
                                finalTrans.data.ExpTypes[replacer] = new ANamedType(new TIdentifier("bool"), null);
                                parent.ReplaceBy(replacer);
                            }
                        }
                    }
                }
            }
        }
        public override void CaseAArrayLvalue(AArrayLvalue node)
        {
            bool containedLiteral = containsLiteral;

            containsLiteral = false;
            node.GetIndex().Apply(this);
            bool hasLiteral = containsLiteral;

            containsLiteral = containedLiteral;
            PType type = data.ExpTypes[node.GetIndex()];

            if (!hasLiteral && type is ANamedType && ((ANamedType)type).IsPrimitive("byte"))
            {
                AIntConstExp intConst = new AIntConstExp(new TIntegerLiteral("0"));
                ABinopExp    binop    = new ABinopExp(node.GetIndex(), new APlusBinop(new TPlus("+")), intConst);
                data.ExpTypes[intConst]  =
                    data.ExpTypes[binop] = new ANamedType(new TIdentifier("int"), null);
                node.SetIndex(binop);
            }
            node.GetBase().Apply(this);
        }
예제 #10
0
        public override void CaseADelegateInvokeExp(ADelegateInvokeExp node)
        {
            //Build a list of the possible methods
            AASourceFile       currentFile    = Util.GetAncestor <AASourceFile>(node);
            List <AMethodDecl> methods        = new List <AMethodDecl>();
            ANamedType         type           = (ANamedType)finalTrans.data.ExpTypes[node.GetReceiver()];
            AMethodDecl        delegateMethod = finalTrans.data.DelegateTypeLinks[type];

            foreach (KeyValuePair <ADelegateExp, AMethodDecl> delegateCreationPair in finalTrans.data.DelegateCreationMethod)
            {
                if (TypeChecking.Assignable(delegateCreationPair.Key.GetType(), type))
                {
                    if (!methods.Contains(delegateCreationPair.Value))
                    {
                        methods.Add(delegateCreationPair.Value);
                    }
                }
            }
            MoveMethodDeclsOut mover;

            if (methods.Count == 0)
            {
                //Can only remove it if the return value is unused
                if (!(node.Parent() is AExpStm))
                {
                    finalTrans.errors.Add(new ErrorCollection.Error(node.GetToken(),
                                                                    currentFile,
                                                                    "No possible methods found for delegate invoke."));
                    throw new ParserException(node.GetToken(), "Delegates.OutADelegateInvokeExp");
                }

                mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data);
                foreach (Node arg in node.GetArgs())
                {
                    arg.Apply(mover);
                }
                node.Parent().Parent().RemoveChild(node.Parent());
                foreach (PStm stm in mover.NewStatements)
                {
                    stm.Apply(this);
                }
                return;
            }
            if (methods.Count == 1)
            {
                ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList());
                while (node.GetArgs().Count > 0)
                {
                    invoke.GetArgs().Add(node.GetArgs()[0]);
                }

                //If we have a struct method, add the pointer from the delegate
                if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(methods[0])))
                {
                    AStructDecl      targetStr        = finalTrans.data.StructMethods.First(str => str.Value.Contains(methods[0])).Key;
                    AMethodDecl      getPointerDecl   = GetPointerMethod(targetStr.GetDimention() != null);
                    ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList()
                    {
                        node.GetReceiver()
                    });
                    invoke.GetArgs().Add(getPointerInvoke);

                    finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl;
                    finalTrans.data.ExpTypes[getPointerInvoke]          = getPointerDecl.GetReturnType();
                }

                finalTrans.data.SimpleMethodLinks[invoke] = methods[0];
                finalTrans.data.ExpTypes[invoke]          = methods[0].GetReturnType();
                node.ReplaceBy(invoke);
                return;
            }
            //Multiple methods. Make

            /*
             * <Methods moved out from reciever>
             * string delegate = GetMethodPart(<reciever>);
             * if (delegate == "...")
             * {
             *    Foo(...);
             * }
             * else if (delegate == "...")
             * {
             *    Bar(..., GetPointerPart(<reciever>);
             * }
             * else if(...)
             * ...
             * else
             * {
             *     UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("[<file>:<line>]: No methods matched delegate."));
             *     int i = 1/0;
             *     return;
             * }
             *
             */
            AABlock block = new AABlock(new ArrayList(), new TRBrace("}"));

            mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data);
            node.GetReceiver().Apply(mover);
            AMethodDecl      methodPartMethod = GetMethodMethod();
            ASimpleInvokeExp methodPartInvoke = new ASimpleInvokeExp(new TIdentifier("GetMethodPart"),
                                                                     new ArrayList()
            {
                Util.MakeClone(node.GetReceiver(),
                               finalTrans.data)
            });

            finalTrans.data.SimpleMethodLinks[methodPartInvoke] = methodPartMethod;
            finalTrans.data.ExpTypes[methodPartInvoke]          = methodPartMethod.GetReturnType();
            AALocalDecl methodPartDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("methodPart"), methodPartInvoke);

            block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), methodPartDecl));
            //If the invoke's return value is used, get the lvalue
            PLvalue leftSide;

            if (node.Parent() is AALocalDecl)
            {
                leftSide = new ALocalLvalue(new TIdentifier("renameMe"));
                finalTrans.data.LocalLinks[(ALocalLvalue)leftSide] = (AALocalDecl)node.Parent();
                finalTrans.data.LvalueTypes[leftSide] = new ANamedType(new TIdentifier("string"), null);
                PStm    pStm   = Util.GetAncestor <PStm>(node);
                AABlock pBlock = (AABlock)pStm.Parent();
                pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm) + 1, new ABlockStm(new TLBrace("{"), block));
                node.Parent().RemoveChild(node);
            }
            else if (node.Parent() is AAssignmentExp)
            {
                AAssignmentExp assignExp = (AAssignmentExp)node.Parent();
                leftSide = assignExp.GetLvalue();
                leftSide.Apply(mover);

                PStm pStm = Util.GetAncestor <PStm>(node);
                pStm.ReplaceBy(new ABlockStm(new TLBrace("{"), block));
            }
            else if (node.Parent() is AExpStm)
            {
                //No assignments needed
                leftSide = null;
                node.Parent().ReplaceBy(new ABlockStm(new TLBrace("{"), block));
            }
            else
            {
                //Create a new local
                AALocalDecl leftSideDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                           Util.MakeClone(delegateMethod.GetReturnType(),
                                                                          finalTrans.data),
                                                           new TIdentifier("delegateVar"), null);
                ALocalLvalue leftSideLink    = new ALocalLvalue(new TIdentifier("delegateVar"));
                ALvalueExp   leftSideLinkExp = new ALvalueExp(leftSideLink);


                PStm    pStm   = Util.GetAncestor <PStm>(node);
                AABlock pBlock = (AABlock)pStm.Parent();
                pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ABlockStm(new TLBrace("{"), block));

                node.ReplaceBy(leftSideLinkExp);

                finalTrans.data.LocalLinks[leftSideLink]      = leftSideDecl;
                finalTrans.data.LvalueTypes[leftSideLink]     =
                    finalTrans.data.ExpTypes[leftSideLinkExp] = leftSideDecl.GetType();

                leftSide = leftSideLink;
                block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), leftSideDecl));
            }

            ABlockStm elseBranch;

            //Make final else branch

            /* {
             *     UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("<file>[<line>, <pos>]: No methods matched delegate."));
             *     IntToString(1/0);
             *     return;
             * }
             */
            {
                AABlock          innerBlock         = new AABlock(new ArrayList(), new TRBrace("}"));
                ASimpleInvokeExp playerGroupInvoke  = new ASimpleInvokeExp(new TIdentifier("PlayerGroupAll"), new ArrayList());
                AFieldLvalue     messageAreaLink    = new AFieldLvalue(new TIdentifier("c_messageAreaDebug"));
                ALvalueExp       messageAreaLinkExp = new ALvalueExp(messageAreaLink);
                AStringConstExp  stringConst        =
                    new AStringConstExp(
                        new TStringLiteral("\"" + currentFile.GetName().Text.Replace('\\', '/') + "[" +
                                           node.GetToken().Line + ", " + node.GetToken().Pos +
                                           "]: Got a null delegate.\""));
                ASimpleInvokeExp stringToTextInvoke = new ASimpleInvokeExp(new TIdentifier("StringToText"),
                                                                           new ArrayList()
                {
                    stringConst
                });
                ASimpleInvokeExp displayMessageInvoke = new ASimpleInvokeExp(new TIdentifier("UIDisplayMessage"),
                                                                             new ArrayList()
                {
                    playerGroupInvoke,
                    messageAreaLinkExp,
                    stringToTextInvoke
                });

                AIntConstExp     intConst1         = new AIntConstExp(new TIntegerLiteral("1"));
                AIntConstExp     intConst2         = new AIntConstExp(new TIntegerLiteral("0"));
                ABinopExp        binop             = new ABinopExp(intConst1, new ADivideBinop(new TDiv("/")), intConst2);
                ASimpleInvokeExp intToStringInvoke = new ASimpleInvokeExp(new TIdentifier("IntToString"),
                                                                          new ArrayList()
                {
                    binop
                });

                innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), displayMessageInvoke));
                innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), intToStringInvoke));
                //innerBlock.GetStatements().Add(new AVoidReturnStm(new TReturn("return")));

                elseBranch = new ABlockStm(new TLBrace("{"), innerBlock);


                finalTrans.data.SimpleMethodLinks[playerGroupInvoke] =
                    finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == playerGroupInvoke.GetName().Text);
                finalTrans.data.SimpleMethodLinks[stringToTextInvoke] =
                    finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringToTextInvoke.GetName().Text);
                finalTrans.data.SimpleMethodLinks[displayMessageInvoke] =
                    finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == displayMessageInvoke.GetName().Text);
                finalTrans.data.SimpleMethodLinks[intToStringInvoke] =
                    finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == intToStringInvoke.GetName().Text);
                finalTrans.data.FieldLinks[messageAreaLink] =
                    finalTrans.data.Libraries.Fields.First(m => m.GetName().Text == messageAreaLink.GetName().Text);

                finalTrans.data.ExpTypes[playerGroupInvoke] =
                    finalTrans.data.SimpleMethodLinks[playerGroupInvoke].GetReturnType();
                finalTrans.data.LvalueTypes[messageAreaLink]     =
                    finalTrans.data.ExpTypes[messageAreaLinkExp] =
                        finalTrans.data.FieldLinks[messageAreaLink].GetType();
                finalTrans.data.ExpTypes[stringToTextInvoke] =
                    finalTrans.data.SimpleMethodLinks[stringToTextInvoke].GetReturnType();
                finalTrans.data.ExpTypes[stringConst]           =
                    finalTrans.data.ExpTypes[intToStringInvoke] = new ANamedType(new TIdentifier("string"), null);
                finalTrans.data.ExpTypes[displayMessageInvoke]  = new AVoidType();

                finalTrans.data.ExpTypes[intConst1]     =
                    finalTrans.data.ExpTypes[intConst2] =
                        finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("int"), null);
            }

            foreach (AMethodDecl method in methods)
            {
                /*  * if (delegate == "...")
                 * {
                 *    Foo(...);
                 * }
                 * else if (delegate == "...")
                 * {
                 *    Bar(..., GetPointerPart(<reciever>);
                 * }
                 * else if(...)
                 * ...
                 */
                AABlock          innerBlock = new AABlock(new ArrayList(), new TRBrace("}"));
                ASimpleInvokeExp invoke     = new ASimpleInvokeExp(new TIdentifier(method.GetName().Text), new ArrayList());
                for (int i = 0; i < node.GetArgs().Count; i++)
                {
                    PExp arg = (PExp)node.GetArgs()[i];
                    invoke.GetArgs().Add(Util.MakeClone(arg, finalTrans.data));
                }
                //If we have a struct method, add the pointer from the delegate
                if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(method)))
                {
                    AStructDecl      targetStr        = finalTrans.data.StructMethods.First(str => str.Value.Contains(method)).Key;
                    AMethodDecl      getPointerDecl   = GetPointerMethod(targetStr.GetDimention() != null);
                    ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList()
                    {
                        Util.MakeClone(node.GetReceiver(), data)
                    });
                    invoke.GetArgs().Add(getPointerInvoke);

                    finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl;
                    finalTrans.data.ExpTypes[getPointerInvoke]          = getPointerDecl.GetReturnType();
                }

                finalTrans.data.SimpleMethodLinks[invoke] = method;
                finalTrans.data.ExpTypes[invoke]          = method.GetReturnType();

                if (leftSide == null)
                {
                    innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), invoke));
                }
                else
                {
                    AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(leftSide, finalTrans.data), invoke);
                    finalTrans.data.ExpTypes[assignment] = finalTrans.data.ExpTypes[invoke];
                    innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment));
                }
                ALocalLvalue    methodPartLink    = new ALocalLvalue(new TIdentifier("methodPart"));
                ALvalueExp      methodPartLinkExp = new ALvalueExp(methodPartLink);
                AStringConstExp stringConst       = new AStringConstExp(new TStringLiteral("\"" + GetName(method) + "\""));
                finalTrans.data.LocalLinks[methodPartLink]      = methodPartDecl;
                finalTrans.data.LvalueTypes[methodPartLink]     =
                    finalTrans.data.ExpTypes[methodPartLinkExp] =
                        finalTrans.data.ExpTypes[stringConst]   = new ANamedType(new TIdentifier("string"), null);


                ABinopExp binop = new ABinopExp(methodPartLinkExp, new AEqBinop(new TEq("==")), stringConst);
                finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("bool"), null);

                AIfThenElseStm ifThenElse = new AIfThenElseStm(new TLParen("("), binop, new ABlockStm(new TLBrace("{"), innerBlock), elseBranch);

                elseBranch = new ABlockStm(new TLBrace("{"), new AABlock(new ArrayList()
                {
                    ifThenElse
                }, new TRBrace("}")));
            }

            block.GetStatements().Add(elseBranch);
        }
예제 #11
0
        private AMethodDecl GetMethodMethod()
        {
            if (GetMethodPartMethod != null)
            {
                return(GetMethodPartMethod);
            }

            /*
             *  string GetMethodPart(string delegate)
             *  {
             *          int i = StringFind(delegate, ":", false);
             *          if (i == -1)
             *          {
             *                  return delegate;
             *          }
             *          return StringSub(delegate, 1, i - 1);
             *  }
             */
            AASourceFile sourceFile = Util.GetAncestor <AASourceFile>(finalTrans.mainEntry);

            AALocalDecl delegateFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("delegate"), null);
            ALocalLvalue delegateRef1    = new ALocalLvalue(new TIdentifier("delegate"));
            ALocalLvalue delegateRef2    = new ALocalLvalue(new TIdentifier("delegate"));
            ALocalLvalue delegateRef3    = new ALocalLvalue(new TIdentifier("delegate"));
            ALvalueExp   delegateRef1Exp = new ALvalueExp(delegateRef1);
            ALvalueExp   delegateRef2Exp = new ALvalueExp(delegateRef2);
            ALvalueExp   delegateRef3Exp = new ALvalueExp(delegateRef3);

            AStringConstExp  stringConst  = new AStringConstExp(new TStringLiteral("\":\""));
            ABooleanConstExp booleanConst = new ABooleanConstExp(new AFalseBool());
            AIntConstExp     intConst1    = new AIntConstExp(new TIntegerLiteral("-1"));
            AIntConstExp     intConst2    = new AIntConstExp(new TIntegerLiteral("1"));
            AIntConstExp     intConst3    = new AIntConstExp(new TIntegerLiteral("1"));

            ASimpleInvokeExp stringFindInvoke = new ASimpleInvokeExp(new TIdentifier("StringFind"),
                                                                     new ArrayList()
            {
                delegateRef1Exp, stringConst, booleanConst
            });
            AALocalDecl iDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("int"), null),
                                                new TIdentifier("i"), stringFindInvoke);
            ALocalLvalue iRef1    = new ALocalLvalue(new TIdentifier("i"));
            ALocalLvalue iRef2    = new ALocalLvalue(new TIdentifier("i"));
            ALvalueExp   iRef1Exp = new ALvalueExp(iRef1);
            ALvalueExp   iRef2Exp = new ALvalueExp(iRef2);

            ABinopExp  binop1 = new ABinopExp(iRef1Exp, new AEqBinop(new TEq("==")), intConst1);
            AIfThenStm ifThen = new AIfThenStm(new TLParen("("), binop1,
                                               new ABlockStm(new TLBrace("{"),
                                                             new AABlock(
                                                                 new ArrayList()
            {
                new AValueReturnStm(new TReturn("return"),
                                    delegateRef2Exp)
            },
                                                                 new TRBrace("}"))));

            ABinopExp        binop2          = new ABinopExp(iRef2Exp, new AMinusBinop(new TMinus("-")), intConst3);
            ASimpleInvokeExp stringSubInvoke = new ASimpleInvokeExp(new TIdentifier("StringSub"),
                                                                    new ArrayList()
            {
                delegateRef3Exp, intConst2, binop2
            });

            GetMethodPartMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                  new ANamedType(new TIdentifier("string"), null),
                                                  new TIdentifier("GetMethodPart", finalTrans.data.LineCounts[sourceFile] + 1, 1), new ArrayList()
            {
                delegateFormal
            },
                                                  new AABlock(
                                                      new ArrayList()
            {
                new ALocalDeclStm(new TSemicolon(";"), iDecl),
                ifThen,
                new AValueReturnStm(new TReturn("return"), stringSubInvoke)
            },
                                                      new TRBrace("}")));
            sourceFile.GetDecl().Add(GetMethodPartMethod);

            finalTrans.data.LocalLinks[delegateRef1]         =
                finalTrans.data.LocalLinks[delegateRef2]     =
                    finalTrans.data.LocalLinks[delegateRef3] = delegateFormal;
            finalTrans.data.LocalLinks[iRef1]                                     =
                finalTrans.data.LocalLinks[iRef2]                                 = iDecl;
            finalTrans.data.LvalueTypes[delegateRef1]                             =
                finalTrans.data.LvalueTypes[delegateRef2]                         =
                    finalTrans.data.LvalueTypes[delegateRef3]                     =
                        finalTrans.data.ExpTypes[delegateRef1Exp]                 =
                            finalTrans.data.ExpTypes[delegateRef2Exp]             =
                                finalTrans.data.ExpTypes[delegateRef3Exp]         =
                                    finalTrans.data.ExpTypes[stringConst]         =
                                        finalTrans.data.ExpTypes[stringSubInvoke] = new ANamedType(new TIdentifier("string"), null);
            finalTrans.data.LvalueTypes[iRef1]                                    =
                finalTrans.data.LvalueTypes[iRef2]                                =
                    finalTrans.data.ExpTypes[iRef1Exp]                            =
                        finalTrans.data.ExpTypes[iRef2Exp]                        =
                            finalTrans.data.ExpTypes[stringFindInvoke]            =
                                finalTrans.data.ExpTypes[intConst1]               =
                                    finalTrans.data.ExpTypes[intConst2]           =
                                        finalTrans.data.ExpTypes[intConst3]       =
                                            finalTrans.data.ExpTypes[binop2]      = new ANamedType(new TIdentifier("int"), null);
            finalTrans.data.ExpTypes[booleanConst]                                =
                finalTrans.data.ExpTypes[binop1]                                  = new ANamedType(new TIdentifier("bool"), null);


            finalTrans.data.SimpleMethodLinks[stringFindInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringFindInvoke.GetName().Text);
            finalTrans.data.SimpleMethodLinks[stringSubInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringSubInvoke.GetName().Text);

            return(GetMethodPartMethod);
        }
예제 #12
0
        private AMethodDecl GetStringPointerMethod()
        {
            if (GetStringPointerPartMethod != null)
            {
                return(GetStringPointerPartMethod);
            }

            /*
             *  string GetPointerPart(string delegate)
             *  {
             *      return StringSub(delegate, StringFind(delegate, ":", false) + 1, StringLength(delegate));
             *  }
             */

            AASourceFile sourceFile     = Util.GetAncestor <AASourceFile>(finalTrans.mainEntry);
            AALocalDecl  delegateFormal = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                          new ANamedType(new TIdentifier("string"), null),
                                                          new TIdentifier("delegate"), null);
            ALocalLvalue delegateRef1    = new ALocalLvalue(new TIdentifier("delegate"));
            ALocalLvalue delegateRef2    = new ALocalLvalue(new TIdentifier("delegate"));
            ALocalLvalue delegateRef3    = new ALocalLvalue(new TIdentifier("delegate"));
            ALvalueExp   delegateRef1Exp = new ALvalueExp(delegateRef1);
            ALvalueExp   delegateRef2Exp = new ALvalueExp(delegateRef2);
            ALvalueExp   delegateRef3Exp = new ALvalueExp(delegateRef3);

            AStringConstExp  stringConst      = new AStringConstExp(new TStringLiteral("\":\""));
            ABooleanConstExp booleanConst     = new ABooleanConstExp(new AFalseBool());
            ASimpleInvokeExp stringFindInvoke = new ASimpleInvokeExp(new TIdentifier("StringFind"), new ArrayList()
            {
                delegateRef2Exp, stringConst, booleanConst
            });
            AIntConstExp intConst = new AIntConstExp(new TIntegerLiteral("1"));
            ABinopExp    binop    = new ABinopExp(stringFindInvoke, new APlusBinop(new TPlus("+")), intConst);

            ASimpleInvokeExp stringLengthInvoke = new ASimpleInvokeExp(new TIdentifier("StringLength"), new ArrayList()
            {
                delegateRef3Exp
            });

            ASimpleInvokeExp stringSubInvoke = new ASimpleInvokeExp(new TIdentifier("StringSub"), new ArrayList()
            {
                delegateRef1Exp, binop, stringLengthInvoke
            });

            GetStringPointerPartMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                         new ANamedType(new TIdentifier("string"), null),
                                                         new TIdentifier("GetPointerPart", finalTrans.data.LineCounts[sourceFile] + 1, 1), new ArrayList()
            {
                delegateFormal
            },
                                                         new AABlock(
                                                             new ArrayList()
            {
                new AValueReturnStm(new TReturn("return"), stringSubInvoke)
            },
                                                             new TRBrace("}")));
            sourceFile.GetDecl().Add(GetStringPointerPartMethod);
            data.Methods.Add(new SharedData.DeclItem <AMethodDecl>(sourceFile, GetStringPointerPartMethod));

            finalTrans.data.LocalLinks[delegateRef1]                              =
                finalTrans.data.LocalLinks[delegateRef2]                          =
                    finalTrans.data.LocalLinks[delegateRef3]                      = delegateFormal;
            finalTrans.data.LvalueTypes[delegateRef1]                             =
                finalTrans.data.LvalueTypes[delegateRef2]                         =
                    finalTrans.data.LvalueTypes[delegateRef3]                     =
                        finalTrans.data.ExpTypes[delegateRef1Exp]                 =
                            finalTrans.data.ExpTypes[delegateRef2Exp]             =
                                finalTrans.data.ExpTypes[delegateRef3Exp]         =
                                    finalTrans.data.ExpTypes[stringConst]         =
                                        finalTrans.data.ExpTypes[stringSubInvoke] = new ANamedType(new TIdentifier("string"), null);
            finalTrans.data.ExpTypes[booleanConst]                   = new ANamedType(new TIdentifier("bool"), null);
            finalTrans.data.ExpTypes[intConst]                       =
                finalTrans.data.ExpTypes[binop]                      =
                    finalTrans.data.ExpTypes[stringFindInvoke]       =
                        finalTrans.data.ExpTypes[stringLengthInvoke] = new ANamedType(new TIdentifier("int"), null);


            finalTrans.data.SimpleMethodLinks[stringFindInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringFindInvoke.GetName().Text);
            finalTrans.data.SimpleMethodLinks[stringLengthInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringLengthInvoke.GetName().Text);
            finalTrans.data.SimpleMethodLinks[stringSubInvoke] =
                finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringSubInvoke.GetName().Text);

            return(GetStringPointerPartMethod);
        }
예제 #13
0
        public override void CaseABinopExp(ABinopExp node)
        {
            bool pushed = false;

            if (!(node.Parent() is ABinopExp))
            {
                PushStack();
                pushed = true;
            }
            try
            {
                bool isIntegerType = data.ExpTypes[node] is ANamedType &&
                                     (((ANamedType)data.ExpTypes[node]).IsPrimitive("int") ||
                                      ((ANamedType)data.ExpTypes[node]).IsPrimitive("byte"));
                if (isIntegerType)
                {
                    if (node.GetBinop() is APlusBinop || node.GetBinop() is AMinusBinop)
                    {
                        node.GetLeft().Apply(this);
                        if (!Util.HasAncestor <AAProgram>(node))
                        {
                            return;
                        }
                        if (node.GetBinop() is AMinusBinop)
                        {
                            isNegativeRightSide = !isNegativeRightSide;
                        }
                        node.GetRight().Apply(this);
                        if (node.GetBinop() is AMinusBinop)
                        {
                            isNegativeRightSide = !isNegativeRightSide;
                        }
                        if (!Util.HasAncestor <AAProgram>(node))
                        {
                            return;
                        }
                        for (int i = 0; i < intConsts.Count; i++)
                        {
                            for (int j = i + 1; j < intConsts.Count; j++)
                            {
                                Pair <AIntConstExp, bool> const1 = intConsts[i];
                                Pair <AIntConstExp, bool> const2 = intConsts[j];

                                ABinopExp pBinop1 = (ABinopExp)const1.Car.Parent();
                                ABinopExp pBinop2 = (ABinopExp)const2.Car.Parent();

                                int a = int.Parse(const1.Car.GetIntegerLiteral().Text);
                                int b = int.Parse(const2.Car.GetIntegerLiteral().Text);
                                int c;

                                if (const1.Cdr != const2.Cdr)
                                {
                                    c = a - b;
                                }
                                else
                                {
                                    c = a + b;
                                }

                                //Eliminate stuff like <exp> + -1
                                if (c < 0 && pBinop1.GetRight() == const1.Car)
                                {
                                    c = -c;
                                    if (pBinop1.GetBinop() is AMinusBinop)
                                    {
                                        pBinop1.SetBinop(new APlusBinop(new TPlus("+")));
                                    }
                                    else
                                    {
                                        pBinop1.SetBinop(new AMinusBinop(new TMinus("-")));
                                    }
                                    const1.Cdr = !const1.Cdr;
                                }
                                const1.Car.GetIntegerLiteral().Text = c.ToString();

                                //Remove binop2
                                if (pBinop2.GetLeft() == const2.Car)
                                {
                                    if (pBinop2.GetBinop() is AMinusBinop)
                                    {
                                        if (pBinop2.GetRight() is AIntConstExp)
                                        {
                                            AIntConstExp const3             = (AIntConstExp)pBinop2.GetRight();
                                            const3.GetIntegerLiteral().Text =
                                                (-int.Parse(const3.GetIntegerLiteral().Text)).ToString();
                                            pBinop2.ReplaceBy(const3);
                                            intConsts.Add(new Pair <AIntConstExp, bool>(const3, isNegativeRightSide));
                                        }
                                        else
                                        {
                                            AUnopExp unop = new AUnopExp(new ANegateUnop(new TMinus("-")),
                                                                         pBinop2.GetRight());
                                            data.ExpTypes[unop] = new ANamedType(new TIdentifier("int"), null);
                                            pBinop2.ReplaceBy(unop);
                                        }
                                    }
                                    else
                                    {
                                        pBinop2.ReplaceBy(pBinop2.GetRight());
                                    }
                                }
                                else
                                {
                                    pBinop2.ReplaceBy(pBinop2.GetLeft());
                                }

                                intConsts.RemoveAt(j);
                                j--;
                            }
                        }
                        return;
                    }
                }


                {
                    PushStack();
                    node.GetLeft().Apply(this);
                    PopStack();
                    PushStack();
                    node.GetRight().Apply(this);
                    PopStack();
                }

                if (isIntegerType && (node.GetBinop() is ATimesBinop || node.GetBinop() is ADivideBinop) &&
                    node.GetLeft() is AIntConstExp && node.GetRight() is AIntConstExp)
                {
                    AIntConstExp const1 = (AIntConstExp)node.GetLeft();
                    AIntConstExp const2 = (AIntConstExp)node.GetRight();

                    int a = int.Parse(const1.GetIntegerLiteral().Text);
                    int b = int.Parse(const2.GetIntegerLiteral().Text);
                    int c;

                    if (node.GetBinop() is ATimesBinop || b != 0)
                    {
                        if (node.GetBinop() is ATimesBinop)
                        {
                            c = a * b;
                        }
                        else
                        {
                            c = a / b;
                        }
                        const1.GetIntegerLiteral().Text = c.ToString();
                        node.ReplaceBy(const1);
                        const1.Apply(this);
                        return;
                    }
                }

                if (node.GetBinop() is AEqBinop || node.GetBinop() is ANeBinop)
                {
                    if (node.GetLeft() is ABooleanConstExp && node.GetRight() is ABooleanConstExp)
                    {
                        bool b1 = ((ABooleanConstExp)node.GetLeft()).GetBool() is ATrueBool;
                        bool b2 = ((ABooleanConstExp)node.GetRight()).GetBool() is ATrueBool;
                        bool b3 = false;
                        if (node.GetBinop() is AEqBinop)
                        {
                            b3 = b1 == b2;
                        }
                        else if (node.GetBinop() is ANeBinop)
                        {
                            b3 = b1 != b2;
                        }
                        ((ABooleanConstExp)node.GetLeft()).SetBool(b3 ? (PBool) new ATrueBool() : new AFalseBool());
                        node.ReplaceBy(node.GetLeft());
                        return;
                    }
                    else if (node.GetLeft() is AIntConstExp && node.GetRight() is AIntConstExp)
                    {
                        AIntConstExp const1 = (AIntConstExp)node.GetLeft();
                        AIntConstExp const2 = (AIntConstExp)node.GetRight();

                        int  a = int.Parse(const1.GetIntegerLiteral().Text);
                        int  b = int.Parse(const2.GetIntegerLiteral().Text);
                        bool c = false;
                        if (node.GetBinop() is AEqBinop)
                        {
                            c = a == b;
                        }
                        else if (node.GetBinop() is ANeBinop)
                        {
                            c = a != b;
                        }
                        ABooleanConstExp booleanExp = new ABooleanConstExp(c ? (PBool) new ATrueBool() : new AFalseBool());
                        data.ExpTypes[booleanExp] = new ANamedType(new TIdentifier("bool"), null);
                        node.ReplaceBy(booleanExp);
                        return;
                    }
                    else if (node.GetLeft() is ANullExp && node.GetRight() is ANullExp)
                    {
                        ABooleanConstExp booleanExp = new ABooleanConstExp(node.GetBinop() is AEqBinop ? (PBool) new ATrueBool() : new AFalseBool());
                        data.ExpTypes[booleanExp] = new ANamedType(new TIdentifier("bool"), null);
                        node.ReplaceBy(booleanExp);
                        return;
                    }
                    else if (node.GetLeft() is AStringConstExp && node.GetRight() is AStringConstExp)
                    {
                        AStringConstExp const1 = (AStringConstExp)node.GetLeft();
                        AStringConstExp const2 = (AStringConstExp)node.GetRight();

                        string a = const1.GetStringLiteral().Text;
                        string b = const2.GetStringLiteral().Text;
                        bool   c = false;
                        if (node.GetBinop() is AEqBinop)
                        {
                            c = a == b;
                        }
                        else if (node.GetBinop() is ANeBinop)
                        {
                            c = a != b;
                        }
                        ABooleanConstExp booleanExp = new ABooleanConstExp(c ? (PBool) new ATrueBool() : new AFalseBool());
                        data.ExpTypes[booleanExp] = new ANamedType(new TIdentifier("bool"), null);
                        node.ReplaceBy(booleanExp);
                        return;
                    }
                }
                if ((node.GetLeft() is ABooleanConstExp || node.GetRight() is ABooleanConstExp) &&
                    (node.GetBinop() is ALazyAndBinop || node.GetBinop() is ALazyOrBinop))
                {
                    ABooleanConstExp boolExp;
                    PExp             other;
                    if (node.GetLeft() is ABooleanConstExp)
                    {
                        boolExp = (ABooleanConstExp)node.GetLeft();
                        other   = node.GetRight();
                    }
                    else
                    {
                        boolExp = (ABooleanConstExp)node.GetRight();
                        other   = node.GetLeft();
                    }
                    if (node.GetBinop() is ALazyAndBinop)
                    {
                        if (boolExp.GetBool() is ATrueBool)
                        {
                            //true && <exp>
                            node.ReplaceBy(other);
                        }
                        else
                        {
                            //false && <exp>
                            node.ReplaceBy(boolExp);
                        }
                    }
                    else
                    {
                        if (boolExp.GetBool() is ATrueBool)
                        {
                            //true || <exp>
                            node.ReplaceBy(boolExp);
                        }
                        else
                        {
                            //false || <exp>
                            node.ReplaceBy(other);
                        }
                    }
                    return;
                }
            }
            finally
            {
                if (pushed)
                {
                    PopStack();
                }
            }
        }
예제 #14
0
 private static void MakeCloneRefferences(PExp clone, PExp exp, SharedData data)
 {
     data.ExpTypes[clone] = data.ExpTypes[exp];
     if (exp is AIntConstExp || exp is AHexConstExp || exp is AOctalConstExp ||
         exp is AFixedConstExp || exp is AStringConstExp || exp is ACharConstExp ||
         exp is ABooleanConstExp || exp is ANullExp)
     {
         //No more required
     }
     else if (exp is AIncDecExp)
     {
         AIncDecExp aExp   = (AIncDecExp)exp;
         AIncDecExp aClone = (AIncDecExp)clone;
         MakeCloneRefferences(aClone.GetLvalue(), aExp.GetLvalue(), data);
     }
     else if (exp is ABinopExp)
     {
         ABinopExp aExp   = (ABinopExp)exp;
         ABinopExp aClone = (ABinopExp)clone;
         MakeCloneRefferences(aClone.GetLeft(), aExp.GetLeft(), data);
         MakeCloneRefferences(aClone.GetRight(), aExp.GetRight(), data);
     }
     else if (exp is AUnopExp)
     {
         AUnopExp aExp   = (AUnopExp)exp;
         AUnopExp aClone = (AUnopExp)clone;
         MakeCloneRefferences(aClone.GetExp(), aExp.GetExp(), data);
     }
     else if (exp is ASimpleInvokeExp)
     {
         ASimpleInvokeExp aExp   = (ASimpleInvokeExp)exp;
         ASimpleInvokeExp aClone = (ASimpleInvokeExp)clone;
         data.SimpleMethodLinks[aClone] = data.SimpleMethodLinks[aExp];
         for (int i = 0; i < aExp.GetArgs().Count; i++)
         {
             MakeCloneRefferences((PExp)aClone.GetArgs()[i], (PExp)aExp.GetArgs()[i], data);
         }
     }
     else if (exp is ANonstaticInvokeExp)
     {
         ANonstaticInvokeExp aExp   = (ANonstaticInvokeExp)exp;
         ANonstaticInvokeExp aClone = (ANonstaticInvokeExp)clone;
         data.StructMethodLinks[aClone] = data.StructMethodLinks[aExp];
         for (int i = 0; i < aExp.GetArgs().Count; i++)
         {
             MakeCloneRefferences((PExp)aClone.GetArgs()[i], (PExp)aExp.GetArgs()[i], data);
         }
         MakeCloneRefferences(aClone.GetReceiver(), aExp.GetReceiver(), data);
     }
     else if (exp is ALvalueExp)
     {
         ALvalueExp aExp   = (ALvalueExp)exp;
         ALvalueExp aClone = (ALvalueExp)clone;
         MakeCloneRefferences(aClone.GetLvalue(), aExp.GetLvalue(), data);
     }
     else if (exp is AAssignmentExp)
     {
         AAssignmentExp aExp   = (AAssignmentExp)exp;
         AAssignmentExp aClone = (AAssignmentExp)clone;
         MakeCloneRefferences(aClone.GetLvalue(), aExp.GetLvalue(), data);
         MakeCloneRefferences(aClone.GetExp(), aExp.GetExp(), data);
     }
     else if (exp is AParenExp)
     {
         AParenExp aExp   = (AParenExp)exp;
         AParenExp aClone = (AParenExp)clone;
         MakeCloneRefferences(aClone.GetExp(), aExp.GetExp(), data);
     }
     else if (exp is AStringConstExp)
     {
         AStringConstExp aExp   = (AStringConstExp)exp;
         AStringConstExp aClone = (AStringConstExp)clone;
         if (data.ObfuscatedStrings.ContainsKey(aExp))
         {
             data.ObfuscatedStrings[aClone] = data.ObfuscatedStrings[aExp];
         }
         if (data.StringsDontJoinRight.Contains(aExp))
         {
             data.StringsDontJoinRight.Add(aClone);
         }
     }
     else if (exp is ANewExp)
     {
         ANewExp aExp   = (ANewExp)exp;
         ANewExp aClone = (ANewExp)clone;
         if (data.ConstructorLinks.ContainsKey(aExp))
         {
             data.ConstructorLinks[aClone] = data.ConstructorLinks[aExp];
         }
         MakeCloneRefferences(aClone.GetType(), aExp.GetType(), data);
         for (int i = 0; i < aExp.GetArgs().Count; i++)
         {
             MakeCloneRefferences((PExp)aClone.GetArgs()[i], (PExp)aExp.GetArgs()[i], data);
         }
     }
     else if (exp is ACastExp)
     {
         ACastExp aExp   = (ACastExp)exp;
         ACastExp aClone = (ACastExp)clone;
         MakeCloneRefferences(aClone.GetType(), aExp.GetType(), data);
         MakeCloneRefferences(aClone.GetExp(), aExp.GetExp(), data);
     }
     else if (exp is ADelegateExp)
     {
         ADelegateExp aExp   = (ADelegateExp)exp;
         ADelegateExp aClone = (ADelegateExp)clone;
         if (data.DelegateCreationMethod.ContainsKey(aExp))
         {
             data.DelegateCreationMethod[aClone] = data.DelegateCreationMethod[aExp];
         }
         MakeCloneRefferences(aClone.GetType(), aExp.GetType(), data);
         MakeCloneRefferences(aClone.GetLvalue(), aExp.GetLvalue(), data);
     }
     else if (exp is ADelegateInvokeExp)
     {
         ADelegateInvokeExp aExp   = (ADelegateInvokeExp)exp;
         ADelegateInvokeExp aClone = (ADelegateInvokeExp)clone;
         MakeCloneRefferences(aClone.GetReceiver(), aExp.GetReceiver(), data);
         for (int i = 0; i < aExp.GetArgs().Count; i++)
         {
             MakeCloneRefferences((PExp)aClone.GetArgs()[i], (PExp)aExp.GetArgs()[i], data);
         }
     }
     else if (exp is AIfExp)
     {
         AIfExp aExp   = (AIfExp)exp;
         AIfExp aClone = (AIfExp)clone;
         MakeCloneRefferences(aClone.GetCond(), aExp.GetCond(), data);
         MakeCloneRefferences(aClone.GetThen(), aExp.GetThen(), data);
         MakeCloneRefferences(aClone.GetElse(), aExp.GetElse(), data);
     }
     else
     {
         throw new Exception("Unexpect exp. Got " + exp.GetType());
     }
 }
        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);
        }
예제 #16
0
 public override void CaseABinopExp(ABinopExp node)
 {
     if (folding)
     {
         node.GetLeft().Apply(this);
         int left = value;
         node.GetRight().Apply(this);
         int right = value;
         if (node.GetBinop() is APlusBinop)
         {
             value = left + right;
         }
         else if (node.GetBinop() is AMinusBinop)
         {
             value = left - right;
         }
         else if (node.GetBinop() is ATimesBinop)
         {
             value = left * right;
         }
         else if (node.GetBinop() is ADivideBinop)
         {
             if (right == 0)
             {
                 errors.Add(new ErrorCollection.Error(((ADivideBinop)node.GetBinop()).GetToken(), LocRM.GetString("ErrorText58")), true);
                 throw new ParserException(null, "SetArrayIndexes.CaseABinopExp");
             }
             value = left / right;
         }
         else if (node.GetBinop() is AModuloBinop)
         {
             if (right == 0)
             {
                 errors.Add(new ErrorCollection.Error(((AModuloBinop)node.GetBinop()).GetToken(), LocRM.GetString("ErrorText58")), true);
                 throw new ParserException(null, "EnviromentChecking.CaseABinopExp");
             }
             value = left % right;
         }
         else if (node.GetBinop() is AAndBinop)
         {
             value = left & right;
         }
         else if (node.GetBinop() is AOrBinop)
         {
             value = left | right;
         }
         else if (node.GetBinop() is AXorBinop)
         {
             value = left ^ right;
         }
         else if (node.GetBinop() is ALBitShiftBinop)
         {
             value = left << right;
         }
         else if (node.GetBinop() is ARBitShiftBinop)
         {
             value = left >> right;
         }
     }
     else
     {
         base.CaseABinopExp(node);
     }
 }
예제 #17
0
        //Join string + string to string
        public override void CaseABinopExp(ABinopExp node)
        {
            if (node.GetBinop() is APlusBinop)
            {
                PType type = data.ExpTypes[node];
                if (type is ANamedType && ((ANamedType)type).IsPrimitive("string"))
                {
                    PExp other = null;
                    if (node.GetLeft() is ANullExp)
                    {
                        other = node.GetRight();
                    }
                    if (node.GetRight() is ANullExp)
                    {
                        other = node.GetLeft();
                    }
                    if (other != null)
                    {
                        node.ReplaceBy(other);
                        other.Apply(this);
                        return;
                    }
                }


                //Case (string + string)
                if (node.GetLeft() is AStringConstExp && node.GetRight() is AStringConstExp)
                {
                    AStringConstExp left  = (AStringConstExp)node.GetLeft();
                    AStringConstExp right = (AStringConstExp)node.GetRight();

                    if (!IsJoinAllowed(left.GetStringLiteral().Text, right.GetStringLiteral().Text))
                    {
                        base.CaseABinopExp(node);
                        return;
                    }
                    left.GetStringLiteral().Text = left.GetStringLiteral().Text.Substring(0,
                                                                                          left.GetStringLiteral().Text.
                                                                                          Length - 1);
                    left.GetStringLiteral().Text += right.GetStringLiteral().Text.Substring(1);
                    node.ReplaceBy(left);
                    CaseAStringConstExp(left);
                    return;
                }
                //Case (<exp> + string) + string
                if (node.GetLeft() is ABinopExp && node.GetRight() is AStringConstExp)
                {
                    ABinopExp leftBinop = (ABinopExp)node.GetLeft();
                    if (leftBinop.GetBinop() is APlusBinop && leftBinop.GetRight() is AStringConstExp)
                    {
                        AStringConstExp left  = (AStringConstExp)leftBinop.GetRight();
                        AStringConstExp right = (AStringConstExp)node.GetRight();
                        if (!IsJoinAllowed(left.GetStringLiteral().Text, right.GetStringLiteral().Text))
                        {
                            base.CaseABinopExp(node);
                            return;
                        }
                        left.GetStringLiteral().Text = left.GetStringLiteral().Text.Substring(0,
                                                                                              left.GetStringLiteral().
                                                                                              Text.
                                                                                              Length - 1);
                        left.GetStringLiteral().Text += right.GetStringLiteral().Text.Substring(1);
                        node.ReplaceBy(leftBinop);
                        CaseABinopExp(leftBinop);
                        return;
                    }
                }
                //Case string + (string + <exp>)
                //Case (<exp> + string) + (string + <exp>)
            }
            //Case (int + int)

            /*if (node.GetLeft() is AIntConstExp && node.GetRight() is AIntConstExp)
             * {
             *  AIntConstExp left = (AIntConstExp) node.GetLeft();
             *  AIntConstExp right = (AIntConstExp) node.GetRight();
             *
             *  int a = int.Parse(left.GetIntegerLiteral().Text);
             *  int b = int.Parse(right.GetIntegerLiteral().Text);
             *
             *  if (node.GetBinop() is APlusBinop)
             *  {
             *      a += b;
             *  }
             *  else if (node.GetBinop() is AMinusBinop)
             *  {
             *      a -= b;
             *  }
             *  else if (node.GetBinop() is ATimesBinop)
             *  {
             *      a *= b;
             *  }
             *  else if (node.GetBinop() is ADivideBinop)
             *  {
             *      if (b == 0)
             *      {
             *          base.CaseABinopExp(node);
             *          return;
             *      }
             *      a /= b;
             *  }
             *  else
             *  {
             *      base.CaseABinopExp(node);
             *      return;
             *  }
             *
             *  left.GetIntegerLiteral().Text = a.ToString();
             *  node.ReplaceBy(left);
             *  left.Apply(this);
             *  return;
             * }
             * //Case (<exp> + int) + int
             * if (node.GetLeft() is ABinopExp && node.GetRight() is AIntConstExp && (node.GetBinop() is APlusBinop || node.GetBinop() is AMinusBinop))
             * {
             *  ABinopExp leftBinop = (ABinopExp) node.GetLeft();
             *  PType leftType = data.ExpTypes[leftBinop];
             *  if (leftBinop.GetRight() is AIntConstExp && leftType is ANamedType && ((ANamedType) leftType).GetName().Text == "int" &&
             *       (leftBinop.GetBinop() is APlusBinop || leftBinop.GetBinop() is AMinusBinop))
             *  {
             *      AIntConstExp left = (AIntConstExp)leftBinop.GetRight();
             *      AIntConstExp right = (AIntConstExp)node.GetRight();
             *      int a = int.Parse(left.GetIntegerLiteral().Text);
             *      int b = int.Parse(right.GetIntegerLiteral().Text);
             *
             *      if (node.GetBinop() is APlusBinop)
             *      {
             *          if (leftBinop.GetBinop() is APlusBinop)
             *          {
             *              //(<exp> + int) + int
             *              int c = a + b;
             *              //Test for overflow
             *              if (a > 0 && b > 0 && (c < a || c < b) ||
             *                  a < 0 && b < 0 && (c > a || c > b))
             *              {
             *                  //Don't add them
             *                  base.CaseABinopExp(node);
             *                  return;
             *              }
             *              if (c < 0)
             *              {
             *                  //Change binop to <exp> - c
             *                  if (c != int.MinValue)
             *                  {
             *                      c = -c;
             *                      leftBinop.SetBinop(new AMinusBinop(new TMinus("-")));
             *                  }
             *              }
             *              //Replace node with leftbinop
             *              left.GetIntegerLiteral().Text = c.ToString();
             *              node.ReplaceBy(leftBinop);
             *              leftBinop.Apply(this);
             *              return;
             *          }
             *          else
             *          {
             *              //(<exp> - int) + int
             *              int c = b - a;
             *              //Test for overflow
             *              if (a < 0 && b > 0 && (c < a || c < b) ||
             *                  a > 0 && b < 0 && (c > a || c > b))
             *              {
             *                  //Don't add them
             *                  base.CaseABinopExp(node);
             *                  return;
             *              }
             *              if (c > 0 || c == int.MinValue)
             *              {
             *                  //Change binop to <exp> + c
             *                  leftBinop.SetBinop(new APlusBinop(new TPlus("+")));
             *
             *              }
             *              else
             *                  c = -c;
             *              //Replace node with leftbinop
             *              left.GetIntegerLiteral().Text = c.ToString();
             *              node.ReplaceBy(leftBinop);
             *              leftBinop.Apply(this);
             *              return;
             *          }
             *      }
             *      else
             *      {
             *          if (leftBinop.GetBinop() is APlusBinop)
             *          {
             *              //(<exp> + int) - int
             *              //ALso need to consider <exp> in the other position, and int on the other side of the binop
             *              //Make a more general algorithm
             *          }
             *          else
             *          {
             *
             *          }
             *      }
             *  }
             * }*/
            base.CaseABinopExp(node);
        }
예제 #18
0
 public static bool ReturnsTheSame(PExp left, PExp right, SharedData data)
 {
     if (left.GetType() != right.GetType())
     {
         return(false);
     }
     if (left is ABinopExp)
     {
         ABinopExp aLeft  = (ABinopExp)left;
         ABinopExp aRight = (ABinopExp)right;
         if (aLeft.GetBinop().GetType() != aRight.GetBinop().GetType())
         {
             return(false);
         }
         return(ReturnsTheSame(aLeft.GetLeft(), aRight.GetLeft(), data) &&
                ReturnsTheSame(aLeft.GetRight(), aRight.GetRight(), data));
     }
     if (left is AUnopExp)
     {
         AUnopExp aLeft  = (AUnopExp)left;
         AUnopExp aRight = (AUnopExp)right;
         if (aLeft.GetUnop().GetType() != aRight.GetUnop().GetType())
         {
             return(false);
         }
         return(ReturnsTheSame(aLeft.GetExp(), aRight.GetExp(), data));
     }
     if (left is AIntConstExp)
     {
         AIntConstExp aLeft  = (AIntConstExp)left;
         AIntConstExp aRight = (AIntConstExp)right;
         return(int.Parse(aLeft.GetIntegerLiteral().Text) == int.Parse(aRight.GetIntegerLiteral().Text));
     }
     if (left is AFixedConstExp)
     {
         AFixedConstExp aLeft  = (AFixedConstExp)left;
         AFixedConstExp aRight = (AFixedConstExp)right;
         return(aLeft.GetFixedLiteral().Text == aRight.GetFixedLiteral().Text);
     }
     if (left is AStringConstExp)
     {
         AStringConstExp aLeft  = (AStringConstExp)left;
         AStringConstExp aRight = (AStringConstExp)right;
         return(aLeft.GetStringLiteral().Text == aRight.GetStringLiteral().Text);
     }
     if (left is ACharConstExp)
     {
         ACharConstExp aLeft  = (ACharConstExp)left;
         ACharConstExp aRight = (ACharConstExp)right;
         return(aLeft.GetCharLiteral().Text == aRight.GetCharLiteral().Text);
     }
     if (left is ABooleanConstExp)
     {
         ABooleanConstExp aLeft  = (ABooleanConstExp)left;
         ABooleanConstExp aRight = (ABooleanConstExp)right;
         return(aLeft.GetBool().GetType() == aRight.GetBool().GetType());
     }
     if (left is ASimpleInvokeExp)
     {
         //A method might not return the same thing each time it is called
         return(false);
     }
     if (left is ALvalueExp)
     {
         ALvalueExp aLeft  = (ALvalueExp)left;
         ALvalueExp aRight = (ALvalueExp)right;
         return(ReturnsTheSame(aLeft.GetLvalue(), aRight.GetLvalue(), data));
     }
     if (left is AParenExp)
     {
         AParenExp aLeft  = (AParenExp)left;
         AParenExp aRight = (AParenExp)right;
         return(ReturnsTheSame(aLeft.GetExp(), aRight.GetExp(), data));
     }
     throw new Exception("Util.ReturnsTheSame. Unexpected type, got " + left.GetType());
 }