feat(lexer): add numbers
This commit is contained in:
2
main.py
2
main.py
@@ -6,6 +6,8 @@ def main():
|
||||
source: str = """() {} +- += / /= // sefs + {, )
|
||||
}:: *
|
||||
"This is a string"
|
||||
3.1415
|
||||
123
|
||||
"This is
|
||||
another string" """
|
||||
lexer: Lexer = Lexer()
|
||||
|
||||
22
src/lexer.py
22
src/lexer.py
@@ -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,7 +127,10 @@ class Lexer:
|
||||
case '"':
|
||||
self.scan_string()
|
||||
case _:
|
||||
self.error("Unexpected character")
|
||||
if char.isdigit():
|
||||
self.scan_number()
|
||||
else:
|
||||
self.error("Unexpected character")
|
||||
return None
|
||||
|
||||
def scan_string(self):
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user