1.1 --- a/simplified.py Tue Jul 25 23:04:14 2006 +0200
1.2 +++ b/simplified.py Fri Jul 28 00:20:57 2006 +0200
1.3 @@ -190,4 +190,61 @@
1.4 class Instance(Structure): "An instance."
1.5 class Constant(Instance): "A constant."
1.6
1.7 +class Namespace:
1.8 +
1.9 + """
1.10 + A local namespace which may either relate to a genuine set of function
1.11 + locals or the initialisation of a structure.
1.12 + """
1.13 +
1.14 + def __init__(self, structure=None):
1.15 + self.structure = structure
1.16 + if structure is not None:
1.17 + self.local = "structure"
1.18 + else:
1.19 + self.local = "local"
1.20 + self.names = {}
1.21 + self.not_local = []
1.22 +
1.23 + def make_global(self, name):
1.24 + if name not in self.not_local:
1.25 + self.not_local.append(name)
1.26 +
1.27 + def find_for_store(self, name):
1.28 + if name not in self.not_local:
1.29 + return self.local
1.30 + else:
1.31 + return "global"
1.32 +
1.33 + def find_for_load(self, name):
1.34 + if name not in self.not_local and self.names.has_key(name):
1.35 + return self.local
1.36 + else:
1.37 + return "global"
1.38 +
1.39 + def store(self, name, types=None):
1.40 + if name not in self.not_local:
1.41 + self.names[name] = types
1.42 + else:
1.43 + raise KeyError, name
1.44 +
1.45 + def load(self, name):
1.46 + if name in self.not_local or not self.names.has_key(name):
1.47 + raise KeyError, name
1.48 + else:
1.49 + return self.names[name]
1.50 +
1.51 + def merge(self, namespace):
1.52 + self.merge_items(namespace.names.items())
1.53 +
1.54 + def merge_items(self, items):
1.55 + for name, types in items:
1.56 + if not self.names.has_key(name):
1.57 + self.names[name] = types
1.58 + else:
1.59 + existing = self.names[name]
1.60 + for type in types:
1.61 + if type not in existing:
1.62 + existing.append(type)
1.63 +
1.64 # vim: tabstop=4 expandtab shiftwidth=4