Search in sources :

Example 51 with BLangBlockStmt

use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt in project ballerina by ballerina-lang.

the class EndpointDesugar method rewriteEndpoint.

void rewriteEndpoint(BLangEndpoint endpoint, SymbolEnv env) {
    final BSymbol encSymbol, varSymbol;
    final BLangBlockStmt initBlock, startBlock;
    BLangBlockStmt stopBlock = null;
    if (env.enclInvokable != null) {
        // Function, Action, Resource. Code generate to its body directly.
        encSymbol = varSymbol = env.enclInvokable.symbol;
        initBlock = startBlock = ((BLangInvokableNode) env.node).body;
    } else if (env.enclService != null) {
        encSymbol = env.enclService.symbol;
        varSymbol = ((BLangService) env.node).initFunction.symbol;
        initBlock = startBlock = ((BLangService) env.node).initFunction.body;
    } else {
        // Pkg level endpoint.
        encSymbol = env.enclPkg.symbol;
        varSymbol = ((BLangPackage) env.node).initFunction.symbol;
        initBlock = ((BLangPackage) env.node).initFunction.body;
        startBlock = ((BLangPackage) env.node).startFunction.body;
        stopBlock = ((BLangPackage) env.node).stopFunction.body;
    }
    BLangBlockStmt genInit, genInitCall, genStartCall, genStopCall;
    genInit = generateEndpointInit(endpoint, env, encSymbol);
    genInitCall = generateEndpointInitFunctionCall(endpoint, env, encSymbol, varSymbol);
    genStartCall = generateEndpointStartOrStop(endpoint, endpoint.symbol.startFunction, env, encSymbol);
    genStopCall = generateEndpointStartOrStop(endpoint, endpoint.symbol.stopFunction, env, encSymbol);
    if (env.enclInvokable != null) {
        ASTBuilderUtil.prependStatements(genStartCall, startBlock);
        ASTBuilderUtil.prependStatements(genInitCall, initBlock);
        // TODO : Implement stop.
        ASTBuilderUtil.prependStatements(genInit, initBlock);
    } else if (env.enclService != null) {
        ASTBuilderUtil.appendStatements(genInit, initBlock);
        ASTBuilderUtil.appendStatements(genInitCall, initBlock);
        ASTBuilderUtil.appendStatements(genStartCall, startBlock);
    // TODO : Implement stop.
    } else {
        ASTBuilderUtil.appendStatements(genInit, initBlock);
        ASTBuilderUtil.appendStatements(genInitCall, initBlock);
        ASTBuilderUtil.appendStatements(genStartCall, startBlock);
        ASTBuilderUtil.appendStatements(genStopCall, Objects.requireNonNull(stopBlock));
    }
}
Also used : BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BLangService(org.wso2.ballerinalang.compiler.tree.BLangService) BLangInvokableNode(org.wso2.ballerinalang.compiler.tree.BLangInvokableNode)

Example 52 with BLangBlockStmt

use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt 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;
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr) BLangInvocation(org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation) BLangExpressionStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable) Name(org.wso2.ballerinalang.compiler.util.Name)

Example 53 with BLangBlockStmt

use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt in project ballerina by ballerina-lang.

the class IterableCodeDesugar method generateMap.

/**
 * Generates statements for Map/Select operation. Select operation is similar to Map except that Select returns
 * a Table where as Map returns an Array.
 *
 * v3,v4 = lambda(v1,v2);
 *
 * @param blockStmt target
 * @param operation operation instance
 */
private void generateMap(BLangBlockStmt blockStmt, Operation operation) {
    final DiagnosticPos pos = operation.pos;
    final BLangAssignment assignment = ASTBuilderUtil.createAssignmentStmt(pos, blockStmt);
    assignment.varRefs.add(ASTBuilderUtil.createVariableRef(operation.pos, operation.retVar.symbol));
    assignment.expr = ASTBuilderUtil.createInvocationExpr(pos, operation.lambdaSymbol, Lists.of(operation.argVar), symResolver);
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)

Example 54 with BLangBlockStmt

use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt 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));
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangAssignment(org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment) BLangReturn(org.wso2.ballerinalang.compiler.tree.statements.BLangReturn) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf) BLangBinaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)

Example 55 with BLangBlockStmt

use of org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt in project ballerina by ballerina-lang.

the class IterableCodeDesugar method generateFilter.

/**
 * Generates statements for filter operation.
 *
 * if(!lambda(...)){
 * next;
 * }
 *
 * @param blockStmt target
 * @param operation operation instance
 */
private void generateFilter(BLangBlockStmt blockStmt, Operation operation) {
    final DiagnosticPos pos = operation.pos;
    final BLangIf ifNode = ASTBuilderUtil.createIfStmt(pos, blockStmt);
    final BLangUnaryExpr notExpr = (BLangUnaryExpr) TreeBuilder.createUnaryExpressionNode();
    notExpr.pos = pos;
    notExpr.operator = OperatorKind.NOT;
    notExpr.opSymbol = (BOperatorSymbol) symResolver.resolveUnaryOperator(pos, notExpr.operator, symTable.booleanType);
    notExpr.expr = ASTBuilderUtil.createInvocationExpr(pos, operation.lambdaSymbol, Lists.of(operation.argVar), symResolver);
    notExpr.type = symTable.booleanType;
    ifNode.expr = notExpr;
    ifNode.body = ASTBuilderUtil.createBlockStmt(pos);
    ASTBuilderUtil.createNextStmt(pos, ifNode.body);
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangUnaryExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangUnaryExpr) BLangIf(org.wso2.ballerinalang.compiler.tree.statements.BLangIf)

Aggregations

BLangBlockStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt)33 DiagnosticPos (org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos)22 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)19 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)16 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)12 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)9 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)8 BLangIf (org.wso2.ballerinalang.compiler.tree.statements.BLangIf)8 BLangExpressionStmt (org.wso2.ballerinalang.compiler.tree.statements.BLangExpressionStmt)7 BLangBinaryExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangBinaryExpr)6 BLangIndexBasedAccess (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess)6 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)6 BLangReturn (org.wso2.ballerinalang.compiler.tree.statements.BLangReturn)6 Name (org.wso2.ballerinalang.compiler.util.Name)6 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)5 BLangMatchStmtPatternClause (org.wso2.ballerinalang.compiler.tree.statements.BLangMatch.BLangMatchStmtPatternClause)5 ArrayList (java.util.ArrayList)4 BLangNode (org.wso2.ballerinalang.compiler.tree.BLangNode)4 BLangForeach (org.wso2.ballerinalang.compiler.tree.statements.BLangForeach)4 Whitespace (org.ballerinalang.model.Whitespace)3