public override void CaseAVoidReturnStm(AVoidReturnStm node)
            {
                if (!neededWhile)
                {
                    node.Parent().RemoveChild(node);
                    return;
                }

                /*
                 * return;
                 * ->
                 * hasMethodReturnedVar = true;
                 * break;
                 */
                ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(hasMethodReturnedVar.GetName().Text));

                data.LvalueTypes[lvalue] = hasMethodReturnedVar.GetType();
                data.LocalLinks[lvalue]  = hasMethodReturnedVar;
                PExp exp = new ABooleanConstExp(new ATrueBool());

                data.ExpTypes[exp] = new ANamedType(new TIdentifier("bool"), null);
                exp = new AAssignmentExp(new TAssign("="), lvalue, exp);
                data.ExpTypes[exp] = hasMethodReturnedVar.GetType();
                PStm    stm   = new AExpStm(new TSemicolon(";"), exp);
                AABlock block = new AABlock();

                block.GetStatements().Add(stm);

                block.GetStatements().Add(new ABreakStm(new TBreak("break")));

                node.ReplaceBy(new ABlockStm(new TLBrace("{"), block));
            }
Exemple #2
0
            public override void CaseAVoidReturnStm(AVoidReturnStm node)
            {
                ABooleanConstExp trueBool = new ABooleanConstExp(new ATrueBool());
                AValueReturnStm  replacer = new AValueReturnStm(node.GetToken(), trueBool);

                node.ReplaceBy(replacer);
                data.ExpTypes[trueBool] = new ANamedType(new TIdentifier("bool"), null);
            }
        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);
        }
Exemple #4
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);
                            }
                        }
                    }
                }
            }
        }
        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);
        }
        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);
        }
Exemple #7
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();
                }
            }
        }
Exemple #8
0
        private List <PStm> AssignDefault(PLvalue lvalue)
        {
            List <PStm> returner  = new List <PStm>();
            PType       type      = data.LvalueTypes[lvalue];
            PExp        rightSide = null;

            if (type is ANamedType)
            {
                ANamedType aType = (ANamedType)type;
                if (aType.IsPrimitive("string"))//aType.GetName().Text == "string")
                {
                    rightSide = new AStringConstExp(new TStringLiteral("\"\""));
                    data.ExpTypes[rightSide] = new ANamedType(new TIdentifier("string"), null);
                }
                else if (aType.IsPrimitive(GalaxyKeywords.NullablePrimitives.words)) //GalaxyKeywords.NullablePrimitives.words.Contains(aType.GetName().Text))
                {
                    rightSide = new ANullExp();
                    data.ExpTypes[rightSide] = new ANamedType(new TIdentifier("null"), null);
                }
                else if (aType.IsPrimitive(new [] { "int", "byte", "fixed" }))

                /*aType.GetName().Text == "int" ||
                *  aType.GetName().Text == "byte" ||
                *  aType.GetName().Text == "fixed")*/
                {
                    rightSide = new AIntConstExp(new TIntegerLiteral("0"));
                    data.ExpTypes[rightSide] = type;
                }
                else if (aType.IsPrimitive("bool"))//aType.GetName().Text == "bool")
                {
                    rightSide = new ABooleanConstExp(new AFalseBool());
                    data.ExpTypes[rightSide] = type;
                }
                else if (aType.IsPrimitive("color"))//aType.GetName().Text == "color")
                {
                    PExp             arg1   = new AIntConstExp(new TIntegerLiteral("0"));
                    PExp             arg2   = new AIntConstExp(new TIntegerLiteral("0"));
                    PExp             arg3   = new AIntConstExp(new TIntegerLiteral("0"));
                    ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("Color"), new ArrayList()
                    {
                        arg1, arg2, arg3
                    });
                    rightSide = invoke;
                    data.ExpTypes[rightSide]       = type;
                    data.ExpTypes[arg1]            =
                        data.ExpTypes[arg2]        =
                            data.ExpTypes[arg3]    = new ANamedType(new TIdentifier("int"), null);
                    data.SimpleMethodLinks[invoke] =
                        data.Libraries.Methods.First(func => func.GetName().Text == invoke.GetName().Text);
                }
                else if (aType.IsPrimitive("char"))//aType.GetName().Text == "char")
                {
                    //Dunno?!
                    rightSide = new ACharConstExp(new TCharLiteral("'\0'"));
                    data.ExpTypes[rightSide] = type;
                }
                else //Struct
                {
                    AStructDecl str = data.StructTypeLinks[aType];
                    foreach (AALocalDecl localDecl in str.GetLocals())
                    {
                        ALvalueExp    reciever  = new ALvalueExp(Util.MakeClone(lvalue, data));
                        AStructLvalue newLvalue = new AStructLvalue(reciever, new ADotDotType(new TDot(".")), new TIdentifier(localDecl.GetName().Text));
                        data.StructFieldLinks[newLvalue] = localDecl;
                        data.ExpTypes[reciever]          = type;
                        data.LvalueTypes[newLvalue]      = localDecl.GetType();
                        returner.AddRange(AssignDefault(newLvalue));
                    }
                    return(returner);
                }
                AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(lvalue, data), rightSide);
                data.ExpTypes[assignment] = type;
                return(new List <PStm>()
                {
                    new AExpStm(new TSemicolon(";"), assignment)
                });
            }
            if (type is AArrayTempType)
            {
                AArrayTempType aType = (AArrayTempType)type;
                for (int i = 0; i < int.Parse(aType.GetIntDim().Text); i++)
                {
                    ALvalueExp   reciever  = new ALvalueExp(Util.MakeClone(lvalue, data));
                    AArrayLvalue newLvalue = new AArrayLvalue(new TLBracket("["), reciever, new AIntConstExp(new TIntegerLiteral(i.ToString())));
                    data.ExpTypes[reciever]             = type;
                    data.LvalueTypes[newLvalue]         = aType.GetType();
                    data.ExpTypes[newLvalue.GetIndex()] = new ANamedType(new TIdentifier("int"), null);
                    returner.AddRange(AssignDefault(newLvalue));
                }
                return(returner);
            }

            throw new Exception("Unexpected type. (LivenessAnalasys.AssignDefault), got " + type);
        }
        public static List <AABlock> Inline(ASimpleInvokeExp node, FinalTransformations finalTrans)
        {
            /*if (Util.GetAncestor<AMethodDecl>(node) != null && Util.GetAncestor<AMethodDecl>(node).GetName().Text == "UIChatFrame_LeaveChannel")
             *  node = node;*/

            SharedData data = finalTrans.data;
            //If this node is inside the condition of a while, replace it with a new local var,
            //make a clone before the while, one before each continue in the while, and one at the end of the while
            //(unless the end is a return or break)
            AABlock pBlock;

            if (Util.HasAncestor <AWhileStm>(node))
            {
                AWhileStm whileStm = Util.GetAncestor <AWhileStm>(node);
                if (Util.IsAncestor(node, whileStm.GetCondition()))
                {
                    List <ASimpleInvokeExp> toInline = new List <ASimpleInvokeExp>();
                    //Above while
                    AALocalDecl replaceVarDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                                 Util.MakeClone(data.ExpTypes[node], data),
                                                                 new TIdentifier("whileVar"), null);
                    ALocalLvalue replaceVarRef    = new ALocalLvalue(new TIdentifier("whileVar"));
                    ALvalueExp   replaceVarRefExp = new ALvalueExp(replaceVarRef);
                    data.LocalLinks[replaceVarRef]  = replaceVarDecl;
                    data.ExpTypes[replaceVarRefExp] = data.LvalueTypes[replaceVarRef] = replaceVarDecl.GetType();
                    node.ReplaceBy(replaceVarRefExp);
                    replaceVarDecl.SetInit(node);
                    pBlock = (AABlock)whileStm.Parent();
                    pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(whileStm), new ALocalDeclStm(new TSemicolon(";"), replaceVarDecl));
                    toInline.Add(node);


                    //In the end of the while
                    PStm lastStm = whileStm.GetBody();
                    while (lastStm is ABlockStm)
                    {
                        AABlock block = (AABlock)((ABlockStm)lastStm).GetBlock();
                        if (block.GetStatements().Count == 0)
                        {
                            lastStm = null;
                            break;
                        }
                        lastStm = (PStm)block.GetStatements()[block.GetStatements().Count - 1];
                    }
                    if (lastStm == null || !(lastStm is AValueReturnStm || lastStm is AVoidReturnStm || lastStm is ABreakStm))
                    {
                        lastStm = whileStm.GetBody();
                        AABlock block;
                        if (lastStm is ABlockStm)
                        {
                            block = (AABlock)((ABlockStm)lastStm).GetBlock();
                        }
                        else
                        {
                            block = new AABlock(new ArrayList(), new TRBrace("}"));
                            block.GetStatements().Add(lastStm);
                            whileStm.SetBody(new ABlockStm(new TLBrace("{"), block));
                        }

                        replaceVarRef = new ALocalLvalue(new TIdentifier("whileVar"));
                        ASimpleInvokeExp clone = (ASimpleInvokeExp)Util.MakeClone(node, data);
                        toInline.Add(clone);
                        AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), replaceVarRef, clone);
                        data.LocalLinks[replaceVarRef] = replaceVarDecl;
                        data.ExpTypes[assignment]      = data.LvalueTypes[replaceVarRef] = replaceVarDecl.GetType();
                        block.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment));
                    }

                    //After each continue
                    CloneBeforeContinue cloner = new CloneBeforeContinue(node, replaceVarDecl, data);
                    whileStm.GetBody().Apply(cloner);
                    toInline.AddRange(cloner.replacementExpressions);
                    List <AABlock> visitBlocks = new List <AABlock>();
                    foreach (ASimpleInvokeExp invoke in toInline)
                    {
                        visitBlocks.AddRange(Inline(invoke, finalTrans));
                    }
                    return(visitBlocks);
                }
            }



            AMethodDecl           decl = finalTrans.data.SimpleMethodLinks[node];
            FindAssignedToFormals assignedToFormalsFinder = new FindAssignedToFormals(finalTrans.data);

            decl.Apply(assignedToFormalsFinder);
            List <AALocalDecl> assignedToFormals = assignedToFormalsFinder.AssignedFormals;


            /*
             * inline int foo(int a)
             * {
             *      int b = 2;
             *      int c;
             *      ...
             *      while(...)
             *      {
             *          ...
             *          break;
             *          ...
             *          return c;
             *      }
             *      ...
             *      return 2;
             * }
             *
             * bar(foo(<arg for a>));
             * ->
             *
             * {
             *      bool inlineMethodReturned = false;
             *      int inlineReturner;
             *      int a = <arg for a>;
             *      while (!inlineMethodReturned)
             *      {
             *          int b = 2;
             *          int c;
             *          ...
             *          while(...)
             *          {
             *              ...
             *              break
             *              ...
             *              inlineReturner = c;
             *              inlineMethodReturned = true;
             *              break;
             *          }
             *          if (inlineMethodReturned)
             *          {
             *              break;
             *          }
             *          ...
             *          inlineReturner = 2;
             *          inlineMethodReturned = true;
             *          break;
             *          break;
             *      }
             *      bar(inlineReturner);
             * }
             *
             *
             */


            AABlock outerBlock = new AABlock();
            PExp    exp        = new ABooleanConstExp(new AFalseBool());

            finalTrans.data.ExpTypes[exp] = new ANamedType(new TIdentifier("bool"), null);
            AALocalDecl hasMethodReturnedVar = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("bool"), null),
                                                               new TIdentifier("hasInlineReturned"), exp);

            finalTrans.data.GeneratedVariables.Add(hasMethodReturnedVar);
            PStm stm = new ALocalDeclStm(new TSemicolon(";"), hasMethodReturnedVar);

            outerBlock.GetStatements().Add(stm);

            AALocalDecl methodReturnerVar = null;

            if (!(decl.GetReturnType() is AVoidType))
            {
                methodReturnerVar = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(decl.GetReturnType(), finalTrans.data),
                                                    new TIdentifier("inlineReturner"), null);
                stm = new ALocalDeclStm(new TSemicolon(";"), methodReturnerVar);
                outerBlock.GetStatements().Add(stm);
            }

            AABlock afterBlock = new AABlock();

            //A dictionary from the formals of the inline method to a cloneable replacement lvalue
            Dictionary <AALocalDecl, PLvalue> Parameters    = new Dictionary <AALocalDecl, PLvalue>();
            Dictionary <AALocalDecl, PExp>    ParameterExps = new Dictionary <AALocalDecl, PExp>();

            for (int i = 0; i < decl.GetFormals().Count; i++)
            {
                AALocalDecl formal = (AALocalDecl)decl.GetFormals()[i];
                PExp        arg    = (PExp)node.GetArgs()[0];
                PLvalue     lvalue;
                //if ref, dont make a new var
                if (formal.GetRef() != null && arg is ALvalueExp)
                {
                    arg.Apply(new MoveMethodDeclsOut("inlineVar", finalTrans.data));
                    arg.Parent().RemoveChild(arg);
                    lvalue = ((ALvalueExp)arg).GetLvalue();
                }
                else if (!assignedToFormals.Contains(formal) && Util.IsLocal(arg, finalTrans.data))
                {
                    lvalue = new ALocalLvalue(new TIdentifier("I hope I dont make it"));
                    finalTrans.data.LvalueTypes[lvalue] = formal.GetType();
                    finalTrans.data.LocalLinks[(ALocalLvalue)lvalue] = formal;
                    ParameterExps[formal] = arg;
                    arg.Parent().RemoveChild(arg);
                }
                else
                {
                    AAssignmentExp assExp = null;
                    if (formal.GetOut() != null)
                    {
                        //Dont initialize with arg, but assign arg after
                        arg.Apply(new MoveMethodDeclsOut("inlineVar", finalTrans.data));
                        lvalue = ((ALvalueExp)arg).GetLvalue();
                        assExp = new AAssignmentExp(new TAssign("="), lvalue, null);
                        finalTrans.data.ExpTypes[assExp] = finalTrans.data.LvalueTypes[lvalue];
                        arg.Parent().RemoveChild(arg);
                        arg = null;
                    }
                    AALocalDecl parameter = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(formal.GetType(), finalTrans.data),
                                                            new TIdentifier(formal.GetName().Text),
                                                            arg);
                    stm = new ALocalDeclStm(new TSemicolon(";"), parameter);
                    outerBlock.GetStatements().Add(stm);

                    lvalue = new ALocalLvalue(new TIdentifier(parameter.GetName().Text));
                    finalTrans.data.LvalueTypes[lvalue] = parameter.GetType();
                    finalTrans.data.LocalLinks[(ALocalLvalue)lvalue] = parameter;


                    if (formal.GetOut() != null)
                    {
                        //Dont initialize with arg, but assign arg after
                        ALvalueExp lvalueExp = new ALvalueExp(Util.MakeClone(lvalue, finalTrans.data));
                        finalTrans.data.ExpTypes[lvalueExp] = finalTrans.data.LvalueTypes[lvalue];
                        assExp.SetExp(lvalueExp);
                        afterBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assExp));
                    }
                }
                Parameters.Add(formal, lvalue);
            }

            AABlock innerBlock = (AABlock)decl.GetBlock().Clone();

            exp = new ABooleanConstExp(new ATrueBool());
            finalTrans.data.ExpTypes[exp] = new ANamedType(new TIdentifier("bool"), null);
            ABlockStm innerBlockStm = new ABlockStm(new TLBrace("{"), innerBlock);

            bool needWhile = CheckIfWhilesIsNeeded.IsWhileNeeded(decl.GetBlock());

            if (needWhile)
            {
                stm = new AWhileStm(new TLParen("("), exp, innerBlockStm);
            }
            else
            {
                stm = innerBlockStm;
            }
            outerBlock.GetStatements().Add(stm);

            outerBlock.GetStatements().Add(new ABlockStm(new TLBrace("{"), afterBlock));

            //Clone method contents to inner block.
            CloneMethod cloneFixer = new CloneMethod(finalTrans, Parameters, ParameterExps, innerBlockStm);

            decl.GetBlock().Apply(cloneFixer);
            foreach (KeyValuePair <PLvalue, PExp> pair in cloneFixer.ReplaceUsAfter)
            {
                PLvalue    lvalue       = pair.Key;
                PExp       replacement  = Util.MakeClone(pair.Value, finalTrans.data);
                ALvalueExp lvalueParent = (ALvalueExp)lvalue.Parent();
                lvalueParent.ReplaceBy(replacement);
            }
            innerBlockStm.Apply(new FixTypes(finalTrans.data));

            innerBlock.Apply(new FixReturnsAndWhiles(hasMethodReturnedVar, methodReturnerVar, finalTrans.data, needWhile));

            GetNonBlockStm stmFinder = new GetNonBlockStm(false);

            innerBlock.Apply(stmFinder);
            if (needWhile && (stmFinder.Stm == null || !(stmFinder.Stm is ABreakStm)))
            {
                innerBlock.GetStatements().Add(new ABreakStm(new TBreak("break")));
            }

            //Insert before current statement
            ABlockStm outerBlockStm = new ABlockStm(new TLBrace("{"), outerBlock);

            PStm pStm = Util.GetAncestor <PStm>(node);

            pBlock = (AABlock)pStm.Parent();

            pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), outerBlockStm);

            if (node.Parent() == pStm && pStm is AExpStm)
            {
                pBlock.RemoveChild(pStm);
            }
            else
            {
                PLvalue lvalue = new ALocalLvalue(new TIdentifier(methodReturnerVar.GetName().Text));
                finalTrans.data.LvalueTypes[lvalue] = methodReturnerVar.GetType();
                finalTrans.data.LocalLinks[(ALocalLvalue)lvalue] = methodReturnerVar;
                exp = new ALvalueExp(lvalue);
                finalTrans.data.ExpTypes[exp] = methodReturnerVar.GetType();

                node.ReplaceBy(exp);
            }
            return(new List <AABlock>()
            {
                outerBlock
            });
        }
Exemple #10
0
        private void MakeInitializerInvokes()
        {
            AABlock      block = (AABlock)mainEntry.GetBlock();
            AASourceFile file  = Util.GetAncestor <AASourceFile>(mainEntry);
            AMethodDecl  invokeMethod;

            /* Add
             * void Invoke(string methodName)
             * {
             *     trigger initTrigger = TriggerCreate(methodName);
             *     TriggerExecute(initTrigger, false, true);
             *     TriggerDestroy(initTrigger);
             * }
             */

            {
                //void Invoke(string methodName)
                AALocalDecl parameter = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                        new ANamedType(new TIdentifier("string"), null),
                                                        new TIdentifier("methodName"), null);
                AABlock methodBody = new AABlock();
                invokeMethod = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                               new AVoidType(new TVoid("void")), new TIdentifier("Invoke"),
                                               new ArrayList()
                {
                    parameter
                }, methodBody);

                //trigger initTrigger = TriggerCreate(methodName);
                ALocalLvalue parameterLvalue = new ALocalLvalue(new TIdentifier("methodName"));
                data.LocalLinks[parameterLvalue] = parameter;
                ALvalueExp parameterLvalueExp = new ALvalueExp(parameterLvalue);
                data.LvalueTypes[parameterLvalue]     =
                    data.ExpTypes[parameterLvalueExp] = parameter.GetType();
                ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("TriggerCreate"), new ArrayList()
                {
                    parameterLvalueExp
                });
                data.ExpTypes[invoke] = new ANamedType(new TIdentifier("trigger"), null);
                foreach (AMethodDecl methodDecl in data.Libraries.Methods)
                {
                    if (methodDecl.GetName().Text == "TriggerCreate")
                    {
                        data.SimpleMethodLinks[invoke] = methodDecl;
                        break;
                    }
                }
                AALocalDecl initTriggerDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                              new ANamedType(new TIdentifier("trigger"), null),
                                                              new TIdentifier("initTrigger"), invoke);
                methodBody.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), initTriggerDecl));

                //TriggerExecute(initTrigger, false, true);
                ALocalLvalue initTriggerLvalue = new ALocalLvalue(new TIdentifier("initTrigger"));
                data.LocalLinks[initTriggerLvalue] = initTriggerDecl;
                ALvalueExp initTriggerLvalueExp = new ALvalueExp(initTriggerLvalue);
                data.LvalueTypes[initTriggerLvalue]     =
                    data.ExpTypes[initTriggerLvalueExp] = initTriggerDecl.GetType();
                ABooleanConstExp falseBool = new ABooleanConstExp(new AFalseBool());
                ABooleanConstExp trueBool  = new ABooleanConstExp(new ATrueBool());
                data.ExpTypes[falseBool]    =
                    data.ExpTypes[trueBool] = new ANamedType(new TIdentifier("bool"), null);
                invoke = new ASimpleInvokeExp(new TIdentifier("TriggerExecute"), new ArrayList()
                {
                    initTriggerLvalueExp, falseBool, trueBool
                });
                data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                foreach (AMethodDecl methodDecl in data.Libraries.Methods)
                {
                    if (methodDecl.GetName().Text == "TriggerExecute")
                    {
                        data.SimpleMethodLinks[invoke] = methodDecl;
                        break;
                    }
                }
                methodBody.GetStatements().Add(new AExpStm(new TSemicolon(";"), invoke));

                //TriggerDestroy(initTrigger);
                initTriggerLvalue = new ALocalLvalue(new TIdentifier("initTrigger"));
                data.LocalLinks[initTriggerLvalue] = initTriggerDecl;
                initTriggerLvalueExp = new ALvalueExp(initTriggerLvalue);
                data.LvalueTypes[initTriggerLvalue]     =
                    data.ExpTypes[initTriggerLvalueExp] = initTriggerDecl.GetType();
                invoke = new ASimpleInvokeExp(new TIdentifier("TriggerDestroy"), new ArrayList()
                {
                    initTriggerLvalueExp
                });
                data.ExpTypes[invoke] = new AVoidType(new TVoid("void"));
                foreach (AMethodDecl methodDecl in data.Libraries.Methods)
                {
                    if (methodDecl.GetName().Text == "TriggerDestroy")
                    {
                        data.SimpleMethodLinks[invoke] = methodDecl;
                        break;
                    }
                }
                methodBody.GetStatements().Add(new AExpStm(new TSemicolon(";"), invoke));

                file.GetDecl().Add(invokeMethod);
            }

            for (int i = data.InitializerMethods.Count - 1; i >= 0; i--)
            {
                AMethodDecl method = data.InitializerMethods[i];
                //Turn method into a trigger
                method.SetReturnType(new ANamedType(new TIdentifier("bool"), null));
                method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                        new ANamedType(new TIdentifier("bool"), null),
                                                        new TIdentifier("testConds"), null));
                method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                        new ANamedType(new TIdentifier("bool"), null),
                                                        new TIdentifier("runActions"), null));
                method.SetTrigger(new TTrigger("trigger"));
                ((AABlock)method.GetBlock()).GetStatements().Add(new AVoidReturnStm(new TReturn("return")));
                TriggerConvertReturn returnConverter = new TriggerConvertReturn(data);
                method.Apply(returnConverter);
                data.TriggerDeclarations[method] = new List <TStringLiteral>();


                //Add Invoke(<name>); to main entry
                TStringLiteral literal = new TStringLiteral(method.GetName().Text);
                data.TriggerDeclarations[method].Add(literal);
                AStringConstExp stringConst = new AStringConstExp(literal);
                data.ExpTypes[stringConst] = new ANamedType(new TIdentifier("string"), null);
                ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("Invoke"), new ArrayList()
                {
                    stringConst
                });
                data.SimpleMethodLinks[invoke] = invokeMethod;
                data.ExpTypes[invoke]          = invokeMethod.GetReturnType();
                block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), invoke));



                //ASyncInvokeExp syncInvokeExp = new ASyncInvokeExp(new TSyncInvoke("Invoke"), new AAmbiguousNameLvalue(new ASimpleName(new TIdentifier(method.GetName().Text))), new ArrayList());
                //data.Invokes.Add(method, new List<InvokeStm>(){new InvokeStm(syncInvokeExp)});
                //data.ExpTypes[syncInvokeExp] = new AVoidType(new TVoid("void"));

                //block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), syncInvokeExp));
            }
            for (int i = data.InvokeOnIniti.Count - 1; i >= 0; i--)
            {
                AMethodDecl method = data.InvokeOnIniti[i];

                //Turn method into a trigger
                method.SetReturnType(new ANamedType(new TIdentifier("bool"), null));
                method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                        new ANamedType(new TIdentifier("bool"), null),
                                                        new TIdentifier("testConds"), null));
                method.GetFormals().Add(new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                        new ANamedType(new TIdentifier("bool"), null),
                                                        new TIdentifier("runActions"), null));
                method.SetTrigger(new TTrigger("trigger"));
                ((AABlock)method.GetBlock()).GetStatements().Add(new AVoidReturnStm(new TReturn("return")));
                TriggerConvertReturn returnConverter = new TriggerConvertReturn(data);
                method.Apply(returnConverter);
                data.TriggerDeclarations[method] = new List <TStringLiteral>();


                //Add Invoke(<name>); to main entry
                TStringLiteral literal = new TStringLiteral(method.GetName().Text);
                data.TriggerDeclarations[method].Add(literal);
                AStringConstExp stringConst = new AStringConstExp(literal);
                data.ExpTypes[stringConst] = new ANamedType(new TIdentifier("string"), null);
                ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("Invoke"), new ArrayList()
                {
                    stringConst
                });
                data.SimpleMethodLinks[invoke] = invokeMethod;
                data.ExpTypes[invoke]          = invokeMethod.GetReturnType();
                block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), invoke));


                /*
                 * ASyncInvokeExp syncInvokeExp = new ASyncInvokeExp(new TSyncInvoke("Invoke"),  new AAmbiguousNameLvalue(new ASimpleName(new TIdentifier(method.GetName().Text))), new ArrayList());
                 * data.Invokes.Add(method, new List<InvokeStm>() { new InvokeStm(syncInvokeExp) });
                 * data.ExpTypes[syncInvokeExp] = new AVoidType(new TVoid("void"));
                 *
                 * block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), syncInvokeExp));*/
            }
            for (int i = data.FieldsToInitInMapInit.Count - 1; i >= 0; i--)
            {
                AFieldDecl field = data.FieldsToInitInMapInit[i];
                if (field.GetInit() == null)
                {
                    continue;
                }

                AFieldLvalue   lvalue     = new AFieldLvalue(new TIdentifier(field.GetName().Text));
                AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), lvalue, field.GetInit());

                data.ExpTypes[assignment]    =
                    data.LvalueTypes[lvalue] = field.GetType();
                data.FieldLinks[lvalue]      = field;

                block.GetStatements().Insert(0, new AExpStm(new TSemicolon(";"), assignment));
            }
            block.RemoveChild(mainEntryFieldInitBlock);
            block.GetStatements().Insert(0, mainEntryFieldInitBlock);
        }
Exemple #11
0
 public override void CaseABooleanConstExp(ABooleanConstExp node)
 {
     Write(node.GetBool() is ATrueBool ? "true" : "false");
 }
Exemple #12
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());
 }