//----------------------------------------------------------- public void Visit(NIfStmt node) { // if (Visit((dynamic) node[0]) != Type.BOOL) { // throw new SemanticError( // "Expecting type " + Type.BOOL // + " in conditional statement", // node.AnchorToken); // } VisitChildren(node); }
// Returns NIfStmt public Node IfStmt() { NIfStmt nIfStmt = new NIfStmt(); Expect(TokenCategory.PARENTHESIS_LEFT); nIfStmt.Add(Expr()); Expect(TokenCategory.PARENTHESIS_RIGHT); Expect(TokenCategory.CURLY_BRACE_LEFT); NStmtList nStmtList = new NStmtList(); while (firstOfStatement.Contains(CurrentToken)) { Stmt(nStmtList); } nIfStmt.Add(nStmtList); Expect(TokenCategory.CURLY_BRACE_RIGHT); while (CurrentToken == TokenCategory.ELSE) { Expect(TokenCategory.ELSE); if (CurrentToken == TokenCategory.IF) { Expect(TokenCategory.IF); Expect(TokenCategory.PARENTHESIS_LEFT); nIfStmt.Add(Expr()); Expect(TokenCategory.PARENTHESIS_RIGHT); Expect(TokenCategory.CURLY_BRACE_LEFT); NStmtList otherNStmtList = new NStmtList(); while (firstOfStatement.Contains(CurrentToken)) { Stmt(otherNStmtList); } nIfStmt.Add(otherNStmtList); Expect(TokenCategory.CURLY_BRACE_RIGHT); } else if (CurrentToken == TokenCategory.CURLY_BRACE_LEFT) { Expect(TokenCategory.CURLY_BRACE_LEFT); NStmtList otherNStmtList = new NStmtList(); while (firstOfStatement.Contains(CurrentToken)) { Stmt(otherNStmtList); } nIfStmt.Add(otherNStmtList); Expect(TokenCategory.CURLY_BRACE_RIGHT); break; } } return(nIfStmt); }
public void Visit(NIfStmt nIfStmt) { Visit((dynamic)nIfStmt[0]); // Visit condition Visit((dynamic)nIfStmt[1]); // Visit body int currentChildIndex = 2; while (currentChildIndex < nIfStmt.children.Count) { if (nIfStmt[currentChildIndex].GetType() == typeof(NExpr)) // Assume else if { Visit((dynamic)nIfStmt[currentChildIndex++]); // Visit else if condition Visit((dynamic)nIfStmt[currentChildIndex++]); // Visit else if body } else // Assume else { Visit((dynamic)nIfStmt[currentChildIndex++]); // Visit else body } } }
public string Visit(NIfStmt nIfStmt) { string retVal = ""; string exitAll = GenerateLabel(); string jumpNext = GenerateLabel(); retVal += Visit((dynamic)nIfStmt[0]); // Visit condition retVal += "\t\tbrfalse " + jumpNext + "\n"; retVal += Visit((dynamic)nIfStmt[1]); // Visit body retVal += "\t\tbr " + exitAll + "\n"; int currentChildIndex = 2; bool endedInElse = false; while (currentChildIndex < nIfStmt.children.Count) { if (nIfStmt.children.Count - currentChildIndex >= 2) // Assume else if { retVal += jumpNext + ":\n"; jumpNext = GenerateLabel(); retVal += Visit((dynamic)nIfStmt[currentChildIndex++]); // Visit else if condition retVal += "\t\tbrfalse " + jumpNext + "\n"; retVal += Visit((dynamic)nIfStmt[currentChildIndex++]); // Visit else if body retVal += "\t\tbr " + exitAll + "\n"; } else // Assume else { retVal += jumpNext + ":\n"; retVal += Visit((dynamic)nIfStmt[currentChildIndex++]); // Visit else body endedInElse = true; } } if (!endedInElse) { retVal += jumpNext + ":\n"; } retVal += exitAll + ":\n"; return(retVal); }