// IF public override TypeNode Visit(IfNode node, object obj) { TypeNode predicateType = Visit(node.Predicate); // If predicate type is not Bool, log error if (predicateType != StandardTypes.Bool) { ErrorLogger.LogError(new IncompatibleTypesError(predicateType, StandardTypes.Bool, node.Predicate.SourcePosition)); return(new ErrorTypeNode(node.SourcePosition)); } Visit(node.Block); // Visit each ELSE IF in the chain foreach (ElseIfNode elseIf in node.ElseIfChain) { Visit(elseIf); } // Visit ELSE block, if has any if (node.ElseBlock != null) { Visit(node.ElseBlock); } return(null); }
// IF public override string Visit(IfNode node, object obj) { StringBuilder builder = new StringBuilder(); builder.Append($"if ({Visit(node.Predicate)})\n"); builder.Append(Visit(node.Block)); // Append all ELSE IF statements, if any foreach (ElseIfNode elseIf in node.ElseIfChain) { builder.Append(Visit(elseIf)); } // Append ELSE statement, if has if (node.ElseBlock != null) { builder.Append($"else {Visit(node.ElseBlock)}"); } return(builder.ToString()); }
// IF public override ASTNode VisitIfStmt(CLUBSParser.IfStmtContext context) { IfNode node = new IfNode(new SourcePosition(context.start)); node.Predicate = Visit(context.expr()) as ExpressionNode; node.Block = Visit(context.ifBlock) as BlockNode; // If has any ELSE IF, visit and add to chain if (context.elseIfStmt() != null) { foreach (CLUBSParser.ElseIfStmtContext elseIf in context.elseIfStmt()) { node.ElseIfChain.Add(Visit(elseIf) as ElseIfNode); } } // Visit ELSE if has any if (context.elseBlock != null) { node.ElseBlock = Visit(context.elseBlock) as BlockNode; } return(node); }
public abstract T Visit(IfNode node, object obj);