From b18ef44fecc184e3a0c7744f04ff3fc7e4640cf8 Mon Sep 17 00:00:00 2001 From: Marion Date: Mon, 16 Sep 2013 14:21:25 +0000 Subject: show the NAN logoerror if a block wants a number but gets a non-number --- diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index 86027ee..5212acb 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -40,7 +40,7 @@ from tablock import (Block, Media, media_blocks_dictionary) from taconstants import (TAB_LAYER, DEFAULT_SCALE) from tajail import myfunc from tapalette import (block_names, value_blocks) -from tatype import TATypeError +from tatype import (TATypeError, TYPES_NUMERIC) from tautils import (get_pixbuf_from_journal, data_from_file, text_media_type, round_int, debug_output, find_group, get_stack_name) @@ -532,50 +532,46 @@ class LogoCode: try: while (_millisecond() - starttime) < 120: try: - if self.step is not None: - if self.tw.running_turtleart: - try: - self.step.next() - except ValueError, ve: - debug_output('generator already executing', - self.tw.running_sugar) - self.tw.running_blocks = False - return False - except TATypeError as tte: - # TODO insert the correct block name - # (self.cfun.name is only the name of the - # outermost block in this statement/ line of code) - # TODO use logoerror("#notanumber") when possible - raise logoerror("%s %s %s %s" % (self.cfun.name, - _("doesn't like"), str(tte.bad_value), - _("as input"))) - except ZeroDivisionError: - raise logoerror("#zerodivide") - except NegativeRootError: - raise logoerror("#negroot") - except IndexError: - raise logoerror("#emptyheap") - else: - try: - self.step.next() - except TATypeError as tte: - # TODO write better messages for TATypeErrors - # TODO then remove this except clause + if self.step is None: + return False + if self.tw.running_turtleart: + try: + self.step.next() + except ValueError, ve: + debug_output('generator already executing', + self.tw.running_sugar) + self.tw.running_blocks = False + return False + except TATypeError as tte: + # TODO insert the correct block name + # (self.cfun.name is only the name of the + # outermost block in this statement/ line of code) + # use logoerror("#notanumber") when possible + if (tte.req_type in TYPES_NUMERIC and + tte.bad_type not in TYPES_NUMERIC): + raise logoerror("#notanumber") + else: + raise logoerror("%s %s %s %s" + % (self.cfun.name, _("doesn't like"), + str(tte.bad_value), _("as input"))) + except ZeroDivisionError: + raise logoerror("#zerodivide") + except NegativeRootError: + raise logoerror("#negroot") + except IndexError: + raise logoerror("#emptyheap") + else: + try: + self.step.next() + except BaseException as error: + if isinstance(error, (StopIteration, + logoerror)): + raise error + else: traceback.print_exc() - raise logoerror("%s %s %s %s" % (self.cfun.name, - _("doesn't like"), str(tte.bad_value), - _("as input"))) - except Exception as error: - if isinstance(error, (StopIteration, - logoerror)): - raise error - else: - traceback.print_exc() - self.tw.showlabel('status', '%s: %s' - % (type(error).__name__, str(error))) + self.tw.showlabel('status', '%s: %s' + % (type(error).__name__, str(error))) return False - else: - return False except StopIteration: if self.tw.running_turtleart: # self.tw.turtles.show_all() diff --git a/TurtleArt/tatype.py b/TurtleArt/tatype.py index 1402014..46c5921 100644 --- a/TurtleArt/tatype.py +++ b/TurtleArt/tatype.py @@ -68,6 +68,7 @@ class TypeDisjunction(tuple,Type): return "".join(s) +# individual types TYPE_OBJECT = Type('TYPE_OBJECT', 0) TYPE_BOOL = Type('TYPE_BOOL', 5) TYPE_BOX = Type('TYPE_BOX', 8) # special type for the unknown content of a box @@ -81,10 +82,14 @@ TYPE_NUMBER = Type('TYPE_NUMBER', 6) # shortcut to avoid a TypeDisjunction TYPE_NUMERIC_STRING = Type('TYPE_NUMERIC_STRING', 7) TYPE_STRING = Type('TYPE_STRING', 9) +# groups/ classes of types +TYPES_NUMERIC = (TYPE_FLOAT, TYPE_INT, TYPE_NUMBER) + BOX_AST = ast.Name(id='BOX', ctx=ast.Load) ACTION_AST = ast.Name(id='ACTION', ctx=ast.Load) + def get_type(x): """ Return the most specific type in the type hierarchy that applies to x and a boolean indicating whether x is an AST. If the type cannot be -- cgit v0.9.1