public override void OutASAssignmentExp(ASAssignmentExp node) { AAssignmentExp replacer = null; PAssignop assignop = node.GetAssignop(); Token token = null; PBinop binop = null; if (assignop is AAddAssignop) { token = ((AAddAssignop)assignop).GetToken(); binop = new APlusBinop(new TPlus("+", token.Line, token.Pos)); } else if (assignop is ASubAssignop) { token = ((ASubAssignop)assignop).GetToken(); binop = new AMinusBinop(new TMinus("-", token.Line, token.Pos)); } else if (assignop is AMulAssignop) { token = ((AMulAssignop)assignop).GetToken(); binop = new ATimesBinop(new TStar("*", token.Line, token.Pos)); } else if (assignop is ADivAssignop) { token = ((ADivAssignop)assignop).GetToken(); binop = new ADivideBinop(new TDiv("/", token.Line, token.Pos)); } else if (assignop is AModAssignop) { token = ((AModAssignop)assignop).GetToken(); binop = new AModuloBinop(new TMod("%", token.Line, token.Pos)); } else// if (assignop is AAssignAssignop) { token = ((AAssignAssignop)assignop).GetToken(); } PExp rightSide; if (binop != null) rightSide = new ABinopExp(new ALvalueExp((PLvalue)node.GetLvalue().Clone()), binop, (PExp)node.GetExp().Clone()); else rightSide = (PExp)node.GetExp().Clone(); replacer = new AAssignmentExp(new TAssign("=", token.Line, token.Pos), (PLvalue)node.GetLvalue().Clone(), rightSide); node.ReplaceBy(replacer); replacer.Apply(this); }
private void CheckDynamicLvalue(PLvalue node) { if (node.Parent() is ALvalueExp) { ALvalueExp parent = (ALvalueExp)node.Parent(); if (parent.Parent() is AAssignmentExp) return; if (!(parent.Parent() is PLvalue)) {//Then this should be a data table get //If this is a bulk copy type, move it out to a new variable. //var pointerVar; //pointerVar = dataTableGet(...) //... PType type = data.ExpTypes[parent]; if (Util.IsBulkCopy(type)) { PStm pStm = Util.GetAncestor<PStm>(node); AABlock pBlock = (AABlock)pStm.Parent(); AAssignmentExp assignment; if (parent.Parent() is AALocalDecl) { //Turn into assignment ALocalLvalue leftSide = new ALocalLvalue(new TIdentifier("tempName")); data.LocalLinks[leftSide] = (AALocalDecl) parent.Parent(); assignment = new AAssignmentExp(new TAssign("="), leftSide, parent); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm) + 1, new AExpStm(new TSemicolon(";"), assignment)); data.ExpTypes[assignment] = data.LvalueTypes[leftSide] = data.LocalLinks[leftSide].GetType(); assignment.Apply(this); return; } pStm = Util.GetAncestor<PStm>(node); AALocalDecl localDecl = new AALocalDecl(new APublicVisibilityModifier(), null, null, null, null, Util.MakeClone(type, data), new TIdentifier("pointerVar"), null); ALocalLvalue lvalue = new ALocalLvalue(new TIdentifier("pointerVar")); ALocalLvalue lvalue2 = new ALocalLvalue(new TIdentifier("pointerVar")); ALvalueExp lvalue2Exp = new ALvalueExp(lvalue2); parent.ReplaceBy(lvalue2Exp); assignment = new AAssignmentExp(new TAssign("="), lvalue, parent); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new ALocalDeclStm(new TSemicolon(";"), localDecl)); pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), new AExpStm(new TSemicolon(";"), assignment)); data.LocalLinks[lvalue] = localDecl; data.LocalLinks[lvalue2] = localDecl; data.ExpTypes[assignment] = data.ExpTypes[lvalue2Exp] = data.LvalueTypes[lvalue] = data.LvalueTypes[lvalue2] = type; assignmentRightSideSet = true; assignment.Apply(this); } else { string s = "string"; if (type is ANamedType) s = ((ANamedType) type).AsIdentifierString(); else if (type is APointerType && Util.IsIntPointer(node, ((APointerType)type).GetType(), data)) { s = "int"; } parent.ReplaceBy(CreateDynaicGetStm(s)); } hadPointer = false; } } }
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; } }
public override void OutAAssignmentExp(AAssignmentExp node) { PType type = data.ExpTypes[node]; if (Util.IsBulkCopy(type)) { node.Apply(mover); List<PStm> replacementStatements = MakeAssignments(node.GetLvalue(), node.GetExp(), new List<AALocalDecl>() {null}); PStm pStm = Util.GetAncestor<PStm>(node); AABlock pBlock = (AABlock) pStm.Parent(); foreach (PStm stm in replacementStatements) { pBlock.GetStatements().Insert(pBlock.GetStatements().IndexOf(pStm), stm); } pBlock.RemoveChild(pStm); } base.OutAAssignmentExp(node); }