Search in sources :

Example 31 with BLangExpression

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

the class TypeChecker method concatSimilarKindXMLNodes.

/**
 * Concatenate the consecutive text type nodes, and get the reduced set of children.
 *
 * @param exprs         Child nodes
 * @param xmlElementEnv
 * @return Reduced set of children
 */
private List<BLangExpression> concatSimilarKindXMLNodes(List<BLangExpression> exprs, SymbolEnv xmlElementEnv) {
    List<BLangExpression> newChildren = new ArrayList<BLangExpression>();
    BLangExpression strConcatExpr = null;
    for (BLangExpression expr : exprs) {
        BType exprType = checkExpr((BLangExpression) expr, xmlElementEnv).get(0);
        if (exprType == symTable.xmlType) {
            if (strConcatExpr != null) {
                newChildren.add(getXMLTextLiteral(strConcatExpr));
                strConcatExpr = null;
            }
            newChildren.add(expr);
            continue;
        }
        BSymbol opSymbol = symResolver.resolveBinaryOperator(OperatorKind.ADD, symTable.stringType, exprType);
        if (opSymbol == symTable.notFoundSymbol && exprType != symTable.errType) {
            dlog.error(expr.pos, DiagnosticCode.INCOMPATIBLE_TYPES, symTable.xmlType, exprType);
        }
        if (strConcatExpr == null) {
            strConcatExpr = expr;
            continue;
        }
        strConcatExpr = getBinaryAddExpr(strConcatExpr, expr, opSymbol);
    }
    // Add remaining concatenated text nodes as children
    if (strConcatExpr != null) {
        newChildren.add(getXMLTextLiteral(strConcatExpr));
    }
    return newChildren;
}
Also used : BSymbol(org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol) BType(org.wso2.ballerinalang.compiler.semantics.model.types.BType) ArrayList(java.util.ArrayList) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)

Example 32 with BLangExpression

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

the class CodeGenerator method visit.

@Override
public void visit(BLangJSONArrayLiteral arrayLiteral) {
    arrayLiteral.regIndex = calcAndGetExprRegIndex(arrayLiteral);
    List<BLangExpression> argExprs = arrayLiteral.exprs;
    BLangLiteral arraySizeLiteral = new BLangLiteral();
    arraySizeLiteral.pos = arrayLiteral.pos;
    arraySizeLiteral.value = (long) argExprs.size();
    arraySizeLiteral.type = symTable.intType;
    genNode(arraySizeLiteral, this.env);
    emit(InstructionCodes.JSONNEWARRAY, arrayLiteral.regIndex, arraySizeLiteral.regIndex);
    for (int i = 0; i < argExprs.size(); i++) {
        BLangExpression argExpr = argExprs.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);
        emit(InstructionCodes.JSONASTORE, arrayLiteral.regIndex, indexLiteral.regIndex, argExpr.regIndex);
    }
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral) BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 33 with BLangExpression

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

the class CodeGenerator method visitFunctionPointerLoad.

private void visitFunctionPointerLoad(BLangExpression fpExpr, BInvokableSymbol funcSymbol) {
    int pkgRefCPIndex = addPackageRefCPEntry(currentPkgInfo, funcSymbol.pkgID);
    int funcNameCPIndex = addUTF8CPEntry(currentPkgInfo, funcSymbol.name.value);
    FunctionRefCPEntry funcRefCPEntry = new FunctionRefCPEntry(pkgRefCPIndex, funcNameCPIndex);
    int funcRefCPIndex = currentPkgInfo.addCPEntry(funcRefCPEntry);
    RegIndex nextIndex = calcAndGetExprRegIndex(fpExpr);
    emit(InstructionCodes.FPLOAD, getOperand(funcRefCPIndex), nextIndex);
}
Also used : FunctionRefCPEntry(org.wso2.ballerinalang.programfile.cpentries.FunctionRefCPEntry) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 34 with BLangExpression

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

the class CodeGenerator method visitXMLTagName.

/**
 * Visit XML tag name and return the index of the tag name in the reference registry.
 *
 * @param tagName           Tag name expression
 * @param xmlElementEnv     Environment of the XML element of the tag
 * @param xmlElementLiteral XML element literal to which the tag name belongs to
 * @return Index of the tag name, in the reference registry
 */
private RegIndex visitXMLTagName(BLangExpression tagName, SymbolEnv xmlElementEnv, BLangXMLElementLiteral xmlElementLiteral) {
    genNode(tagName, xmlElementEnv);
    RegIndex startTagNameRegIndex = tagName.regIndex;
    // If this is a string representation of element name, generate the namespace lookup instructions
    if (tagName.getKind() != NodeKind.XML_QNAME) {
        RegIndex localNameRegIndex = getRegIndex(TypeTags.STRING);
        RegIndex uriRegIndex = getRegIndex(TypeTags.STRING);
        emit(InstructionCodes.S2QNAME, startTagNameRegIndex, localNameRegIndex, uriRegIndex);
        startTagNameRegIndex = getRegIndex(TypeTags.XML);
        generateURILookupInstructions(xmlElementLiteral.namespacesInScope, localNameRegIndex, uriRegIndex, startTagNameRegIndex, xmlElementLiteral.pos, xmlElementEnv);
        tagName.regIndex = startTagNameRegIndex;
    }
    return startTagNameRegIndex;
}
Also used : RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Example 35 with BLangExpression

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

the class CodeGenerator method visit.

@Override
public void visit(BLangBinaryExpr binaryExpr) {
    if (OperatorKind.AND.equals(binaryExpr.opKind)) {
        visitAndExpression(binaryExpr);
    } else if (OperatorKind.OR.equals(binaryExpr.opKind)) {
        visitOrExpression(binaryExpr);
    } else if (binaryExpr.opSymbol.opcode == InstructionCodes.REQ_NULL || binaryExpr.opSymbol.opcode == InstructionCodes.RNE_NULL || binaryExpr.opSymbol.opcode == InstructionCodes.SEQ_NULL || binaryExpr.opSymbol.opcode == InstructionCodes.SNE_NULL) {
        BLangExpression expr = (binaryExpr.lhsExpr.type.tag == TypeTags.NULL) ? binaryExpr.rhsExpr : binaryExpr.lhsExpr;
        genNode(expr, this.env);
        emit(binaryExpr.opSymbol.opcode, expr.regIndex, calcAndGetExprRegIndex(binaryExpr));
    } else {
        genNode(binaryExpr.lhsExpr, this.env);
        genNode(binaryExpr.rhsExpr, this.env);
        RegIndex regIndex = calcAndGetExprRegIndex(binaryExpr);
        emit(binaryExpr.opSymbol.opcode, binaryExpr.lhsExpr.regIndex, binaryExpr.rhsExpr.regIndex, regIndex);
    }
}
Also used : BLangExpression(org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression) RegIndex(org.wso2.ballerinalang.programfile.Instruction.RegIndex)

Aggregations

BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)101 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)27 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)26 ArrayList (java.util.ArrayList)22 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)19 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)15 BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)15 SelectExpressionNode (org.ballerinalang.model.tree.clauses.SelectExpressionNode)14 ExpressionNode (org.ballerinalang.model.tree.expressions.ExpressionNode)14 RegIndex (org.wso2.ballerinalang.programfile.Instruction.RegIndex)14 BSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol)13 BVarSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BVarSymbol)11 BLangArrayLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangArrayLiteral)11 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)11 BLangTypeConversionExpr (org.wso2.ballerinalang.compiler.tree.expressions.BLangTypeConversionExpr)11 SymbolEnv (org.wso2.ballerinalang.compiler.semantics.model.SymbolEnv)10 BLangInvocation (org.wso2.ballerinalang.compiler.tree.expressions.BLangInvocation)10 BLangXMLQName (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQName)10 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)10 BLangAssignment (org.wso2.ballerinalang.compiler.tree.statements.BLangAssignment)10