def joy(stack, expression, dictionary, viewer=None):
- '''Evaluate a Joy expression on a stack.
+ '''Evaluate a Joy expression on a stack.
++<<<<<<< working copy
+ This function iterates through a sequence of terms which are either
+ literals (strings, numbers, sequences of terms) or function symbols.
+ Literals are put onto the stack and functions are looked up in the
+ dictionary and executed.
++=======
+ This function iterates through a sequence of terms which are either
+ literals (strings, numbers, sequences of terms) or function symbols.
+ Literals are put onto the stack and functions are looked up in the
+ disctionary and executed.
++>>>>>>> merge rev
- The viewer is a function that is called with the stack and expression
- on every iteration, its return value is ignored.
+ The viewer is a function that is called with the stack and expression
+ on every iteration, its return value is ignored.
- :param stack stack: The stack.
- :param stack expression: The expression to evaluate.
- :param dict dictionary: A ``dict`` mapping names to Joy functions.
- :param function viewer: Optional viewer function.
- :rtype: (stack, (), dictionary)
+ :param stack stack: The stack.
+ :param stack expression: The expression to evaluate.
+ :param dict dictionary: A ``dict`` mapping names to Joy functions.
+ :param function viewer: Optional viewer function.
+ :rtype: (stack, (), dictionary)
- '''
- while expression:
+ '''
+ while expression:
- if viewer: viewer(stack, expression)
+ if viewer: viewer(stack, expression)
- term, expression = expression
- if isinstance(term, Symbol):
- term = dictionary[term]
- stack, expression, dictionary = term(stack, expression, dictionary)
- else:
- stack = term, stack
+ term, expression = expression
+ if isinstance(term, Symbol):
+ term = dictionary[term]
+ stack, expression, dictionary = term(stack, expression, dictionary)
+ else:
+ stack = term, stack
- if viewer: viewer(stack, expression)
- return stack, expression, dictionary
+ if viewer: viewer(stack, expression)
+ return stack, expression, dictionary
def run(text, stack, dictionary, viewer=None):