From 72042fc36f503894741ea4086bb89076b58542dc Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Mon, 09 Jul 2012 21:13:32 +0000 Subject: fix problem with nested while loops --- diff --git a/TurtleArt/tabasics.py b/TurtleArt/tabasics.py index 06b8d1c..56a2fd1 100644 --- a/TurtleArt/tabasics.py +++ b/TurtleArt/tabasics.py @@ -785,6 +785,7 @@ boolean operators from Numbers palette')) help_string=_('if-then-else operator that uses \ boolean operators from Numbers palette')) + # Deprecated palette.add_block('hspace', style='flow-style-tail', hidden=True, diff --git a/TurtleArt/tablock.py b/TurtleArt/tablock.py index 7e48d43..dd1ac04 100644 --- a/TurtleArt/tablock.py +++ b/TurtleArt/tablock.py @@ -49,6 +49,12 @@ class Blocks: else: return(self.list[i]) + def swap(self, blk1, blk2): + i1 = self.list.index(blk1) + i2 = self.list.index(blk2) + self.list[i1] = blk2 + self.list[i2] = blk1 + def length_of_list(self): return(len(self.list)) diff --git a/TurtleArt/talogo.py b/TurtleArt/talogo.py index 3e8fb0f..8d957bc 100644 --- a/TurtleArt/talogo.py +++ b/TurtleArt/talogo.py @@ -803,8 +803,6 @@ class LogoCode: # into trouble if any of these block types (forever, while, # until. ifelse, stopstack, or stack) is changed in tablock.py - # TODO: Detect nesting, e.g., forever while - if b.name == 'while': while_blk = True else: @@ -819,7 +817,7 @@ class LogoCode: self.save_blocks = blocks[:] # Create an action block that will jump to the new stack - action_name = '#s_forever %d' % (len(self.save_while_blks) + 1) + action_name = '_forever %d' % (len(self.save_while_blks) + 1) action_blk = HiddenBlock('stack') action_label_blk = HiddenBlock('string', value=action_name) @@ -841,7 +839,7 @@ class LogoCode: # Create action block(s) to run the code inside the forever loop if until_blk and whileflow is not None: # run until flow at least once - action_flow_name = '#s_flow %d' % (len(self.save_while_blks) + 1) + action_flow_name = '_flow %d' % (len(self.save_while_blks) + 1) action_first = HiddenBlock('stack') first_label_blk = HiddenBlock('string', value=action_flow_name) diff --git a/TurtleArt/tautils.py b/TurtleArt/tautils.py index bd7309f..676c4ca 100644 --- a/TurtleArt/tautils.py +++ b/TurtleArt/tautils.py @@ -657,13 +657,15 @@ def find_group(blk): return group -def find_blk_below(blk, name): +def find_blk_below(blk, namelist): ''' Find a specific block below this block. ''' if blk == None or len(blk.connections) == 0: return + if type(namelist) != list: + namelist = [namelist] group = find_group(blk) for gblk in group: - if gblk.name == name: + if gblk.name in namelist: return gblk return None diff --git a/TurtleArt/tawindow.py b/TurtleArt/tawindow.py index d187580..ad0f0db 100644 --- a/TurtleArt/tawindow.py +++ b/TurtleArt/tawindow.py @@ -2592,12 +2592,39 @@ class TurtleArtWindow(): # If we are in an expandable flow, expand it... if best_destination is not None: self._resize_parent_clamps(best_destination) - ''' - blk, dockn = self._expandable_flow_above(best_destination) - while blk is not None: - self._resize_clamp(blk, blk.connections[dockn], dockn=dockn) - blk, dockn = self._expandable_flow_above(blk) - ''' + # Check for while nesting + if best_destination is not None: + while_blk = self._while_in_drag_group(self.drag_group[0]) + if while_blk is not None: + self._check_while_nesting(best_destination, + self.drag_group[0], while_blk) + + def _while_in_drag_group(self, blk): + ''' Is there a contained while or until block? ''' + if blk.name in ['while', 'until']: + return blk + return find_blk_below(blk, ['while', 'until']) + + def _check_while_nesting(self, blk, dock_blk, while_blk): + ''' Is there a containing while or until block? If so, swap them ''' + if blk.name in ['while', 'until']: + if blk.connections[2] == dock_blk: + self._swap_while_blocks(blk, while_blk) + while blk.connections[-1] is not None: + blk = blk.connections[-1] + if blk.name in ['while', 'until']: + if blk.connections[2] == dock_blk: + self._swap_while_blocks(blk, while_blk) + dock_blk = blk + + def _swap_while_blocks(self, blk1, blk2): + ''' Swap postion in block list of nested while blocks ''' + # Check to see if blk1 comes before blk2 in the block list. + # If so, swap them. + i1 = self.just_blocks().index(blk1) + i2 = self.just_blocks().index(blk2) + if i1 < i2: + self.block_list.swap(blk1, blk2) def _disconnect(self, blk): ''' Disconnect block from stack above it. ''' -- cgit v0.9.1