public override void CaseAAssignmentExp(AAssignmentExp node) { if (!(node.Parent() is AExpStm)) { PStm parentStm = Util.GetAncestor<PStm>(node); MoveMethodDeclsOut mover = new MoveMethodDeclsOut("multipleAssignmentsVar", finalTrans.data); node.GetLvalue().Apply(mover); PLvalue lvalue = Util.MakeClone(node.GetLvalue(), finalTrans.data); ALvalueExp exp = new ALvalueExp(lvalue); finalTrans.data.ExpTypes[exp] = finalTrans.data.LvalueTypes[lvalue]; node.ReplaceBy(exp); AExpStm stm = new AExpStm(new TSemicolon(";"), node); AABlock block = (AABlock) parentStm.Parent(); //block.GetStatements().Insert(block.GetStatements().IndexOf(parentStm), localDeclStm); block.GetStatements().Insert(block.GetStatements().IndexOf(parentStm), stm); //localDeclStm.Apply(this); stm.Apply(this); if (parentStm is AWhileStm && Util.IsAncestor(exp, ((AWhileStm)parentStm).GetCondition())) { AWhileStm aStm = (AWhileStm)parentStm; //Copy assignment before continues //Before each continue in the while, and at the end. //Add continue statement, if not present block = (AABlock)((ABlockStm)aStm.GetBody()).GetBlock(); if (block.GetStatements().Count == 0 || !(block.GetStatements()[block.GetStatements().Count - 1] is AContinueStm)) block.GetStatements().Add(new AContinueStm(new TContinue("continue"))); //Get all continue statements in the while ContinueFinder finder = new ContinueFinder(); block.Apply(finder); foreach (AContinueStm continueStm in finder.Continues) { stm = new AExpStm(new TSemicolon(";"), Util.MakeClone(node, finalTrans.data)); block.GetStatements().Insert(block.GetStatements().IndexOf(continueStm), stm); stm.Apply(this); } } return; } base.CaseAAssignmentExp(node); }
public override void OutAIncDecExp(AIncDecExp node) { PIncDecOp op = node.GetIncDecOp(); if (!Util.HasAncestor<AABlock>(node)) { Token token = null; if (op is APostDecIncDecOp) token = ((APostDecIncDecOp) op).GetToken(); else if (op is APreDecIncDecOp) token = ((APreDecIncDecOp)op).GetToken(); else if (op is APostIncIncDecOp) token = ((APostIncIncDecOp)op).GetToken(); else if (op is APreIncIncDecOp) token = ((APreIncIncDecOp)op).GetToken(); errors.Add(new ErrorCollection.Error(token, LocRM.GetString("ErrorText114"))); throw new ParserException(token, "TypeChecking.OutAIncDecExp"); } bool plus = op is APreIncIncDecOp || op is APostIncIncDecOp; if (op is APreIncIncDecOp || op is APreDecIncDecOp || node.Parent() is AExpStm) {//++i, --i, i++; or i--; //Replace with assignment //<exp>++ => <exp> + 1 //(... foo = <exp> ...)++ => (... foo = <exp> ...) = (... foo ...) + 1 //(... foo++ ...)++ => (... foo++ ...) = (... foo ...) + 1 PLvalue clone = Util.MakeClone(node.GetLvalue(), data); clone.Apply(new AssignFixup(data)); PBinop binop; if (plus) { binop = new APlusBinop(new TPlus("+")); } else { binop = new AMinusBinop(new TMinus("-")); } ABinopExp addExp = new ABinopExp(new ALvalueExp(clone), binop, new AIntConstExp(new TIntegerLiteral("1"))); AAssignmentExp exp = new AAssignmentExp(new TAssign("="), node.GetLvalue(), addExp); node.ReplaceBy(exp); exp.Apply(this); return; } {//i++ or i-- //Make a new local so //int newLocal = i; //++i; //...newLocal...; PLvalue lvalueClone = Util.MakeClone(node.GetLvalue(), data); PExp exp = new ALvalueExp(Util.MakeClone(node.GetLvalue(), data)); data.ExpTypes[exp] = data.LvalueTypes[node.GetLvalue()]; AALocalDecl localDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(data.LvalueTypes[node.GetLvalue()], data), new TIdentifier("incDecVar"), exp); ALocalDeclStm localDeclStm = new ALocalDeclStm(new TSemicolon(";"), localDecl); node.SetIncDecOp(plus ? (PIncDecOp) new APreIncIncDecOp(new TPlusPlus("++")) : new APreDecIncDecOp(new TMinusMinus("--"))); ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier(localDecl.GetName().Text)); exp = new ALvalueExp(lvalue); data.ExpTypes[exp] = data.LvalueTypes[lvalue] = data.LvalueTypes[node.GetLvalue()]; data.LocalLinks[lvalue] = localDecl; PStm pStm = Util.GetAncestor<PStm>(node); node.ReplaceBy(exp); PStm nodeStm = new AExpStm(new TSemicolon(";"), node); AABlock block = (AABlock) pStm.Parent(); block.GetStatements().Insert(block.GetStatements().IndexOf(pStm), localDeclStm); block.GetStatements().Insert(block.GetStatements().IndexOf(pStm), nodeStm); localDeclStm.Apply(this); nodeStm.Apply(this); exp.Apply(this); if (pStm is AWhileStm && Util.IsAncestor(exp, ((AWhileStm)pStm).GetCondition())) { AWhileStm aStm = (AWhileStm)pStm; //Insert // newLocal = i // ++i //Before each continue in the while, and at the end. //Add continue statement, if not present block = (AABlock)((ABlockStm)aStm.GetBody()).GetBlock(); if (block.GetStatements().Count == 0 || !(block.GetStatements()[block.GetStatements().Count - 1] is AContinueStm)) block.GetStatements().Add(new AContinueStm(new TContinue("continue"))); //Get all continue statements in the while ContinueFinder finder = new ContinueFinder(); block.Apply(finder); foreach (AContinueStm continueStm in finder.Continues) { PLvalue nodeLvalue1 = Util.MakeClone(lvalueClone, data); PExp nodeLvalue1Exp = new ALvalueExp(nodeLvalue1); PLvalue nodeLvalue2 = Util.MakeClone(lvalueClone, data); ALocalLvalue newLocalLvalue = new ALocalLvalue(new TIdentifier("newLocal")); data.LocalLinks[newLocalLvalue] = localDecl; AAssignmentExp assignment = new AAssignmentExp(new TAssign("="), newLocalLvalue, nodeLvalue1Exp); PStm assignmentStm = new AExpStm(new TSemicolon(";"), assignment); AIncDecExp newIncDecExp = new AIncDecExp(nodeLvalue2, plus ? (PIncDecOp) new APreIncIncDecOp( new TPlusPlus("++")) : new APreDecIncDecOp( new TMinusMinus("--"))); PStm newIncDecExpStm = new AExpStm(new TSemicolon(";"), newIncDecExp); block = (AABlock)continueStm.Parent(); block.GetStatements().Insert(block.GetStatements().IndexOf(continueStm), assignmentStm); block.GetStatements().Insert(block.GetStatements().IndexOf(continueStm), newIncDecExpStm); assignment.Apply(this); newIncDecExp.Apply(this); } } return; } }