Cleaner iteration of expression.

It's a destructive operation.  It breaks functional purity (but it could
be made functional by using a stack instead of a list for the stack.)

There's no point to returning the expression object now because it's
being mutated by the prepend() method (rather than being replaced by new
forms.)
This commit is contained in:
Simon Forman 2022-09-10 17:37:07 -07:00
parent 15c143e3d1
commit 4156da3511
1 changed files with 7 additions and 9 deletions

View File

@ -93,8 +93,7 @@ def joy(stack, expression, dictionary):
'''
expr = Expression(expression)
while expr:
term, expr = expr
for term in expr:
if isinstance(term, Symbol):
try:
func = dictionary[term]
@ -164,6 +163,10 @@ tuples used in expressions where they would be redundant.)
'''
class StackUnderflowError(Exception):
pass
def list_to_stack(el, stack=()):
'''
Convert a Python list (or other sequence) to a Joy stack::
@ -269,7 +272,7 @@ class Expression:
self.stack = []
def __iter__(self):
return iter((self.__next__(), self))
return self
def __next__(self):
if self.current:
@ -507,8 +510,7 @@ def run(text, stack, dictionary):
:rtype: (stack, (), dictionary)
'''
expr = text_to_expression(text)
return joy(stack, expr, dictionary)
return joy(stack, text_to_expression(text), dictionary)
def interp(stack=(), dictionary=None):
@ -1308,10 +1310,6 @@ class NotABoolError(Exception):
pass
class StackUnderflowError(Exception):
pass
class UnknownSymbolError(KeyError):
pass