diff options
author | Frederick Yin <fkfd@fkfd.me> | 2022-09-03 11:56:52 +0800 |
---|---|---|
committer | Frederick Yin <fkfd@fkfd.me> | 2022-09-03 11:56:52 +0800 |
commit | c040e67c4c0cd29e02d4195e3ab85610b457d27e (patch) | |
tree | 4785c2178af349b555a70ec6e35a150476c1616f | |
parent | dfb5ee45fb68aca09bd0ac9967c6b574f932d39f (diff) |
-rw-r--r-- | projects/hackc/expressions.py | 2 | ||||
-rw-r--r-- | projects/hackc/statements.py | 22 |
2 files changed, 15 insertions, 9 deletions
diff --git a/projects/hackc/expressions.py b/projects/hackc/expressions.py index 2e08cef..dcdb961 100644 --- a/projects/hackc/expressions.py +++ b/projects/hackc/expressions.py @@ -55,7 +55,7 @@ class SubscriptTerm: """Construct a subscripted array term. Format: - <var> '[' <subscript> ']' + <var> '[' <expression> ']' """ if tokens[0].type != "identifier" or tokens[1] != LEFT_BRACKET: return (None, 0) diff --git a/projects/hackc/statements.py b/projects/hackc/statements.py index 458ba01..dc76228 100644 --- a/projects/hackc/statements.py +++ b/projects/hackc/statements.py @@ -1,4 +1,4 @@ -from .expressions import Expression, SubroutineCall +from .expressions import Expression, SubroutineCall, VarTerm, SubscriptTerm from .utils import * @@ -55,19 +55,25 @@ class LetStatement: """Construct let statement. Format: - 'let' <name> '=' <expression> ';' + 'let' (<var term> | <subscript term>) '=' <expression> ';' """ if len(tokens) < 5 or tokens[0] != "let": return (None, 0) - name = tokens[1] - if name.type != "identifier": - raise UnexpectedToken("variable name", name) + if tokens[1].type != "identifier": + raise UnexpectedToken("variable name", tokens[1]) - if tokens[2] != "=": - raise UnexpectedToken("=", tokens[2]) + t = 1 + name, dt = SubscriptTerm.from_tokens(tokens[1:]) + if name is None: + name = VarTerm(tokens[1]) + dt = 1 + t += dt + + if tokens[t] != "=": + raise UnexpectedToken("=", tokens[t]) + t += 1 - t = 3 expr, dt = Expression.from_tokens(tokens[t:]) if expr is None: raise UnexpectedToken(f"Expected expression", tokens[3]) |