Reverse tokens instead of temp lists.

This way we can build the tuples representing the Thun lists as we go
and I bet it's more efficient, eh?
This commit is contained in:
Simon Forman 2024-04-11 11:01:14 -07:00
parent c07883f4cb
commit 3c276c0913
1 changed files with 14 additions and 15 deletions

View File

@ -349,22 +349,21 @@ def text_to_expression(text):
:rtype: stack
:raises ParseError: if the parse fails.
'''
frame = []
stack = []
lists = []
stack = ()
for tok in text.replace('[', ' [ ').replace(']', ' ] ').split():
for tok in reversed(text.replace('[', ' [ ').replace(']', ' ] ').split()):
if tok == '[':
stack.append(frame)
frame = []
if not lists:
raise ParseError('Unclosed bracket.')
thing, stack = stack, lists.pop()
elif tok == ']':
lists.append(stack)
stack = ()
continue
if tok == ']':
thing = list_to_stack(frame)
try:
frame = stack.pop()
except IndexError:
raise ParseError('Extra closing bracket.') from None
elif tok == _T:
thing = True
elif tok == _F:
@ -375,12 +374,12 @@ def text_to_expression(text):
except ValueError:
thing = Symbol(tok)
frame.append(thing)
stack = thing, stack
if stack:
raise ParseError('Unclosed bracket.')
if lists:
raise ParseError('Extra closing bracket.')
return list_to_stack(frame)
return stack
'''