feat(lexer): add numbers

This commit is contained in:
2026-02-05 03:28:06 +01:00
parent f5e2d791b5
commit aa760a85c3
2 changed files with 23 additions and 1 deletions

View File

@@ -6,6 +6,8 @@ def main():
source: str = """() {} +- += / /= // sefs + {, )
}:: *
"This is a string"
3.1415
123
"This is
another string" """
lexer: Lexer = Lexer()

View File

@@ -49,6 +49,11 @@ class Lexer:
return self.source[self.idx]
return ""
def peek_next(self) -> str:
if self.idx + 1 < self.length:
return self.source[self.idx + 1]
return ""
def advance(self) -> str:
char: str = self.peek()
self.idx += 1
@@ -122,6 +127,9 @@ class Lexer:
case '"':
self.scan_string()
case _:
if char.isdigit():
self.scan_number()
else:
self.error("Unexpected character")
return None
@@ -138,3 +146,15 @@ class Lexer:
self.advance()
value: str = self.source[self.start + 1:self.idx - 1]
self.add_token(TokenType.STRING, value)
def scan_number(self):
while self.peek().isdigit():
self.advance()
if self.peek() == "." and self.peek_next().isdigit():
self.advance()
while self.peek().isdigit():
self.advance()
value: float = float(self.source[self.start:self.idx])
self.add_token(TokenType.NUMBER, value)