Search in sources :

Example 1 with BArrayType

use of org.ballerinalang.model.types.BArrayType in project ballerina by ballerina-lang.

the class CPU method checkArrayEquivalent.

private static boolean checkArrayEquivalent(BType actualType, BType expType) {
    if (expType.getTag() == TypeTags.ARRAY_TAG && actualType.getTag() == TypeTags.ARRAY_TAG) {
        // Both types are array types
        BArrayType lhrArrayType = (BArrayType) expType;
        BArrayType rhsArrayType = (BArrayType) actualType;
        return checkArrayEquivalent(lhrArrayType.getElementType(), rhsArrayType.getElementType());
    }
    // Now one or both types are not array types and they have to be equal
    if (expType == actualType) {
        return true;
    }
    return false;
}
Also used : BArrayType(org.ballerinalang.model.types.BArrayType)

Example 2 with BArrayType

use of org.ballerinalang.model.types.BArrayType in project ballerina by ballerina-lang.

the class BLangProgramRunner method getMainFunction.

public static FunctionInfo getMainFunction(PackageInfo mainPkgInfo) {
    String errorMsg = "main function not found in  '" + mainPkgInfo.getProgramFile().getProgramFilePath() + "'";
    FunctionInfo mainFuncInfo = mainPkgInfo.getFunctionInfo("main");
    if (mainFuncInfo == null) {
        throw new BallerinaException(errorMsg);
    }
    BType[] paramTypes = mainFuncInfo.getParamTypes();
    BType[] retParamTypes = mainFuncInfo.getRetParamTypes();
    BArrayType argsType = new BArrayType(BTypes.typeString);
    if (paramTypes.length != 1 || !paramTypes[0].equals(argsType) || retParamTypes.length != 0) {
        throw new BallerinaException(errorMsg);
    }
    return mainFuncInfo;
}
Also used : BArrayType(org.ballerinalang.model.types.BArrayType) BType(org.ballerinalang.model.types.BType) FunctionInfo(org.ballerinalang.util.codegen.FunctionInfo) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException)

Example 3 with BArrayType

use of org.ballerinalang.model.types.BArrayType in project ballerina by ballerina-lang.

the class JSONUtils method convertJSON.

private static Object convertJSON(JsonNode jsonValue, BType targetType) {
    switch(targetType.getTag()) {
        case TypeTags.INT_TAG:
            return jsonNodeToInt(jsonValue);
        case TypeTags.FLOAT_TAG:
            return jsonNodeToFloat(jsonValue);
        case TypeTags.STRING_TAG:
            if (jsonValue.isString()) {
                return jsonValue.stringValue();
            } else {
                return jsonValue.toString();
            }
        case TypeTags.BOOLEAN_TAG:
            return jsonNodeToBool(jsonValue);
        case TypeTags.UNION_TAG:
            BUnionType type = (BUnionType) targetType;
            if (jsonValue.isNull() && type.isNullable()) {
                return null;
            }
            List<BType> matchingTypes = type.getMemberTypes().stream().filter(memberType -> memberType != BTypes.typeNull).collect(Collectors.toList());
            if (matchingTypes.size() == 1) {
                return convertJSON(jsonValue, matchingTypes.get(0));
            }
            break;
        case TypeTags.STRUCT_TAG:
            return convertJSONNodeToStruct(jsonValue, (BStructType) targetType);
        case TypeTags.ANY_TAG:
        case TypeTags.JSON_TAG:
            if (jsonValue.isNull()) {
                return null;
            }
            return new BJSON(jsonValue);
        case TypeTags.ARRAY_TAG:
            return jsonNodeToBArray(jsonValue, (BArrayType) targetType);
        case TypeTags.MAP_TAG:
            return jsonNodeToBMap(jsonValue, (BMapType) targetType);
        case TypeTags.NULL_TAG:
            if (jsonValue.isNull()) {
                return null;
            }
            break;
        default:
            break;
    }
    throw BLangExceptionHelper.getRuntimeException(RuntimeErrors.INCOMPATIBLE_TYPE_FOR_CASTING, targetType, getTypeName(jsonValue));
}
Also used : BUnionType(org.ballerinalang.model.types.BUnionType) BUnionType(org.ballerinalang.model.types.BUnionType) BFloat(org.ballerinalang.model.values.BFloat) BType(org.ballerinalang.model.types.BType) BBoolean(org.ballerinalang.model.values.BBoolean) OMText(org.apache.axiom.om.OMText) BallerinaException(org.ballerinalang.util.exceptions.BallerinaException) BMapType(org.ballerinalang.model.types.BMapType) RuntimeErrors(org.ballerinalang.util.exceptions.RuntimeErrors) ArrayList(java.util.ArrayList) BJSON(org.ballerinalang.model.values.BJSON) BNewArray(org.ballerinalang.model.values.BNewArray) BXMLSequence(org.ballerinalang.model.values.BXMLSequence) OMNamespace(org.apache.axiom.om.OMNamespace) BString(org.ballerinalang.model.values.BString) TableJSONDataSource(org.ballerinalang.model.TableJSONDataSource) BInteger(org.ballerinalang.model.values.BInteger) BXML(org.ballerinalang.model.values.BXML) BTypes(org.ballerinalang.model.types.BTypes) BStruct(org.ballerinalang.model.values.BStruct) StructInfo(org.ballerinalang.util.codegen.StructInfo) BMap(org.ballerinalang.model.values.BMap) OMElement(org.apache.axiom.om.OMElement) BFloatArray(org.ballerinalang.model.values.BFloatArray) StructFieldInfo(org.ballerinalang.util.codegen.StructFieldInfo) Iterator(java.util.Iterator) Type(org.ballerinalang.model.util.JsonNode.Type) Set(java.util.Set) BStructType(org.ballerinalang.model.types.BStructType) BXMLItem(org.ballerinalang.model.values.BXMLItem) BLangExceptionHelper(org.ballerinalang.util.exceptions.BLangExceptionHelper) TypeTags(org.ballerinalang.model.types.TypeTags) Collectors(java.util.stream.Collectors) BStringArray(org.ballerinalang.model.values.BStringArray) BIntArray(org.ballerinalang.model.values.BIntArray) BArrayType(org.ballerinalang.model.types.BArrayType) List(java.util.List) OMAbstractFactory(org.apache.axiom.om.OMAbstractFactory) BRefType(org.ballerinalang.model.values.BRefType) Entry(java.util.Map.Entry) BRefValueArray(org.ballerinalang.model.values.BRefValueArray) OMFactory(org.apache.axiom.om.OMFactory) BTable(org.ballerinalang.model.values.BTable) BBooleanArray(org.ballerinalang.model.values.BBooleanArray) BValue(org.ballerinalang.model.values.BValue) BType(org.ballerinalang.model.types.BType) BJSON(org.ballerinalang.model.values.BJSON)

Example 4 with BArrayType

use of org.ballerinalang.model.types.BArrayType in project ballerina by ballerina-lang.

the class BTypeDescValue method equals.

public boolean equals(Object obj) {
    // if obj == 'null' or not instance of BTypeValue - return false
    if (obj == null || !(obj instanceof BTypeDescValue)) {
        return false;
    }
    BTypeDescValue typeValue = (BTypeDescValue) obj;
    if ((typeValue.value() instanceof BArrayType) && (this.value() instanceof BArrayType)) {
        BArrayType objArrayType = (BArrayType) typeValue.value();
        BArrayType thisArrayType = (BArrayType) this.value();
        if (objArrayType.getDimensions() != thisArrayType.getDimensions()) {
            return false;
        }
        return (new BTypeDescValue(thisArrayType.getElementType())).equals(new BTypeDescValue(objArrayType.getElementType()));
    } else {
        return typeValue.value() == this.value();
    }
}
Also used : BArrayType(org.ballerinalang.model.types.BArrayType)

Example 5 with BArrayType

use of org.ballerinalang.model.types.BArrayType in project ballerina by ballerina-lang.

the class ProgramFileReader method createBTypeFromSig.

private int createBTypeFromSig(char[] chars, int index, Stack<BType> typeStack, PackageInfo packageInfo) {
    int nameIndex;
    char ch = chars[index];
    switch(ch) {
        case 'I':
            typeStack.push(BTypes.typeInt);
            return index + 1;
        case 'F':
            typeStack.push(BTypes.typeFloat);
            return index + 1;
        case 'S':
            typeStack.push(BTypes.typeString);
            return index + 1;
        case 'B':
            typeStack.push(BTypes.typeBoolean);
            return index + 1;
        case 'L':
            typeStack.push(BTypes.typeBlob);
            return index + 1;
        case 'Y':
            typeStack.push(BTypes.typeDesc);
            return index + 1;
        case 'A':
            typeStack.push(BTypes.typeAny);
            return index + 1;
        case 'R':
            // TODO Improve this logic
            index++;
            nameIndex = index;
            while (chars[nameIndex] != ';') {
                nameIndex++;
            }
            String typeName = new String(Arrays.copyOfRange(chars, index, nameIndex));
            typeStack.push(BTypes.getTypeFromName(typeName));
            return nameIndex + 1;
        case 'C':
        case 'J':
        case 'T':
        case 'E':
        case 'D':
        case 'H':
        case 'Z':
            char typeChar = chars[index];
            // TODO Improve this logic
            index++;
            nameIndex = index;
            int colonIndex = -1;
            while (chars[nameIndex] != ';') {
                if (chars[nameIndex] == ':') {
                    colonIndex = nameIndex;
                }
                nameIndex++;
            }
            String pkgPath;
            String name;
            PackageInfo packageInfoOfType;
            if (colonIndex != -1) {
                pkgPath = new String(Arrays.copyOfRange(chars, index, colonIndex));
                name = new String(Arrays.copyOfRange(chars, colonIndex + 1, nameIndex));
                packageInfoOfType = programFile.getPackageInfo(pkgPath);
            } else {
                name = new String(Arrays.copyOfRange(chars, index, nameIndex));
                // Setting the current package;
                packageInfoOfType = packageInfo;
            }
            if (typeChar == 'C') {
                typeStack.push(packageInfoOfType.getConnectorInfo(name).getType());
            } else if (typeChar == 'J') {
                if (name.isEmpty()) {
                    typeStack.push(BTypes.typeJSON);
                } else {
                    typeStack.push(new BJSONType(packageInfoOfType.getStructInfo(name).getType()));
                }
            } else if (typeChar == 'D') {
                if (name.isEmpty()) {
                    typeStack.push(BTypes.typeTable);
                } else {
                    typeStack.push(new BTableType(packageInfoOfType.getStructInfo(name).getType()));
                }
            } else if (typeChar == 'H') {
                if (name.isEmpty()) {
                    typeStack.push(BTypes.typeStream);
                } else {
                    typeStack.push(new BStreamType(packageInfoOfType.getStructInfo(name).getType()));
                }
            } else if (typeChar == 'E') {
                typeStack.push(packageInfoOfType.getEnumInfo(name).getType());
            } else {
                // This is a struct type
                typeStack.push(packageInfoOfType.getStructInfo(name).getType());
            }
            return nameIndex + 1;
        case '[':
            index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo);
            BType elemType = typeStack.pop();
            BArrayType arrayType = new BArrayType(elemType);
            typeStack.push(arrayType);
            return index;
        case 'M':
            index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo);
            BType constrainedType = typeStack.pop();
            BType mapType;
            if (constrainedType == BTypes.typeAny) {
                mapType = BTypes.typeMap;
            } else {
                mapType = new BMapType(constrainedType);
            }
            typeStack.push(mapType);
            return index;
        case 'U':
            // TODO : Fix this for type casting.
            typeStack.push(new BFunctionType());
            return index + 1;
        case 'O':
        case 'P':
            typeChar = chars[index];
            index++;
            nameIndex = index;
            while (chars[nameIndex] != ';') {
                nameIndex++;
            }
            List<BType> memberTypes = new ArrayList<>();
            int memberCount = Integer.parseInt(new String(Arrays.copyOfRange(chars, index, nameIndex)));
            index = nameIndex;
            for (int i = 0; i < memberCount; i++) {
                index = createBTypeFromSig(chars, index + 1, typeStack, packageInfo) - 1;
                memberTypes.add(typeStack.pop());
            }
            if (typeChar == 'O') {
                typeStack.push(new BUnionType(memberTypes));
            } else if (typeChar == 'P') {
                typeStack.push(new BTupleType(memberTypes));
            }
            return index + 1;
        case 'N':
            typeStack.push(BTypes.typeNull);
            return index + 1;
        default:
            throw new ProgramFileFormatException("unsupported base type char: " + ch);
    }
}
Also used : BArrayType(org.ballerinalang.model.types.BArrayType) BJSONType(org.ballerinalang.model.types.BJSONType) BFunctionType(org.ballerinalang.model.types.BFunctionType) ArrayList(java.util.ArrayList) BTupleType(org.ballerinalang.model.types.BTupleType) ProgramFileFormatException(org.ballerinalang.util.exceptions.ProgramFileFormatException) BStreamType(org.ballerinalang.model.types.BStreamType) BUnionType(org.ballerinalang.model.types.BUnionType) BMapType(org.ballerinalang.model.types.BMapType) BType(org.ballerinalang.model.types.BType) BTableType(org.ballerinalang.model.types.BTableType)

Aggregations

BArrayType (org.ballerinalang.model.types.BArrayType)11 BType (org.ballerinalang.model.types.BType)7 BMapType (org.ballerinalang.model.types.BMapType)4 BallerinaException (org.ballerinalang.util.exceptions.BallerinaException)4 ArrayList (java.util.ArrayList)3 BStructType (org.ballerinalang.model.types.BStructType)3 BRefValueArray (org.ballerinalang.model.values.BRefValueArray)3 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 BFunctionType (org.ballerinalang.model.types.BFunctionType)2 BJSONType (org.ballerinalang.model.types.BJSONType)2 BStreamType (org.ballerinalang.model.types.BStreamType)2 BTableType (org.ballerinalang.model.types.BTableType)2 BUnionType (org.ballerinalang.model.types.BUnionType)2 TypeTags (org.ballerinalang.model.types.TypeTags)2 BRefType (org.ballerinalang.model.values.BRefType)2 BStruct (org.ballerinalang.model.values.BStruct)2 Arrays (java.util.Arrays)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1