diff options
| author | mryouse | 2022-05-21 04:44:29 +0000 |
|---|---|---|
| committer | mryouse | 2022-05-21 04:44:29 +0000 |
| commit | 16c75f926f41cb9aa89a7cef4e859c10dd08b6e5 (patch) | |
| tree | 71354fbdf56c6273800804118c8d14e1e9f339b8 /lexer.py | |
| parent | 310a53dc06a166c5ab448fa16461b110329d1c1c (diff) | |
lex and parse types
Diffstat (limited to 'lexer.py')
| -rw-r--r-- | lexer.py | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -55,6 +55,18 @@ class TokenType(Enum): # symbols SYMBOL = auto() + # types + INT_TYPE = auto() + FLOAT_TYPE = auto() + STRING_TYPE = auto() + ANY_TYPE = auto() + +types = { + ":int": TokenType.INT_TYPE, + ":float": TokenType.FLOAT_TYPE, + ":string": TokenType.STRING_TYPE, + ":any": TokenType.ANY_TYPE } + keywords = { "print": TokenType.PRINT, "+": TokenType.PLUS, @@ -123,6 +135,11 @@ def lex(data): tok, length = get_bool(data[current+1:], line) tokens.append(tok) current += length + #types + elif char == ":": + tok, length = get_type(data[current:], line) # include : + tokens.append(tok) + current += length # symbols else: tok, length = get_symbol(data[current:], line) @@ -188,6 +205,17 @@ def get_symbol(data, line): break return Token(TokenType.SYMBOL, value, None, line), counter - 1 +def get_type(data, line): + counter = 0 + value = "" + while data[counter] not in SEPARATORS: + value += data[counter] + counter += 1 + if counter >= len(data): + break + if value not in types: + raise LexError(f"unrecognized type {value}", line) + return Token(types[value], value, None, line), counter - 1 def main(data): try: |
