use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.
the class Desugar method addArgInitExpr.
private void addArgInitExpr(BLangTransformer transformerNode, BLangVariable var) {
BLangSimpleVarRef varRef = new BLangLocalVarRef(var.symbol);
varRef.lhsVar = true;
varRef.pos = var.pos;
varRef.type = var.type;
BLangExpression initExpr = null;
switch(var.type.tag) {
case TypeTags.MAP:
initExpr = new BLangMapLiteral(new ArrayList<>(), var.type);
break;
case TypeTags.JSON:
initExpr = new BLangJSONLiteral(new ArrayList<>(), var.type);
break;
case TypeTags.STRUCT:
initExpr = new BLangStructLiteral(new ArrayList<>(), var.type);
break;
case TypeTags.INT:
case TypeTags.FLOAT:
case TypeTags.STRING:
case TypeTags.BOOLEAN:
case TypeTags.BLOB:
case TypeTags.XML:
return;
case TypeTags.TABLE:
case TypeTags.STREAM:
// TODO: add this once the able initializing is supported.
return;
default:
return;
}
initExpr.pos = var.pos;
BLangAssignment assignStmt = (BLangAssignment) TreeBuilder.createAssignmentNode();
assignStmt.pos = var.pos;
assignStmt.addVariable(varRef);
assignStmt.expr = initExpr;
transformerNode.body.stmts.add(0, assignStmt);
}
use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.
the class Desugar method generateIfElseStmt.
/**
* Generate an if-else statement from the given match statement.
*
* @param patternClause match pattern statement node
* @param matchExprVar variable node of the match expression
* @return if else statement node
*/
private BLangIf generateIfElseStmt(BLangMatchStmtPatternClause patternClause, BLangVariable matchExprVar) {
BLangExpression patternIfCondition = createPatternIfCondition(patternClause, matchExprVar.symbol);
BLangBlockStmt patternBody = getMatchPatternBody(patternClause, matchExprVar);
return ASTBuilderUtil.createIfElseStmt(patternClause.pos, patternIfCondition, patternBody, null);
}
use of org.wso2.ballerinalang.compiler.tree.BLangVariable in project ballerina by ballerina-lang.
the class EndpointDesugar method generateServiceRegistered.
private BLangBlockStmt generateServiceRegistered(BEndpointVarSymbol endpoint, BLangService service, SymbolEnv env, BSymbol encSymbol, BSymbol varEncSymbol) {
final DiagnosticPos pos = service.pos;
final String epName = endpoint.name.value;
BLangBlockStmt temp = new BLangBlockStmt();
final BLangVariable epVariable = ASTBuilderUtil.createVariable(pos, epName, endpoint.type);
final Name name = endpoint.name;
epVariable.symbol = (BVarSymbol) symResolver.lookupMemberSymbol(pos, encSymbol.scope, env, name, SymTag.VARIABLE);
final BLangVariableDef serviceTypeDef = ASTBuilderUtil.createVariableDefStmt(pos, temp);
serviceTypeDef.var = ASTBuilderUtil.createVariable(pos, service.name + "type", symTable.typeDesc);
ASTBuilderUtil.defineVariable(serviceTypeDef.var, varEncSymbol, names);
final BLangUnaryExpr typeOfExpr = ASTBuilderUtil.createUnaryExpr(pos);
serviceTypeDef.var.expr = typeOfExpr;
typeOfExpr.operator = OperatorKind.TYPEOF;
typeOfExpr.expr = getTypeAccessExpression(pos, service.symbol.type);
typeOfExpr.type = symTable.typeDesc;
List<BLangVariable> args = Lists.of(serviceTypeDef.var);
if (endpoint.registerFunction != null && endpoint.registerFunction.params.size() == 2) {
// Endpoint is already desugared. Fix this correctly.
args.add(0, epVariable);
}
final BLangExpressionStmt expressionStmt = ASTBuilderUtil.createExpressionStmt(pos, temp);
final BLangInvocation iExpr = ASTBuilderUtil.createInvocationExpr(pos, endpoint.registerFunction, args, symResolver);
if (endpoint.registerFunction != null && endpoint.registerFunction.params.size() != 2) {
iExpr.expr = ASTBuilderUtil.createVariableRef(epVariable.pos, epVariable.symbol);
}
expressionStmt.expr = iExpr;
return temp;
}
use of org.wso2.ballerinalang.compiler.tree.BLangVariable 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.tree.BLangVariable in project ballerina by ballerina-lang.
the class IterableCodeDesugar method generateCountAggregator.
/**
* Generates following.
*
* variable = variable + 1;
*
* @param blockStmt target
* @param variable variable to increment
*/
private void generateCountAggregator(BLangBlockStmt blockStmt, BLangVariable variable) {
final DiagnosticPos pos = blockStmt.pos;
// create count = count + 1;
final BLangBinaryExpr add = (BLangBinaryExpr) TreeBuilder.createBinaryExpressionNode();
add.pos = pos;
add.type = symTable.intType;
add.opKind = OperatorKind.ADD;
add.lhsExpr = ASTBuilderUtil.createVariableRef(pos, variable.symbol);
add.rhsExpr = ASTBuilderUtil.createLiteral(pos, symTable.intType, 1L);
add.opSymbol = (BOperatorSymbol) symResolver.resolveBinaryOperator(OperatorKind.ADD, symTable.intType, symTable.intType);
final BLangAssignment countAdd = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
countAdd.varRefs.add(ASTBuilderUtil.createVariableRef(pos, variable.symbol));
countAdd.expr = add;
}
Aggregations