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;
}
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;
}
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));
}
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();
}
}
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);
}
}
Aggregations