public override AstNode Visit(WhileStatement node) { // Begin the node. builder.BeginNode(node); // Get the condition and the job. Expression cond = node.GetCondition(); AstNode job = node.GetJob(); // Create the basic blocks. BasicBlock condBlock = CreateBasicBlock(); BasicBlock contentBlock = CreateBasicBlock(); BasicBlock endBlock = CreateBasicBlock(); // Set the blocks names. condBlock.SetName("cond"); contentBlock.SetName("content"); endBlock.SetName("end"); // Store the old break and continue. BasicBlock oldBreak = currentBreak; BasicBlock oldContinue = currentContinue; // Set the new break and continue. currentBreak = endBlock; currentContinue = condBlock; // Jump into the condition. builder.CreateJmp(condBlock); // Write the condition. builder.SetBlock(condBlock); cond.Accept(this); // Coerce the condition. IChelaType condType = cond.GetNodeType(); if(condType != ChelaType.GetBoolType()) Cast(node, cond.GetNodeValue(), condType, ChelaType.GetBoolType()); // Perform the loop conditional branch. builder.CreateBr(contentBlock, endBlock); // Write the loop content. builder.SetBlock(contentBlock); VisitList(job); // Close the loop. if(!builder.IsLastTerminator()) builder.CreateJmp(condBlock); // Continue as normal. builder.SetBlock(endBlock); // Restore the break and continue blocks. currentBreak = oldBreak; currentContinue = oldContinue; return builder.EndNode(); }
public override AstNode Visit(WhileStatement node) { // Get the condition and the job. Expression condition = node.GetCondition(); AstNode job = node.GetJob(); // Visit the condition and the job. condition.Accept(this); VisitList(job); // Check the condition type. IChelaType condType = condition.GetNodeType(); if(condType != ChelaType.GetBoolType() && Coerce(condType, ChelaType.GetBoolType()) != ChelaType.GetBoolType()) Error(node, "condition must be a boolean expression."); return node; }