summaryrefslogtreecommitdiff
path: root/projects/hackc/tokens.py
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-08-30 14:46:23 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-08-30 14:46:23 +0800
commitb439d663a3f3d4d275f07339c1c0e794808f67d9 (patch)
tree6ab4a6b152336271f4d38c961cee67922020192e /projects/hackc/tokens.py
parentd303447dc7a830489828be2e66ccf8c36af4aed6 (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.py13
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."""