Expressions
- Split `NumberLiteral` into an abstract class with two sub-classes for `IntegerLiteral` and `FloatingLiteral` (floating-point literals) Parser - Use new `IntegerLiteral` and `FloatingLiteral` based on the type of token (whether a `.` is present or not) TypeChecker/Codegen - Updated code to check for `NumberLiteral` sub-type Test cases - Added `simple_literals.t` for testing `literal_encodings` branch
This commit is contained in:
parent
a1c023ecc3
commit
96f8230bd1
|
@ -1060,6 +1060,20 @@ public final class Parser
|
|||
{
|
||||
gprintln("parseExpression(): Enter", DebugType.WARNING);
|
||||
|
||||
|
||||
/**
|
||||
* Helper methods
|
||||
*
|
||||
* (TODO: These should be moved elsewhere)
|
||||
*/
|
||||
bool isFloatLiteral(string numberLiteral)
|
||||
{
|
||||
import std.string : indexOf;
|
||||
bool isFloat = indexOf(numberLiteral, ".") > -1;
|
||||
return isFloat;
|
||||
}
|
||||
|
||||
|
||||
/* The expression to be returned */
|
||||
Expression[] retExpression;
|
||||
|
||||
|
@ -1113,8 +1127,19 @@ public final class Parser
|
|||
/* If it is a number literal */
|
||||
if (symbol == SymbolType.NUMBER_LITERAL)
|
||||
{
|
||||
/* TODO: Do number checking here to get correct NUmberLiteral */
|
||||
NumberLiteral numberLiteral = new NumberLiteral(getCurrentToken().getToken());
|
||||
string numberLiteralStr = getCurrentToken().getToken();
|
||||
NumberLiteral numberLiteral;
|
||||
|
||||
// If floating point literal
|
||||
if(isFloatLiteral(numberLiteralStr))
|
||||
{
|
||||
numberLiteral = new FloatingLiteral(getCurrentToken().getToken());
|
||||
}
|
||||
// Else, then an integer literal
|
||||
else
|
||||
{
|
||||
numberLiteral = new IntegerLiteral(getCurrentToken().getToken());
|
||||
}
|
||||
|
||||
/* Add expression to stack */
|
||||
addRetExp(numberLiteral);
|
||||
|
|
|
@ -85,7 +85,7 @@ public class BinaryOperatorExpression : OperatorExpression
|
|||
}
|
||||
}
|
||||
|
||||
public enum NumberLiteralEncoding
|
||||
public enum IntegerLiteralEncoding
|
||||
{
|
||||
SIGNED_INTEGER,
|
||||
UNSIGNED_INTEGER,
|
||||
|
@ -93,30 +93,54 @@ public enum NumberLiteralEncoding
|
|||
UNSIGNED_LONG
|
||||
}
|
||||
|
||||
public class NumberLiteral : Expression
|
||||
public final class IntegerLiteral : NumberLiteral
|
||||
{
|
||||
private string numberLiteral;
|
||||
private NumberLiteralEncoding encoding;
|
||||
private IntegerLiteralEncoding encoding;
|
||||
|
||||
/* TODO: Take in info like tyoe */
|
||||
this(string numberLiteral)
|
||||
this(string integerLiteral)
|
||||
{
|
||||
this.numberLiteral = numberLiteral;
|
||||
super(integerLiteral);
|
||||
}
|
||||
|
||||
public string getNumber()
|
||||
{
|
||||
return numberLiteral;
|
||||
}
|
||||
|
||||
public NumberLiteralEncoding getEncoding()
|
||||
public IntegerLiteralEncoding getEncoding()
|
||||
{
|
||||
return encoding;
|
||||
}
|
||||
|
||||
public override string toString()
|
||||
{
|
||||
return "[numberLiteral: "~numberLiteral~" ("~to!(string)(encoding)~")]";
|
||||
return "[integerLiteral: "~numberLiteral~" ("~to!(string)(encoding)~")]";
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: Work on floating point literal encodings
|
||||
public final class FloatingLiteral : NumberLiteral
|
||||
{
|
||||
// TODO: Put the equivalent of FloatingLiteralEncoding here
|
||||
|
||||
this(string floatingLiteral)
|
||||
{
|
||||
super(floatingLiteral);
|
||||
}
|
||||
|
||||
public override string toString()
|
||||
{
|
||||
return "[floatingLiteral: "~numberLiteral~"]"; // ("~to!(string)(encoding)~")]";
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class NumberLiteral : Expression
|
||||
{
|
||||
private string numberLiteral;
|
||||
|
||||
this(string numberLiteral)
|
||||
{
|
||||
this.numberLiteral = numberLiteral;
|
||||
}
|
||||
|
||||
public final string getNumber()
|
||||
{
|
||||
return numberLiteral;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -410,21 +410,6 @@ public final class TypeChecker
|
|||
|
||||
if(cast(NumberLiteral)statement)
|
||||
{
|
||||
/* TODO: For now */
|
||||
|
||||
/**
|
||||
* Typechecking
|
||||
*
|
||||
* If the number literal contains a `.` then it is a float
|
||||
* else if is an int (NOTE: This may need to be more specific
|
||||
* with literal encoders down the line)
|
||||
*/
|
||||
NumberLiteral numLit = cast(NumberLiteral)statement;
|
||||
import std.string : indexOf;
|
||||
bool isFloat = indexOf(numLit.getNumber(), ".") > -1;
|
||||
gprintln("NUMBER LIT: isFloat: "~to!(string)(isFloat));
|
||||
addType(getType(modulle, isFloat ? "float" : "int"));
|
||||
|
||||
/**
|
||||
* Codegen
|
||||
*
|
||||
|
@ -435,21 +420,34 @@ public final class TypeChecker
|
|||
*/
|
||||
Value valInstr;
|
||||
|
||||
/* Generate a LiteralValue (Integer literal) */
|
||||
if(!isFloat)
|
||||
/* Generate a LiteralValue (IntegerLiteral) */
|
||||
if(cast(IntegerLiteral)statement)
|
||||
{
|
||||
ulong i = to!(ulong)((cast(NumberLiteral)statement).getNumber());
|
||||
IntegerLiteral integerLitreal = cast(IntegerLiteral)statement;
|
||||
|
||||
|
||||
ulong i = to!(ulong)(integerLitreal.getNumber());
|
||||
|
||||
// TODO: Insert getEncoding stuff here
|
||||
LiteralValue litValInstr = new LiteralValue(i, 4);
|
||||
|
||||
valInstr = litValInstr;
|
||||
|
||||
// TODO: Insert get encoding stuff here
|
||||
addType(getType(modulle, "int"));
|
||||
}
|
||||
/* Generate a LiteralValueFloat (Floating point literal) */
|
||||
/* Generate a LiteralValueFloat (FloatingLiteral) */
|
||||
else
|
||||
{
|
||||
double i = to!(float)((cast(NumberLiteral)statement).getNumber());
|
||||
FloatingLiteral floatLiteral = cast(FloatingLiteral)statement;
|
||||
|
||||
double i = to!(float)(floatLiteral.getNumber());
|
||||
LiteralValueFloat litValInstr = new LiteralValueFloat(i, 4);
|
||||
|
||||
valInstr = litValInstr;
|
||||
|
||||
// TODO: Insert get encoding stuff here
|
||||
addType(getType(modulle, "float"));
|
||||
}
|
||||
|
||||
addInstr(valInstr);
|
||||
|
|
|
@ -0,0 +1,3 @@
|
|||
module simple_literals;
|
||||
|
||||
int var = 1;
|
Loading…
Reference in New Issue