Search in sources :

Example 1 with BLangWhile

use of org.wso2.ballerinalang.compiler.tree.statements.BLangWhile in project ballerina by ballerina-lang.

the class TaintAnalyzer method visit.

public void visit(BLangWhile whileNode) {
    SymbolEnv blockEnv = SymbolEnv.createBlockEnv(whileNode.body, env);
    analyzeNode(whileNode.body, blockEnv);
}
Also used : SymbolEnv(org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)

Example 2 with BLangWhile

use of org.wso2.ballerinalang.compiler.tree.statements.BLangWhile in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addWhileStmt.

public void addWhileStmt(DiagnosticPos pos, Set<Whitespace> ws) {
    BLangWhile whileNode = (BLangWhile) TreeBuilder.createWhileNode();
    whileNode.setCondition(exprNodeStack.pop());
    whileNode.pos = pos;
    whileNode.addWS(ws);
    BLangBlockStmt whileBlock = (BLangBlockStmt) this.blockNodeStack.pop();
    whileBlock.pos = pos;
    whileNode.setBody(whileBlock);
    addStmtToCurrentBlock(whileNode);
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangWhile(org.wso2.ballerinalang.compiler.tree.statements.BLangWhile)

Example 3 with BLangWhile

use of org.wso2.ballerinalang.compiler.tree.statements.BLangWhile 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 4 with BLangWhile

use of org.wso2.ballerinalang.compiler.tree.statements.BLangWhile in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

public void visit(BLangWhile whileNode) {
    Instruction gotoTopJumpInstr = InstructionFactory.get(InstructionCodes.GOTO, getOperand(this.nextIP()));
    this.genNode(whileNode.expr, this.env);
    Operand exitLoopJumpAddr = getOperand(-1);
    Instruction exitLoopJumpInstr = InstructionFactory.get(InstructionCodes.GOTO, exitLoopJumpAddr);
    emit(InstructionCodes.BR_FALSE, whileNode.expr.regIndex, exitLoopJumpAddr);
    this.loopResetInstructionStack.push(gotoTopJumpInstr);
    this.loopExitInstructionStack.push(exitLoopJumpInstr);
    this.genNode(whileNode.body, this.env);
    this.loopResetInstructionStack.pop();
    this.loopExitInstructionStack.pop();
    this.emit(gotoTopJumpInstr);
    exitLoopJumpAddr.value = nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) Instruction(org.wso2.ballerinalang.programfile.Instruction)

Aggregations

BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)2 BLangWhile (org.wso2.ballerinalang.compiler.tree.statements.BLangWhile)2 StatementNode (org.ballerinalang.model.tree.statements.StatementNode)1 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)1 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)1 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)1 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)1 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)1 BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)1 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)1 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)1 Instruction (org.wso2.ballerinalang.programfile.Instruction)1 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)1