1.1 --- a/lib/builtins.py Mon Mar 31 00:54:03 2008 +0200
1.2 +++ b/lib/builtins.py Sun Apr 06 02:53:43 2008 +0200
1.3 @@ -24,24 +24,48 @@
1.4 def __bool__(self): pass
1.5 def __iadd__(self, other): pass
1.6
1.7 -class bool:
1.8 +class basestring(object):
1.9 + def __init__(self, x=None): pass
1.10 + def __getitem__(self, index): pass
1.11 + def __getslice__(self, start, end=None): pass
1.12 + def __iadd__(self, other): pass
1.13 + def __add__(self, other): pass
1.14 + def __radd__(self, other): pass
1.15 + def __mul__(self, other): pass
1.16 + def __radd__(self, other): pass
1.17 + def __mod__(self, other): pass
1.18 + def __lt__(self, other): pass
1.19 + def __gt__(self, other): pass
1.20 + def __le__(self, other): pass
1.21 + def __ge__(self, other): pass
1.22 + def __eq__(self, other): pass
1.23 + def __ne__(self, other): pass
1.24 + def __len__(self): pass
1.25 + def __str__(self): pass
1.26 + def __bool__(self): pass
1.27 + def join(self, l): pass
1.28 +
1.29 +class bool(object):
1.30 def __bool__(self): pass
1.31 def __str__(self): pass
1.32
1.33 -class buffer:
1.34 +class buffer(object):
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 +class complex(object):
1.41 + def __init__(self, real, imag=None): pass
1.42 +
1.43 +class dict(object):
1.44 def __init__(self, *args): pass
1.45 def __setitem__(self, key, value): pass
1.46 def __getitem__(self, key): pass
1.47
1.48 -class file:
1.49 +class file(object):
1.50 def write(self, s): pass
1.51
1.52 -class float:
1.53 +class float(object):
1.54 def __init__(self, number_or_string=None): pass
1.55 def __iadd__(self, other): pass
1.56 def __isub__(self, other): pass
1.57 @@ -69,7 +93,10 @@
1.58 def __str__(self): pass
1.59 def __bool__(self): pass
1.60
1.61 -class int:
1.62 +class frozenset(object):
1.63 + def __init__(self, iterable): pass
1.64 +
1.65 +class int(object):
1.66 def __init__(self, number_or_string=None): pass
1.67 def __iadd__(self, other): pass
1.68 def __isub__(self, other): pass
1.69 @@ -102,7 +129,7 @@
1.70 def __str__(self): pass
1.71 def __bool__(self): pass
1.72
1.73 -class list:
1.74 +class list(object):
1.75 def __init__(self, args=()): pass
1.76 def __getitem__(self, index): pass
1.77 def __setitem__(self, index, value): pass
1.78 @@ -116,7 +143,7 @@
1.79 def __iter__(self): pass
1.80 def __bool__(self): pass
1.81
1.82 -class long:
1.83 +class long(object):
1.84 def __init__(self, number_or_string=None): pass
1.85 def __iadd__(self, other): pass
1.86 def __isub__(self, other): pass
1.87 @@ -147,37 +174,19 @@
1.88 def __str__(self): pass
1.89 def __bool__(self): pass
1.90
1.91 -class none:
1.92 - def __bool__(self): pass
1.93 - def __str__(self): pass
1.94 +class set(object):
1.95 + def __init__(self, iterable): pass
1.96
1.97 -NoneType = none
1.98 -
1.99 -class slice:
1.100 +class slice(object):
1.101 def __init__(self, start_or_end, end=None, step=None): pass
1.102
1.103 -class str:
1.104 - def __init__(self, x=None): pass
1.105 - def __getitem__(self, index): pass
1.106 - def __getslice__(self, start, end=None): pass
1.107 - def __iadd__(self, other): pass
1.108 - def __add__(self, other): pass
1.109 - def __radd__(self, other): pass
1.110 - def __mul__(self, other): pass
1.111 - def __radd__(self, other): pass
1.112 - def __mod__(self, other): pass
1.113 - def __lt__(self, other): pass
1.114 - def __gt__(self, other): pass
1.115 - def __le__(self, other): pass
1.116 - def __ge__(self, other): pass
1.117 - def __eq__(self, other): pass
1.118 - def __ne__(self, other): pass
1.119 - def __len__(self): pass
1.120 - def __str__(self): pass
1.121 - def __bool__(self): pass
1.122 - def join(self, l): pass
1.123 +class str(basestring):
1.124 + pass
1.125
1.126 -class tuple:
1.127 +class type(object):
1.128 + pass
1.129 +
1.130 +class tuple(object):
1.131 def __init__(self, args): pass
1.132 def __getitem__(self, index): pass
1.133 def __getslice__(self, start, end=None): pass
1.134 @@ -187,48 +196,141 @@
1.135 def __iter__(self): pass
1.136 def __bool__(self): pass
1.137
1.138 -class xrange:
1.139 +class unicode(basestring):
1.140 + pass
1.141 +
1.142 +class xrange(object):
1.143 def __init__(self, start_or_end, end=None, step=1): pass
1.144 def __iter__(self): pass
1.145 def next(self): pass
1.146
1.147 -class Exception:
1.148 +# Exceptions and warnings.
1.149 +
1.150 +class BaseException(object):
1.151 def __init__(self, *args): pass
1.152
1.153 -class AssertionError(Exception):
1.154 - pass
1.155 -
1.156 -class AttributeError(Exception):
1.157 - pass
1.158 -
1.159 -class IndexError(Exception):
1.160 - pass
1.161 -
1.162 -class StopIteration(Exception):
1.163 - pass
1.164 +class Exception(BaseException): pass
1.165 +class Warning(object): pass
1.166
1.167 -class TypeError(Exception):
1.168 - pass
1.169 -
1.170 -class NotImplementedType:
1.171 - pass
1.172 +class ArithmeticError(Exception): pass
1.173 +class AssertionError(Exception): pass
1.174 +class AttributeError(Exception): pass
1.175 +class DeprecationWarning(Exception): pass
1.176 +class EOFError(Exception): pass
1.177 +class EnvironmentError(Exception): pass
1.178 +class FloatingPointError(Exception): pass
1.179 +class FutureWarning(Warning): pass
1.180 +class GeneratorExit(Exception): pass
1.181 +class IndexError(Exception): pass
1.182 +class IOError(Exception): pass
1.183 +class ImportError(Exception): pass
1.184 +class ImportWarning(Warning): pass
1.185 +class IndentationError(Exception): pass
1.186 +class IndexError(Exception): pass
1.187 +class KeyError(Exception): pass
1.188 +class KeyboardInterrupt(Exception): pass
1.189 +class LookupError(Exception): pass
1.190 +class MemoryError(Exception): pass
1.191 +class NameError(Exception): pass
1.192 +class NotImplementedError(Exception): pass
1.193 +class OSError(Exception): pass
1.194 +class OverflowError(Exception): pass
1.195 +class PendingDeprecationWarning(Warning): pass
1.196 +class ReferenceError(Exception): pass
1.197 +class RuntimeError(Exception): pass
1.198 +class RuntimeWarning(Warning): pass
1.199 +class StandardError(Exception): pass
1.200 +class StopIteration(Exception): pass
1.201 +class SyntaxError(Exception): pass
1.202 +class SyntaxWarning(Warning): pass
1.203 +class SystemError(Exception): pass
1.204 +class SystemExit(Exception): pass
1.205 +class TabError(Exception): pass
1.206 +class TypeError(Exception): pass
1.207 +class UnboundLocalError(Exception): pass
1.208 +class UnicodeDecodeError(Exception): pass
1.209 +class UnicodeEncodeError(Exception): pass
1.210 +class UnicodeError(Exception): pass
1.211 +class UnicodeTranslateError(Exception): pass
1.212 +class UnicodeWarning(Warning): pass
1.213 +class UserWarning(Warning): pass
1.214 +class ValueError(Exception): pass
1.215 +class ZeroDivisionError(Exception): pass
1.216
1.217 -# General functions.
1.218 +# Various types.
1.219 +
1.220 +class EllipsisType(object): pass
1.221
1.222 -def isinstance(obj, cls): pass
1.223 -def issubclass(cls1, cls2): pass
1.224 -def len(x): pass
1.225 -def max(*l): pass
1.226 -def range(start_or_end, end=None, step=None): pass
1.227 +class NoneType(object):
1.228 + def __bool__(self): pass
1.229 + def __str__(self): pass
1.230 +
1.231 +class NotImplementedType: pass
1.232
1.233 # Special values.
1.234
1.235 True = bool()
1.236 False = bool()
1.237 None = none()
1.238 -stdin = file()
1.239 -stdout = file()
1.240 -stderr = file()
1.241 +Ellipsis = ellipsis()
1.242 NotImplemented = NotImplementedType()
1.243
1.244 +# General functions.
1.245 +# NOTE: Some of these are actually provided by classes in CPython.
1.246 +# NOTE: We may refuse to support some of these in practice, such as...
1.247 +# NOTE: super, reload.
1.248 +
1.249 +def __import__(name, globals=None, locals=None, fromlist=None, level=-1): pass
1.250 +def abs(number): pass
1.251 +def all(iterable): pass
1.252 +def any(iterable): pass
1.253 +def callable(obj): pass
1.254 +def chr(i): pass
1.255 +def classmethod(function): pass
1.256 +def cmp(x, y): pass
1.257 +def compile(source, filename, mode, flags=None, dont_inherit=None): pass
1.258 +def delattr(obj, name): pass
1.259 +def dir(obj=None): pass
1.260 +def divmod(x, y): pass
1.261 +def enumerate(iterable): pass
1.262 +def eval(source, globals=None, locals=None): pass
1.263 +def execfile(filename, globals=None, locals=None): pass
1.264 +def filter(function, sequence): pass
1.265 +def getattr(obj, name, default=None): pass
1.266 +def globals(): pass
1.267 +def hasattr(obj, name): pass
1.268 +def hash(obj): pass
1.269 +def help(*args, **kw): pass
1.270 +def hex(number): pass
1.271 +def id(obj): pass
1.272 +def input(prompt=None): pass
1.273 +def isinstance(obj, cls_or_tuple): pass
1.274 +def issubclass(obj, cls_or_tuple): pass
1.275 +def iter(collection_or_callable, sentinel=None): pass
1.276 +def len(obj): pass
1.277 +def locals(): pass
1.278 +def map(function, *args): pass
1.279 +def max(*args, **kw): pass
1.280 +def min(*args, **kw): pass
1.281 +def oct(number): pass
1.282 +def open(name, mode=None, buffering=None): pass
1.283 +def ord(c): pass
1.284 +def pow(x, y, z=None): pass
1.285 +def property(fget=None, fset=None, fdel=None, doc=None): pass
1.286 +def range(start_or_end, end=None, step=None): pass
1.287 +def raw_input(prompt=None): pass
1.288 +def reduce(function, sequence, initial=None): pass
1.289 +def reload(module): pass
1.290 +def repr(obj): pass
1.291 +def reversed(sequence): pass
1.292 +def round(number, ndigits=None): pass
1.293 +def setattr(obj, name, value): pass
1.294 +def sorted(iterable, cmp=None, key=None, reverse=False): pass
1.295 +def staticmethod(function): pass
1.296 +def sum(sequence, start=0): pass
1.297 +def super(*args): pass
1.298 +def unichr(i): pass
1.299 +def vars(obj=None): pass
1.300 +def zip(*args): pass
1.301 +
1.302 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/lib/sys.py Sun Apr 06 02:53:43 2008 +0200
2.3 @@ -0,0 +1,26 @@
2.4 +#!/usr/bin/env python
2.5 +
2.6 +"""
2.7 +System functions and objects.
2.8 +
2.9 +Copyright (C) 2008 Paul Boddie <paul@boddie.org.uk>
2.10 +
2.11 +This program is free software; you can redistribute it and/or modify it under
2.12 +the terms of the GNU General Public License as published by the Free Software
2.13 +Foundation; either version 3 of the License, or (at your option) any later
2.14 +version.
2.15 +
2.16 +This program is distributed in the hope that it will be useful, but WITHOUT
2.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
2.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
2.19 +details.
2.20 +
2.21 +You should have received a copy of the GNU General Public License along with
2.22 +this program. If not, see <http://www.gnu.org/licenses/>.
2.23 +"""
2.24 +
2.25 +stdin = file()
2.26 +stdout = file()
2.27 +stderr = file()
2.28 +
2.29 +# vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/micropython/__init__.py Mon Mar 31 00:54:03 2008 +0200
3.2 +++ b/micropython/__init__.py Sun Apr 06 02:53:43 2008 +0200
3.3 @@ -50,6 +50,8 @@
3.4 class supports the generation of a program image.
3.5 """
3.6
3.7 + supported_optimisations = micropython.ast.Translation.supported_optimisations
3.8 +
3.9 def __init__(self, path=None, verbose=0):
3.10
3.11 """
4.1 --- a/micropython/ast.py Mon Mar 31 00:54:03 2008 +0200
4.2 +++ b/micropython/ast.py Sun Apr 06 02:53:43 2008 +0200
4.3 @@ -29,7 +29,7 @@
4.4 except NameError:
4.5 from sets import Set as set
4.6
4.7 -class TranslateError(ProcessingError): pass
4.8 +class TranslateError(NodeProcessingError): pass
4.9
4.10 class Label:
4.11
4.12 @@ -195,9 +195,9 @@
4.13 """
4.14
4.15 self.dispatch(node.expr)
4.16 - self._generateAttr(node.attrname, classes)
4.17 + self._generateAttr(node, node.attrname, classes)
4.18
4.19 - def _generateAttr(self, attrname, classes):
4.20 + def _generateAttr(self, node, attrname, classes):
4.21
4.22 """
4.23 Generate code for the access to 'attrname' using the given 'classes'.
4.24 @@ -222,21 +222,39 @@
4.25
4.26 target = last.attr.value
4.27 target_name = target.full_name()
4.28 - table_entry = self.objtable.table[target_name]
4.29 - pos = table_entry[attrname]
4.30 +
4.31 + try:
4.32 + table_entry = self.objtable.table[target_name]
4.33 + except KeyError:
4.34 + raise TranslateError(self.module.full_name(), node,
4.35 + "No object entry exists for target %r." % target_name)
4.36 +
4.37 + try:
4.38 + pos = table_entry[attrname]
4.39 + except KeyError:
4.40 + raise TranslateError(self.module.full_name(), node,
4.41 + "No attribute entry exists for name %r in target %r." % (attrname, target_name))
4.42 +
4.43 self.replace_op(AttrInstruction(pos))
4.44
4.45 # Where the last operation involves the special 'self' name, check to
4.46 # see if the attribute is acceptably positioned.
4.47
4.48 - elif isinstance(last, LoadName) and last.attr.name == "self" and not self.unit.is_relocated(attrname):
4.49 + elif self.unit.is_method() and isinstance(last, LoadName) and \
4.50 + last.attr.name == "self" and not self.unit.is_relocated(attrname):
4.51 +
4.52 attr = self.unit.parent.all_attributes()[attrname]
4.53 self.new_op(AttrInstruction(attr))
4.54
4.55 # Otherwise, perform a normal operation.
4.56
4.57 else:
4.58 - index = self.objtable.get_index(attrname)
4.59 + try:
4.60 + index = self.objtable.get_index(attrname)
4.61 + except self.objtable.TableError:
4.62 + raise TranslateError(self.module.full_name(), node,
4.63 + "No attribute entry exists for name %r." % attrname)
4.64 +
4.65 self.new_op(AttrIndexInstruction(index))
4.66
4.67 def _startCallFunc(self):
4.68 @@ -437,7 +455,10 @@
4.69
4.70 def _get_builtin(self, name, node):
4.71 if self.builtins is not None:
4.72 - return self.builtins[name]
4.73 + try:
4.74 + return self.builtins[name]
4.75 + except KeyError:
4.76 + raise TranslateError(self.module.full_name(), node, "No __builtins__ definition is available for name %r." % name)
4.77 else:
4.78 raise TranslateError(self.module.full_name(), node, "No __builtins__ module is available for name %r." % name)
4.79
4.80 @@ -534,7 +555,7 @@
4.81
4.82 self._startCallFunc()
4.83 self.new_op(LoadTemp(1))
4.84 - self._generateAttr(left_method, (LoadAttr, LoadAttrIndex))
4.85 + self._generateAttr(node, left_method, (LoadAttr, LoadAttrIndex))
4.86 self.new_op(LoadTemp(1)) # Explicit context as first argument.
4.87 self.new_op(LoadTemp(2))
4.88 self._endCallFunc()
4.89 @@ -548,7 +569,7 @@
4.90 self.set_label(right_label)
4.91 self._startCallFunc()
4.92 self.new_op(LoadTemp(2))
4.93 - self._generateAttr(right_method, (LoadAttr, LoadAttrIndex))
4.94 + self._generateAttr(node, right_method, (LoadAttr, LoadAttrIndex))
4.95 self.new_op(LoadTemp(2)) # Explicit context as first argument.
4.96 self.new_op(LoadTemp(1))
4.97 self._endCallFunc()
4.98 @@ -661,7 +682,7 @@
4.99
4.100 self._startCallFunc()
4.101 self.dispatch(node.list)
4.102 - self._generateAttr("__iter__", (LoadAttr, LoadAttrIndex))
4.103 + self._generateAttr(node, "__iter__", (LoadAttr, LoadAttrIndex))
4.104 self._generateCallFunc([], node)
4.105 self._endCallFunc()
4.106
4.107 @@ -675,7 +696,7 @@
4.108
4.109 self._startCallFunc()
4.110 self.new_op(Duplicate())
4.111 - self._generateAttr("next", (LoadAttr, LoadAttrIndex))
4.112 + self._generateAttr(node, "next", (LoadAttr, LoadAttrIndex))
4.113 self._generateCallFunc([], node)
4.114 self._endCallFunc()
4.115
5.1 --- a/micropython/common.py Mon Mar 31 00:54:03 2008 +0200
5.2 +++ b/micropython/common.py Sun Apr 06 02:53:43 2008 +0200
5.3 @@ -23,6 +23,12 @@
5.4
5.5 "A processing error."
5.6
5.7 + pass
5.8 +
5.9 +class NodeProcessingError(ProcessingError):
5.10 +
5.11 + "A processing error associated with a particular program node."
5.12 +
5.13 def __init__(self, unit_name, node, message):
5.14 self.unit_name = unit_name
5.15 self.node = node
6.1 --- a/micropython/inspect.py Mon Mar 31 00:54:03 2008 +0200
6.2 +++ b/micropython/inspect.py Sun Apr 06 02:53:43 2008 +0200
6.3 @@ -312,6 +312,10 @@
6.4 self.allattr = None # cache for all_attributes
6.5 self.allattr_names = None # from allattr
6.6
6.7 + # Add this class to its attributes.
6.8 +
6.9 + self.set("__class__", self)
6.10 +
6.11 # Image generation details.
6.12
6.13 self.location = None
6.14 @@ -653,6 +657,12 @@
6.15 del self.localnames[name]
6.16 return self.localnames
6.17
6.18 + def is_method(self):
6.19 +
6.20 + "Return whether this function is a method."
6.21 +
6.22 + return isinstance(self.parent, Class)
6.23 +
6.24 def is_relocated(self, name):
6.25
6.26 """
6.27 @@ -669,12 +679,14 @@
6.28
6.29 "Make sure all attributes are fully defined."
6.30
6.31 + i = None
6.32 for i, name in enumerate(self.argnames):
6.33 self[name].position = i
6.34
6.35 - j = i
6.36 - for i, attr in enumerate(self.locals().values()):
6.37 - attr.position = i + j
6.38 + if i is not None:
6.39 + j = i
6.40 + for i, attr in enumerate(self.locals().values()):
6.41 + attr.position = i + j
6.42
6.43 def function_from_method(self):
6.44
7.1 --- a/micropython/table.py Mon Mar 31 00:54:03 2008 +0200
7.2 +++ b/micropython/table.py Sun Apr 06 02:53:43 2008 +0200
7.3 @@ -3,7 +3,7 @@
7.4 """
7.5 Preparation of run-time attribute lookup tables.
7.6
7.7 -Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk>
7.8 +Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk>
7.9
7.10 This program is free software; you can redistribute it and/or modify it under
7.11 the terms of the GNU General Public License as published by the Free Software
7.12 @@ -19,16 +19,20 @@
7.13 this program. If not, see <http://www.gnu.org/licenses/>.
7.14 """
7.15
7.16 +from micropython.common import *
7.17 try:
7.18 set
7.19 except NameError:
7.20 from sets import Set as set
7.21
7.22 +class TableError(ProcessingError): pass
7.23 +
7.24 class List:
7.25
7.26 """
7.27 A displaced list containing attribute details and an index mapping names to
7.28 - offsets in the list.
7.29 + offsets in the list. This is the optimised form of the table initially
7.30 + constructed to record object attributes.
7.31 """
7.32
7.33 def __init__(self, names):
7.34 @@ -135,6 +139,8 @@
7.35
7.36 "A lookup table."
7.37
7.38 + TableError = TableError
7.39 +
7.40 def __init__(self):
7.41 self.attributes = set()
7.42 self.table = {}
7.43 @@ -171,13 +177,19 @@
7.44
7.45 "Return the code of the given 'name'."
7.46
7.47 - return self.object_names().index(name)
7.48 + try:
7.49 + return self.object_names().index(name)
7.50 + except ValueError:
7.51 + raise TableError, "Name %r is not registered as an object in the table." % name
7.52
7.53 def get_index(self, name):
7.54
7.55 "Return the index of the given 'name'."
7.56
7.57 - return self.attribute_names().index(name)
7.58 + try:
7.59 + return self.attribute_names().index(name)
7.60 + except ValueError:
7.61 + raise TableError, "Name %r is not registered as an attribute in the table." % name
7.62
7.63 def as_matrix(self):
7.64
8.1 --- a/test.py Mon Mar 31 00:54:03 2008 +0200
8.2 +++ b/test.py Sun Apr 06 02:53:43 2008 +0200
8.3 @@ -13,12 +13,17 @@
8.4 print name, attr
8.5
8.6 if __name__ == "__main__":
8.7 - requested_optimisations = []
8.8 - for arg in sys.argv[2:]:
8.9 - if arg.startswith("-o"):
8.10 - requested_optimisations.append(arg[2:])
8.11 + args = sys.argv[2:]
8.12 + i = micropython.Importer(sys.path, "-v" in args)
8.13
8.14 - i = micropython.Importer(sys.path, "-v" in sys.argv)
8.15 + if "-omax" in args:
8.16 + requested_optimisations = i.supported_optimisations
8.17 + else:
8.18 + requested_optimisations = []
8.19 + for arg in args:
8.20 + if arg.startswith("-o"):
8.21 + requested_optimisations.append(arg[2:])
8.22 +
8.23 try:
8.24 builtins = i.load_from_file("lib/builtins.py", "__builtins__")
8.25 if len(sys.argv) < 2: