use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.
the class IterableCodeDesugar method createIteratorResultVariables.
private List<BLangVariable> createIteratorResultVariables(IterableContext ctx, BLangVariable lastOperationArg, BLangFunction funcNode) {
List<BLangVariable> resultVariables = new ArrayList<>();
if (lastOperationArg.type.tag != TypeTags.TUPLE) {
resultVariables.add(lastOperationArg);
defineVariable(lastOperationArg, ctx.env.enclPkg.symbol.pkgID, funcNode);
return resultVariables;
}
final List<BType> tupleTypes = ((BTupleType) lastOperationArg.type).tupleTypes;
int index = 0;
for (BType type : tupleTypes) {
String varName = VAR_RESULT_VAL + index++;
final BLangVariable variable = ASTBuilderUtil.createVariable(funcNode.pos, varName, type);
resultVariables.add(variable);
defineVariable(variable, ctx.env.enclPkg.symbol.pkgID, funcNode);
}
return resultVariables;
}
use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateSimpleIteratorBlock.
private void generateSimpleIteratorBlock(IterableContext ctx, BLangFunction funcNode) {
final Operation firstOperation = ctx.getFirstOperation();
final DiagnosticPos pos = firstOperation.pos;
// return result;
if (isReturningIteratorFunction(ctx)) {
createCounterVarDefStmt(funcNode, ctx);
createResultVarDefStmt(funcNode, ctx);
}
// Create variables required.
final List<BLangVariable> foreachVariables = createForeachVariables(ctx, ctx.getFirstOperation().argVar, funcNode);
ctx.iteratorResultVariables = foreachVariables;
final BLangForeach foreachStmt = ASTBuilderUtil.createForeach(pos, funcNode.body, ASTBuilderUtil.createVariableRef(pos, ctx.collectionVar.symbol), ASTBuilderUtil.createVariableRefList(pos, foreachVariables), ctx.foreachTypes);
if (isReturningIteratorFunction(ctx)) {
generateAggregator(foreachStmt.body, ctx);
generateFinalResult(funcNode.body, ctx);
}
final BLangReturn returnStmt = ASTBuilderUtil.createReturnStmt(firstOperation.pos, funcNode.body);
if (isReturningIteratorFunction(ctx)) {
returnStmt.addExpression(ASTBuilderUtil.createVariableRef(pos, ctx.resultVar.symbol));
}
}
use of org.wso2.ballerinalang.compiler.tree.BLangVariable 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.tree.BLangVariable in project ballerina by ballerina-lang.
the class SiddhiQueryBuilder method visit.
public void visit(BLangForever foreverStatement) {
siddhiQuery = new StringBuilder();
streamDefinitionQuery = new StringBuilder();
streamIds = new HashSet<>();
inStreamRefs = new ArrayList<>();
outStreamRefs = new ArrayList<>();
inTableRefs = new ArrayList<>();
outTableRefs = new ArrayList<>();
binaryExpr = null;
setExpr = null;
orderByClause = null;
whereClause = null;
windowClause = null;
joinStreamingInputClause = null;
streamingInputClause = null;
selectExprClause = null;
selectExpr = null;
setAssignmentClause = null;
groupByClause = null;
havingClause = null;
patternStreamingClause = null;
streamActionClause = null;
intRangeExpr = null;
List<VariableNode> globalVariables = foreverStatement.getGlobalVariables();
if (globalVariables != null) {
for (VariableNode variable : globalVariables) {
((BLangVariable) variable).accept(this);
}
}
List<VariableSymbol> functionVariables = foreverStatement.getFunctionVariables();
if (functionVariables != null) {
for (VariableSymbol variable : functionVariables) {
getStreamDefintionForFuntionVariable((BVarSymbol) variable);
}
}
List<? extends StatementNode> statementNodes = foreverStatement.gettreamingQueryStatements();
for (StatementNode statementNode : statementNodes) {
((BLangStatement) statementNode).accept(this);
}
foreverStatement.setSiddhiQuery(this.getSiddhiQuery());
foreverStatement.setStreamIdsAsString(String.join(",", streamIds));
}
use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.
the class BLangPackageBuilder method addConstVariable.
public void addConstVariable(DiagnosticPos pos, Set<Whitespace> ws, String identifier, boolean publicVar, boolean safeAssignment) {
BLangVariable var = (BLangVariable) this.generateBasicVarNode(pos, ws, identifier, true);
var.flagSet.add(Flag.CONST);
if (publicVar) {
var.flagSet.add(Flag.PUBLIC);
}
var.docTag = DocTag.VARIABLE;
var.safeAssignment = safeAssignment;
attachAnnotations(var);
attachDocumentations(var);
attachDeprecatedNode(var);
this.compUnit.addTopLevelNode(var);
}
Aggregations