public override void OutAIfThenElseStm(AIfThenElseStm node) { bool value; if (IsConst(node.GetCondition(), out value)) { if (value) { node.ReplaceBy(node.GetThenBody()); } else { node.ReplaceBy(node.GetElseBody()); } changedSomething = true; } }
public virtual void OutAIfThenElseStm(AIfThenElseStm node) { DefaultOut(node); }
public override void CaseAIfThenElseStm(AIfThenElseStm node) { //Create graph node Node graphNode = GetNode(node); //Process inner if node.GetThenBody().Apply(this); node.GetElseBody().Apply(this); //Add successor and predessors PStm stm = GetFirst(node.GetThenBody()); if (stm != null) graphNode.AddSucc(GetNode(stm)); stm = GetFirst(node.GetElseBody()); if (stm != null) graphNode.AddSucc(GetNode(stm)); stm = GetNext(node); if (stm != null) { Node nextGraphNode = GetNode(stm); List<PStm> stms = GetLast(node.GetThenBody()); if (stms.Count > 0) foreach (PStm pStm in stms) { nextGraphNode.AddPred(GetNode(pStm)); } else graphNode.AddSucc(nextGraphNode); stms = GetLast(node.GetElseBody()); if (stms.Count > 0) foreach (PStm pStm in stms) { nextGraphNode.AddPred(GetNode(pStm)); } else graphNode.AddSucc(nextGraphNode); } }
public virtual void InAIfThenElseStm(AIfThenElseStm node) { DefaultIn(node); }
public virtual void CaseAIfThenElseStm(AIfThenElseStm node) { DefaultCase(node); }
public override void CaseAIfThenElseStm(AIfThenElseStm node) { node.GetThenBody().Apply(this); node.GetElseBody().Apply(this); }
public override void CaseAIfThenElseStm(AIfThenElseStm node) { InAIfThenElseStm(node); if (node.GetElseBody() != null) { node.GetElseBody().Apply(this); } if (node.GetThenBody() != null) { node.GetThenBody().Apply(this); } if (node.GetCondition() != null) { node.GetCondition().Apply(this); } if (node.GetToken() != null) { node.GetToken().Apply(this); } OutAIfThenElseStm(node); }
public override void OutAIfThenElseStm(AIfThenElseStm 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.OutAIfThenElseStm(node); }
public override void CaseAIfThenElseStm(AIfThenElseStm node) { if (Returned) return; node.GetThenBody().Apply(this); bool returned = Returned; Returned = false; node.GetElseBody().Apply(this); Returned &= returned; }
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 CaseAIfThenElseStm(AIfThenElseStm node) { Write("if("); node.GetCondition().Apply(this); Write(")\n"); node.GetThenBody().Apply(this); Write("else\n"); node.GetElseBody().Apply(this); }
ArrayList New308() { ArrayList nodeList = new ArrayList(); ArrayList nodeArrayList7 = (ArrayList) Pop(); ArrayList nodeArrayList6 = (ArrayList) Pop(); 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]; PStm pstmNode5 = (PStm)nodeArrayList7[0]; AIfThenElseStm pstmNode1 = new AIfThenElseStm ( tlparenNode2, pexpNode3, pstmNode4, pstmNode5 ); nodeList.Add(pstmNode1); return nodeList; }
public override void CaseADelegateInvokeExp(ADelegateInvokeExp node) { //Build a list of the possible methods AASourceFile currentFile = Util.GetAncestor<AASourceFile>(node); List<AMethodDecl> methods = new List<AMethodDecl>(); ANamedType type = (ANamedType) finalTrans.data.ExpTypes[node.GetReceiver()]; AMethodDecl delegateMethod = finalTrans.data.DelegateTypeLinks[type]; foreach (KeyValuePair<ADelegateExp, AMethodDecl> delegateCreationPair in finalTrans.data.DelegateCreationMethod) { if (TypeChecking.Assignable(delegateCreationPair.Key.GetType(), type)) { if (!methods.Contains(delegateCreationPair.Value)) methods.Add(delegateCreationPair.Value); } } MoveMethodDeclsOut mover; if (methods.Count == 0) { //Can only remove it if the return value is unused if (!(node.Parent() is AExpStm)) { finalTrans.errors.Add(new ErrorCollection.Error(node.GetToken(), currentFile, LocRM.GetString("Delegates_Text1"))); throw new ParserException(node.GetToken(), "Delegates.OutADelegateInvokeExp"); } mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data); foreach (Node arg in node.GetArgs()) { arg.Apply(mover); } node.Parent().Parent().RemoveChild(node.Parent()); foreach (PStm stm in mover.NewStatements) { stm.Apply(this); } return; } if (methods.Count == 1) { ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList()); while (node.GetArgs().Count > 0) { invoke.GetArgs().Add(node.GetArgs()[0]); } //If we have a struct method, add the pointer from the delegate if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(methods[0]))) { AStructDecl targetStr = finalTrans.data.StructMethods.First(str => str.Value.Contains(methods[0])).Key; AMethodDecl getPointerDecl = GetPointerMethod(targetStr.GetDimention() != null); ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList(){node.GetReceiver()}); invoke.GetArgs().Add(getPointerInvoke); finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl; finalTrans.data.ExpTypes[getPointerInvoke] = getPointerDecl.GetReturnType(); } finalTrans.data.SimpleMethodLinks[invoke] = methods[0]; finalTrans.data.ExpTypes[invoke] = methods[0].GetReturnType(); node.ReplaceBy(invoke); return; } //Multiple methods. Make /* * <Methods moved out from reciever> * string delegate = GetMethodPart(<reciever>); * if (delegate == "...") * { * Foo(...); * } * else if (delegate == "...") * { * Bar(..., GetPointerPart(<reciever>); * } * else if(...) * ... * else * { * UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("[<file>:<line>]: No methods matched delegate.")); * int i = 1/0; * return; * } * */ AABlock block = new AABlock(new ArrayList(), new TRBrace("}")); mover = new MoveMethodDeclsOut("delegateVar", finalTrans.data); node.GetReceiver().Apply(mover); AMethodDecl methodPartMethod = GetMethodMethod(); ASimpleInvokeExp methodPartInvoke = new ASimpleInvokeExp(new TIdentifier("GetMethodPart"), new ArrayList() { Util.MakeClone(node.GetReceiver(), finalTrans.data) }); finalTrans.data.SimpleMethodLinks[methodPartInvoke] = methodPartMethod; finalTrans.data.ExpTypes[methodPartInvoke] = methodPartMethod.GetReturnType(); AALocalDecl methodPartDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, new ANamedType(new TIdentifier("string"), null), new TIdentifier("methodPart"), methodPartInvoke); block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), methodPartDecl)); //If the invoke's return value is used, get the lvalue PLvalue leftSide; if (node.Parent() is AALocalDecl) { leftSide = new ALocalLvalue(new TIdentifier("renameMe")); finalTrans.data.LocalLinks[(ALocalLvalue) leftSide] = (AALocalDecl) node.Parent(); finalTrans.data.LvalueTypes[leftSide] = new ANamedType(new TIdentifier("string"), null); PStm pStm = Util.GetAncestor<PStm>(node); AABlock pBlock = (AABlock) pStm.Parent(); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm) + 1, new ABlockStm(new TLBrace("{"), block)); node.Parent().RemoveChild(node); } else if (node.Parent() is AAssignmentExp) { AAssignmentExp assignExp = (AAssignmentExp) node.Parent(); leftSide = assignExp.GetLvalue(); leftSide.Apply(mover); PStm pStm = Util.GetAncestor<PStm>(node); pStm.ReplaceBy(new ABlockStm(new TLBrace("{"), block)); } else if (node.Parent() is AExpStm) { //No assignments needed leftSide = null; node.Parent().ReplaceBy(new ABlockStm(new TLBrace("{"), block)); } else { //Create a new local AALocalDecl leftSideDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(delegateMethod.GetReturnType(), finalTrans.data), new TIdentifier("delegateVar"), null); ALocalLvalue leftSideLink = new ALocalLvalue(new TIdentifier("delegateVar")); ALvalueExp leftSideLinkExp = new ALvalueExp(leftSideLink); PStm pStm = Util.GetAncestor<PStm>(node); AABlock pBlock = (AABlock)pStm.Parent(); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ABlockStm(new TLBrace("{"), block)); node.ReplaceBy(leftSideLinkExp); finalTrans.data.LocalLinks[leftSideLink] = leftSideDecl; finalTrans.data.LvalueTypes[leftSideLink] = finalTrans.data.ExpTypes[leftSideLinkExp] = leftSideDecl.GetType(); leftSide = leftSideLink; block.GetStatements().Add(new ALocalDeclStm(new TSemicolon(";"), leftSideDecl)); } ABlockStm elseBranch; //Make final else branch /* { * UIDisplayMessage(PlayerGroupAll(), c_messageAreaDebug, StringToText("<file>[<line>, <pos>]: No methods matched delegate.")); * IntToString(1/0); * return; * } */ { AABlock innerBlock = new AABlock(new ArrayList(), new TRBrace("}")); ASimpleInvokeExp playerGroupInvoke = new ASimpleInvokeExp(new TIdentifier("PlayerGroupAll"), new ArrayList()); AFieldLvalue messageAreaLink = new AFieldLvalue(new TIdentifier("c_messageAreaDebug")); ALvalueExp messageAreaLinkExp = new ALvalueExp(messageAreaLink); AStringConstExp stringConst = new AStringConstExp( new TStringLiteral("\"" + currentFile.GetName().Text.Replace('\\', '/') + "[" + node.GetToken().Line + ", " + node.GetToken().Pos + "]: Got a null delegate.\"")); ASimpleInvokeExp stringToTextInvoke = new ASimpleInvokeExp(new TIdentifier("StringToText"), new ArrayList() {stringConst}); ASimpleInvokeExp displayMessageInvoke = new ASimpleInvokeExp(new TIdentifier("UIDisplayMessage"), new ArrayList() { playerGroupInvoke, messageAreaLinkExp, stringToTextInvoke }); AIntConstExp intConst1 = new AIntConstExp(new TIntegerLiteral("1")); AIntConstExp intConst2 = new AIntConstExp(new TIntegerLiteral("0")); ABinopExp binop = new ABinopExp(intConst1, new ADivideBinop(new TDiv("/")), intConst2); ASimpleInvokeExp intToStringInvoke = new ASimpleInvokeExp(new TIdentifier("IntToString"), new ArrayList() {binop}); innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), displayMessageInvoke)); innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), intToStringInvoke)); //innerBlock.GetStatements().Add(new AVoidReturnStm(new TReturn("return"))); elseBranch = new ABlockStm(new TLBrace("{"), innerBlock); finalTrans.data.SimpleMethodLinks[playerGroupInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == playerGroupInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[stringToTextInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == stringToTextInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[displayMessageInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == displayMessageInvoke.GetName().Text); finalTrans.data.SimpleMethodLinks[intToStringInvoke] = finalTrans.data.Libraries.Methods.First(m => m.GetName().Text == intToStringInvoke.GetName().Text); finalTrans.data.FieldLinks[messageAreaLink] = finalTrans.data.Libraries.Fields.First(m => m.GetName().Text == messageAreaLink.GetName().Text); finalTrans.data.ExpTypes[playerGroupInvoke] = finalTrans.data.SimpleMethodLinks[playerGroupInvoke].GetReturnType(); finalTrans.data.LvalueTypes[messageAreaLink] = finalTrans.data.ExpTypes[messageAreaLinkExp] = finalTrans.data.FieldLinks[messageAreaLink].GetType(); finalTrans.data.ExpTypes[stringToTextInvoke] = finalTrans.data.SimpleMethodLinks[stringToTextInvoke].GetReturnType(); finalTrans.data.ExpTypes[stringConst] = finalTrans.data.ExpTypes[intToStringInvoke] = new ANamedType(new TIdentifier("string"), null); finalTrans.data.ExpTypes[displayMessageInvoke] = new AVoidType(); finalTrans.data.ExpTypes[intConst1] = finalTrans.data.ExpTypes[intConst2] = finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("int"), null); } foreach (AMethodDecl method in methods) { /* * if (delegate == "...") * { * Foo(...); * } * else if (delegate == "...") * { * Bar(..., GetPointerPart(<reciever>); * } * else if(...) * ... */ AABlock innerBlock = new AABlock(new ArrayList(), new TRBrace("}")); ASimpleInvokeExp invoke = new ASimpleInvokeExp(new TIdentifier(method.GetName().Text), new ArrayList()); for (int i = 0; i < node.GetArgs().Count; i++) { PExp arg = (PExp) node.GetArgs()[i]; invoke.GetArgs().Add(Util.MakeClone(arg, finalTrans.data)); } //If we have a struct method, add the pointer from the delegate if (finalTrans.data.StructMethods.Any(str => str.Value.Contains(method))) { AStructDecl targetStr = finalTrans.data.StructMethods.First(str => str.Value.Contains(method)).Key; AMethodDecl getPointerDecl = GetPointerMethod(targetStr.GetDimention() != null); ASimpleInvokeExp getPointerInvoke = new ASimpleInvokeExp(new TIdentifier("renameMe"), new ArrayList() { Util.MakeClone(node.GetReceiver(), data) }); invoke.GetArgs().Add(getPointerInvoke); finalTrans.data.SimpleMethodLinks[getPointerInvoke] = getPointerDecl; finalTrans.data.ExpTypes[getPointerInvoke] = getPointerDecl.GetReturnType(); } finalTrans.data.SimpleMethodLinks[invoke] = method; finalTrans.data.ExpTypes[invoke] = method.GetReturnType(); if (leftSide == null) { innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), invoke)); } else { AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), Util.MakeClone(leftSide, finalTrans.data), invoke); finalTrans.data.ExpTypes[assignment] = finalTrans.data.ExpTypes[invoke]; innerBlock.GetStatements().Add(new AExpStm(new TSemicolon(";"), assignment)); } ALocalLvalue methodPartLink = new ALocalLvalue(new TIdentifier("methodPart")); ALvalueExp methodPartLinkExp = new ALvalueExp(methodPartLink); AStringConstExp stringConst = new AStringConstExp(new TStringLiteral("\"" + GetName(method) + "\"")); finalTrans.data.LocalLinks[methodPartLink] = methodPartDecl; finalTrans.data.LvalueTypes[methodPartLink] = finalTrans.data.ExpTypes[methodPartLinkExp] = finalTrans.data.ExpTypes[stringConst] = new ANamedType(new TIdentifier("string"), null); ABinopExp binop = new ABinopExp(methodPartLinkExp, new AEqBinop(new TEq("==")), stringConst); finalTrans.data.ExpTypes[binop] = new ANamedType(new TIdentifier("bool"), null); AIfThenElseStm ifThenElse = new AIfThenElseStm(new TLParen("("), binop, new ABlockStm(new TLBrace("{"), innerBlock), elseBranch); elseBranch = new ABlockStm(new TLBrace("{"), new AABlock(new ArrayList() { ifThenElse }, new TRBrace("}"))); } block.GetStatements().Add(elseBranch); }
public override void OutAIfExp(AIfExp node) { //Transform to /* * var expIfVar; * if (<cond>) * { * expIfVar = <then>; * } * else * { * expIfVar = <else>; * } * ... expIfVar ... */ AALocalDecl expIfVarDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(data.ExpTypes[node], data), new TIdentifier("expIfVar"), null); ALocalLvalue thenExpIfVarLink = new ALocalLvalue(new TIdentifier("expIfVar")); ALocalLvalue elseExpIfVarLink = new ALocalLvalue(new TIdentifier("expIfVar")); ALocalLvalue usageExpIfVarLink = new ALocalLvalue(new TIdentifier("expIfVar")); ALvalueExp usageExpIfVarLinkExp = new ALvalueExp(usageExpIfVarLink); AAssignmentExp thenAssignment = new AAssignmentExp(new TAssign("="), thenExpIfVarLink, node.GetThen()); AAssignmentExp elseAssignment = new AAssignmentExp(new TAssign("="), elseExpIfVarLink, node.GetElse()); AIfThenElseStm ifStm = new AIfThenElseStm(new TLParen("("), node.GetCond(), new ABlockStm(new TLBrace("{"), new AABlock( new ArrayList() { new AExpStm(new TSemicolon(";"), thenAssignment) }, new TRBrace("}"))), new ABlockStm(new TLBrace("{"), new AABlock( new ArrayList() { new AExpStm(new TSemicolon(";"), elseAssignment) }, new TRBrace("}")))); data.LocalLinks[thenExpIfVarLink] = data.LocalLinks[elseExpIfVarLink] = data.LocalLinks[usageExpIfVarLink] = expIfVarDecl; data.LvalueTypes[thenExpIfVarLink] = data.LvalueTypes[elseExpIfVarLink] = data.LvalueTypes[usageExpIfVarLink] = data.ExpTypes[usageExpIfVarLinkExp] = data.ExpTypes[thenAssignment] = data.ExpTypes[elseAssignment] = expIfVarDecl.GetType(); PStm pStm = Util.GetAncestor<PStm>(node); AABlock pBlock = (AABlock) pStm.Parent(); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ALocalDeclStm(new TSemicolon(";"), expIfVarDecl)); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), ifStm); node.ReplaceBy(usageExpIfVarLinkExp); base.OutAIfExp(node); }