Search in sources :

Example 1 with BLangArrayAccessExpr

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

the class CodeGenerator method visit.

@Override
public void visit(BLangArrayAccessExpr arrayIndexAccessExpr) {
    boolean variableStore = this.varAssignment;
    this.varAssignment = false;
    genNode(arrayIndexAccessExpr.expr, this.env);
    Operand varRefRegIndex = arrayIndexAccessExpr.expr.regIndex;
    genNode(arrayIndexAccessExpr.indexExpr, this.env);
    Operand indexRegIndex = arrayIndexAccessExpr.indexExpr.regIndex;
    BArrayType arrayType = (BArrayType) arrayIndexAccessExpr.expr.type;
    if (variableStore) {
        int opcode = getOpcode(arrayType.eType.tag, InstructionCodes.IASTORE);
        emit(opcode, varRefRegIndex, indexRegIndex, arrayIndexAccessExpr.regIndex);
    } else {
        int opcode = getOpcode(arrayType.eType.tag, InstructionCodes.IALOAD);
        emit(opcode, varRefRegIndex, indexRegIndex, calcAndGetExprRegIndex(arrayIndexAccessExpr));
    }
    this.varAssignment = variableStore;
}
Also used : BArrayType(org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 2 with BLangArrayAccessExpr

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

the class Desugar method visit.

@Override
public void visit(BLangIndexBasedAccess indexAccessExpr) {
    BLangVariableReference targetVarRef = indexAccessExpr;
    indexAccessExpr.indexExpr = rewriteExpr(indexAccessExpr.indexExpr);
    indexAccessExpr.expr = rewriteExpr(indexAccessExpr.expr);
    BType varRefType = indexAccessExpr.expr.type;
    if (varRefType.tag == TypeTags.STRUCT) {
        targetVarRef = new BLangStructFieldAccessExpr(indexAccessExpr.pos, indexAccessExpr.expr, indexAccessExpr.symbol);
    } else if (varRefType.tag == TypeTags.MAP) {
        targetVarRef = new BLangMapAccessExpr(indexAccessExpr.pos, indexAccessExpr.expr, indexAccessExpr.indexExpr);
    } else if (varRefType.tag == TypeTags.JSON || getElementType(varRefType).tag == TypeTags.JSON) {
        targetVarRef = new BLangJSONAccessExpr(indexAccessExpr.pos, indexAccessExpr.expr, indexAccessExpr.indexExpr);
    } else if (varRefType.tag == TypeTags.ARRAY) {
        targetVarRef = new BLangArrayAccessExpr(indexAccessExpr.pos, indexAccessExpr.expr, indexAccessExpr.indexExpr);
    } else if (varRefType.tag == TypeTags.XML) {
        targetVarRef = new BLangXMLAccessExpr(indexAccessExpr.pos, indexAccessExpr.expr, indexAccessExpr.indexExpr);
    }
    targetVarRef.lhsVar = indexAccessExpr.lhsVar;
    targetVarRef.type = indexAccessExpr.type;
    result = targetVarRef;
}
Also used : BLangStructFieldAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess.BLangStructFieldAccessExpr) BLangArrayAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangArrayAccessExpr) BLangVariableReference(org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) BLangMapAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangMapAccessExpr) BLangXMLAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangXMLAccessExpr) BLangJSONAccessExpr(org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangJSONAccessExpr)

Aggregations

BArrayType (org.wso2.ballerinalang.compiler.semantics.model.types.BArrayType)1 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)1 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)1 BLangStructFieldAccessExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangFieldBasedAccess.BLangStructFieldAccessExpr)1 BLangArrayAccessExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangArrayAccessExpr)1 BLangJSONAccessExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangJSONAccessExpr)1 BLangMapAccessExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangMapAccessExpr)1 BLangXMLAccessExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangIndexBasedAccess.BLangXMLAccessExpr)1 BLangVariableReference (org.wso2.ballerinalang.compiler.tree.expressions.BLangVariableReference)1 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)1