use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr in project ballerina by ballerina-lang.
the class TypeChecker method visit.
public void visit(BLangTernaryExpr ternaryExpr) {
BType expType = checkExpr(ternaryExpr.expr, env, Lists.of(this.symTable.booleanType)).get(0);
BType thenType = checkExpr(ternaryExpr.thenExpr, env, expTypes).get(0);
BType elseType = checkExpr(ternaryExpr.elseExpr, env, expTypes).get(0);
if (expType == symTable.errType || thenType == symTable.errType || elseType == symTable.errType) {
resultTypes = Lists.of(symTable.errType);
} else if (expTypes.get(0) == symTable.noType) {
if (thenType == elseType) {
resultTypes = Lists.of(thenType);
} else {
dlog.error(ternaryExpr.pos, DiagnosticCode.INCOMPATIBLE_TYPES, thenType, elseType);
resultTypes = Lists.of(symTable.errType);
}
} else {
resultTypes = expTypes;
}
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateCompareAggregator.
/**
* Generates following.
*
* result = result (Operator) value ? result : value
*
* @param blockStmt target
* @param ctx current context
* @param operator compare operator
*/
private void generateCompareAggregator(BLangBlockStmt blockStmt, IterableContext ctx, OperatorKind operator) {
final DiagnosticPos pos = blockStmt.pos;
final BLangSimpleVarRef resultVar = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
final BLangSimpleVarRef valueVar = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
final BLangBinaryExpr compare = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
compare.pos = pos;
compare.type = symTable.booleanType;
compare.opKind = operator;
compare.lhsExpr = resultVar;
compare.rhsExpr = valueVar;
compare.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(operator, resultVar.symbol.type, valueVar.symbol.type);
final BLangTernaryExpr ternaryExpr = (BLangTernaryExpr) TreeBuilder.createTernaryExpressionNode();
ternaryExpr.pos = pos;
ternaryExpr.expr = compare;
ternaryExpr.thenExpr = resultVar;
ternaryExpr.elseExpr = valueVar;
ternaryExpr.type = compare.type;
final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
countAdd.varRefs.add(resultVar);
countAdd.expr = ternaryExpr;
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr in project ballerina by ballerina-lang.
the class BLangPackageBuilder method createTernaryExpr.
public void createTernaryExpr(DiagnosticPos pos, Set<Whitespace> ws) {
BLangTernaryExpr ternaryExpr = (BLangTernaryExpr) TreeBuilder.createTernaryExpressionNode();
ternaryExpr.pos = pos;
ternaryExpr.addWS(ws);
ternaryExpr.elseExpr = (BLangExpression) exprNodeStack.pop();
ternaryExpr.thenExpr = (BLangExpression) exprNodeStack.pop();
ternaryExpr.expr = (BLangExpression) exprNodeStack.pop();
if (ternaryExpr.expr.getKind() == NodeKind.TERNARY_EXPR) {
// Re-organizing ternary expression tree if there nested ternary expressions.
BLangTernaryExpr root = (BLangTernaryExpr) ternaryExpr.expr;
BLangTernaryExpr parent = root;
while (parent.elseExpr.getKind() == NodeKind.TERNARY_EXPR) {
parent = (BLangTernaryExpr) parent.elseExpr;
}
ternaryExpr.expr = parent.elseExpr;
parent.elseExpr = ternaryExpr;
ternaryExpr = root;
}
addExpressionNode(ternaryExpr);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangTernaryExpr 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();
}
Aggregations