Search in sources :

Example 6 with BLangArrayLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral 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;
    }
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) BLangBlockStmt(org.wso2.ballerinalang.compiler.tree.statements.BLangBlockStmt) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) IterableKind(org.wso2.ballerinalang.compiler.semantics.model.iterable.IterableKind) BLangVariableDef(org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType) BLangVariable(org.wso2.ballerinalang.compiler.tree.BLangVariable)

Example 7 with BLangArrayLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral in project ballerina by ballerina-lang.

the class BLangPackageBuilder method addArrayInitExpr.

public void addArrayInitExpr(DiagnosticPos pos, Set<Whitespace> ws, boolean argsAvailable) {
    List<ExpressionNode> argExprList;
    BLangArrayLiteral arrayLiteral = (BLangArrayLiteral) TreeBuilder.createArrayLiteralNode();
    if (argsAvailable) {
        arrayLiteral.addWS(commaWsStack.pop());
        argExprList = exprNodeListStack.pop();
    } else {
        argExprList = new ArrayList<>(0);
    }
    arrayLiteral.exprs = argExprList.stream().map(expr -> (BLangExpression) expr).collect(Collectors.toList());
    arrayLiteral.pos = pos;
    arrayLiteral.addWS(ws);
    addExpressionNode(arrayLiteral);
}
Also used : ExpressionNode(org.ballerinalang.model.tree.expressions.ExpressionNode) SelectExpressionNode(org.ballerinalang.model.tree.clauses.SelectExpressionNode) BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)

Example 8 with BLangArrayLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral in project kubernetes by ballerinax.

the class KubernetesAnnotationProcessor method processSecrets.

/**
 * Process Secrets annotations.
 *
 * @param attachmentNode Attachment Node
 * @return List of @{@link SecretModel} objects
 */
Set<SecretModel> processSecrets(AnnotationAttachmentNode attachmentNode) throws KubernetesPluginException {
    Set<SecretModel> secrets = new HashSet<>();
    List<BLangRecordLiteral.BLangRecordKeyValue> keyValues = ((BLangRecordLiteral) ((BLangAnnotationAttachment) attachmentNode).expr).getKeyValuePairs();
    for (BLangRecordLiteral.BLangRecordKeyValue keyValue : keyValues) {
        List<BLangExpression> secretAnnotation = ((BLangArrayLiteral) keyValue.valueExpr).exprs;
        for (BLangExpression bLangExpression : secretAnnotation) {
            SecretModel secretModel = new SecretModel();
            List<BLangRecordLiteral.BLangRecordKeyValue> annotationValues = ((BLangRecordLiteral) bLangExpression).getKeyValuePairs();
            for (BLangRecordLiteral.BLangRecordKeyValue annotation : annotationValues) {
                VolumeMountConfig volumeMountConfig = VolumeMountConfig.valueOf(annotation.getKey().toString());
                String annotationValue = resolveValue(annotation.getValue().toString());
                switch(volumeMountConfig) {
                    case name:
                        secretModel.setName(getValidName(annotationValue));
                        break;
                    case mountPath:
                        secretModel.setMountPath(annotationValue);
                        break;
                    case data:
                        List<BLangExpression> data = ((BLangArrayLiteral) annotation.valueExpr).exprs;
                        secretModel.setData(getDataForSecret(data));
                        break;
                    case readOnly:
                        secretModel.setReadOnly(Boolean.parseBoolean(annotationValue));
                        break;
                    default:
                        break;
                }
            }
            secrets.add(secretModel);
        }
    }
    return secrets;
}
Also used : BLangArrayLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral) SecretModel(org.ballerinax.kubernetes.models.SecretModel) BLangRecordLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) HashSet(java.util.HashSet)

Example 9 with BLangArrayLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral in project ballerina by ballerina-lang.

the class TypeChecker method visit.

public void visit(BLangArrayLiteral arrayLiteral) {
    // Check whether the expected type is an array type
    // var a = []; and var a = [1,2,3,4]; are illegal statements, because we cannot infer the type here.
    BType actualType = symTable.errType;
    int expTypeTag = expTypes.get(0).tag;
    if (expTypeTag == TypeTags.JSON || expTypeTag == TypeTags.ANY) {
        checkExprs(arrayLiteral.exprs, this.env, expTypes.get(0));
        actualType = expTypes.get(0);
    } else if (expTypeTag == TypeTags.ARRAY) {
        BArrayType arrayType = (BArrayType) expTypes.get(0);
        checkExprs(arrayLiteral.exprs, this.env, arrayType.eType);
        actualType = new BArrayType(arrayType.eType);
    } else if (expTypeTag != TypeTags.ERROR) {
        List<BType> resTypes = checkExprs(arrayLiteral.exprs, this.env, symTable.noType);
        Set<BType> arrayLitExprTypeSet = new HashSet<>(resTypes);
        BType[] uniqueExprTypes = arrayLitExprTypeSet.toArray(new BType[0]);
        if (uniqueExprTypes.length == 0) {
            actualType = symTable.anyType;
        } else if (uniqueExprTypes.length == 1) {
            actualType = resTypes.get(0);
        } else {
            BType superType = uniqueExprTypes[0];
            for (int i = 1; i < uniqueExprTypes.length; i++) {
                if (types.isAssignable(superType, uniqueExprTypes[i])) {
                    superType = uniqueExprTypes[i];
                } else if (!types.isAssignable(uniqueExprTypes[i], superType)) {
                    superType = symTable.anyType;
                    break;
                }
            }
            actualType = superType;
        }
        actualType = new BArrayType(actualType);
    }
    resultTypes = types.checkTypes(arrayLiteral, Lists.of(actualType), expTypes);
}
Also used : BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) HashSet(java.util.HashSet)

Example 10 with BLangArrayLiteral

use of org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangArrayLiteral arrayLiteral) {
    BType etype;
    if (arrayLiteral.type.tag == TypeTags.ANY) {
        etype = arrayLiteral.type;
    } else {
        etype = ((BArrayType) arrayLiteral.type).eType;
    }
    // Emit create array instruction
    int opcode = getOpcode(etype.tag, InstructionCodes.INEWARRAY);
    Operand arrayVarRegIndex = calcAndGetExprRegIndex(arrayLiteral);
    Operand typeCPIndex = getTypeCPIndex(arrayLiteral.type);
    emit(opcode, arrayVarRegIndex, typeCPIndex);
    // Emit instructions populate initial array values;
    for (int i = 0; i < arrayLiteral.exprs.size(); i++) {
        BLangExpression argExpr = arrayLiteral.exprs.get(i);
        genNode(argExpr, this.env);
        BLangLiteral indexLiteral = new BLangLiteral();
        indexLiteral.pos = arrayLiteral.pos;
        indexLiteral.value = (long) i;
        indexLiteral.type = symTable.intType;
        genNode(indexLiteral, this.env);
        opcode = getOpcode(argExpr.type.tag, InstructionCodes.IASTORE);
        emit(opcode, arrayVarRegIndex, indexLiteral.regIndex, argExpr.regIndex);
    }
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Aggregations

BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)12 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)6 HashSet (java.util.HashSet)5 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)5 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)4 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 StreamingQueryStatementNode (org.ballerinalang.model.tree.statements.StreamingQueryStatementNode)2 BInvokableSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BInvokableSymbol)2 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)2 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)2 BArrayType (org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 BLangJSONArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral.BLangJSONArrayLiteral)2 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)2 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1