diff --git a/source/tlang/compiler/parsing/core.d b/source/tlang/compiler/parsing/core.d index b3638a49..bf69485a 100644 --- a/source/tlang/compiler/parsing/core.d +++ b/source/tlang/compiler/parsing/core.d @@ -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); diff --git a/source/tlang/compiler/symbols/expressions.d b/source/tlang/compiler/symbols/expressions.d index d4ae2169..4717adee 100644 --- a/source/tlang/compiler/symbols/expressions.d +++ b/source/tlang/compiler/symbols/expressions.d @@ -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; } } diff --git a/source/tlang/compiler/typecheck/core.d b/source/tlang/compiler/typecheck/core.d index 63354312..096d19d7 100644 --- a/source/tlang/compiler/typecheck/core.d +++ b/source/tlang/compiler/typecheck/core.d @@ -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); diff --git a/source/tlang/testing/simple_literals.t b/source/tlang/testing/simple_literals.t new file mode 100644 index 00000000..644a37ae --- /dev/null +++ b/source/tlang/testing/simple_literals.t @@ -0,0 +1,3 @@ +module simple_literals; + +int var = 1; \ No newline at end of file