Search in sources :

Example 31 with BLangLiteral

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

the class CodeGenerator method createStringLiteral.

/**
 * Creates a string literal expression, generate the code and returns the registry index.
 *
 * @param value    String value to generate the string literal
 * @param regIndex String literal expression's reg index
 * @param env      Environment
 * @return String registry index of the generated string
 */
private RegIndex createStringLiteral(String value, RegIndex regIndex, SymbolEnv env) {
    BLangLiteral prefixLiteral = (BLangLiteral) TreeBuilder.createLiteralExpression();
    prefixLiteral.value = value;
    prefixLiteral.typeTag = TypeTags.STRING;
    prefixLiteral.type = symTable.stringType;
    prefixLiteral.regIndex = regIndex;
    genNode(prefixLiteral, env);
    return prefixLiteral.regIndex;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)

Example 32 with BLangLiteral

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

the class CodeGenerator method visit.

// Expressions
@Override
public void visit(BLangLiteral literalExpr) {
    int opcode;
    Operand regIndex = calcAndGetExprRegIndex(literalExpr);
    int typeTag = literalExpr.type.tag;
    switch(typeTag) {
        case TypeTags.INT:
            long longVal = (Long) literalExpr.value;
            if (longVal >= 0 && longVal <= 5) {
                opcode = InstructionCodes.ICONST_0 + (int) longVal;
                emit(opcode, regIndex);
            } else {
                int intCPEntryIndex = currentPkgInfo.addCPEntry(new IntegerCPEntry(longVal));
                emit(InstructionCodes.ICONST, getOperand(intCPEntryIndex), regIndex);
            }
            break;
        case TypeTags.FLOAT:
            double doubleVal = (Double) literalExpr.value;
            if (doubleVal == 0 || doubleVal == 1 || doubleVal == 2 || doubleVal == 3 || doubleVal == 4 || doubleVal == 5) {
                opcode = InstructionCodes.FCONST_0 + (int) doubleVal;
                emit(opcode, regIndex);
            } else {
                int floatCPEntryIndex = currentPkgInfo.addCPEntry(new FloatCPEntry(doubleVal));
                emit(InstructionCodes.FCONST, getOperand(floatCPEntryIndex), regIndex);
            }
            break;
        case TypeTags.STRING:
            String strValue = (String) literalExpr.value;
            StringCPEntry stringCPEntry = new StringCPEntry(addUTF8CPEntry(currentPkgInfo, strValue), strValue);
            int strCPIndex = currentPkgInfo.addCPEntry(stringCPEntry);
            emit(InstructionCodes.SCONST, getOperand(strCPIndex), regIndex);
            break;
        case TypeTags.BOOLEAN:
            boolean booleanVal = (Boolean) literalExpr.value;
            if (!booleanVal) {
                opcode = InstructionCodes.BCONST_0;
            } else {
                opcode = InstructionCodes.BCONST_1;
            }
            emit(opcode, regIndex);
            break;
        case TypeTags.NULL:
            emit(InstructionCodes.RCONST_NULL, regIndex);
    }
}
Also used : StringCPEntry(org.wso2.ballerinalang.programfile.cpentries.StringCPEntry) Operand(org.wso2.ballerinalang.programfile.Instruction.Operand) IntegerCPEntry(org.wso2.ballerinalang.programfile.cpentries.IntegerCPEntry) FloatCPEntry(org.wso2.ballerinalang.programfile.cpentries.FloatCPEntry) BLangXMLQuotedString(org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString) BLangEndpoint(org.wso2.ballerinalang.compiler.tree.BLangEndpoint)

Example 33 with BLangLiteral

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

the class ASTBuilderUtil method createLiteral.

static BLangLiteral createLiteral(DiagnosticPos pos, BType type, Object value) {
    final BLangLiteral literal = (BLangLiteral) TreeBuilder.createLiteralExpression();
    literal.pos = pos;
    literal.value = value;
    literal.typeTag = type.tag;
    literal.type = type;
    return literal;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)

Example 34 with BLangLiteral

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

the class Desugar method createStringLiteral.

private BLangLiteral createStringLiteral(DiagnosticPos pos, String value) {
    BLangLiteral stringLit = new BLangLiteral();
    stringLit.pos = pos;
    stringLit.value = value;
    stringLit.type = symTable.stringType;
    return stringLit;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)

Example 35 with BLangLiteral

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

the class Desugar method getSQLPreparedStatement.

private BLangLiteral getSQLPreparedStatement(BLangTableQueryExpression tableQueryExpression) {
    // create a literal to represent the sql query.
    BLangLiteral sqlQueryLiteral = (BLangLiteral) TreeBuilder.createLiteralExpression();
    sqlQueryLiteral.typeTag = TypeTags.STRING;
    // assign the sql query from table expression to the literal.
    sqlQueryLiteral.value = tableQueryExpression.getSqlQuery();
    sqlQueryLiteral.type = symTable.getTypeFromTag(sqlQueryLiteral.typeTag);
    return sqlQueryLiteral;
}
Also used : BLangLiteral(org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)

Aggregations

BLangLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangLiteral)23 BLangExpression (org.wso2.ballerinalang.compiler.tree.expressions.BLangExpression)15 BLangEndpoint (org.wso2.ballerinalang.compiler.tree.BLangEndpoint)11 BType (org.wso2.ballerinalang.compiler.semantics.model.types.BType)8 HashMap (java.util.HashMap)4 BLangVariable (org.wso2.ballerinalang.compiler.tree.BLangVariable)4 BLangRecordLiteral (org.wso2.ballerinalang.compiler.tree.expressions.BLangRecordLiteral)4 ArrayList (java.util.ArrayList)3 AnnotationAttachmentNode (org.ballerinalang.model.tree.AnnotationAttachmentNode)3 BLangSimpleVarRef (org.wso2.ballerinalang.compiler.tree.expressions.BLangSimpleVarRef)3 BLangXMLQuotedString (org.wso2.ballerinalang.compiler.tree.expressions.BLangXMLQuotedString)3 Operand (org.wso2.ballerinalang.programfile.Instruction.Operand)3 Path (java.nio.file.Path)2 NodeKind (org.ballerinalang.model.tree.NodeKind)2 BStructSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BStructSymbol)2 BXMLNSSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BXMLNSSymbol)2 BLangVariableDef (org.wso2.ballerinalang.compiler.tree.statements.BLangVariableDef)2 DefaultValue (org.wso2.ballerinalang.programfile.DefaultValue)2 ParamDefaultValueAttributeInfo (org.wso2.ballerinalang.programfile.attributes.ParamDefaultValueAttributeInfo)2 FloatCPEntry (org.wso2.ballerinalang.programfile.cpentries.FloatCPEntry)2