Search in sources :

Example 96 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

public void visit(BLangTernaryExpr ternaryExpr) {
    // Determine the reg index of the ternary expression and this reg index will be used by both then and else
    // expressions to store their result
    RegIndex ternaryExprRegIndex = calcAndGetExprRegIndex(ternaryExpr);
    // Generate code for the condition
    this.genNode(ternaryExpr.expr, this.env);
    Operand ifFalseJumpAddr = getOperand(-1);
    this.emit(InstructionCodes.BR_FALSE, ternaryExpr.expr.regIndex, ifFalseJumpAddr);
    // Generate code for the then expression
    ternaryExpr.thenExpr.regIndex = createLHSRegIndex(ternaryExprRegIndex);
    this.genNode(ternaryExpr.thenExpr, this.env);
    Operand endJumpAddr = getOperand(-1);
    this.emit(InstructionCodes.GOTO, endJumpAddr);
    ifFalseJumpAddr.value = nextIP();
    // Generate code for the then expression
    ternaryExpr.elseExpr.regIndex = createLHSRegIndex(ternaryExprRegIndex);
    this.genNode(ternaryExpr.elseExpr, this.env);
    endJumpAddr.value = nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 97 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class CodeGenerator method visitOrExpression.

private void visitOrExpression(BLangBinaryExpr binaryExpr) {
    // short-circuit evaluation
    // Code address to jump if the lhs expression gets evaluated to 'true'.
    Operand lExprTrueJumpAddr = getOperand(-1);
    // Code address to jump if the rhs expression gets evaluated to 'false'.
    Operand rExprFalseJumpAddr = getOperand(-1);
    // Generate code for the left hand side
    genNode(binaryExpr.lhsExpr, this.env);
    emit(InstructionCodes.BR_TRUE, binaryExpr.lhsExpr.regIndex, lExprTrueJumpAddr);
    // Generate code for the right hand side
    genNode(binaryExpr.rhsExpr, this.env);
    emit(InstructionCodes.BR_FALSE, binaryExpr.rhsExpr.regIndex, rExprFalseJumpAddr);
    lExprTrueJumpAddr.value = nextIP();
    RegIndex exprRegIndex = calcAndGetExprRegIndex(binaryExpr);
    emit(InstructionCodes.BCONST_1, exprRegIndex);
    Operand gotoAddr = getOperand(-1);
    emit(InstructionCodes.GOTO, gotoAddr);
    rExprFalseJumpAddr.value = nextIP();
    // Load 'false' if the both conditions are false;
    emit(InstructionCodes.BCONST_0, exprRegIndex);
    gotoAddr.value = nextIP();
}
Also used : Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 98 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class CodeGenerator method createStringLiteral.

/**
 * Creates a string literal expression, generate the code and returns the registry index.
 *
 * @param value    String value to generate the string literal
 * @param regIndex String literal expression's reg index
 * @param env      Environment
 * @return String registry index of the generated string
 */
private RegIndex createStringLiteral(String value, RegIndex regIndex, SymbolEnv env) {
    BLangLiteral prefixLiteral = (BLangLiteral) TreeBuilder.createLiteralExpression();
    prefixLiteral.value = value;
    prefixLiteral.typeTag = TypeTags.STRING;
    prefixLiteral.type = symTable.stringType;
    prefixLiteral.regIndex = regIndex;
    genNode(prefixLiteral, env);
    return prefixLiteral.regIndex;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)

Example 99 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class AnnotationDesugar method initAnnotation.

private void initAnnotation(BLangAnnotationAttachment attachment, BLangVariable annotationMapEntryVar, BLangBlockStmt target, BSymbol parentSymbol, int index) {
    BLangVariable annotationVar = null;
    if (attachment.annotationSymbol.attachedType != null) {
        // create: AttachedType annotationVar = { annotation-expression }
        annotationVar = ASTBuilderUtil.createVariable(attachment.pos, attachment.annotationName.value, attachment.annotationSymbol.attachedType.type);
        annotationVar.expr = attachment.expr;
        ASTBuilderUtil.defineVariable(annotationVar, parentSymbol, names);
        ASTBuilderUtil.createVariableDefStmt(attachment.pos, target).var = annotationVar;
    }
    // create: annotationMapEntryVar["name$index"] = annotationVar;
    BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(target.pos, target);
    if (annotationVar != null) {
        assignmentStmt.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationVar.symbol);
    } else {
        assignmentStmt.expr = ASTBuilderUtil.createLiteral(target.pos, symTable.nullType, null);
    }
    BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
    indexAccessNode.pos = target.pos;
    indexAccessNode.indexExpr = ASTBuilderUtil.createLiteral(target.pos, symTable.stringType, attachment.annotationSymbol.bvmAlias() + "$" + index);
    indexAccessNode.expr = ASTBuilderUtil.createVariableRef(target.pos, annotationMapEntryVar.symbol);
    indexAccessNode.type = annotationMapEntryVar.symbol.type;
    assignmentStmt.varRefs.add(indexAccessNode);
}
Also used : BLangIndexBasedAccess(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 100 with Expression

use of org.wso2.siddhi.query.api.expression.Expression in project ballerina by ballerina-lang.

the class Desugar method getSQLPreparedStatement.

private BLangLiteral getSQLPreparedStatement(BLangTableQueryExpression tableQueryExpression) {
    // create a literal to represent the sql query.
    BLangLiteral sqlQueryLiteral = (BLangLiteral) TreeBuilder.createLiteralExpression();
    sqlQueryLiteral.typeTag = TypeTags.STRING;
    // assign the sql query from table expression to the literal.
    sqlQueryLiteral.value = tableQueryExpression.getSqlQuery();
    sqlQueryLiteral.type = symTable.getTypeFromTag(sqlQueryLiteral.typeTag);
    return sqlQueryLiteral;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)

Aggregations

Expression (org.wso2.siddhi.query.api.expression.Expression)32 ArrayList (java.util.ArrayList)20 Attribute (org.wso2.siddhi.query.api.definition.Attribute)16 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)15 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)15 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)13 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)13 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)13 Variable (org.wso2.siddhi.query.api.expression.Variable)11 Test (org.testng.annotations.Test)10 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)10 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)10 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)10 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)9 OutputAttribute (org.wso2.siddhi.query.api.execution.query.selection.OutputAttribute)8 CompiledCondition (org.wso2.siddhi.core.util.collection.operator.CompiledCondition)7 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)6 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)6 MatchingMetaInfoHolder (org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)6 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)6