Customizing error messages.

This commit is contained in:
Simon Forman 2021-04-09 17:41:42 -07:00
parent 810a6afdbb
commit e417842923
2 changed files with 17 additions and 5 deletions

View File

@ -136,8 +136,8 @@ def interp(stack=(), dictionary=None):
stack, _, dictionary = run(text, stack, dictionary) stack, _, dictionary = run(text, stack, dictionary)
except UnknownSymbolError as sym: except UnknownSymbolError as sym:
print('Unknown:', sym) print('Unknown:', sym)
except StackUnderflowError: except StackUnderflowError as e:
print('Not enough values on stack.') print(e) # 'Not enough values on stack.'
except NotAnIntError: except NotAnIntError:
print('Not an integer.') print('Not an integer.')
except NotAListError as e: except NotAListError as e:

View File

@ -158,7 +158,10 @@ def pop(stack):
(a1 --) (a1 --)
""" """
(a1, s23) = stack try:
(a1, s23) = stack
except ValueError:
raise StackUnderflowError('Cannot pop empty stack.')
return s23 return s23
@ -224,8 +227,17 @@ def rest(stack):
([a1 ...0] -- [...0]) ([a1 ...0] -- [...0])
""" """
((a1, s0), s23) = stack try:
return (s0, s23) s0, stack = stack
except ValueError:
raise StackUnderflowError
if not isinstance(s0, tuple):
raise NotAListError('Not a list.')
try:
_, s1 = s0
except ValueError:
raise StackUnderflowError('Cannot take rest of empty list.')
return (s1, stack)
def rolldown(stack): def rolldown(stack):