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