diff --git a/joy/library.py b/joy/library.py index c5d7429..dc56dc5 100644 --- a/joy/library.py +++ b/joy/library.py @@ -842,7 +842,10 @@ def i(stack, expression, dictionary): Q ''' - quote, stack = stack + try: + quote, stack = stack + except ValueError: + raise StackUnderflowError return stack, concat(quote, expression), dictionary diff --git a/joy/utils/stack.py b/joy/utils/stack.py index 2f5a653..c460384 100644 --- a/joy/utils/stack.py +++ b/joy/utils/stack.py @@ -70,6 +70,7 @@ printed left-to-right. These functions are written to support :doc:`../pretty`. .. _cons list: https://en.wikipedia.org/wiki/Cons#Lists ''' +from .errors import NotAListError def list_to_stack(el, stack=()): @@ -164,7 +165,7 @@ def concat(quote, expression): # RuntimeError: maximum recursion depth exceeded # on quotes longer than sys.getrecursionlimit(). - return (quote[0], concat(quote[1], expression)) if quote else expression +## return (quote[0], concat(quote[1], expression)) if quote else expression # Original implementation. @@ -173,13 +174,15 @@ def concat(quote, expression): # In-lining is slightly faster (and won't break the # recursion limit on long quotes.) -## temp = [] -## while quote: -## item, quote = quote -## temp.append(item) -## for item in reversed(temp): -## expression = item, expression -## return expression + temp = [] + while quote: + if not isinstance(quote, tuple): + raise NotAListError + item, quote = quote + temp.append(item) + for item in reversed(temp): + expression = item, expression + return expression