From 21b5205a773fb9a5049a7ed9b973f268fd598d63 Mon Sep 17 00:00:00 2001 From: Walter Bender Date: Tue, 09 Feb 2010 16:17:44 +0000 Subject: expandable no. args to user-defined blocks --- diff --git a/tablock.py b/tablock.py index 5d46a97..895d31b 100644 --- a/tablock.py +++ b/tablock.py @@ -152,10 +152,10 @@ class Block: self.spr.draw() # We may want to add additional slots for arguments ("innies"). - def add_arg(self): + def add_arg(self, keep_expanding=True): h = self.svg.get_height() self._ei += 1 - if self.type == 'block': + if self.type == 'block' and keep_expanding: self.svg.set_show(True) else: self.svg.set_show(False) @@ -307,6 +307,8 @@ class Block: self._make_number_style(svg) elif self.name in NUMBER_STYLE_BLOCK: self._make_number_style_block(svg) + elif self.name in NUMBER_STYLE_VAR_ARG: + self._make_number_style_var_arg(svg) elif self.name in NUMBER_STYLE_1ARG: self._make_number_style_1arg(svg) elif self.name in NUMBER_STYLE_1STRARG: @@ -458,6 +460,26 @@ class Block: ['number', False, self.svg.docks[1][0], self.svg.docks[1][1]]] + def _make_number_style_var_arg(self, svg): + self.svg.expand(self._dx+self._ex, self._ey) + innie = [True] + for i in range(self._ei+1): + innie.append(True) + self.svg.set_innie(innie) + self.svg.set_outie(True) + self.svg.set_tab(False) + self.svg.set_slot(False) + self._make_basic_block(svg) + self.docks = [['number', True, self.svg.docks[2+self._ei][0], + self.svg.docks[2+self._ei][1]], + ['number', False, self.svg.docks[0][0], + self.svg.docks[0][1], '[']] + for i in range(self._ei+1): + self.docks.append(['number', False, self.svg.docks[i+1][0], + self.svg.docks[i+1][1]]) + self.docks.append(['unavailable', False, 0, 0, ']']) + print self.docks + def _make_number_style_block(self, svg): self.svg.expand(self._dx+self._ex, self._ey) self.svg.set_innie([True,True]) diff --git a/taconstants.py b/taconstants.py index fa3f1b5..09baad6 100644 --- a/taconstants.py +++ b/taconstants.py @@ -184,7 +184,8 @@ BOX_STYLE = ['number', 'xcor', 'ycor', 'heading', 'pensize', 'color', 'shade', 'red', 'orange', 'yellow', 'green', 'cyan', 'blue', 'purple', 'titlex', 'titley', 'leftx', 'topy', 'rightx', 'bottomy'] BOX_STYLE_MEDIA = ['description', 'audio', 'journal'] -NUMBER_STYLE = ['plus2', 'product2', 'myfunc'] +NUMBER_STYLE = ['plus2', 'product2'] +NUMBER_STYLE_VAR_ARG = ['myfunc'] NUMBER_STYLE_BLOCK = ['random'] NUMBER_STYLE_PORCH = ['minus2', 'division2', 'remainder2'] NUMBER_STYLE_1ARG = ['sqrt', 'identity2'] @@ -319,7 +320,7 @@ MACROS = { # # blocks that are expandable # -EXPANDABLE = ['vspace', 'hspace', 'templatelist', 'list', 'identity2'] +EXPANDABLE = ['vspace', 'hspace', 'templatelist', 'list', 'identity2', 'myfunc'] # # Old block styles that need dock adjustments @@ -379,7 +380,7 @@ BLOCK_NAMES = { 'less2':['<'], 'list':['list'], 'minus2':['–'], - 'myfunc':[_('Python'),_('code'),_('value')], + 'myfunc':[_('Python'),'f(x)',_('x')], 'nop':[_(' ')], 'not':[_('not')], 'number':['100'], @@ -657,6 +658,9 @@ OLD_NAMES = {'product':'product2', 'storeinbox':'storein', 'minus':'minus2', # TITLEXY = (0.9375, 0.875) +# +# Relative placement of portfolio objects (used by depreciated blocks) +# TEMPLATES = {'t1x1': (0.5, 0.5, 0.0625, 0.125, 1.05, 0), 't2z1': (0.5, 0.5, 0.0625, 0.125, 1.05, 1.05), 't1x2': (0.45, 0.45, 0.0625, 0.125, 1.05, 1.05), diff --git a/tajail.py b/tajail.py index 3283723..9954eb3 100644 --- a/tajail.py +++ b/tajail.py @@ -26,15 +26,32 @@ try: except ImportError: pass -def myfunc(lc, f, x): +def myfunc(f, args): # check to make sure no import calls are made - myf = "def f(x): return " + f.replace("import","") - userdefined = {} - try: - exec myf in globals(), userdefined - return userdefined.values()[0](x) - except: - return None + if len(args) == 1: + myf = "def f(x): return " + f.replace("import","") + userdefined = {} + try: + exec myf in globals(), userdefined + return userdefined.values()[0](args[0]) + except: + return None + elif len(args) == 2: + myf = "def f(x,y): return " + f.replace("import","") + userdefined = {} + try: + exec myf in globals(), userdefined + return userdefined.values()[0](args[0],args[1]) + except: + return None + elif len(args) == 3: + myf = "def f(x,y,z): return " + f.replace("import","") + userdefined = {} + try: + exec myf in globals(), userdefined + return userdefined.values()[0](args[0],args[1],args[2]) + except: + return None def myfunc_import(lc, f, x): userdefined = {} diff --git a/talogo.py b/talogo.py index bfa5309..20856b5 100644 --- a/talogo.py +++ b/talogo.py @@ -250,7 +250,8 @@ class LogoCode: 'less?':[2, lambda self,x,y: taless(x,y)], 'minus':[2, lambda self,x,y: taminus(x,y)], 'mod':[2, lambda self,x,y: tamod(x,y)], - 'myfunc':[2, lambda self,f,x: self.prim_myfunc(f, x)], + 'myfunc':[1, self.prim_myfunc, True], + 'myfunction':[1, lambda self, x: self.myfunction(x)], 'nop':[0, lambda self: None], 'nop1':[0, lambda self: None], 'nop2':[0, lambda self: None], @@ -767,6 +768,22 @@ class LogoCode: self.ireturn() yield True + def prim_myfunc(self, list): + new_list = [self.intern('myfunction')] + new_list.append(list) + self.icall(self.evline, new_list) + yield True + self.ireturn() + yield True + + def myfunction(self, list): + y = myfunc(list[0], list[1:]) + if y == None: + raise logoerror("#syntaxerror") + stop_logo(self.tw) + else: + return y + def prim_forever(self, list): while True: self.icall(self.evline, list[:]) @@ -862,14 +879,6 @@ class LogoCode: raise logoerror("#nocode") return - def prim_myfunc(self, f, x): - y = myfunc(self, f, x) - if y == None: - raise logoerror("#syntaxerror") - stop_logo(self.tw) - else: - return y - def prim_print(self, n, flag): if flag and self.tw.hide: return diff --git a/tawindow.py b/tawindow.py index 9199b85..0c522e4 100644 --- a/tawindow.py +++ b/tawindow.py @@ -1132,7 +1132,14 @@ class TurtleArtWindow(): if self._show_button_hit(blk.spr, x, y): n = len(blk.connections) group = self._find_group(blk.connections[n-1]) - dy = blk.add_arg() + if blk.name == 'myfunc' and n == 4: + blk.spr.labels[1] = 'f(x,y)' + blk.spr.labels[2] = ' ' + if blk.name == 'myfunc' and n == 5: + blk.spr.labels[1] = 'f(x,y,z)' + dy = blk.add_arg(False) + else: + dy = blk.add_arg() for b in group: b.spr.move_relative((0, dy)) blk.connections.append(blk.connections[n-1]) @@ -1462,12 +1469,20 @@ class TurtleArtWindow(): """ def _process_alphanumeric_input(self, keyname, keyunicode): if len(self.selected_blk.spr.labels[0]) > 0: - if self.selected_blk.spr.labels[0].count(CURSOR) == -1: + if self.selected_blk.spr.labels[0].count(CURSOR) == 0: oldleft = self.selected_blk.spr.labels[0] oldright = '' - else: - oldleft, oldright =\ - self.selected_blk.spr.labels[0].rsplit(CURSOR) + elif len(self.selected_blk.spr.labels[0]) == 1: + oldleft = '' + oldright = '' + else: # Why are getting a ValueError on occasion? + try: + oldleft, oldright =\ + self.selected_blk.spr.labels[0].rsplit(CURSOR) + except ValueError: + print "[%s]" % self.selected_blk.spr.labels[0] + oldleft = self.selected_blk.spr.labels[0] + oldright = '' else: oldleft = '' oldright = '' -- cgit v0.9.1