use of org.wso2.ballerinalang.compiler.tree.statements.BLangIf in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
public void visit(BLangIf ifNode) {
addLineNumberInfo(ifNode.pos);
// Generate code for the if condition evaluation
genNode(ifNode.expr, this.env);
Operand ifCondJumpAddr = getOperand(-1);
emit(InstructionCodes.BR_FALSE, ifNode.expr.regIndex, ifCondJumpAddr);
// Generate code for the then body
genNode(ifNode.body, this.env);
Operand endJumpAddr = getOperand(-1);
emit(InstructionCodes.GOTO, endJumpAddr);
ifCondJumpAddr.value = nextIP();
// Visit else statement if any
if (ifNode.elseStmt != null) {
genNode(ifNode.elseStmt, this.env);
}
endJumpAddr.value = nextIP();
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangIf in project ballerina by ballerina-lang.
the class ASTBuilderUtil method createIfElseStmt.
static BLangIf createIfElseStmt(DiagnosticPos pos, BLangExpression conditionExpr, BLangBlockStmt thenBody, BLangStatement elseStmt) {
final BLangIf ifNode = (BLangIf) TreeBuilder.createIfElseStatementNode();
ifNode.pos = pos;
ifNode.expr = conditionExpr;
ifNode.body = thenBody;
ifNode.elseStmt = elseStmt;
return ifNode;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangIf in project ballerina by ballerina-lang.
the class BlockStatementScopeResolver method isCursorBeforeNode.
/**
* Check whether the cursor position is located before the evaluating statement node.
* @param nodePosition position of the node
* @param node statement being evaluated
* @return true|false
*/
@Override
public boolean isCursorBeforeNode(DiagnosticPos nodePosition, Node node, TreeVisitor treeVisitor, TextDocumentServiceContext completionContext) {
int line = completionContext.get(DocumentServiceKeys.POSITION_KEY).getPosition().getLine();
int col = completionContext.get(DocumentServiceKeys.POSITION_KEY).getPosition().getCharacter();
DiagnosticPos zeroBasedPos = CommonUtil.toZeroBasedPosition(nodePosition);
int nodeSLine = zeroBasedPos.sLine;
int nodeSCol = zeroBasedPos.sCol;
// node endLine for the BLangIf node has to calculate by considering the else node. End line of the BLangIf
// node is the endLine of the else node.
int nodeELine = node instanceof BLangIf ? getIfElseNodeEndLine((BLangIf) node) : zeroBasedPos.eLine;
int nodeECol = zeroBasedPos.eCol;
BLangBlockStmt bLangBlockStmt = treeVisitor.getBlockStmtStack().peek();
Node blockOwner = treeVisitor.getBlockOwnerStack().peek();
boolean isLastStatement = this.isNodeLastStatement(bLangBlockStmt, blockOwner, node);
boolean isWithinScopeAfterLastChild = this.isWithinScopeAfterLastChildNode(treeVisitor, isLastStatement, nodeELine, nodeECol, line, col, node);
if (line < nodeSLine || (line == nodeSLine && col < nodeSCol) || isWithinScopeAfterLastChild) {
Map<Name, Scope.ScopeEntry> visibleSymbolEntries = treeVisitor.resolveAllVisibleSymbols(treeVisitor.getSymbolEnv());
treeVisitor.populateSymbols(visibleSymbolEntries, null);
treeVisitor.setTerminateVisitor(true);
return true;
}
return false;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangIf in project ballerina by ballerina-lang.
the class ServiceProtoUtils method getInvocationExpression.
private static BLangInvocation getInvocationExpression(BlockNode body) {
if (body == null) {
return null;
}
for (StatementNode statementNode : body.getStatements()) {
BLangExpression expression = null;
// example : conn.send inside while block.
if (statementNode instanceof BLangWhile) {
BLangWhile langWhile = (BLangWhile) statementNode;
BLangInvocation invocExp = getInvocationExpression(langWhile.getBody());
if (invocExp != null) {
return invocExp;
}
}
// example : conn.send inside for block.
if (statementNode instanceof BLangForeach) {
BLangForeach langForeach = (BLangForeach) statementNode;
BLangInvocation invocExp = getInvocationExpression(langForeach.getBody());
if (invocExp != null) {
return invocExp;
}
}
// example : conn.send inside if block.
if (statementNode instanceof BLangIf) {
BLangIf langIf = (BLangIf) statementNode;
BLangInvocation invocExp = getInvocationExpression(langIf.getBody());
if (invocExp != null) {
return invocExp;
}
invocExp = getInvocationExpression((BLangBlockStmt) langIf.getElseStatement());
if (invocExp != null) {
return invocExp;
}
}
// example : _ = conn.send(msg);
if (statementNode instanceof BLangAssignment) {
BLangAssignment assignment = (BLangAssignment) statementNode;
expression = assignment.getExpression();
}
// example : grpc:HttpConnectorError err = conn.send(msg);
if (statementNode instanceof BLangVariableDef) {
BLangVariableDef variableDef = (BLangVariableDef) statementNode;
BLangVariable variable = variableDef.getVariable();
expression = variable.getInitialExpression();
}
if (expression != null && expression instanceof BLangInvocation) {
BLangInvocation invocation = (BLangInvocation) expression;
if ("send".equals(invocation.getName().getValue())) {
return invocation;
}
}
}
return null;
}
use of org.wso2.ballerinalang.compiler.tree.statements.BLangIf in project ballerina by ballerina-lang.
the class ASTBuilderUtil method createIfStmt.
static BLangIf createIfStmt(DiagnosticPos pos, BLangBlockStmt target) {
final BLangIf ifNode = (BLangIf) TreeBuilder.createIfElseStatementNode();
ifNode.pos = pos;
target.addStatement(ifNode);
return ifNode;
}
Aggregations