| 177 | |
| 178 | |
| 179 | class Function(SymbolTable): |
| 180 | |
| 181 | # Default values for instance variables |
| 182 | __params = None |
| 183 | __locals = None |
| 184 | __frees = None |
| 185 | __globals = None |
| 186 | __nonlocals = None |
| 187 | |
| 188 | def __idents_matching(self, test_func): |
| 189 | return tuple(ident for ident in self.get_identifiers() |
| 190 | if test_func(self._table.symbols[ident])) |
| 191 | |
| 192 | def get_parameters(self): |
| 193 | """Return a tuple of parameters to the function. |
| 194 | """ |
| 195 | if self.__params is None: |
| 196 | self.__params = self.__idents_matching(lambda x:x & DEF_PARAM) |
| 197 | return self.__params |
| 198 | |
| 199 | def get_locals(self): |
| 200 | """Return a tuple of locals in the function. |
| 201 | """ |
| 202 | if self.__locals is None: |
| 203 | locs = (LOCAL, CELL) |
| 204 | test = lambda x: _get_scope(x) in locs |
| 205 | self.__locals = self.__idents_matching(test) |
| 206 | return self.__locals |
| 207 | |
| 208 | def get_globals(self): |
| 209 | """Return a tuple of globals in the function. |
| 210 | """ |
| 211 | if self.__globals is None: |
| 212 | glob = (GLOBAL_IMPLICIT, GLOBAL_EXPLICIT) |
| 213 | test = lambda x: _get_scope(x) in glob |
| 214 | self.__globals = self.__idents_matching(test) |
| 215 | return self.__globals |
| 216 | |
| 217 | def get_nonlocals(self): |
| 218 | """Return a tuple of nonlocals in the function. |
| 219 | """ |
| 220 | if self.__nonlocals is None: |
| 221 | self.__nonlocals = self.__idents_matching(lambda x:x & DEF_NONLOCAL) |
| 222 | return self.__nonlocals |
| 223 | |
| 224 | def get_frees(self): |
| 225 | """Return a tuple of free variables in the function. |
| 226 | """ |
| 227 | if self.__frees is None: |
| 228 | is_free = lambda x: _get_scope(x) == FREE |
| 229 | self.__frees = self.__idents_matching(is_free) |
| 230 | return self.__frees |
| 231 | |
| 232 | |
| 233 | class Class(SymbolTable): |