def _decl(self): name = self._match('ID') argnames = [] while self.cur_token.typ == 'ID': argnames.append(self.cur_token.val) self._get_next_token() sefl._match("=") expr = self._expr() if len(argnames) > 0: return ast.Decl(name, ast.LambdaExpr(argnames, expr)) else: return ast.Decl(name, expr)
def _decl(self): name = self._match('ID') argnames = [] # If we have arguments, collect them. Only IDs allowed here. while self.cur_token.type == 'ID': argnames.append(self.cur_token.val) self._get_next_token() self._match('=') expr = self._expr() if len(argnames) > 0: return ast.Decl(name, ast.LambdaExpr(argnames, expr)) else: return ast.Decl(name, expr)
def p_struct_declaration(self, p): """ struct_declaration : specifier_qualifier_list struct_declarator_list SEMI """ spec_qual = p[1] struct = None if isinstance(spec_qual['spec'][0], ast.Struct): struct = spec_qual['spec'][0] struct_decl_list = p[2] p[0] = [] for struct_decl in struct_decl_list: if struct is not None: if isinstance(struct_decl, ast.IdentifierType): decl = ast.Decl(name=struct_decl.name, quals=spec_qual['qual'], spec=spec_qual['spec'], storage=[], type=struct, init=None) else: type = struct_decl while not isinstance(type.type, ast.IdentifierType): type = type.type declname = type.type.name type.type = struct decl = ast.Decl(name=declname, quals=spec_qual['qual'], spec=spec_qual['spec'], storage=[], type=struct_decl, init=None) else: type = struct_decl while not isinstance(type, ast.IdentifierType): type = type.type type.spec = spec_qual['spec'] decl = ast.Decl(name=type.name, quals=spec_qual['qual'], spec=spec_qual['spec'], storage=[], type=struct_decl, init=None) p[0].insert(0, decl)
def p_declaration(self, p): """ declaration : declaration_specifiers init_declarator_list_opt SEMI """ decl_spec = p[1] struct = None if isinstance(decl_spec['spec'][0], ast.Struct): struct = decl_spec['spec'][0] init_decl_list = p[2] p[0] = [] for init_decl in init_decl_list: type = init_decl['type'] if struct is not None: if isinstance(type, ast.IdentifierType): decl = ast.Decl(name=type.name, quals=decl_spec['qual'], storage=decl_spec['storage'], spec=decl_spec['spec'], type=struct, init=init_decl['init']) else: while not isinstance(type.type, ast.IdentifierType): type = type.type declname = type.type.name type.type = struct decl = ast.Decl(name=declname, quals=decl_spec['qual'], storage=decl_spec['storage'], spec=decl_spec['spec'], type=init_decl['type'], init=None) else: while not isinstance(type, ast.IdentifierType): type = type.type type.spec = decl_spec['spec'] decl = ast.Decl(name=type.name, quals=decl_spec['qual'], storage=decl_spec['storage'], spec=decl_spec['spec'], type=init_decl['type'], init=init_decl['init']) p[0].insert(0, decl)
def p_parameter_declaration_1(self, p): """ parameter_declaration : declaration_specifiers declarator """ decl_spec = p[1] struct = None if isinstance(decl_spec['spec'][0], ast.Struct): struct = decl_spec['spec'][0] type = p[2] if struct is not None: if isinstance(type, ast.IdentifierType): declaration = ast.Decl(name=type.name, quals=decl_spec['qual'], storage=decl_spec['storage'], spec=decl_spec['spec'], type=struct, init=None) else: while not isinstance(type.type, ast.IdentifierType): type = type.type declname = type.type.name type.type = struct declaration = ast.Decl(name=declname, quals=decl_spec['qual'], storage=decl_spec['storage'], spec=decl_spec['spec'], type=p[2], init=None) else: while not isinstance(type, ast.IdentifierType): type = type.type type.spec = decl_spec['spec'] declaration = ast.Decl(name=type.name, quals=decl_spec['qual'], storage=decl_spec['storage'], spec=decl_spec['spec'], type=p[2], init=None) p[0] = declaration
def p_function_definition_2(self, p): """ function_definition : declaration_specifiers declarator declaration_list_opt compound_statement """ decl_spec = p[1] struct = None if isinstance(decl_spec['spec'][0], ast.Struct): struct = decl_spec['spec'][0] type = p[2] if struct is not None: if isinstance(type, ast.IdentifierType): declaration = ast.Decl(name=type.name, quals=decl_spec['qual'], storage=decl_spec['storage'], spec=decl_spec['spec'], type=struct, init=None) else: while not isinstance(type.type, ast.IdentifierType): type = type.type declname = type.type.name type.type = struct declaration = ast.Decl(name=declname, quals=decl_spec['qual'], storage=decl_spec['storage'], spec=decl_spec['spec'], type=p[2], init=None) else: while not isinstance(type, ast.IdentifierType): type = type.type type.spec = decl_spec['spec'] declaration = ast.Decl(name=type.name, quals=decl_spec['qual'], storage=decl_spec['storage'], spec=decl_spec['spec'], type=p[2], init=None) p[0] = ast.FuncDef(decl=declaration, param_decls=p[3], body=p[4])
def _build_declarations(self, spec, decls): """ Builds a list of declarations all sharing the given specifiers. """ declarations = [] for decl in decls: assert decl['decl'] is not None declaration = ast.Decl(name=None, type=decl['decl'], init=decl.get('init'), coord=decl['decl'].coord) fixed_decl = self._fix_decl_name_type(declaration, spec) declarations.append(fixed_decl) return declarations
def walk_program(self, node): for x in node.defs: # Add a declaration for the variable if self.stmt(x.stmt): x.decls.append(ast.Decl(PROC_ID_VAR, T_VAR_SINGLE, None))