diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-08-30 14:46:23 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-08-30 14:46:23 +0800 |
commit | b439d663a3f3d4d275f07339c1c0e794808f67d9 (patch) | |
tree | 6ab4a6b152336271f4d38c961cee67922020192e /projects/hackc/tokens.py | |
parent | d303447dc7a830489828be2e66ccf8c36af4aed6 (diff) |
hackc: parse variable declaration
Also add a handful of overloading operators to Token
Diffstat (limited to 'projects/hackc/tokens.py')
-rw-r--r-- | projects/hackc/tokens.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/projects/hackc/tokens.py b/projects/hackc/tokens.py index 50c4173..1ed94ae 100644 --- a/projects/hackc/tokens.py +++ b/projects/hackc/tokens.py @@ -26,6 +26,7 @@ KEYWORDS = [ SYMBOLS = "{}()[].,;+-*/&|<>=~" TOKEN_TYPES = ["keyword", "symbol", "integer", "string", "identifier"] + class Token: def __init__(self, type: str, token: str, line_no: int, column: int): """A token in JACK.""" @@ -34,6 +35,18 @@ class Token: self.line_no = line_no self.column = column + def __len__(self) -> int: + return self.length() + + def __eq__(self, other) -> bool: + if type(other) == str: + return self.token == other + if type(other) == Token: + return self.token == other.token + + def __str__(self) -> str: + return self.token + @classmethod def from_line(cls, line: str, line_no: int, column: int, extensions=[]): """Extract first token from line and return it as an instance of Token.""" |