From 4156da35110a55ad60a0e2bfb970d6d21aca8af0 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Sat, 10 Sep 2022 17:37:07 -0700 Subject: [PATCH] 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.) --- implementations/Python/simplejoy.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/implementations/Python/simplejoy.py b/implementations/Python/simplejoy.py index e494624..c15fa2a 100755 --- a/implementations/Python/simplejoy.py +++ b/implementations/Python/simplejoy.py @@ -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