From: ahmedsamyh Date: Sat, 16 Nov 2024 18:16:41 +0000 (+0500) Subject: Can parse integer numbers in base-10. X-Git-Url: https://www.git.momoyon.org/?a=commitdiff_plain;h=7d19b6b3fa9b21257be1d7c145cfdd611e9574ec;p=lang.git Can parse integer numbers in base-10. - Update tests. --- diff --git a/main.momo b/main.momo index 41c73a8..229bc10 100644 --- a/main.momo +++ b/main.momo @@ -1,6 +1,3 @@ -() {} [] -> -- + / * % -= ! ->= > < <= != == -, : ; +69 +100 diff --git a/main.py b/main.py index c8be475..a4443c2 100644 --- 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 index 0000000..229bc10 --- /dev/null +++ b/tests/07-numbers.momo @@ -0,0 +1,3 @@ +69 + +100 diff --git a/tests/07-numbers.momo.test b/tests/07-numbers.momo.test new file mode 100644 index 0000000..164c751 --- /dev/null +++ b/tests/07-numbers.momo.test @@ -0,0 +1,3 @@ +"Token (Number, '69', ./tests/07-numbers.momo:1:1)" +"Token (Number, '100', ./tests/07-numbers.momo:3:1)" +'None'