Search in sources :

Example 1 with BLangIf

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();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand)

Example 2 with BLangIf

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;
}
Also used : BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf)

Example 3 with BLangIf

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;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangNode(org.wso2.ballerinalang.compiler.tree.BLangNode) Node(org.ballerinalang.model.tree.Node) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 4 with BLangIf

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;
}
Also used : BLangForeach(org.wso2.ballerinalang.compiler.tree.statements.BLangForeach) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) StatementNode(org.ballerinalang.model.tree.statements.StatementNode) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) BLangWhile(org.wso2.ballerinalang.compiler.tree.statements.BLangWhile) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 5 with BLangIf

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;
}
Also used : BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf)

Aggregations

BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)8 BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)3 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)3 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)2 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)2 Node (org.ballerinalang.model.tree.Node)1 StatementNode (org.ballerinalang.model.tree.statements.StatementNode)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)1 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)1 BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)1 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)1 BLangUnaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr)1 BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)1 BLangMatchStmtPatternClause (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause)1 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)1 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)1 BLangWhile (org.wso2.ballerinalang.compiler.tree.statements.BLangWhile)1 Name (org.wso2.ballerinalang.compiler.util.Name)1 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)1