From 810a6afdbbe6e162e95e0af0e8b1424b8eda3252 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Fri, 9 Apr 2021 17:32:51 -0700 Subject: [PATCH] Bring it inline with Nim version. See https://git.sr.ht/~sforman/joytest --- joy/joy.py | 4 ++-- joy/library.py | 17 +++++++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/joy/joy.py b/joy/joy.py index 2870f51..3f61e80 100644 --- a/joy/joy.py +++ b/joy/joy.py @@ -140,8 +140,8 @@ def interp(stack=(), dictionary=None): print('Not enough values on stack.') except NotAnIntError: print('Not an integer.') - except NotAListError: - print('Not a list.') + except NotAListError as e: + print(e) # 'Not a list.' except: print_exc() print(stack_to_string(stack)) diff --git a/joy/library.py b/joy/library.py index dc56dc5..6e7f817 100644 --- a/joy/library.py +++ b/joy/library.py @@ -30,7 +30,11 @@ import operator, math from .parser import text_to_expression, Symbol from .utils import generated_library as genlib -from .utils.errors import NotAnIntError, StackUnderflowError +from .utils.errors import ( + NotAListError, + NotAnIntError, + StackUnderflowError, + ) from .utils.stack import ( concat, expression_to_string, @@ -1371,7 +1375,16 @@ def loop(stack, expression, dictionary): ... ''' - quote, (flag, stack) = stack + try: + quote, stack = stack + except ValueError: + raise StackUnderflowError + if not isinstance(quote, tuple): + raise NotAListError('Loop body not a list.') + try: + (flag, stack) = stack + except ValueError: + raise StackUnderflowError if flag: expression = concat(quote, (quote, (S_loop, expression))) return stack, expression, dictionary