diff options
author | Marion <marion.zepf@gmail.com> | 2013-09-04 09:03:36 (GMT) |
---|---|---|
committer | Marion <marion.zepf@gmail.com> | 2013-09-04 09:03:36 (GMT) |
commit | 6ee1bb7f1a827d2b6e1d898c63d8017fa95d7222 (patch) | |
tree | 2e18ce5da1fa720da68a44f729dcf1ebb8d0e7a2 /TurtleArt | |
parent | dad6e183fe30339a6bb450ee8e9a02fe534dbb32 (diff) |
update Primitives for the 'store in' and 'box' blocks
- They are not yet exportable.
- Fix TypeDisjunctions as return types of Primitives.
Diffstat (limited to 'TurtleArt')
-rw-r--r-- | TurtleArt/tabasics.py | 26 | ||||
-rw-r--r-- | TurtleArt/taprimitive.py | 32 | ||||
-rw-r--r-- | TurtleArt/tatype.py | 14 |
3 files changed, 44 insertions, 28 deletions
diff --git a/TurtleArt/tabasics.py b/TurtleArt/tabasics.py index db27eaf..ed7c337 100644 --- a/TurtleArt/tabasics.py +++ b/TurtleArt/tabasics.py @@ -994,7 +994,6 @@ buttons')) self.tw.lc.def_prim('stack', 1, Primitive(self.tw.lc.prim_invoke_stack), True) - primitive_dictionary['setbox'] = Primitive(self.tw.lc.prim_set_box) palette.add_block('storeinbox1', hidden=True, style='basic-style-1arg', @@ -1005,7 +1004,8 @@ buttons')) logo_command='make "box1', help_string=_('stores numeric value in Variable 1')) self.tw.lc.def_prim('storeinbox1', 1, - Primitive(self.tw.lc.prim_set_box, constant_args={0: 'box1'})) + Primitive(self.tw.lc.prim_set_box, + arg_descs=[ConstantArg('box1'), ArgSlot(TYPE_OBJECT)])) palette.add_block('storeinbox2', hidden=True, @@ -1017,7 +1017,8 @@ buttons')) logo_command='make "box2', help_string=_('stores numeric value in Variable 2')) self.tw.lc.def_prim('storeinbox2', 1, - Primitive(self.tw.lc.prim_set_box, constant_args={0: 'box2'})) + Primitive(self.tw.lc.prim_set_box, + arg_descs=[ConstantArg('box2'), ArgSlot(TYPE_OBJECT)])) palette.add_block('box1', hidden=True, @@ -1028,7 +1029,8 @@ buttons')) help_string=_('Variable 1 (numeric value)'), value_block=True) self.tw.lc.def_prim('box1', 0, - Primitive(self.tw.lc.prim_get_box, constant_args={0: 'box1'})) + Primitive(self.tw.lc.prim_get_box, + arg_descs=[ConstantArg('box1')])) palette.add_block('box2', hidden=True, @@ -1039,8 +1041,11 @@ buttons')) help_string=_('Variable 2 (numeric value)'), value_block=True) self.tw.lc.def_prim('box2', 0, - Primitive(self.tw.lc.prim_get_box, constant_args={0: 'box2'})) + Primitive(self.tw.lc.prim_get_box, + arg_descs=[ConstantArg('box2')])) + primitive_dictionary['setbox'] = Primitive(self.tw.lc.prim_set_box, + arg_descs=[ArgSlot(TYPE_STRING), ArgSlot(TYPE_OBJECT)]) palette.add_block('storein', style='basic-style-2arg', label=[_('store in'), _('box'), _('value')], @@ -1050,10 +1055,12 @@ buttons')) default=[_('my box'), 100], help_string=_('stores numeric value in named \ variable')) - self.tw.lc.def_prim('storeinbox', 2, - Primitive(self.tw.lc.prim_set_box)) + self.tw.lc.def_prim('storeinbox', 2, primitive_dictionary['setbox']) - primitive_dictionary['box'] = Primitive(self.tw.lc.prim_get_box) + primitive_dictionary['box'] = Primitive(self.tw.lc.prim_get_box, + return_type=or_(TYPE_OBJECT, TYPE_STRING, TYPE_NUMBER, TYPE_FLOAT, + TYPE_INT, TYPE_NUMERIC_STRING, TYPE_CHAR, TYPE_COLOR), + arg_descs=[ArgSlot(TYPE_STRING)]) palette.add_block('box', style='number-style-1strarg', hidden=True, @@ -1064,8 +1071,7 @@ variable')) logo_command='box', value_block=True, help_string=_('named variable (numeric value)')) - self.tw.lc.def_prim('box', 1, - Primitive(self.tw.lc.prim_get_box)) + self.tw.lc.def_prim('box', 1, primitive_dictionary['box']) palette.add_block('hat1', hidden=True, diff --git a/TurtleArt/taprimitive.py b/TurtleArt/taprimitive.py index 217b0cb..cf15ca5 100644 --- a/TurtleArt/taprimitive.py +++ b/TurtleArt/taprimitive.py @@ -295,7 +295,7 @@ class Primitive(object): debug_output(" arg_asts: " + repr(arg_asts)) new_prim = self.fill_slots(arg_asts, kwarg_asts, convert_to_ast=True) if not new_prim.are_slots_filled(): - raise PyExportError("not enough arguments") + raise PyExportError("not enough arguments") # TODO better msg if Primitive._DEBUG: debug_output(" new_prim.arg_descs: " + repr(new_prim.arg_descs)) @@ -816,11 +816,6 @@ class Disjunction(tuple): return self -# make TypeDisjunction 'inherit' the methods of the abstract Disjunction class -TypeDisjunction.__repr__ = Disjunction.__repr__ -TypeDisjunction.get_alternatives = Disjunction.get_alternatives - - class PrimitiveDisjunction(Disjunction,Primitive): """ Disjunction of two or more Primitives. PrimitiveDisjunctions may not be nested. """ @@ -931,23 +926,26 @@ class ArgSlot(object): # check if the argument can fill this slot (type-wise) if wrapper is not None: - arg_type = get_type(wrapper)[0] + arg_types = get_type(wrapper)[0] bad_value = wrapper elif func is not None: - arg_type = get_type(func)[0] + arg_types = get_type(func)[0] bad_value = func else: - arg_type = get_type(argument)[0] + arg_types = get_type(argument)[0] bad_value = argument converter = None + if not isinstance(arg_types, TypeDisjunction): + arg_types = TypeDisjunction((arg_types, )) if isinstance(slot.type, TypeDisjunction): - for type_ in slot.type: - converter = get_converter(arg_type, type_) + slot_types = slot.type + else: + slot_types = TypeDisjunction((slot.type, )) + for old_type in arg_types: + for new_type in slot_types: + converter = get_converter(old_type, new_type) if converter is not None: break - else: - type_ = slot.type - converter = get_converter(arg_type, type_) # unable to convert, try next wrapper/ slot/ func if converter is None: continue @@ -1006,7 +1004,7 @@ class ArgSlot(object): # 3. check the type and convert the argument if necessary try: - converted_argument = convert(wrapped_argument, type_, + converted_argument = convert(wrapped_argument, new_type, converter=converter) except TATypeError as error: # on failure, try next wrapper/ slot/ func @@ -1020,8 +1018,8 @@ class ArgSlot(object): if error is not None: raise error else: - raise TATypeError(bad_value=bad_value, bad_type=arg_type, - req_type=type_, + raise TATypeError(bad_value=bad_value, bad_type=old_type, + req_type=new_type, message="filling slot " + repr(self)) diff --git a/TurtleArt/tatype.py b/TurtleArt/tatype.py index 164ba2a..86119b6 100644 --- a/TurtleArt/tatype.py +++ b/TurtleArt/tatype.py @@ -49,7 +49,19 @@ class Type(object): class TypeDisjunction(tuple,Type): """ Disjunction of two or more Types (from the type hierarchy) """ - pass + + def __init__(self, iterable): + self = tuple(iterable) + + + def __str__(self): + s = ["("] + for disj in self: + s.append(str(disj)) + s.append(" or ") + s.pop() + s.append(")") + return "".join(s) TYPE_OBJECT = Type('object', 0) |