def lookup_name(self, name): """ Determine the full name (including context) for a symbol name. - If the name begins with a context mark, it's in the context given by $Context. - Otherwise, if it contains a context mark, it's already fully specified. - Otherwise, it doesn't contain a context mark: try $Context, then each element of $ContextPath, taking the first existing symbol. - Otherwise, it's a new symbol in $Context. """ assert isinstance(name, six.string_types) # Bail out if the name we're being asked to look up is already # fully qualified. if fully_qualified_symbol_name(name): return name current_context = self.get_current_context() if '`' in name: if name.startswith('`'): return current_context + name.lstrip('`') return name with_context = current_context + name if not self.have_definition(with_context): for ctx in self.get_context_path(): n = ctx + name if self.have_definition(n): return n return with_context
def dynamic_scoping(func, vars, evaluation): original_definitions = {} for var_name, new_def in vars.items(): assert fully_qualified_symbol_name(var_name) original_definitions[ var_name] = evaluation.definitions.get_user_definition(var_name) evaluation.definitions.reset_user_definition(var_name) if new_def is not None: new_def = new_def.evaluate(evaluation) evaluation.definitions.set_ownvalue(var_name, new_def) try: result = func(evaluation) finally: for name, definition in original_definitions.items(): evaluation.definitions.add_user_definition(name, definition) return result
def dynamic_scoping(func, vars, evaluation: Evaluation): """ Changes temporarily the value of a set of symbols listed in vars, and evaluates func(evaluation) """ original_definitions = {} for var_name, new_def in vars.items(): assert fully_qualified_symbol_name(var_name) original_definitions[ var_name] = evaluation.definitions.get_user_definition(var_name) evaluation.definitions.reset_user_definition(var_name) if new_def is not None: new_def = new_def.evaluate(evaluation) evaluation.definitions.set_ownvalue(var_name, new_def) try: result = func(evaluation) finally: for name, definition in original_definitions.items(): evaluation.definitions.add_user_definition(name, definition) return result