public override void OutAIfThenStm(AIfThenStm node)
 {
     bool value;
     if (IsConst(node.GetCondition(), out value))
     {
         if (value)
         {
             node.ReplaceBy(node.GetBody());
         }
         else
         {
             node.Parent().RemoveChild(node);
         }
         changedSomething = true;
     }
 }
        public override void OutAAProgram(AAProgram node)
        {
            if (strings.Count == 0)
                return;

            //Obfuscate all strings
            List<string> obfuscated = new List<string>();
            foreach (AStringConstExp stringConstExp in strings)
            {
                TStringLiteral token = stringConstExp.GetStringLiteral();
                string s = token.Text.Substring(1, token.Text.Length - 2);
                obfuscated.Add(Obfuscate(s));
            }

            //Set invokes instead of string constants, and move varaiabes down
            List<AFieldDecl> ignoredFields = new List<AFieldDecl>();
            List<AFieldDecl> moveFieldsIn = new List<AFieldDecl>();
            Dictionary<AFieldDecl, AMethodDecl> fieldMethods = new Dictionary<AFieldDecl, AMethodDecl>();
            for (int i = 0; i < strings.Count; i++)
            {
                AStringConstExp stringExp = strings[i];
                Token token = stringExp.GetStringLiteral();
                bool inDeobfuscator = Util.GetAncestor<AMethodDecl>(stringExp) == finalTrans.data.DeobfuscateMethod;

                if (inDeobfuscator)
                {
                    AFieldDecl field = finalTrans.data.UnobfuscatedStrings[stringExp];

                    AStringConstExp newStringConst = new AStringConstExp(stringExp.GetStringLiteral());

                    field.SetInit(newStringConst);

                    AFieldLvalue fieldRef = new AFieldLvalue(new TIdentifier(field.GetName().Text, token.Line, token.Pos));
                    finalTrans.data.FieldLinks[fieldRef] = field;

                    stringExp.ReplaceBy(new ALvalueExp(fieldRef));
                }
                else
                {
                    AFieldDecl field;
                    if (!finalTrans.data.ObfuscatedStrings.ContainsKey(stringExp))
                    {
                        int line = -finalTrans.data.ObfuscatedStrings.Count - 1;
                        field = new AFieldDecl(new APublicVisibilityModifier(), null, new TConst("const", line, 0),
                                                new ANamedType(new TIdentifier("string", line, 1), null),
                                                new TIdentifier("Galaxy_pp_stringO" +
                                                                finalTrans.data.ObfuscatedStrings.Count), null);
                        //If the strings are the same - point them to same field
                        bool newField = true;
                        foreach (AStringConstExp oldStringConstExp in finalTrans.data.ObfuscatedStrings.Keys)
                        {
                            if (stringExp.GetStringLiteral().Text == oldStringConstExp.GetStringLiteral().Text)
                            {
                                field = finalTrans.data.ObfuscatedStrings[oldStringConstExp];
                                newField = false;
                                break;
                            }
                        }
                        if (newField)
                        {
                            AASourceFile file = (AASourceFile)finalTrans.data.DeobfuscateMethod.Parent();
                            file.GetDecl().Insert(file.GetDecl().IndexOf(finalTrans.data.DeobfuscateMethod) + 1, field);

                            finalTrans.data.Fields.Add(new SharedData.DeclItem<AFieldDecl>(file, field));
                        }
                        finalTrans.data.ObfuscatedStrings.Add(stringExp, field);

                    }
                    field = finalTrans.data.ObfuscatedStrings[stringExp];
                    string obfuscatedString = obfuscated[i];

                    ASimpleInvokeExp invoke = new ASimpleInvokeExp();
                    invoke.SetName(new TIdentifier(finalTrans.data.DeobfuscateMethod.GetName().Text,
                                                   stringExp.GetStringLiteral().Line,
                                                   stringExp.GetStringLiteral().Pos));

                    AStringConstExp newStringConst =
                        new AStringConstExp(new TStringLiteral("\"" + obfuscatedString + "\""));
                    invoke.GetArgs().Add(newStringConst);
                    finalTrans.data.SimpleMethodLinks[invoke] = finalTrans.data.DeobfuscateMethod;

                    if (Util.GetAncestor<PStm>(stringExp) == null && false)
                    {
                        ignoredFields.Add(field);
                        /*if (Util.GetAncestor<ASimpleInvokeExp>(stringExp) == null)
                            stringExp.ReplaceBy(invoke);*/
                        //Add obfuscate call to this location);
                        continue;
                        /*ASimpleInvokeExp invoke = new ASimpleInvokeExp();
                        invoke.SetName(new TIdentifier(finalTrans.data.DeobfuscateMethod.GetName().Text,
                                                       stringExp.GetStringLiteral().Line,
                                                       stringExp.GetStringLiteral().Pos));

                        AStringConstExp newStringConst =
                            new AStringConstExp(new TStringLiteral("\"" + obfuscatedString + "\""));
                        invoke.GetArgs().Add(newStringConst);
                        stringExp.ReplaceBy(invoke);

                        finalTrans.data.SimpleMethodLinks[invoke] = finalTrans.data.DeobfuscateMethod;
                        continue;*/
                    }

                    if (field.GetInit() == null)
                    {
                        /*field.SetInit(invoke);
                        field.SetConst(null);*/

                        if (
                            stringExp.GetStringLiteral().Text.Remove(0, 1).Substring(0,
                                                                                     stringExp.GetStringLiteral().Text.
                                                                                         Length - 2) == "")
                        {
                            //Make method
                            /*
                                string <field>Method()
                                {
                                    return "";
                                }
                             *
                             */
                            ANullExp nullExp = new ANullExp();

                            field.SetInit(nullExp);
                            field.SetConst(null);

                            AStringConstExp stringConst = new AStringConstExp(new TStringLiteral("\"\""));
                            AMethodDecl method = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                                 new ANamedType(new TIdentifier("string"), null),
                                                                 new TIdentifier("Get" + field.GetName()),
                                                                 new ArrayList(),
                                                                 new AABlock(
                                                                     new ArrayList()
                                                                         {
                                                                             new AValueReturnStm(new TReturn("return"),
                                                                                                 stringConst)
                                                                         },
                                                                     new TRBrace("}")));

                            AASourceFile pFile = (AASourceFile)field.Parent();
                            pFile.GetDecl().Insert(pFile.GetDecl().IndexOf(field) + 1, method);
                            finalTrans.data.ExpTypes[stringConst] = new ANamedType(new TIdentifier("string"), null);
                            finalTrans.data.ExpTypes[nullExp] = new ANamedType(new TIdentifier("null"), null);

                            fieldMethods[field] = method;
                        }
                        else
                        {
                            //Make method
                            /*
                                string <field>Method()
                                {
                                    if (field == null)
                                    {
                                        field = Invoke;
                                    }
                                    if (field == null)
                                    {
                                        return Invoke;
                                    }
                                    return field;
                                }
                             */

                            ANullExp nullExp1 = new ANullExp();

                            field.SetInit(nullExp1);
                            field.SetConst(null);

                            ANullExp nullExp2 = new ANullExp();
                            AFieldLvalue fieldRef1 = new AFieldLvalue(new TIdentifier(field.GetName().Text));
                            AFieldLvalue fieldRef2 = new AFieldLvalue(new TIdentifier(field.GetName().Text));
                            AFieldLvalue fieldRef3 = new AFieldLvalue(new TIdentifier(field.GetName().Text));
                            ALvalueExp fieldRef1Exp = new ALvalueExp(fieldRef1);
                            ALvalueExp fieldRef3Exp = new ALvalueExp(fieldRef3);
                            ABinopExp binop1 = new ABinopExp(fieldRef1Exp, new AEqBinop(new TEq("==")), nullExp2);
                            AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), fieldRef2, invoke);

                            AIfThenStm ifStm1 = new AIfThenStm(new TLParen("("), binop1,
                                                              new ABlockStm(new TLBrace("{"),
                                                                            new AABlock(
                                                                                new ArrayList()
                                                                                    {
                                                                                        new AExpStm(new TSemicolon(";"),
                                                                                                    assignment)
                                                                                    },
                                                                                new TRBrace("}"))));

                            /*ANullExp nullExp3 = new ANullExp();
                            AFieldLvalue fieldRef4 = new AFieldLvalue(new TIdentifier(field.GetName().Text));
                            ALvalueExp fieldRef4Exp = new ALvalueExp(fieldRef4);
                            AStringConstExp invokeArgClone =
                                new AStringConstExp(new TStringLiteral("\"" + obfuscatedString + "\""));
                            ASimpleInvokeExp invokeClone = new ASimpleInvokeExp(new TIdentifier(invoke.GetName().Text),
                                                                                new ArrayList() { invokeArgClone });
                            finalTrans.data.SimpleMethodLinks[invokeClone] = finalTrans.data.DeobfuscateMethod;
                            ABinopExp binop2 = new ABinopExp(fieldRef4Exp, new AEqBinop(new TEq("==")), nullExp3);

                            AIfThenStm ifStm2 = new AIfThenStm(new TLParen("("), binop2,
                                                              new ABlockStm(new TLBrace("{"),
                                                                            new AABlock(
                                                                                new ArrayList()
                                                                                    {
                                                                                        new AValueReturnStm(new TReturn("return"), invokeClone)
                                                                                    },
                                                                                new TRBrace("}"))));*/

                            AMethodDecl method = new AMethodDecl(new APublicVisibilityModifier(), null, null, null, null, null,
                                                                 new ANamedType(new TIdentifier("string"), null),
                                                                 new TIdentifier("Get" + field.GetName()),
                                                                 new ArrayList(),
                                                                 new AABlock(
                                                                     new ArrayList()
                                                                         {
                                                                             ifStm1,
                                                                             //ifStm2,
                                                                             new AValueReturnStm(new TReturn("return"),
                                                                                                 fieldRef3Exp)
                                                                         },
                                                                     new TRBrace("}")));
                            AASourceFile pFile = (AASourceFile) field.Parent();
                            pFile.GetDecl().Insert(pFile.GetDecl().IndexOf(field) + 1, method);

                            finalTrans.data.FieldLinks[fieldRef1] =
                                finalTrans.data.FieldLinks[fieldRef2] =
                                finalTrans.data.FieldLinks[fieldRef3] =
                                /*finalTrans.data.FieldLinks[fieldRef4] = */field;
                            finalTrans.data.LvalueTypes[fieldRef1] =
                                finalTrans.data.LvalueTypes[fieldRef2] =
                                finalTrans.data.LvalueTypes[fieldRef3] =
                                //finalTrans.data.LvalueTypes[fieldRef4] =
                                finalTrans.data.ExpTypes[fieldRef1Exp] =
                                finalTrans.data.ExpTypes[fieldRef3Exp] =
                                //finalTrans.data.ExpTypes[fieldRef4Exp] =
                                finalTrans.data.ExpTypes[assignment] = field.GetType();

                            finalTrans.data.ExpTypes[nullExp1] =
                                finalTrans.data.ExpTypes[nullExp2] =
                                /*finalTrans.data.ExpTypes[nullExp3] =*/ new ANamedType(new TIdentifier("null"), null);
                            finalTrans.data.ExpTypes[binop1] =
                                /*finalTrans.data.ExpTypes[binop2] = */new ANamedType(new TIdentifier("bool"), null);

                            fieldMethods[field] = method;
                        }

                        /* AFieldLvalue fieldRef = new AFieldLvalue(new TIdentifier(field.GetName().Text, token.Line, token.Pos));
                         finalTrans.data.FieldLinks[fieldRef] = field;*/

                        //stringExp.ReplaceBy(new ALvalueExp(fieldRef));
                    }
                    ASimpleInvokeExp invoke2 =
                            new ASimpleInvokeExp(new TIdentifier(fieldMethods[field].GetName().Text), new ArrayList());
                    finalTrans.data.SimpleMethodLinks[invoke2] = fieldMethods[field];
                    stringExp.ReplaceBy(invoke2);

                    //If we are in a field, move it in
                    if (Util.GetAncestor<AFieldDecl>(invoke2) != null)
                        moveFieldsIn.Add(Util.GetAncestor<AFieldDecl>(invoke2));
                }
            }

            foreach (AFieldDecl field in finalTrans.data.ObfuscationFields)
            {
                if (field.GetInit() == null && field.Parent() != null)
                {
                    field.Parent().RemoveChild(field);
                }
            }

            //A constant field, or a field used by a constant field cannot be moved in
            List<AFieldDecl> constantFields = new List<AFieldDecl>();
            foreach (SharedData.DeclItem<AFieldDecl> field in finalTrans.data.Fields)
            {
                if (field.Decl.GetConst() != null)
                    constantFields.Add(field.Decl);
            }
            for (int i = 0; i < constantFields.Count; i++)
            {
                GetFieldLvalues lvalues = new GetFieldLvalues();
                constantFields[i].Apply(lvalues);
                foreach (AFieldLvalue lvalue in lvalues.Lvalues)
                {
                    AFieldDecl field = finalTrans.data.FieldLinks[lvalue];
                    if (!constantFields.Contains(field))
                        constantFields.Add(field);
                }
            }
            moveFieldsIn.RemoveAll(constantFields.Contains);
            Dictionary<AFieldDecl, List<AFieldDecl>> dependancies = new Dictionary<AFieldDecl, List<AFieldDecl>>();
            //Order the fields so any dependancies are instansiated first
            foreach (AFieldDecl field in moveFieldsIn)
            {
                dependancies.Add(field, new List<AFieldDecl>());
                GetFieldLvalues lvalues = new GetFieldLvalues();
                field.Apply(lvalues);
                foreach (AFieldLvalue lvalue in lvalues.Lvalues)
                {
                    AFieldDecl dependancy = finalTrans.data.FieldLinks[lvalue];
                    if (!dependancies[field].Contains(dependancy))
                        dependancies[field].Add(dependancy);
                }
            }
            List<PStm> newStatements = new List<PStm>();
            while (dependancies.Keys.Count > 0)
            {
                AFieldDecl field = dependancies.FirstOrDefault(f1 => f1.Value.Count == 0).Key ??
                                   dependancies.Keys.First(f => true);

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

                newStatements.Add(new AExpStm(new TSemicolon(";"), assignment));

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

                foreach (KeyValuePair<AFieldDecl, List<AFieldDecl>> dependancy in dependancies)
                {
                    if (dependancy.Value.Contains(field))
                        dependancy.Value.Remove(field);
                }

                dependancies.Remove(field);
            }
            AABlock initBody = (AABlock) finalTrans.mainEntry.GetBlock();
            for (int i = newStatements.Count - 1; i >= 0; i--)
            {
                initBody.GetStatements().Insert(0, newStatements[i]);
            }
        }
 public virtual void OutAIfThenStm(AIfThenStm node)
 {
     DefaultOut(node);
 }
 /*public override void CaseAValueReturnStm(AValueReturnStm node)
 {
     GetNode(node);
     PStm stm = GetNext(whileStm);
     if (stm != null)
         graphNode.AddSucc(GetNode(stm));
 }*/
 public override void CaseAIfThenStm(AIfThenStm node)
 {
     //Create graph node
     Node graphNode = GetNode(node);
     //Process inner if
     node.GetBody().Apply(this);
     //Add successor and predessors
     PStm stm = GetFirst(node.GetBody());
     if (stm != null)
         graphNode.AddSucc(GetNode(stm));
     stm = GetNext(node);
     if (stm != null)
     {
         Node nextGraphNode = GetNode(stm);
         graphNode.AddSucc(nextGraphNode);
         List<PStm> stms = GetLast(node.GetBody());
         foreach (PStm pStm in stms)
         {
             nextGraphNode.AddPred(GetNode(pStm));
         }
     }
 }
 public virtual void InAIfThenStm(AIfThenStm node)
 {
     DefaultIn(node);
 }
 public virtual void CaseAIfThenStm(AIfThenStm node)
 {
     DefaultCase(node);
 }
 public override void CaseAIfThenStm(AIfThenStm node)
 {
     node.GetBody().Apply(this);
 }
 public override void CaseAIfThenStm(AIfThenStm node)
 {
     InAIfThenStm(node);
     if (node.GetBody() != null)
     {
         node.GetBody().Apply(this);
     }
     if (node.GetCondition() != null)
     {
         node.GetCondition().Apply(this);
     }
     if (node.GetToken() != null)
     {
         node.GetToken().Apply(this);
     }
     OutAIfThenStm(node);
 }
        public override void OutAIfThenStm(AIfThenStm node)
        {
            //Check that the type of the exp is not an array
            PType type = data.ExpTypes[node.GetCondition()];
            bool isValid = true;
            if (type is AArrayTempType)
                isValid = false;
            else if (type is ANamedType)
            {
                ANamedType aType = (ANamedType)type;
                if (data.StructTypeLinks.ContainsKey(aType))
                {
                    isValid = false;
                }
            }
            if (!isValid)
            {
                errors.Add(new ErrorCollection.Error(node.GetToken(), LocRM.GetString("ErrorText134")));
            }

            base.OutAIfThenStm(node);
        }
 public override void CaseAIfThenStm(AIfThenStm node)
 {
     //Skip the block.. we will need something below anyway
 }
        public override void CaseASwitchStm(ASwitchStm node)
        {
            node.GetTest().Apply(this);

            AALocalDecl fallThroughDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                          new ANamedType(new TIdentifier("bool"), null),
                                                          new TIdentifier(MakeUniqueLocalName(node, "switchFallThrough")),
                                                          new ABooleanConstExp(new AFalseBool()));
            AALocalDecl continueDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                          new ANamedType(new TIdentifier("bool"), null),
                                                          new TIdentifier(MakeUniqueLocalName(node, "switchContinue")),
                                                          new ABooleanConstExp(new ATrueBool()));

            AALocalDecl testVar = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null,
                                                          Util.MakeClone(data.ExpTypes[node.GetTest()], data),
                                                          new TIdentifier(MakeUniqueLocalName(node, "switchTestVar")),
                                                          node.GetTest());

            AABlock bigBlock = new AABlock();
            //AABlock previousBlock = bigBlock;
            if (node.GetCases().Count > 0)
            {
                List<SwitchCaseData> switchCaseDatas = new List<SwitchCaseData>();
                //Join cases without a body
                for (int i = node.GetCases().Count - 1; i >= 0; i--)
                {
                    ASwitchCaseStm caseStm = (ASwitchCaseStm) node.GetCases()[i];
                    SwitchCaseData caseData = new SwitchCaseData();
                    caseData.Block = (AABlock) caseStm.GetBlock();
                    if (caseStm.GetType() is ACaseSwitchCaseType)
                        caseData.Tests.Add(((ACaseSwitchCaseType)caseStm.GetType()).GetExp());
                    else
                        caseData.ContainsDefault = true;

                    caseData.IsLast = switchCaseDatas.Count == 0;

                    if (switchCaseDatas.Count == 0 || caseData.Block.GetStatements().Count > 0)
                    {
                        switchCaseDatas.Insert(0, caseData);
                        continue;
                    }
                    switchCaseDatas[0].Tests.AddRange(caseData.Tests);
                }
                for (int i = switchCaseDatas.Count - 1; i >= 0; i--)
                {
                    switchCaseDatas[i].ContainsFallthrough = CanFallthrough(switchCaseDatas[i].Block, out switchCaseDatas[i].HasBreaks, out switchCaseDatas[i].RequiresWhile);
                    if (i == switchCaseDatas.Count - 1)
                        continue;

                    switchCaseDatas[i + 1].TargetForFallThrough = switchCaseDatas[i].ContainsFallthrough;
                    switchCaseDatas[i].RequiresContinue = !switchCaseDatas[i].ContainsFallthrough &&
                                                          (switchCaseDatas[i + 1].RequiresContinue ||
                                                           switchCaseDatas[i + 1].ContainsFallthrough);
                }

                AABlock previousBlock = bigBlock;
                //Make code for specific case
                foreach (SwitchCaseData switchCase in switchCaseDatas)
                {
                    List<PExp> tests = new List<PExp>();
                    AABlock nextBlock;
                    if (switchCase.TargetForFallThrough)
                    {//Add if (continueSwitch) {}
                        ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(continueDecl.GetName().Text));
                        ALvalueExp test = new ALvalueExp(lvalue);
                        nextBlock = new AABlock();
                        AIfThenStm ifStm = new AIfThenStm(new TLParen("("), test, new ABlockStm(new TLBrace("{"), nextBlock));
                        previousBlock.GetStatements().Add(ifStm);
                        previousBlock = nextBlock;

                        data.LocalLinks[lvalue] = continueDecl;
                        data.LvalueTypes[lvalue] = data.ExpTypes[test] = continueDecl.GetType();

                        //First test in next if: if (fallThrough || ...
                        lvalue = new ALocalLvalue(new TIdentifier(fallThroughDecl.GetName().Text));
                        test = new ALvalueExp(lvalue);
                        tests.Add(test);

                        data.LocalLinks[lvalue] = fallThroughDecl;
                        data.LvalueTypes[lvalue] = data.ExpTypes[test] = fallThroughDecl.GetType();
                    }
                    //Make code for the test in the if
                    foreach (PExp exp in switchCase.Tests)
                    {
                        ALocalLvalue leftSide = new ALocalLvalue(new TIdentifier(testVar.GetName().Text));
                        ALvalueExp lvalueExp = new ALvalueExp(leftSide);
                        ABinopExp test = new ABinopExp(lvalueExp, new AEqBinop(new TEq("==")), exp);
                        tests.Add(test);

                        data.LocalLinks[leftSide] = testVar;
                        data.LvalueTypes[leftSide] = data.ExpTypes[lvalueExp] = testVar.GetType();
                        data.ExpTypes[test] = new ANamedType(new TIdentifier("bool"), null);
                    }

                    if (switchCase.ContainsDefault)
                    {
                        ABooleanConstExp test = new ABooleanConstExp(new ATrueBool());
                        tests.Add(test);

                        data.ExpTypes[test] = new ANamedType(new TIdentifier("bool"), null);
                    }

                    PExp finalTest = tests[0];
                    tests.RemoveAt(0);
                    foreach (PExp exp in tests)
                    {
                        finalTest = new ABinopExp(finalTest, new ALazyOrBinop(new TOrOr("||")), exp);

                        data.ExpTypes[finalTest] = new ANamedType(new TIdentifier("bool"), null);
                    }

                    //Transform breaks into assignments

                    //If we can fallthrough, and there are breaks, encase in a while stm
                    AABlock testBlock = switchCase.Block;
                    if (switchCase.RequiresWhile)
                    {
                        AABlock newBlock = new AABlock();
                        PExp whileTest = new ABooleanConstExp(new ATrueBool());
                        AWhileStm whileStm = new AWhileStm(new TLParen("("), whileTest, new ABlockStm(new TLBrace("{"), switchCase.Block));
                        newBlock.GetStatements().Add(whileStm);
                        switchCase.Block = newBlock;
                    }

                    TransformBreaks(testBlock, switchCase, continueDecl, fallThroughDecl);

                    if (switchCase.ContainsFallthrough && !switchCase.TargetForFallThrough)
                    {//Add fallthrough = true;
                        ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(fallThroughDecl.GetName().Text));
                        ABooleanConstExp rightSide = new ABooleanConstExp(new ATrueBool());
                        AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), lvalue, rightSide);
                        testBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment));

                        data.LocalLinks[lvalue] = fallThroughDecl;
                        data.LvalueTypes[lvalue] = data.ExpTypes[rightSide] = data.ExpTypes[assignment] = fallThroughDecl.GetType();
                    }

                    if (switchCase.RequiresWhile)
                    {//Add break at the end of the while
                        testBlock.GetStatements().Add(new ABreakStm(new TBreak("break")));
                    }

                    //Make if
                    PStm finalIfStm;
                    if (finalTest is ABooleanConstExp)
                    {//Final if is if(true). dont add it.
                        finalIfStm = new ABlockStm(new TLBrace("{"), switchCase.Block);
                        nextBlock = new AABlock();
                    }
                    else if (switchCase.IsLast || switchCase.ContainsFallthrough)
                    {//One armed if
                        finalIfStm = new AIfThenStm(new TLParen("("), finalTest,
                                                    new ABlockStm(new TLBrace("{"), switchCase.Block));
                        nextBlock = bigBlock;
                    }
                    else
                    {//Two armed if
                        nextBlock = new AABlock();
                        finalIfStm = new AIfThenElseStm(new TLParen("("), finalTest,
                                                        new ABlockStm(new TLBrace("{"), switchCase.Block),
                                                        new ABlockStm(new TLBrace("{"), nextBlock));
                    }

                    previousBlock.GetStatements().Add(finalIfStm);
                    previousBlock = nextBlock;
                }

                //If needed, add fallThroughDecl and continueDecl
                data.Locals.Add(bigBlock, new List<AALocalDecl>());
                if (data.LocalLinks.Values.Contains(fallThroughDecl))
                {
                    bigBlock.GetStatements().Insert(0, new ALocalDeclStm(new TSemicolon(";"), fallThroughDecl));
                    data.Locals[bigBlock].Add(fallThroughDecl);
                }
                if (data.LocalLinks.Values.Contains(continueDecl))
                {
                    bigBlock.GetStatements().Insert(0, new ALocalDeclStm(new TSemicolon(";"), continueDecl));
                    data.Locals[bigBlock].Add(continueDecl);
                }
                bigBlock.GetStatements().Insert(0, new ALocalDeclStm(new TSemicolon(";"), testVar));
                data.Locals[bigBlock].Add(testVar);

                node.ReplaceBy(new ABlockStm(new TLBrace("{"), bigBlock));
                bigBlock.Apply(this);
            }
        }
 public override void CaseAIfThenStm(AIfThenStm node)
 {
     Write("if(");
     node.GetCondition().Apply(this);
     Write(")\n");
     node.GetBody().Apply(this);
 }
 ArrayList New306()
 {
     ArrayList nodeList = new ArrayList();
     ArrayList nodeArrayList5 = (ArrayList) Pop();
     ArrayList nodeArrayList4 = (ArrayList) Pop();
     ArrayList nodeArrayList3 = (ArrayList) Pop();
     ArrayList nodeArrayList2 = (ArrayList) Pop();
     ArrayList nodeArrayList1 = (ArrayList) Pop();
     TLParen tlparenNode2 = (TLParen)nodeArrayList2[0];
     PExp pexpNode3 = (PExp)nodeArrayList3[0];
     PStm pstmNode4 = (PStm)nodeArrayList5[0];
     AIfThenStm pstmNode1 = new AIfThenStm (
       tlparenNode2,
       pexpNode3,
       pstmNode4
     );
     nodeList.Add(pstmNode1);
     return nodeList;
 }
        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;
        }
            public override void CaseAWhileStm(AWhileStm node)
            {
                /*
                 * while(...){...}
                 * ->
                 * while(...){...}
                 * if (hasMethodReturnedVar)
                 * {
                 *      break;
                 * }
                 */
                if (neededWhile)
                {
                    ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(hasMethodReturnedVar.GetName().Text));
                    data.LvalueTypes[lvalue] = hasMethodReturnedVar.GetType();
                    data.LocalLinks[lvalue] = hasMethodReturnedVar;
                    ALvalueExp exp = new ALvalueExp(lvalue);
                    data.ExpTypes[exp] = hasMethodReturnedVar.GetType();

                    AABlock ifBlock = new AABlock();
                    ifBlock.GetStatements().Add(new ABreakStm(new TBreak("break")));
                    ABlockStm ifBlockStm = new ABlockStm(new TLBrace("{"), ifBlock);

                    AIfThenStm ifStm = new AIfThenStm(new TLParen("("), exp, ifBlockStm);

                    AABlock pBlock = (AABlock) node.Parent();
                    pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(node) + 1, ifStm);
                }
                node.GetBody().Apply(this);
            }