summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederick Yin <fkfd@fkfd.me>2022-08-31 16:59:55 +0800
committerFrederick Yin <fkfd@fkfd.me>2022-08-31 16:59:55 +0800
commit482eed0ae339e977a618c4ab60525596426cf967 (patch)
tree080a8f912e81d4ea6da18a5eeda251938965dd0b
parent61f3490b9e6132cbad70c90637ed349a42fe7704 (diff)
hackc: idk, statements and expressions?
-rw-r--r--projects/hackc/classes.py4
-rw-r--r--projects/hackc/expressions.py12
-rw-r--r--projects/hackc/statements.py3
3 files changed, 18 insertions, 1 deletions
diff --git a/projects/hackc/classes.py b/projects/hackc/classes.py
index 8dc1dd5..a8d446a 100644
--- a/projects/hackc/classes.py
+++ b/projects/hackc/classes.py
@@ -311,3 +311,7 @@ class Subroutine:
def print_verbose(self):
print(f"Define {self.category} {self.type} {self.name}")
self.params.print_verbose()
+ for variable in self.variables:
+ variable.print_verbose()
+ for statement in self.statements:
+ statement.print_verbose()
diff --git a/projects/hackc/expressions.py b/projects/hackc/expressions.py
index de58528..3bc83d2 100644
--- a/projects/hackc/expressions.py
+++ b/projects/hackc/expressions.py
@@ -1,4 +1,5 @@
from .tokens import Token
+from .utils import *
OPS = "+-*/&|<>="
UNARY_OPS = "-~"
@@ -15,7 +16,7 @@ class Term:
return (None, 0)
if tokens[0].type in ["integer", "string"] or tokens[0] in CONSTANTS:
return (ConstantTerm(tokens[0]), 1)
- if tokens[0] in UNARY_OPS:
+ if tokens[0].token in UNARY_OPS:
term, dt = Term.from_tokens(tokens[1:])
return (UnaryTerm(tokens[0], term), dt + 1)
@@ -24,6 +25,9 @@ class ConstantTerm:
def __init__(self, term: Token):
self.term = term
+ def __str__(self):
+ return self.term.token
+
class VarTerm:
def __init__(self, var: Token, subscript=None):
@@ -74,3 +78,9 @@ class Expression:
t += dt
return (Expression(lhs, op, rhs), t)
+
+ def __str__(self):
+ if self.op is not None:
+ return f"({self.lhs}) {self.op} ({self.rhs})"
+ else:
+ return str(self.lhs)
diff --git a/projects/hackc/statements.py b/projects/hackc/statements.py
index e8bc0b3..16c123e 100644
--- a/projects/hackc/statements.py
+++ b/projects/hackc/statements.py
@@ -1,4 +1,5 @@
from .expressions import Expression
+from .utils import *
class Statement:
def __init__(self):
@@ -47,3 +48,5 @@ class LetStatement:
t += 1
return (LetStatement(name, expr), t)
+ def print_verbose(self):
+ print(f"Let {self.name} be {self.expr}")