From 27c87bbc282dcc72fcc137684831551556eb5e24 Mon Sep 17 00:00:00 2001 From: Marion Date: Tue, 10 Sep 2013 22:04:43 +0000 Subject: add Primitives for the 'push', 'pop', and 'is empty heap?' blocks --- (limited to 'TurtleArt/tatype.py') diff --git a/TurtleArt/tatype.py b/TurtleArt/tatype.py index 9a8c17d..3a14fbf 100644 --- a/TurtleArt/tatype.py +++ b/TurtleArt/tatype.py @@ -46,11 +46,9 @@ class Type(object): repr(type(other))) return self.value == other.value - def __repr__(self): - return repr(self.constant_name) - def __str__(self): return str(self.constant_name) + __repr__ = __str__ class TypeDisjunction(tuple,Type): @@ -137,7 +135,11 @@ def get_type(x): return (TYPE_STRING, True) # unary operands never change the type of their argument elif isinstance(x, ast.UnaryOp): - return get_type(x.operand) + if issubclass(x.op, ast.Not): + # 'not' always returns a boolean + return (TYPE_BOOL, True) + else: + return get_type(x.operand) # boolean and comparison operators always return a boolean if isinstance(x, (ast.BoolOp, ast.Compare)): return (TYPE_BOOL, True) @@ -159,11 +161,9 @@ def is_instancemethod(method): # TODO how to access the type `instancemethod` directly? return type(method).__name__ == "instancemethod" -def is_bound_instancemethod(method): - return is_instancemethod(method) and method.im_self is not None - -def is_unbound_instancemethod(method): - return is_instancemethod(method) and method.im_self is None +def is_bound_method(method): + return ((is_instancemethod(method) and method.im_self is not None) or + (hasattr(method, '__self__') and method.__self__ is not None)) def is_staticmethod(method): # TODO how to access the type `staticmethod` directly? @@ -311,8 +311,8 @@ def convert(x, new_type, old_type=None, converter=None): if old_type == new_type: return x - # special case: 'box' block as an AST - if isinstance(x, ast.Subscript) and x.value is BOX_AST: + # special case: 'box' block (or 'pop' block) as an AST + if is_an_ast and old_type == TYPE_BOX: new_type_ast = ast.Name(id=new_type.constant_name) return get_call_ast('convert', [x, new_type_ast], return_type=new_type) @@ -378,7 +378,7 @@ class TypedCall(ast.Call,TypedAST): class TypedSubscript(ast.Subscript,TypedAST): - """ Like a Subscript AST, but with a return type """ + """ Like a Subscript AST, but with a type """ def __init__(self, value, slice_, ctx=ast.Load, return_type=None): @@ -387,6 +387,16 @@ class TypedSubscript(ast.Subscript,TypedAST): self._return_type = return_type +class TypedName(ast.Name,TypedAST): + """ Like a Name AST, but with a type """ + + def __init__(self, id_, ctx=ast.Load, return_type=None): + + ast.Name.__init__(self, id=id_, ctx=ctx) + + self._return_type = return_type + + def get_call_ast(func_name, args=None, kwargs=None, return_type=None): """ Return an AST representing the call to a function with the name func_name, passing it the arguments args (given as a list) and the -- cgit v0.9.1