From c040e67c4c0cd29e02d4195e3ab85610b457d27e Mon Sep 17 00:00:00 2001 From: Frederick Yin Date: Sat, 3 Sep 2022 11:56:52 +0800 Subject: hackc: LetStatement accepts SubscriptTerm --- projects/hackc/expressions.py | 2 +- projects/hackc/statements.py | 22 ++++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) (limited to 'projects') 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: - '[' ']' + '[' ']' """ 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' '=' ';' + 'let' ( | ) '=' ';' """ 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]) -- cgit v1.2.3