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