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