use of org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr in project ballerina by ballerina-lang.
the class BLangPackageBuilder method createBracedOrTupleExpression.
public void createBracedOrTupleExpression(DiagnosticPos pos, Set<Whitespace> ws, int numberOfExpressions) {
final BLangBracedOrTupleExpr expr = (BLangBracedOrTupleExpr) TreeBuilder.createBracedOrTupleExpression();
expr.pos = pos;
expr.addWS(ws);
for (int i = 0; i < numberOfExpressions; i++) {
expr.expressions.add(0, (BLangExpression) exprNodeStack.pop());
}
addExpressionNode(expr);
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr in project ballerina by ballerina-lang.
the class TypeChecker method visit.
@Override
public void visit(BLangBracedOrTupleExpr bracedOrTupleExpr) {
// Handle Tuple Expression.
if (!expTypes.isEmpty() && expTypes.get(0).tag == TypeTags.TUPLE) {
BTupleType tupleType = (BTupleType) this.expTypes.get(0);
// Fix this.
List<BType> expTypes = getListWithErrorTypes(bracedOrTupleExpr.expressions.size());
if (tupleType.tupleTypes.size() != bracedOrTupleExpr.expressions.size()) {
dlog.error(bracedOrTupleExpr.pos, DiagnosticCode.SYNTAX_ERROR, "tuple and expression size does not match");
} else {
expTypes = tupleType.tupleTypes;
}
List<BType> results = new ArrayList<>();
for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
results.add(checkExpr(bracedOrTupleExpr.expressions.get(i), env, Lists.of(expTypes.get(i))).get(0));
}
resultTypes = Lists.of(new BTupleType(results));
} else if (bracedOrTupleExpr.expressions.size() > 1) {
// This is a tuple.
List<BType> results = new ArrayList<>();
for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
results.add(checkExpr(bracedOrTupleExpr.expressions.get(i), env, Lists.of(symTable.noType)).get(0));
}
resultTypes = Lists.of(new BTupleType(results));
} else {
// This is a braced expression.
bracedOrTupleExpr.isBracedExpr = true;
final BLangExpression expr = bracedOrTupleExpr.expressions.get(0);
final BType actualType = checkExpr(expr, env, Lists.of(symTable.noType)).get(0);
types.setImplicitCastExpr(expr, actualType, expTypes.get(0));
resultTypes = Lists.of(actualType);
}
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr in project ballerina by ballerina-lang.
the class CodeGenerator method visit.
@Override
public void visit(BLangBracedOrTupleExpr bracedOrTupleExpr) {
// Emit create array instruction
RegIndex exprRegIndex = calcAndGetExprRegIndex(bracedOrTupleExpr);
Operand typeCPIndex = getTypeCPIndex(bracedOrTupleExpr.type);
emit(InstructionCodes.RNEWARRAY, exprRegIndex, typeCPIndex);
// Emit instructions populate initial array values;
for (int i = 0; i < bracedOrTupleExpr.expressions.size(); i++) {
BLangExpression argExpr = bracedOrTupleExpr.expressions.get(i);
genNode(argExpr, this.env);
BLangLiteral indexLiteral = new BLangLiteral();
indexLiteral.pos = argExpr.pos;
indexLiteral.value = (long) i;
indexLiteral.type = symTable.intType;
genNode(indexLiteral, this.env);
emit(InstructionCodes.RASTORE, exprRegIndex, indexLiteral.regIndex, argExpr.regIndex);
}
}
use of org.wso2.ballerinalang.compiler.tree.expressions.BLangBracedOrTupleExpr in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateStreamingIteratorBlock.
private void generateStreamingIteratorBlock(IterableContext ctx, BLangFunction funcNode, LinkedList<Operation> streamOperations) {
final Operation firstOperation = ctx.getFirstOperation();
final DiagnosticPos pos = firstOperation.pos;
// Generate streaming based function Body.
if (isReturningIteratorFunction(ctx)) {
if (ctx.resultType.tag != TypeTags.TABLE) {
// This should be the select operation. No need of a count variable.
createCounterVarDefStmt(funcNode, ctx);
}
createResultVarDefStmt(funcNode, ctx);
ctx.iteratorResultVariables = createIteratorResultVariables(ctx, streamOperations.getLast().retVar, funcNode);
} else {
ctx.iteratorResultVariables = Collections.emptyList();
}
// Create variables required.
final List<BLangVariable> foreachVariables = createForeachVariables(ctx, ctx.getFirstOperation().argVar, funcNode);
// Define all undefined variables.
defineRequiredVariables(ctx, streamOperations, foreachVariables, funcNode);
// Generate foreach iteration.
final BLangForeach foreachStmt = ASTBuilderUtil.createForeach(pos, funcNode.body, ASTBuilderUtil.createVariableRef(pos, ctx.collectionVar.symbol), ASTBuilderUtil.createVariableRefList(pos, foreachVariables), ctx.foreachTypes);
if (foreachVariables.size() > 1) {
// Create tuple, for lambda invocation.
final BLangAssignment assignmentStmt = ASTBuilderUtil.createAssignmentStmt(pos, foreachStmt.body);
assignmentStmt.declaredWithVar = true;
assignmentStmt.varRefs.add(ASTBuilderUtil.createVariableRef(pos, ctx.getFirstOperation().argVar.symbol));
final BLangBracedOrTupleExpr tupleExpr = (BLangBracedOrTupleExpr) TreeBuilder.createBracedOrTupleExpression();
for (BLangVariable foreachVariable : foreachVariables) {
tupleExpr.expressions.add(ASTBuilderUtil.createVariableRef(pos, foreachVariable.symbol));
}
tupleExpr.isBracedExpr = foreachVariables.size() == 1;
tupleExpr.type = new BTupleType(getTupleTypeList(ctx.getFirstOperation().inputType));
assignmentStmt.expr = tupleExpr;
}
// Generate Operations related
ctx.operations.forEach(operation -> generateOperationCode(foreachStmt.body, operation));
// Generate aggregator and result
if (isReturningIteratorFunction(ctx)) {
if (ctx.iteratorResultVariables.size() > 1) {
// Destructure return Values.
final BLangTupleDestructure tupleAssign = (BLangTupleDestructure) TreeBuilder.createTupleDestructureStatementNode();
tupleAssign.pos = pos;
tupleAssign.declaredWithVar = true;
foreachStmt.body.addStatement(tupleAssign);
tupleAssign.expr = ASTBuilderUtil.createVariableRef(pos, ctx.getLastOperation().retVar.symbol);
tupleAssign.varRefs.addAll(ASTBuilderUtil.createVariableRefList(pos, ctx.iteratorResultVariables));
}
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));
}
}
Aggregations