Search in sources :

Example 6 with BMapType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BMapType in project ballerina by ballerina-lang.

the class Types method checkForeachTypes.

List<BType> checkForeachTypes(BLangNode collection, int variableSize) {
    BType collectionType = collection.type;
    List<BType> errorTypes;
    int maxSupportedTypes;
    switch(collectionType.tag) {
        case TypeTags.ARRAY:
            BArrayType bArrayType = (BArrayType) collectionType;
            if (variableSize == 1) {
                return Lists.of(bArrayType.eType);
            } else if (variableSize == 2) {
                return Lists.of(symTable.intType, bArrayType.eType);
            } else {
                maxSupportedTypes = 2;
                errorTypes = Lists.of(symTable.intType, bArrayType.eType);
            }
            break;
        case TypeTags.MAP:
            BMapType bMapType = (BMapType) collectionType;
            if (variableSize == 1) {
                return Lists.of(bMapType.constraint);
            } else if (variableSize == 2) {
                return Lists.of(symTable.stringType, bMapType.constraint);
            } else {
                maxSupportedTypes = 2;
                errorTypes = Lists.of(symTable.stringType, bMapType.constraint);
            }
            break;
        case TypeTags.JSON:
            if (variableSize == 1) {
                return Lists.of(symTable.jsonType);
            } else {
                maxSupportedTypes = 1;
                errorTypes = Lists.of(symTable.jsonType);
            }
            break;
        case TypeTags.XML:
            if (variableSize == 1) {
                return Lists.of(symTable.xmlType);
            } else if (variableSize == 2) {
                return Lists.of(symTable.intType, symTable.xmlType);
            } else {
                maxSupportedTypes = 2;
                errorTypes = Lists.of(symTable.intType, symTable.xmlType);
            }
            break;
        case TypeTags.TABLE:
            BTableType tableType = (BTableType) collectionType;
            if (variableSize == 1) {
                return Lists.of(tableType.constraint);
            } else {
                maxSupportedTypes = 1;
                errorTypes = Lists.of(tableType.constraint);
            }
            break;
        case TypeTags.ERROR:
            return Collections.nCopies(variableSize, symTable.errType);
        default:
            dlog.error(collection.pos, DiagnosticCode.ITERABLE_NOT_SUPPORTED_COLLECTION, collectionType);
            return Collections.nCopies(variableSize, symTable.errType);
    }
    dlog.error(collection.pos, DiagnosticCode.ITERABLE_TOO_MANY_VARIABLES, collectionType);
    errorTypes.addAll(Collections.nCopies(variableSize - maxSupportedTypes, symTable.errType));
    return errorTypes;
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BTableType(org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)

Example 7 with BMapType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BMapType in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangMapLiteral mapLiteral) {
    Operand mapVarRegIndex = calcAndGetExprRegIndex(mapLiteral);
    Operand typeCPIndex = getTypeCPIndex(mapLiteral.type);
    emit(InstructionCodes.NEWMAP, mapVarRegIndex, typeCPIndex);
    // Handle Map init stuff
    for (BLangRecordKeyValue keyValue : mapLiteral.keyValuePairs) {
        BLangExpression keyExpr = keyValue.key.expr;
        genNode(keyExpr, this.env);
        BLangExpression valueExpr = keyValue.valueExpr;
        genNode(valueExpr, this.env);
        BMapType mapType = (BMapType) mapLiteral.type;
        int opcode = getValueToRefTypeCastOpcode(mapType.constraint.tag);
        if (opcode == InstructionCodes.NOP) {
            emit(InstructionCodes.MAPSTORE, mapVarRegIndex, keyExpr.regIndex, valueExpr.regIndex);
        } else {
            RegIndex refRegMapValue = getRegIndex(TypeTags.ANY);
            emit(opcode, valueExpr.regIndex, refRegMapValue);
            emit(InstructionCodes.MAPSTORE, mapVarRegIndex, keyExpr.regIndex, refRegMapValue);
        }
    }
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangRecordKeyValue(org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 8 with BMapType

use of org.wso2.ballerinalang.compiler.semantics.model.types.BMapType in project ballerina by ballerina-lang.

the class CodeGenerator method visit.

@Override
public void visit(BLangMapAccessExpr mapKeyAccessExpr) {
    boolean variableStore = this.varAssignment;
    this.varAssignment = false;
    genNode(mapKeyAccessExpr.expr, this.env);
    Operand varRefRegIndex = mapKeyAccessExpr.expr.regIndex;
    genNode(mapKeyAccessExpr.indexExpr, this.env);
    Operand keyRegIndex = mapKeyAccessExpr.indexExpr.regIndex;
    BMapType mapType = (BMapType) mapKeyAccessExpr.expr.type;
    if (variableStore) {
        int opcode = getValueToRefTypeCastOpcode(mapType.constraint.tag);
        if (opcode == InstructionCodes.NOP) {
            emit(InstructionCodes.MAPSTORE, varRefRegIndex, keyRegIndex, mapKeyAccessExpr.regIndex);
        } else {
            RegIndex refRegMapValue = getRegIndex(TypeTags.ANY);
            emit(opcode, mapKeyAccessExpr.regIndex, refRegMapValue);
            emit(InstructionCodes.MAPSTORE, varRefRegIndex, keyRegIndex, refRegMapValue);
        }
    } else {
        int opcode = getRefToValueTypeCastOpcode(mapType.constraint.tag);
        if (opcode == InstructionCodes.NOP) {
            emit(InstructionCodes.MAPLOAD, varRefRegIndex, keyRegIndex, calcAndGetExprRegIndex(mapKeyAccessExpr));
        } else {
            RegIndex refRegMapValue = getRegIndex(TypeTags.ANY);
            emit(InstructionCodes.MAPLOAD, varRefRegIndex, keyRegIndex, refRegMapValue);
            emit(opcode, refRegMapValue, calcAndGetExprRegIndex(mapKeyAccessExpr));
        }
    }
    this.varAssignment = variableStore;
}
Also used : BMapType(org.wso2.ballerinalang.compiler.semantics.model.types.BMapType) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Aggregations

BMapType (org.wso2.ballerinalang.compiler.semantics.model.types.BMapType)6 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)6 BJSONType (org.wso2.ballerinalang.compiler.semantics.model.types.BJSONType)4 BTableType (org.wso2.ballerinalang.compiler.semantics.model.types.BTableType)4 BArrayType (org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)3 BIntermediateCollectionType (org.wso2.ballerinalang.compiler.semantics.model.types.BIntermediateCollectionType)2 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)2 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)2 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)2 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)2 Operation (org.wso2.ballerinalang.compiler.semantics.model.iterable.Operation)1 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)1 BEnumType (org.wso2.ballerinalang.compiler.semantics.model.types.BEnumType)1 BFutureType (org.wso2.ballerinalang.compiler.semantics.model.types.BFutureType)1 BStreamType (org.wso2.ballerinalang.compiler.semantics.model.types.BStreamType)1 BTupleType (org.wso2.ballerinalang.compiler.semantics.model.types.BTupleType)1 BXMLType (org.wso2.ballerinalang.compiler.semantics.model.types.BXMLType)1 BLangRecordKeyValue (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral.BLangRecordKeyValue)1 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)1 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)1