use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext in project ballerina by ballerina-lang.
the class IterableCodeDesugar method createResultVarDefStmt.
/**
* Generates following.
*
* array: result =[];
* map: result = {};
*
* @param funcNode functionNode
* @param ctx current context
*/
private void createResultVarDefStmt(BLangFunction funcNode, IterableContext ctx) {
BLangBlockStmt blockStmt = funcNode.body;
final IterableKind kind = ctx.getLastOperation().kind;
if (ctx.resultType.tag != TypeTags.ARRAY && ctx.resultType.tag != TypeTags.MAP && ctx.resultType.tag != TypeTags.TABLE && kind != IterableKind.MAX && kind != IterableKind.MIN) {
return;
}
final DiagnosticPos pos = blockStmt.pos;
final BLangVariableDef defStmt = ASTBuilderUtil.createVariableDefStmt(pos, blockStmt);
defStmt.var = ctx.resultVar;
switch(ctx.resultType.tag) {
case TypeTags.ARRAY:
final BLangArrayLiteral arrayInit = (BLangArrayLiteral) TreeBuilder.createArrayLiteralNode();
arrayInit.pos = pos;
arrayInit.exprs = new ArrayList<>();
arrayInit.type = ctx.resultType;
defStmt.var.expr = arrayInit;
break;
case TypeTags.MAP:
defStmt.var.expr = ASTBuilderUtil.createEmptyRecordLiteral(pos, ctx.resultType);
break;
case TypeTags.TABLE:
BLangVariable retVars = ctx.getFirstOperation().retVar;
BType tableType = new BTableType(TypeTags.TABLE, retVars.type, symTable.tableType.tsymbol);
defStmt.var.expr = ASTBuilderUtil.createEmptyRecordLiteral(pos, tableType);
break;
case TypeTags.INT:
if (kind == IterableKind.MAX) {
defStmt.var.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, Long.MIN_VALUE);
} else if (kind == IterableKind.MIN) {
defStmt.var.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, Long.MAX_VALUE);
}
break;
case TypeTags.FLOAT:
if (kind == IterableKind.MAX) {
defStmt.var.expr = ASTBuilderUtil.createLiteral(pos, symTable.floatType, Double.MIN_NORMAL);
} else if (kind == IterableKind.MIN) {
defStmt.var.expr = ASTBuilderUtil.createLiteral(pos, symTable.floatType, Double.MAX_VALUE);
}
break;
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext in project ballerina by ballerina-lang.
the class IterableCodeDesugar method createForeachVariables.
/* Some Utils methods */
private List<BLangVariable> createForeachVariables(IterableContext ctx, BLangVariable firstOperationArg, BLangFunction funcNode) {
List<BLangVariable> foreachVariables = new ArrayList<>();
if (firstOperationArg.type.tag != TypeTags.TUPLE) {
foreachVariables.add(firstOperationArg);
defineVariable(firstOperationArg, ctx.env.enclPkg.symbol.pkgID, funcNode);
return foreachVariables;
}
final List<BType> tupleTypes = ((BTupleType) firstOperationArg.type).tupleTypes;
int index = 0;
for (BType type : tupleTypes) {
String varName = VAR_FOREACH_VAL + index++;
final BLangVariable variable = ASTBuilderUtil.createVariable(funcNode.pos, varName, type);
foreachVariables.add(variable);
defineVariable(variable, ctx.env.enclPkg.symbol.pkgID, funcNode);
}
return foreachVariables;
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateDefaultIfEmpty.
/**
* Generates following.
*
* if(count == 0){
* return;
* }
*
* @param blockStmt target
* @param ctx current context
*/
private void generateDefaultIfEmpty(BLangBlockStmt blockStmt, IterableContext ctx) {
if (ctx.resultVar.symbol.type.tag > TypeTags.TYPEDESC) {
return;
}
final DiagnosticPos pos = blockStmt.pos;
final BLangBinaryExpr equality = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
equality.pos = pos;
equality.type = symTable.booleanType;
equality.opKind = OperatorKind.EQUAL;
equality.lhsExpr = ASTBuilderUtil.createVariableRef(pos, ctx.countVar.symbol);
equality.rhsExpr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 0L);
equality.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.EQUAL, symTable.intType, symTable.intType);
final BLangIf ifNode = ASTBuilderUtil.createIfStmt(pos, blockStmt);
ifNode.expr = equality;
ifNode.body = ASTBuilderUtil.createBlockStmt(pos);
if (ctx.resultVar.symbol.type.tag <= TypeTags.FLOAT) {
final BLangAssignment assign = ASTBuilderUtil.createAssignmentStmt(pos, ifNode.body);
assign.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
switch(ctx.resultVar.symbol.type.tag) {
case TypeTags.INT:
assign.expr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 0L);
break;
case TypeTags.FLOAT:
assign.expr = ASTBuilderUtil.createLiteral(pos, symTable.floatType, 0D);
break;
}
}
final BLangReturn returnStmt = ASTBuilderUtil.createReturnStmt(pos, ifNode.body);
returnStmt.addExpression(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateMapAggregator.
/**
* Generates following.
*
* result[key] = value;
*
* @param blockStmt target
* @param ctx current context
*/
private void generateMapAggregator(BLangBlockStmt blockStmt, IterableContext ctx) {
final DiagnosticPos pos = blockStmt.pos;
// create assignment result[key] = value
final BLangIndexBasedAccess indexAccessNode = (BLangIndexBasedAccess) TreeBuilder.createIndexBasedAccessNode();
indexAccessNode.pos = pos;
indexAccessNode.indexExpr = ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(0).symbol);
indexAccessNode.expr = ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol);
indexAccessNode.type = ctx.iteratorResultVariables.get(1).symbol.type;
final BLangAssignment valueAssign = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
valueAssign.varRefs.add(indexAccessNode);
valueAssign.expr = ASTBuilderUtil.generateConversionExpr(ASTBuilderUtil.createVariableRef(pos, ctx.iteratorResultVariables.get(1).symbol), symTable.anyType, symResolver);
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableContext in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateIteratorFunction.
private void generateIteratorFunction(IterableContext ctx) {
final Operation firstOperation = ctx.getFirstOperation();
final DiagnosticPos pos = firstOperation.pos;
// Create and define function signature.
final BLangFunction funcNode = ASTBuilderUtil.createFunction(pos, getFunctionName(FUNC_CALLER));
funcNode.requiredParams.add(ctx.collectionVar);
if (isReturningIteratorFunction(ctx)) {
funcNode.retParams.add(ctx.resultVar);
}
defineFunction(funcNode, ctx.env.enclPkg);
ctx.iteratorFuncSymbol = funcNode.symbol;
LinkedList<Operation> streamableOperations = new LinkedList<>();
ctx.operations.stream().filter(op -> op.kind.isLambdaRequired()).forEach(streamableOperations::add);
if (streamableOperations.isEmpty()) {
// Generate simple iterator function body.
generateSimpleIteratorBlock(ctx, funcNode);
return;
}
// Generate Caller Function.
generateStreamingIteratorBlock(ctx, funcNode, streamableOperations);
}
Aggregations