1.1 --- a/micropython/__init__.py Sat Nov 08 21:15:11 2008 +0100
1.2 +++ b/micropython/__init__.py Sat Nov 08 21:47:26 2008 +0100
1.3 @@ -35,6 +35,7 @@
1.4
1.5 from micropython.common import *
1.6 import micropython.ast
1.7 +import micropython.data
1.8 import micropython.opt
1.9 import micropython.inspect
1.10 import micropython.table
1.11 @@ -53,6 +54,14 @@
1.12
1.13 supported_optimisations = micropython.opt.Optimiser.supported_optimisations
1.14
1.15 + predefined_constants = {
1.16 + "None" : None,
1.17 + "True" : True,
1.18 + "False" : False,
1.19 + "Ellipsis" : Ellipsis,
1.20 + "NotImplemented" : NotImplemented
1.21 + }
1.22 +
1.23 def __init__(self, path=None, optimisations=None, verbose=0):
1.24
1.25 """
1.26 @@ -83,12 +92,41 @@
1.27
1.28 self.constant_values = {}
1.29 self.constant_list = None # cache for constants
1.30 + self.init_predefined_constants()
1.31
1.32 # Main program information.
1.33
1.34 self.code = None
1.35 self.code_location = None
1.36
1.37 + def init_predefined_constants(self):
1.38 +
1.39 + "Ensure the predefined constants."
1.40 +
1.41 + for name, value in self.predefined_constants.items():
1.42 + self.make_constant(value)
1.43 +
1.44 + def get_predefined_constant(self, name):
1.45 +
1.46 + "Return the predefined constant for the given 'name'."
1.47 +
1.48 + return self.make_constant(self.predefined_constants[name])
1.49 +
1.50 + def get_constant(self, value):
1.51 +
1.52 + "Return a constant for the given 'value'."
1.53 +
1.54 + return self.constant_values[value]
1.55 +
1.56 + def make_constant(self, value):
1.57 +
1.58 + "Make and return a constant for the given 'value'."
1.59 +
1.60 + if not self.constant_values.has_key(value):
1.61 + const = micropython.data.Const(value)
1.62 + self.constant_values[value] = const
1.63 + return self.constant_values[value]
1.64 +
1.65 def constants(self):
1.66
1.67 "Return a list of constants."