1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/lib/builtins.py Mon Mar 24 23:53:18 2008 +0100
1.3 @@ -0,0 +1,234 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Simple built-in classes and functions.
1.8 +
1.9 +Copyright (C) 2005, 2006, 2007, 2008 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +class object:
1.26 + def __init__(self): pass
1.27 + def __bool__(self): pass
1.28 + def __iadd__(self, other): pass
1.29 +
1.30 +class bool:
1.31 + def __bool__(self): pass
1.32 + def __str__(self): pass
1.33 +
1.34 +class buffer:
1.35 + def __init__(self, size): pass
1.36 + def append(self, s): pass
1.37 + def __str__(self): pass
1.38 +
1.39 +class dict:
1.40 + def __init__(self, *args): pass
1.41 + def __setitem__(self, key, value): pass
1.42 + def __getitem__(self, key): pass
1.43 +
1.44 +class file:
1.45 + def write(self, s): pass
1.46 +
1.47 +class float:
1.48 + def __init__(self, number_or_string=None): pass
1.49 + def __iadd__(self, other): pass
1.50 + def __isub__(self, other): pass
1.51 + def __add__(self, other): pass
1.52 + def __radd__(self, other): pass
1.53 + def __sub__(self, other): pass
1.54 + def __rsub__(self, other): pass
1.55 + def __mul__(self, other): pass
1.56 + def __rmul__(self, other): pass
1.57 + def __div__(self, other): pass
1.58 + def __rdiv__(self, other): pass
1.59 + def __floordiv__(self, other): pass
1.60 + def __rfloordiv__(self, other): pass
1.61 + def __mod__(self, other): pass
1.62 + def __pow__(self, other): pass
1.63 + def __rpow__(self, other): pass
1.64 + def __lt__(self, other): pass
1.65 + def __gt__(self, other): pass
1.66 + def __le__(self, other): pass
1.67 + def __ge__(self, other): pass
1.68 + def __eq__(self, other): pass
1.69 + def __ne__(self, other): pass
1.70 + def __neg__(self): pass
1.71 + def __pos__(self): pass
1.72 + def __str__(self): pass
1.73 + def __bool__(self): pass
1.74 +
1.75 +class int:
1.76 + def __init__(self, number_or_string=None): pass
1.77 + def __iadd__(self, other): pass
1.78 + def __isub__(self, other): pass
1.79 + def __add__(self, other): pass
1.80 + def __radd__(self, other): pass
1.81 + def __sub__(self, other): pass
1.82 + def __rsub__(self, other): pass
1.83 + def __mul__(self, other): pass
1.84 + def __rmul__(self, other): pass
1.85 + def __div__(self, other): pass
1.86 + def __rdiv__(self, other): pass
1.87 + def __floordiv__(self, other): pass
1.88 + def __rfloordiv__(self, other): pass
1.89 + def __mod__(self, other): pass
1.90 + def __pow__(self, other): pass
1.91 + def __and__(self, other): pass
1.92 + def __rand__(self, other): pass
1.93 + def __or__(self, other): pass
1.94 + def __ror__(self, other): pass
1.95 + def __xor__(self, other): pass
1.96 + def __rxor__(self, other): pass
1.97 + def __lt__(self, other): pass
1.98 + def __gt__(self, other): pass
1.99 + def __le__(self, other): pass
1.100 + def __ge__(self, other): pass
1.101 + def __eq__(self, other): pass
1.102 + def __ne__(self, other): pass
1.103 + def __neg__(self): pass
1.104 + def __pos__(self): pass
1.105 + def __str__(self): pass
1.106 + def __bool__(self): pass
1.107 +
1.108 +class list:
1.109 + def __init__(self, args=()): pass
1.110 + def __getitem__(self, index): pass
1.111 + def __setitem__(self, index, value): pass
1.112 + def __getslice__(self, start, end=None): pass
1.113 + def __setslice__(self, start, end, slice): pass
1.114 + def append(self, value): pass
1.115 + def __len__(self): pass
1.116 + def __add__(self, other): pass
1.117 + def __iadd__(self, other): pass
1.118 + def __str__(self): pass
1.119 + def __iter__(self): pass
1.120 + def __bool__(self): pass
1.121 +
1.122 +class long:
1.123 + def __init__(self, number_or_string=None): pass
1.124 + def __iadd__(self, other): pass
1.125 + def __isub__(self, other): pass
1.126 + def __add__(self, other): pass
1.127 + def __radd__(self, other): pass
1.128 + def __sub__(self, other): pass
1.129 + def __rsub__(self, other): pass
1.130 + def __mul__(self, other): pass
1.131 + def __rmul__(self, other): pass
1.132 + def __div__(self, other): pass
1.133 + def __rdiv__(self, other): pass
1.134 + def __floordiv__(self, other): pass
1.135 + def __rfloordiv__(self, other): pass
1.136 + def __and__(self, other): pass
1.137 + def __rand__(self, other): pass
1.138 + def __or__(self, other): pass
1.139 + def __ror__(self, other): pass
1.140 + def __xor__(self, other): pass
1.141 + def __rxor__(self, other): pass
1.142 + def __lt__(self, other): pass
1.143 + def __gt__(self, other): pass
1.144 + def __le__(self, other): pass
1.145 + def __ge__(self, other): pass
1.146 + def __eq__(self, other): pass
1.147 + def __ne__(self, other): pass
1.148 + def __neg__(self): pass
1.149 + def __pos__(self): pass
1.150 + def __str__(self): pass
1.151 + def __bool__(self): pass
1.152 +
1.153 +class none:
1.154 + def __bool__(self): pass
1.155 + def __str__(self): pass
1.156 +
1.157 +NoneType = none
1.158 +
1.159 +class slice:
1.160 + def __init__(self, start_or_end, end=None, step=None): pass
1.161 +
1.162 +class str:
1.163 + def __init__(self, x=None): pass
1.164 + def __getitem__(self, index): pass
1.165 + def __getslice__(self, start, end=None): pass
1.166 + def __iadd__(self, other): pass
1.167 + def __add__(self, other): pass
1.168 + def __radd__(self, other): pass
1.169 + def __mul__(self, other): pass
1.170 + def __radd__(self, other): pass
1.171 + def __mod__(self, other): pass
1.172 + def __lt__(self, other): pass
1.173 + def __gt__(self, other): pass
1.174 + def __le__(self, other): pass
1.175 + def __ge__(self, other): pass
1.176 + def __eq__(self, other): pass
1.177 + def __ne__(self, other): pass
1.178 + def __len__(self): pass
1.179 + def __str__(self): pass
1.180 + def __bool__(self): pass
1.181 + def join(self, l): pass
1.182 +
1.183 +class tuple:
1.184 + def __init__(self, args): pass
1.185 + def __getitem__(self, index): pass
1.186 + def __getslice__(self, start, end=None): pass
1.187 + def __len__(self): pass
1.188 + def __add__(self, other): pass
1.189 + def __str__(self): pass
1.190 + def __iter__(self): pass
1.191 + def __bool__(self): pass
1.192 +
1.193 +class xrange:
1.194 + def __init__(self, start_or_end, end=None, step=1): pass
1.195 + def __iter__(self): pass
1.196 + def next(self): pass
1.197 +
1.198 +class Exception:
1.199 + def __init__(self, *args): pass
1.200 +
1.201 +class AssertionError(Exception):
1.202 + pass
1.203 +
1.204 +class AttributeError(Exception):
1.205 + pass
1.206 +
1.207 +class IndexError(Exception):
1.208 + pass
1.209 +
1.210 +class StopIteration(Exception):
1.211 + pass
1.212 +
1.213 +class TypeError(Exception):
1.214 + pass
1.215 +
1.216 +class NotImplementedType:
1.217 + pass
1.218 +
1.219 +# General functions.
1.220 +
1.221 +def isinstance(obj, cls): pass
1.222 +def issubclass(cls1, cls2): pass
1.223 +def len(x): pass
1.224 +def max(*l): pass
1.225 +def range(start_or_end, end=None, step=None): pass
1.226 +
1.227 +# Special values.
1.228 +
1.229 +True = bool()
1.230 +False = bool()
1.231 +None = none()
1.232 +stdin = file()
1.233 +stdout = file()
1.234 +stderr = file()
1.235 +NotImplemented = NotImplementedType()
1.236 +
1.237 +# vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/micropython/__init__.py Sat Mar 22 22:31:54 2008 +0100
2.2 +++ b/micropython/__init__.py Mon Mar 24 23:53:18 2008 +0100
2.3 @@ -62,7 +62,7 @@
2.4
2.5 self.path = path or [os.getcwd()]
2.6 self.verbose = verbose
2.7 - self.modules = {"__builtins__" : micropython.inspect.builtins}
2.8 + self.modules = {}
2.9 self.loading = set()
2.10
2.11 # Remember the tables once generated.
2.12 @@ -115,7 +115,7 @@
2.13 # Position the module in the image and make a translation.
2.14
2.15 module.location = pos
2.16 - trans = micropython.ast.Translation(module, objtable, paramtable)
2.17 + trans = micropython.ast.Translation(module, objtable, paramtable, self.modules.get("__builtins__"))
2.18
2.19 # Add header details.
2.20
2.21 @@ -222,12 +222,11 @@
2.22 t.add(obj.full_name(), obj.parameters())
2.23
2.24 # Classes are callable, too.
2.25 - # Copy details of the appropriate __init__ method to make an
2.26 - # entry for the class.
2.27 + # Take details of the appropriate __init__ method to make an
2.28 + # entry for an instantiation function for the class.
2.29
2.30 elif isinstance(obj, micropython.inspect.Class):
2.31 - init_method = objtable.table[obj.full_name()]["__init__"].value
2.32 - t.add(obj.full_name(), init_method.parameters())
2.33 + t.add(obj.full_name(), obj.get_instantiator().parameters())
2.34
2.35 return self.paramtable
2.36
3.1 --- a/micropython/ast.py Sat Mar 22 22:31:54 2008 +0100
3.2 +++ b/micropython/ast.py Mon Mar 24 23:53:18 2008 +0100
3.3 @@ -48,7 +48,7 @@
3.4
3.5 "A translated module."
3.6
3.7 - def __init__(self, module, objtable, paramtable):
3.8 + def __init__(self, module, objtable, paramtable, builtins=None):
3.9
3.10 """
3.11 Initialise the translation with an inspected 'module' and an attribute
3.12 @@ -60,6 +60,10 @@
3.13 self.module = module
3.14 self.objtable = objtable
3.15 self.paramtable = paramtable
3.16 + self.builtins = builtins
3.17 +
3.18 + # The current unit being translated.
3.19 +
3.20 self.unit = None
3.21
3.22 # Wiring within the code.
3.23 @@ -410,21 +414,23 @@
3.24 elif isinstance(unit, micropython.inspect.Module):
3.25 self.new_op(AttrInstruction(unit.module_attributes()[name]))
3.26 else:
3.27 - raise TranslateError(self.module.full_name(), node, "Program unit %r has no local %r" % (unit, name))
3.28 + raise TranslateError(self.module.full_name(), node, "Program unit %r has no local %r." % (unit, name))
3.29
3.30 elif scope == "global":
3.31 globals = self.module.module_attributes()
3.32 if globals.has_key(name):
3.33 self.new_op(AttrInstruction(globals[name]))
3.34 else:
3.35 - raise TranslateError(self.module.full_name(), node, "Module %r has no attribute %r" % (self.module, name))
3.36 + raise TranslateError(self.module.full_name(), node, "Module %r has no attribute %r." % (self.module, name))
3.37
3.38 else:
3.39 - self.new_op(AttrInstruction(self._get_builtin(name)))
3.40 + self.new_op(AttrInstruction(self._get_builtin(name, node)))
3.41
3.42 - def _get_builtin(self, name):
3.43 - builtins = micropython.inspect.builtins.module_attributes()
3.44 - return builtins[name]
3.45 + def _get_builtin(self, name, node):
3.46 + if self.builtins is not None:
3.47 + return self.builtins[name]
3.48 + else:
3.49 + raise TranslateError(self.module.full_name(), node, "No __builtins__ module is available for name %r." % name)
3.50
3.51 # Optimisation methods.
3.52
3.53 @@ -460,11 +466,9 @@
3.54 context = last.attr.parent
3.55
3.56 # Handle calls to classes.
3.57 - # NOTE: That the actual invocation target will be a __new__ method
3.58 - # NOTE: which calls the __init__ method, returning the new instance.
3.59
3.60 if isinstance(target, micropython.inspect.Class):
3.61 - target = self.objtable.table[target.full_name()]["__init__"].value
3.62 + target = target.get_instantiator()
3.63 context = micropython.inspect.Instance()
3.64
3.65 # A special context is chosen to avoid generating unnecessary
4.1 --- a/micropython/inspect.py Sat Mar 22 22:31:54 2008 +0100
4.2 +++ b/micropython/inspect.py Mon Mar 24 23:53:18 2008 +0100
4.3 @@ -316,6 +316,7 @@
4.4
4.5 self.location = None
4.6 self.code_location = None
4.7 + self.instantiator = None
4.8
4.9 def __repr__(self):
4.10 if self.location is not None:
4.11 @@ -330,6 +331,15 @@
4.12 self.finalise_class_attributes()
4.13 self.finalise_instance_attributes()
4.14
4.15 + def get_instantiator(self):
4.16 +
4.17 + "Return a function which can be used to instantiate the class."
4.18 +
4.19 + if self.instantiator is None:
4.20 + init_method = self.all_class_attributes()["__init__"].value
4.21 + self.instantiator = init_method.function_from_method()
4.22 + return self.instantiator
4.23 +
4.24 # Class-specific methods.
4.25
4.26 def add_base(self, base):
4.27 @@ -665,6 +675,12 @@
4.28 for i, attr in enumerate(self.locals().values()):
4.29 attr.position = i + j
4.30
4.31 + def function_from_method(self):
4.32 +
4.33 + "Make a function from a method."
4.34 +
4.35 + return Function(self.name, self.parent, self.argnames[1:], self.has_star, self.has_dstar, self.global_namespace, self.node)
4.36 +
4.37 class UnresolvedName(NamespaceDict):
4.38
4.39 "A module, class or function which was mentioned but could not be imported."
4.40 @@ -767,6 +783,7 @@
4.41 self.visitor = self
4.42
4.43 self.importer = importer
4.44 + self.builtins = self.importer.modules.get("__builtins__")
4.45 self.loaded = 0
4.46
4.47 # Current expression state.
4.48 @@ -926,7 +943,7 @@
4.49 # NOTE: Potentially dubious measure to permit __init__ availability.
4.50 # If no bases exist, adopt the 'object' class.
4.51
4.52 - if not node.bases:
4.53 + if not node.bases and not (self.name == "__builtins__" and node.name == "object") :
4.54 expr = self.dispatch(compiler.ast.Name("object"))
4.55 cls.add_base(expr.value)
4.56
4.57 @@ -1054,7 +1071,10 @@
4.58 return value.namespace.get(node.attrname)
4.59 elif isinstance(value, UnresolvedName):
4.60 return UnresolvedName(node.attrname, value.full_name(), self)
4.61 - return builtins.get(node.attrname)
4.62 + if self.builtins is not None:
4.63 + return self.builtins.get(node.attrname)
4.64 + else:
4.65 + return UnresolvedName(node.attrname, value.full_name(), self)
4.66
4.67 def visitGlobal(self, node):
4.68 if self.namespaces:
4.69 @@ -1121,8 +1141,8 @@
4.70 return self.namespaces[-1][name]
4.71 elif self.has_key(name):
4.72 return self[name]
4.73 - elif builtins.has_key(name):
4.74 - return builtins[name]
4.75 + elif self.builtins is not None and self.builtins.has_key(name):
4.76 + return self.builtins[name]
4.77 else:
4.78 return None
4.79
4.80 @@ -1191,71 +1211,4 @@
4.81
4.82 pass
4.83
4.84 -# Built-in types initialisation.
4.85 -
4.86 -class Builtins(Module):
4.87 -
4.88 - "The special built-in types module."
4.89 -
4.90 - def __init__(self):
4.91 - Module.__init__(self, "__builtins__")
4.92 - self.loaded = 1
4.93 - self.module = None
4.94 -
4.95 - for key in ['ArithmeticError', 'AssertionError', 'AttributeError',
4.96 - 'BaseException', 'DeprecationWarning', 'EOFError', 'Ellipsis',
4.97 - 'EnvironmentError', 'Exception', 'False', 'FloatingPointError',
4.98 - 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError',
4.99 - 'ImportWarning', 'IndentationError', 'IndexError', 'KeyError',
4.100 - 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'NameError',
4.101 - 'None', 'NotImplemented', 'NotImplementedError', 'OSError',
4.102 - 'OverflowError', 'PendingDeprecationWarning', 'ReferenceError',
4.103 - 'RuntimeError', 'RuntimeWarning', 'StandardError', 'StopIteration',
4.104 - 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit',
4.105 - 'TabError', 'True', 'TypeError', 'UnboundLocalError',
4.106 - 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError',
4.107 - 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning',
4.108 - 'ValueError', 'Warning', 'ZeroDivisionError',
4.109 - 'basestring', 'bool', 'buffer', 'complex', 'dict', 'file', 'float',
4.110 - 'frozenset', 'int', # 'list',
4.111 - 'long', 'object', 'set', 'slice', 'str',
4.112 - 'tuple', 'type', 'unicode', 'xrange']:
4.113 -
4.114 - cls = Class(key, self, self)
4.115 - cls.set("__init__", Function("__init__", cls, [], 0, 0, cls))
4.116 - self.store(key, cls)
4.117 -
4.118 - # NOTE: Temporary measure - provide detailed built-ins.
4.119 -
4.120 - cls = Class("list", self, self)
4.121 - cls.set("__init__", Function("__init__", cls, [], 0, 0, cls))
4.122 - cls.set("__iter__", Function("__iter__", cls, [], 0, 0, cls))
4.123 - self.store("list", cls)
4.124 -
4.125 - cls = Class("listiterator", self, self)
4.126 - cls.set("__init__", Function("__init__", cls, [], 0, 0, cls))
4.127 - cls.set("next", Function("next", cls, [], 0, 0, cls))
4.128 - self.store("listiterator", cls)
4.129 -
4.130 - # NOTE: Incomplete: some functions have more than one parameter.
4.131 -
4.132 - for key in ['__import__', 'abs', 'all', 'any', 'callable', 'chr',
4.133 - 'classmethod', 'cmp', 'compile', 'delattr', 'dir', 'divmod',
4.134 - 'enumerate', 'eval', 'execfile', 'filter', 'getattr', 'globals',
4.135 - 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'isinstance',
4.136 - 'issubclass', 'iter', 'len', 'locals', 'map', 'max', 'min', 'oct',
4.137 - 'open', 'ord', 'pow', 'property', 'range', 'raw_input', 'reduce',
4.138 - 'reload', 'repr', 'reversed', 'round', 'setattr', 'sorted',
4.139 - 'staticmethod', 'sum', 'super', 'unichr', 'vars', 'zip']:
4.140 - self[key] = Function(key, self, ['arg'], 0, 0, self)
4.141 -
4.142 - def store(self, name, obj):
4.143 - self.set(name, obj)
4.144 - self.all_objects.add(obj)
4.145 -
4.146 - def vacuum(self):
4.147 - pass
4.148 -
4.149 -builtins = Builtins()
4.150 -
4.151 # vim: tabstop=4 expandtab shiftwidth=4
5.1 --- a/test.py Sat Mar 22 22:31:54 2008 +0100
5.2 +++ b/test.py Mon Mar 24 23:53:18 2008 +0100
5.3 @@ -13,6 +13,7 @@
5.4
5.5 i = micropython.Importer(sys.path, "-v" in sys.argv)
5.6 try:
5.7 + builtins = i.load_from_file("lib/builtins.py", "__builtins__")
5.8 if len(sys.argv) < 2:
5.9 m = i.load("micropython")
5.10 #m = i.load_from_file("micropython/__init__.py")