use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind in project ballerina by ballerina-lang.
the class IterableAnalyzer method handlerIterableOperation.
public void handlerIterableOperation(BLangInvocation iExpr, BType expectedType, SymbolEnv env) {
final IterableContext context;
if (iExpr.expr.type.tag != TypeTags.INTERMEDIATE_COLLECTION) {
// This is a new iteration chain.
context = new IterableContext(iExpr.expr, env);
} else {
// Get context from previous invocation.
context = ((BLangInvocation) iExpr.expr).iContext;
}
iExpr.iContext = context;
final IterableKind iterableKind = IterableKind.getFromString(iExpr.name.value);
final Operation iOperation = new Operation(iterableKind, iExpr, expectedType);
iExpr.iContext.addOperation(iOperation);
if (iterableKind.isLambdaRequired()) {
handleLambdaBasedIterableOperation(context, iOperation);
} else {
handleSimpleTerminalOperations(iOperation);
}
validateIterableContext(context);
if (iOperation.resultType != symTable.errType && context.foreachTypes.isEmpty()) {
calculateForeachTypes(context);
}
}
use of org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind 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;
}
}
Aggregations