def __init__(self, kind, node = None, scope = None, parent_scope = None, bases = None, value = None, klass_name = None): self.kind = kind self.node = node self.value = value self.is_method = False self.self_obj = None if scope != None: self.scope = scope else: if parent_scope != None: self.scope = Scope(parent = parent_scope) else: self.scope = Scope(is_root = True) if self.kind == 'obj': if klass_name: class_type = find_by_name(klass_name) self.scope.update({'__class__':class_type}) if self.kind == 'class': if not bases: inf_object = scope.resolve('object', 'cascade') if inf_object: bases = (inf_object,) else: logger.info('nepodarilo sa resolvnut inf_object') logger.debug('nepodarilo sa ... data: ' + str(scope) +' '+ str(scope.parent) + ' ' + str(scope.parent.parent)) bases = () self.bases = bases self.scope['__bases__'] = bases self.scope['__name__'] = klass_name
def _f(*args): node = [_node for _node in args if isinstance(_node, ast.AST)][0] if hasattr(node, 'lineno'): line = str(node.lineno) else: line = '?' logger.debug(line + " "+fn.__name__) return fn(*args)
def exec_Call(self, node, scope): ''' This method is resposible for exectuing calls. For instance : fun(),a.b.c.fun() or A() as a constructor. ''' #first let's find out which function should be called. This should be easy when doing func() #call, but it can be tricky when using constructs such as 'a.b.c.func()' #covers func() case if isinstance(node.func, ast.Name): logger.debug("? exec_Call "+node.func.id +" "+str(node)) call_type = scope.resolve(node.func.id,'cascade') if call_type is None: call_type = any_type #covers a.b.c.func() case elif isinstance(node.func, ast.Attribute): logger.debug("? exec_Call "+node.func.attr +" "+str(node)) call_type = self.eval_code(node.func, scope) if call_type == any_type: return any_type else: raise Exception('should not get here') #now we know what to invoke. Now we must distinguish whether it is func/method call or 'new' #statement. #Call-ing function or method if call_type.kind == 'funcdef': args = [self.eval_code(arg, scope) for arg in node.args] keywords = {} for keyword in node.keywords: keywords[keyword.arg]=self.eval_code(keyword.value, scope) counter = 0 for arg_eval in args: if arg_eval is None and hasattr(node.args[counter], 'id'): self.warn(node, "NameError: Name '"+ node.args[counter].id + "' is not defined", node.args[counter].id) counter = counter + 1 if call_type.is_method: args = [call_type.self_obj]+args return self._exec_fun(call_type, args, keywords, call_type.scope, node=node) #Call-ing as a 'new' statement if call_type.kind == 'classdef': args = [self.eval_code(arg,scope) for arg in node.args] keywords = {} for keyword in node.keywords: keywords[keyword.arg]=self.eval_code(keyword.value, scope) new = Typez(kind = 'obj', parent_scope = scope) args = [new]+args new.scope['__class__'] = call_type constructor = call_type.resolve('__init__', 'class') if constructor: new.obj = self._exec_fun(constructor, args, keywords, new.scope, node=node) return new if hasattr(node.func, 'id'): self.warn(node, message = 'nonexistent_function', symbol = node.func.id) return any_type
def exec_Call(self, node, scope): #first let's find out which function should be called. This should be easy when doing func() #call, but it can be tricky when using constructs such as 'a.b.c.func()' #covers func() case if isinstance(node.func, ast.Name): logger.debug("? exec_Call "+node.func.id +" "+str(node)) call_type = scope.resolve(node.func.id,'cascade') if call_type is None: call_type = any_type #covers a.b.c.func() case elif isinstance(node.func, ast.Attribute): logger.debug("? exec_Call "+node.func.attr +" "+str(node)) call_type = self.eval_code(node.func, scope) if call_type.kind == 'any': return any_type else: raise Exception('should not get here') #now we know what to invoke. Now we must distinguish whether it is func/method call or 'new' #statement. #TODO: dorobit spustanie 'any' kind #Call-ing function or method if call_type.kind == 'func': args = [self.eval_code(arg, scope) for arg in node.args] if call_type.is_method: args = [call_type.self_obj]+args return self._exec_fun(call_type, args, call_type.scope, node=node) #Call-ing as a 'new' statement if call_type.kind == 'class': args = [self.eval_code(arg,scope) for arg in node.args] new = Typez(kind = 'obj', parent_scope = scope) args = [new]+args new.scope['__class__'] = call_type constructor = call_type.resolve('__init__', 'class') if constructor: new.obj = self._exec_fun(constructor, args, new.scope, node=node) return new self.warn(node, message = 'nonexistent_function', symbol = node.func.id)