def visit_For(self, node): # print '{0}: For'.format(node.lineno) utils.add_loop_def(self.data, node.lineno) target_walker = walk_ast_for_names(node.target, self.current_scope) utils.add_string_to_data(node.lineno, target_walker.data, target_walker.line) utils.add_targets_to_data(node.lineno, self.data, target_walker.data) utils.combine_variable_scopes(self.variable_scope, target_walker.variable_scope) expr_walker = walk_ast_for_names(node.iter, self.current_scope) utils.add_string_to_data(node.lineno, expr_walker.data, expr_walker.line) utils.combine_data(node.lineno, self.data, expr_walker.data) utils.combine_variable_scopes(self.variable_scope, expr_walker.variable_scope) for stmts in [node.body, node.orelse]: for stmt in stmts: utils.add_loop_line(self.data, node.lineno, stmt.lineno) walker = walk_ast(stmt, self.current_scope) utils.add_string_to_data(stmt.lineno, walker.data, walker.line) # utils.combine_data(stmt.lineno, self.data, walker.data) utils.combine_all_data(self.data, walker.data) utils.combine_variable_scopes(self.variable_scope, walker.variable_scope) utils.set_type(self.data, node.lineno, 'loop')
def visit_Import(self, node): utils.set_type(self.data, node.lineno, 'import') self.lineno = node.lineno for name in node.names: walker = WalkAST() walker.lineno = node.lineno walker.visit(name) utils.combine_all_data(self.data, walker.data)
def visit_Return(self, node): # print '{0}: self.line +='.format(node.lineno) utils.set_type(self.data, node.lineno, 'return') if node.value is not None: walker = walk_ast_for_expr(node.value, self.current_scope) # print walker.data # print self.data # print walker.line utils.combine_data(node.lineno, self.data, walker.data) utils.add_string_to_data(node.lineno, self.data, walker.line) utils.combine_variable_scopes(self.variable_scope, walker.variable_scope)
def visit_Print(self, node): # print '{0}: Print'.format(node.lineno) # print 'Has New Line: {0}'.format(node.nl) utils.set_type(self.data, node.lineno, 'print') for value in node.values: # walker = walk_ast_for_names(value) walker = walk_ast_for_expr(value, self.current_scope) utils.add_string_to_data(node.lineno, walker.data, walker.line) utils.combine_data(node.lineno, self.data, walker.data) utils.combine_variable_scopes(self.variable_scope, walker.variable_scope) self.generic_visit(node)
def visit_If(self, node): # print '{0}: Stmt If'.format(node.lineno) self.line = '' test_walker = walk_ast_for_expr(node.test, self.current_scope) utils.add_string_to_data(node.lineno, test_walker.data, test_walker.line) utils.combine_data(node.lineno, self.data, test_walker.data) utils.combine_variable_scopes(self.variable_scope, test_walker.variable_scope) for stmts in [node.body, node.orelse]: for stmt in stmts: walker = walk_ast(stmt, self.current_scope) utils.add_string_to_data(stmt.lineno, walker.data, walker.line) utils.combine_data(stmt.lineno, self.data, walker.data) utils.combine_variable_scopes(self.variable_scope, walker.variable_scope) utils.remove_empty_string(self.data, stmt.lineno) utils.set_type(self.data, node.lineno, 'conditional')
def visit_Assign(self, node): # print '{0}: Assign'.format(node.lineno) self.line = '' utils.set_type(self.data, node.lineno, 'assign') for target in node.targets: target_walker = walk_ast_for_names(target, self.current_scope) utils.set_assign(self.data, node.lineno, target_walker.line) utils.set_assigned_expressions( self.data, node.lineno, target_walker.line, target_walker.data[node.lineno]['expressions']) utils.combine_variable_scopes(self.variable_scope, target_walker.variable_scope) # utils.combine_data(node.lineno, self.data, target_walker.data) value_walker = walk_ast_for_expr(node.value, self.current_scope) utils.add_string_to_data(node.lineno, value_walker.data, value_walker.line) utils.combine_data(node.lineno, self.data, value_walker.data) # TODO utils.combine_variable_scopes(self.variable_scope, value_walker.variable_scope)
def visit_ClassDef(self, node): # print '{0}: ClassDef Name: {1}'.format(node.lineno, node.name) utils.set_type(self.data, node.lineno, 'class') utils.set_name(self.data, node.lineno, node.name) utils.add_class(self.data, node.name) # Add class functions # Add class variables (only self.* variables) # self.generic_visit(node) for stmts in [node.bases, node.body, node.decorator_list]: for stmt in stmts: walker = walk_ast( stmt, self.current_scope) # possibly switch scope to Class utils.add_string_to_data(stmt.lineno, walker.data, walker.line) # utils.add_function_line(self.data, node.name, stmt.lineno) utils.combine_all_data(self.data, walker.data) utils.combine_variable_scopes(self.variable_scope, walker.variable_scope) utils.add_function_to_class(self.data, walker.data, node.name) utils.add_variables_to_class(self.data, walker.variable_scope, node.name)
def visit_FunctionDef(self, node): # print '{0}: FunctionDef - def {1}():'.format(node.lineno, node.name) utils.set_type(self.data, node.lineno, 'func') utils.set_name(self.data, node.lineno, node.name) utils.add_function_def(self.data, node.name, node.lineno) self.variable_scope[node.name] = [] arg_walker = WalkAST(node.name) arg_walker.lineno = node.lineno arg_walker.visit(node.args) utils.combine_data(node.lineno, self.data, arg_walker.data) utils.combine_variable_scopes(self.variable_scope, arg_walker.variable_scope) for stmts in [node.body, node.decorator_list]: for stmt in stmts: walker = walk_ast(stmt, node.name) utils.add_string_to_data(stmt.lineno, walker.data, walker.line) utils.add_function_line(self.data, node.name, stmt.lineno) # utils.combine_data(stmt.lineno, self.data, walker.data) utils.combine_all_data(self.data, walker.data) utils.combine_variable_scopes(self.variable_scope, walker.variable_scope)
def visit_While(self, node): # print '{0}: While'.format(node.lineno) utils.add_loop_def(self.data, node.lineno) test_walker = WalkAST(self.current_scope) test_walker.get_names = True test_walker.visit(node.test) utils.add_string_to_data(node.lineno, test_walker.data, test_walker.line) utils.combine_data(node.lineno, self.data, test_walker.data) utils.combine_variable_scopes(self.variable_scope, test_walker.variable_scope) for stmts in [node.body, node.orelse]: for stmt in stmts: utils.add_loop_line(self.data, node.lineno, stmt.lineno) walker = walk_ast(stmt, self.current_scope) utils.add_string_to_data(stmt.lineno, walker.data, walker.line) # utils.combine_data(stmt.lineno, self.data, walker.data) utils.combine_all_data(self.data, walker.data) utils.combine_variable_scopes(self.variable_scope, walker.variable_scope) utils.set_type(self.data, node.lineno, 'loop')
def _putValue(value, fo, index, ctype): """ putValue """ size, ctype = set_type(ctype) cformat = ">" + ctype strVal = struct.pack(cformat, value) fo.seek(index) fo.write(strVal) return None
def visit_With(self, node): # print '{0}: With'.format(node.lineno) context_expr = walk_ast_for_expr(node.context_expr, self.current_scope) utils.add_string_to_data(node.lineno, context_expr.data, context_expr.line) utils.combine_data(node.lineno, self.data, context_expr.data) utils.combine_variable_scopes(self.variable_scope, context_expr.variable_scope) if node.optional_vars is not None: optional_vars_walker = walk_ast_for_names(node.optional_vars, self.current_scope) utils.set_type(self.data, node.lineno, 'assign') utils.set_assign(self.data, node.lineno, optional_vars_walker.line) utils.combine_variable_scopes(self.variable_scope, optional_vars_walker.variable_scope) for stmt in node.body: walker = walk_ast(stmt, self.current_scope) utils.add_string_to_data(stmt.lineno, walker.data, walker.line) utils.combine_data(stmt.lineno, self.data, walker.data) utils.combine_variable_scopes(self.variable_scope, walker.variable_scope) utils.remove_empty_string(self.data, stmt.lineno)
def visit_AugAssign(self, node): # print '{0}: AugAssign'.format(node.lineno) target_walker = walk_ast_for_names(node.target, self.current_scope) utils.add_string_to_data(node.lineno, target_walker.data, target_walker.line) utils.combine_data(node.lineno, self.data, target_walker.data) utils.combine_variable_scopes(self.variable_scope, target_walker.variable_scope) op_walker = walk_ast_for_expr(node.op, self.current_scope) utils.combine_variable_scopes(self.variable_scope, op_walker.variable_scope) value_walker = walk_ast_for_expr(node.value, self.current_scope) utils.combine_variable_scopes(self.variable_scope, value_walker.variable_scope) utils.add_string_to_data(node.lineno, value_walker.data, value_walker.line) utils.combine_data(node.lineno, self.data, value_walker.data) self.line = '{0}{1}{2}'.format(target_walker.line, op_walker.line, value_walker.line) utils.add_string_to_data(node.lineno, self.data, self.line) utils.set_type(self.data, node.lineno, 'assign') utils.set_assign(self.data, node.lineno, target_walker.line)
def _putValue(value, fo, index, ctype): """ putValue """ size, ctype = set_type(ctype) cformat = '>' + ctype strVal = struct.pack(cformat, value) fo.seek(index) fo.write(strVal) return None
def visit_Delete(self, node): # print '{0}: Delete'.format(node.lineno) self.generic_visit(node) utils.set_type(self.data, node.lineno, 'delete')