def make_literal_ast(scope_ast, pt): assert pt.name == 'literal' child_pt = pt.children[0] ast = Node(None, "expression") ast_type_name = TYPENAME_MAP.get(child_pt.name) if ast_type_name is None: raise Error("Unknown literal type %s" % child_pt.name) target_type = infogripper.getNode(ast_type_name, scope_ast, 'type') if target_type is None: raise Error("Couldn't find type %s in AST" % (ast_type_name)) if target_type.has_attribute('smallest') and target_type.has_attribute('largest'): # FIXME: Shouldn't be doing this. ast_type_name = smallest_type_hack(get_python_value(child_pt.leaf)) target_type = infogripper.getNode(ast_type_name, scope_ast, 'type') # FIXME: Use of eval is evil ast.add_attribute('value', get_python_value(child_pt.leaf)) # Remember the original name for later. ast.add_attribute('display_name', child_pt.name) ast.add_child(target_type) return ast
def get_scoped_name(scope_ast, pt, ast_gen): the_name = pt.get_single_attribute('value') scoped_name = Node(scope_ast, 'expression', leaf='scoped_name', source=pt) scoped_name.add_attribute('value', the_name) # Try to find a target. type_ast = getNode(the_name, scope_ast) # It's not the end of the world if we don't find a target. if type_ast: target_ast = Node(scoped_name, 'target', [type_ast]) scoped_name.add_child(target_ast) return scoped_name
def get_scoped_name(scope_ast, pt, ast_gen): the_name = pt.get_single_attribute("value") scoped_name = Node(scope_ast, "expression", leaf="scoped_name", source=pt) scoped_name.add_attribute("value", the_name) # Try to find a target. type_ast = getNode(the_name, scope_ast) # It's not the end of the world if we don't find a target. if type_ast: target_ast = Node(scoped_name, "target", [type_ast]) scoped_name.add_child(target_ast) return scoped_name
def module(self, ast, pt, decorators): assert not decorators module_name = pt.the('identifier').leaf mod = infogripper.getNode(module_name, ast, 'module') mod_is_new = False if mod is None: mod = Node(ast, 'module', None, source = pt) ast.add_child(mod) mod_is_new = True for child in pt.children: if child.type == 'identifier': mod.leaf = child.leaf # Modules can be re-opened, hence the check: if mod_is_new: self._dupes_check(mod) elif child.type == 'definition_list': for def_child in child.children: self.decorated_def(mod, def_child) #def_child = self.definition(def_child) #mod.add_child(def_child) else: mod.add_child(UnknownNode(None,child, source = pt))
def pop_and_add(scope_ast, oper, operands): # Pop and create new tree node = Node(None, 'expression') node.leaf = OPER_NAMES[oper] for count in range(get_arity(oper)): node.add_front(operands.pop()) # Work out the type for the resulting expression. type_ast = get_expr_type(node.children[0]) for new_type_expr in node.children[1:]: new_type_ast = get_expr_type(new_type_expr) if infogripper.can_coerce_expr(new_type_expr, type_ast): type_ast = new_type_ast else: raise error.CannotReconcileTypesError(new_type_ast.leaf, type_ast.leaf, scope_ast) # Store a value, if we can. This over-rides the above discovered type. # FIXME: This is a cheesy hack. if type_ast.has_attribute('smallest') and type_ast.has_attribute('largest'): # It's a scalar type, we can play with it. if len(node.children) > 1: result = oper.join(['(%s)' % (child.attribute('value')) for child in node.children]) else: # FIXME: Even cheesier result = "%s%s" % (PYTHON_UNARY[oper] , node.children[0].attribute('value')) try: result = eval(result) node.add_attribute('value', result) except: pass smallest_type_name = smallest_type_hack(result) if smallest_type_name: type_ast = infogripper.getNode(smallest_type_name, scope_ast, 'type') node.add_child(type_ast) operands.append(node)