public CJumpStatement parseJumpStatement(Queue <CMatchedData> matchedData) { CToken[] currTokens = matchedData.Dequeue().getTokens(); CJumpStatement result; if (currTokens[0].tokenType == CTokenType.RETURN) { if (currTokens.Length > 1) { CExpression returnExpression = new CExpression(getText(currTokens, 1, currTokens.Length - 1)); result = new CJumpStatement(returnExpression, parseStatement(matchedData)); } else { result = new CJumpStatement("return", parseStatement(matchedData)); } this.returnJumps.Add(result); } else { result = new CJumpStatement(getText(currTokens, 0, currTokens.Length - 1), parseStatement(matchedData)); if (currTokens[0].tokenType == CTokenType.GOTO) { int i; /* Match goto with label */ for (i = 0; i < this.labels.Count; i++) { if (this.labels[i].codeString == result.targetIdentifier) { result.setTargetStatement(this.labels[i]); return(result); } } this.gotoJumps.Add(result); } else if (currTokens[0].tokenType == CTokenType.BREAK) { this.breakJumps.Add(result); } else if (currTokens[0].tokenType == CTokenType.CONTINUE) { this.continueJumps.Add(result); } else { throw new System.ArgumentException("matchedData does not represent a valid jump statement!", "matchedData"); } } return(result); }
public CCompoundStatement parseForStatement(Queue <CMatchedData> matchedData) { CToken[] currTokens = matchedData.Dequeue().getTokens(); CExpression startExpression, conditionExpression, iterationExpression; CStatement loopStatement, nextStatement; CLabeledStatement continueTarget, breakTarget, iterationStart; CJumpStatement continueLoop; int i, j; if (currTokens[0].tokenType != CTokenType.FOR) { throw new System.ArgumentException("matchedData should start with a for statement!", "matchedData"); } for (i = 0; i < currTokens.Length && currTokens[i].tokenCode != ";"; i++) { ; } startExpression = new CExpression(getText(currTokens, 2, i)); for (j = i + 1; j < currTokens.Length && currTokens[j].tokenCode != ";"; j++) { ; } conditionExpression = new CExpression(getText(currTokens, i + 1, j)); iterationExpression = new CExpression(getText(currTokens, j + 1, currTokens.Length - 1)); loopStatement = parseStatement(matchedData); nextStatement = loopStatement.nextStatement; /* Create loop */ nextStatement = loopStatement.nextStatement; breakTarget = new CLabeledStatement("for-loop: break target", null, nextStatement != null ? nextStatement : new CExpressionStatement(new CExpression(""), null)); continueLoop = new CJumpStatement("for-loop: next iteration", null); continueTarget = new CLabeledStatement("for-loop: continue target", null, new CExpressionStatement(iterationExpression, continueLoop)); loopStatement.setNextStatement(continueTarget); iterationStart = new CLabeledStatement("for-loop: iteration start", null, new CConditionalStatement( conditionExpression, new CCompoundStatement(new CStatement[] { loopStatement, continueTarget, continueLoop }, null), null )); continueLoop.setTargetStatement(iterationStart); for (i = 0; i < this.breakJumps.Count; i++) { if (loopStatement.contains(this.breakJumps[i])) { this.breakJumps[i].setTargetStatement(breakTarget); this.breakJumps.RemoveAt(i); i--; } } for (i = 0; i < this.continueJumps.Count; i++) { if (loopStatement.contains(this.continueJumps[i])) { this.continueJumps[i].setTargetStatement(continueTarget); this.continueJumps.RemoveAt(i); i--; } } return(new CCompoundStatement( new CStatement[] { new CExpressionStatement(startExpression, iterationStart), iterationStart }, breakTarget)); }
public CLabeledStatement parseWhileStatement(Queue <CMatchedData> matchedData) { CToken[] currTokens = matchedData.Dequeue().getTokens(); CExpression conditionExpression; CStatement loopStatement, nextStatement; CLabeledStatement continueTarget, breakTarget; CJumpStatement continueLoop; int i; if (currTokens[0].tokenType != CTokenType.WHILE) { throw new System.ArgumentException("matchedData should start with a while statement!", "matchedData"); } conditionExpression = new CExpression(getText(currTokens, 2, currTokens.Length - 1)); loopStatement = parseStatement(matchedData); /* Create loop */ nextStatement = loopStatement.nextStatement; breakTarget = new CLabeledStatement("while-loop: break target", null, nextStatement != null ? nextStatement : new CExpressionStatement(new CExpression(""), null)); continueLoop = new CJumpStatement("continue", null); loopStatement.setNextStatement(continueLoop); continueTarget = new CLabeledStatement("while-loop: continue target", null, new CConditionalStatement( conditionExpression, new CCompoundStatement(new CStatement[] { loopStatement, continueLoop }, null), breakTarget )); continueLoop.setTargetStatement(continueTarget); for (i = 0; i < this.breakJumps.Count; i++) { if (continueTarget.contains(this.breakJumps[i])) { this.breakJumps[i].setTargetStatement(breakTarget); this.breakJumps.RemoveAt(i); i--; } } for (i = 0; i < this.continueJumps.Count; i++) { if (continueTarget.contains(this.continueJumps[i])) { this.continueJumps[i].setTargetStatement(continueTarget); this.continueJumps.RemoveAt(i); i--; } } return(continueTarget); }
public CCompoundStatement parseDoWhileStatement(Queue <CMatchedData> matchedData) { CToken[] currTokens = matchedData.Dequeue().getTokens(); CStatement loopStatement, nextStatement; CLabeledStatement continueTarget, breakTarget; CJumpStatement continueLoop; int i; if (currTokens[0].tokenType != CTokenType.DO) { throw new System.ArgumentException("matchedData should start with a do-while statement!", "matchedData"); } loopStatement = parseStatement(matchedData); if (loopStatement.nextStatement is CLabeledStatement && loopStatement.nextStatement.nextStatement is CLabeledStatement) { nextStatement = ((CLabeledStatement)loopStatement.nextStatement.nextStatement).labeledStatement; } else { throw new System.ArgumentException("incorrect do-while statement!", "matchedData"); } /* Create loop */ breakTarget = new CLabeledStatement("do-while-loop: break target", null, nextStatement != null ? nextStatement : new CExpressionStatement(new CExpression(""), null)); continueLoop = new CJumpStatement("continue", null); continueTarget = new CLabeledStatement("do-while-loop: continue target", null, loopStatement); continueLoop.setTargetStatement(continueTarget); loopStatement.setNextStatement( new CConditionalStatement( ((CConditionalStatement)((CLabeledStatement)loopStatement.nextStatement).labeledStatement).condition, continueLoop, null ) ); for (i = 0; i < this.breakJumps.Count; i++) { if (continueTarget.contains(this.breakJumps[i])) { this.breakJumps[i].setTargetStatement(breakTarget); this.breakJumps.RemoveAt(i); i--; } } for (i = 0; i < this.continueJumps.Count; i++) { if (continueTarget.contains(this.continueJumps[i])) { this.continueJumps[i].setTargetStatement(continueTarget); this.continueJumps.RemoveAt(i); i--; } } return(new CCompoundStatement( new CStatement[] { continueTarget, loopStatement.nextStatement }, breakTarget )); }
public void addJumpOrigin(CJumpStatement targetStatement) { this.jumpOrigins.Add(targetStatement); this.jumpOrigintSet = true; }