From ebdb8da03d55a03f14ef04a97ecf498af941293c Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Thu, 8 Sep 2022 08:27:47 -0700 Subject: [PATCH] move type checks and exceptions to bottom --- implementations/Python/simplejoy.py | 115 +++++++++++++++++++----------------- 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/implementations/Python/simplejoy.py b/implementations/Python/simplejoy.py index a481303..91eb352 100755 --- a/implementations/Python/simplejoy.py +++ b/implementations/Python/simplejoy.py @@ -36,59 +36,6 @@ from traceback import print_exc import operator -class NotAListError(Exception): - ''' - Raised when a stack is expected but not received. - ''' - - -class NotAnIntError(Exception): - pass - - -class NotABoolError(Exception): - pass - - -class ParseError(ValueError): - ''' - Raised when there is a error while parsing text. - ''' - - -class StackUnderflowError(Exception): - pass - - -class UnknownSymbolError(KeyError): - pass - - -def isnt_int(i): - ''' - Raise NotAnIntError if i isn't an integer. - (Booleans are not integers in Joy.) - ''' - if not isinstance(i, int) or isinstance(i, bool): - raise NotAnIntError(f'Not an integer: {_s(i)}') - - -def isnt_bool(b): - ''' - Raise NotABoolError if b isn't a Boolean. - ''' - if not isinstance(b, bool): - raise NotABoolError(f'Not a Boolean value: {_s(b)}') - - -def isnt_stack(el): - ''' - Raise NotAListError if el isn't a stack/quote/list. - ''' - if not isinstance(el, tuple): - raise NotAListError(f'Not a list: {_s(el)}') - - ''' ██╗███╗ ██╗████████╗███████╗██████╗ ██████╗ ██████╗ ███████╗████████╗███████╗██████╗ ██║████╗ ██║╚══██╔══╝██╔════╝██╔══██╗██╔══██╗██╔══██╗██╔════╝╚══██╔══╝██╔════╝██╔══██╗ @@ -1298,6 +1245,68 @@ _map2 ≡ [infrst] cons dipd roll< swons ''' +''' +████████╗██╗ ██╗██████╗ ███████╗ ██████╗██╗ ██╗███████╗ ██████╗██╗ ██╗███████╗ +╚══██╔══╝╚██╗ ██╔╝██╔══██╗██╔════╝ ██╔════╝██║ ██║██╔════╝██╔════╝██║ ██╔╝██╔════╝ + ██║ ╚████╔╝ ██████╔╝█████╗ ██║ ███████║█████╗ ██║ █████╔╝ ███████╗ + ██║ ╚██╔╝ ██╔═══╝ ██╔══╝ ██║ ██╔══██║██╔══╝ ██║ ██╔═██╗ ╚════██║ + ██║ ██║ ██║ ███████╗ ╚██████╗██║ ██║███████╗╚██████╗██║ ██╗███████║ + ╚═╝ ╚═╝ ╚═╝ ╚══════╝ ╚═════╝╚═╝ ╚═╝╚══════╝ ╚═════╝╚═╝ ╚═╝╚══════╝ +''' + +class NotAListError(Exception): + ''' + Raised when a stack is expected but not received. + ''' + + +class NotAnIntError(Exception): + pass + + +class NotABoolError(Exception): + pass + + +class ParseError(ValueError): + ''' + Raised when there is a error while parsing text. + ''' + + +class StackUnderflowError(Exception): + pass + + +class UnknownSymbolError(KeyError): + pass + + +def isnt_int(i): + ''' + Raise NotAnIntError if i isn't an integer. + (Booleans are not integers in Joy.) + ''' + if not isinstance(i, int) or isinstance(i, bool): + raise NotAnIntError(f'Not an integer: {_s(i)}') + + +def isnt_bool(b): + ''' + Raise NotABoolError if b isn't a Boolean. + ''' + if not isinstance(b, bool): + raise NotABoolError(f'Not a Boolean value: {_s(b)}') + + +def isnt_stack(el): + ''' + Raise NotAListError if el isn't a stack/quote/list. + ''' + if not isinstance(el, tuple): + raise NotAListError(f'Not a list: {_s(el)}') + + if __name__ == '__main__': import sys -- 2.11.0