]> www.git.momoyon.org Git - lang.git/commitdiff
Can parse integer numbers in base-10.
authorahmedsamyh <ahmedsamyh10@gmail.com>
Sat, 16 Nov 2024 18:16:41 +0000 (23:16 +0500)
committerahmedsamyh <ahmedsamyh10@gmail.com>
Sat, 16 Nov 2024 18:16:41 +0000 (23:16 +0500)
- Update tests.

main.momo
main.py
tests/07-numbers.momo [new file with mode: 0644]
tests/07-numbers.momo.test [new file with mode: 0644]

index 41c73a8ac5f562684106bf0b776aafb1a866ecca..229bc10222e6f7c5554dd0964429ce024ee023da 100644 (file)
--- a/main.momo
+++ b/main.momo
@@ -1,6 +1,3 @@
-() {} [] ->
-- + / * %
-= !
->= > < <= != ==
-, : ;
+69
 
+100
diff --git a/main.py b/main.py
index c8be4752cf46fd66f18acdb2f5b8c9e9efd77d95..a4443c2b892a3a0ef95008b6ca18695bad6f6205 100644 (file)
--- a/main.py
+++ b/main.py
@@ -37,6 +37,7 @@ class Loc:
 class TokenType(IntEnum):
     IDENT = auto()
     STRING = auto()
+
     LEFT_PAREN = auto()
     RIGHT_PAREN = auto()
     MINUS = auto()
@@ -60,6 +61,8 @@ class TokenType(IntEnum):
     SEMICOLON = auto()
     LEFT_SQUARE_BRACE = auto()
     RIGHT_SQUARE_BRACE = auto()
+
+    NUMBER = auto()
     COUNT = auto()
 
 token_type_as_str_map: { TokenType : str } = {
@@ -88,6 +91,7 @@ token_type_as_str_map: { TokenType : str } = {
     TokenType.SEMICOLON            : "Semicolon",
     TokenType.LEFT_SQUARE_BRACE    : "Left Square Brace",
     TokenType.RIGHT_SQUARE_BRACE   : "Right Square Brace",
+    TokenType.NUMBER               : "Number"
 }
 # NOTE: TokenType.COUNT - 1 because auto() starts from 1
 assert len(token_type_as_str_map) == TokenType.COUNT-1
@@ -167,6 +171,20 @@ class Parser:
 
         return (ident, ident_loc)
 
+    def consume_number(self) -> (str, Loc):
+        assert self.current_char().isdigit(), "Called consume_number() at the wrong character!"
+        number: str = self.consume_char()
+
+        # TODO: Handle floating point numbers
+        # TODO: Handle numbers in other formats (Eg, binary, hexadecimal, etc)
+        number_loc: Loc = Loc(self.filename, self.line, self.row())
+
+        while self.current_char().isdigit() and not self.eof():
+            number += self.consume_char()
+
+        return (number, number_loc)
+
+
     def left_trim(self):
         while not self.eof() and self.current_char().isspace():
             if self.current_char() == '\n':
@@ -261,6 +279,9 @@ class Parser:
         elif c == ']':
             loc = Loc(self.filename, self.line, self.row())
             return Token(TokenType.RIGHT_SQUARE_BRACE, self.consume_char(), loc)
+        elif c.isdigit():
+            num, loc = self.consume_number()
+            return Token(TokenType.NUMBER, num, loc)
         else:
             fatal(f"Unrecognized character '{c}'")
 
diff --git a/tests/07-numbers.momo b/tests/07-numbers.momo
new file mode 100644 (file)
index 0000000..229bc10
--- /dev/null
@@ -0,0 +1,3 @@
+69
+
+100
diff --git a/tests/07-numbers.momo.test b/tests/07-numbers.momo.test
new file mode 100644 (file)
index 0000000..164c751
--- /dev/null
@@ -0,0 +1,3 @@
+"Token (Number, '69', ./tests/07-numbers.momo:1:1)"
+"Token (Number, '100', ./tests/07-numbers.momo:3:1)"
+'None'