1.1 --- a/micropython/__init__.py Sat Feb 09 19:37:38 2008 +0100
1.2 +++ b/micropython/__init__.py Sun Feb 10 21:20:38 2008 +0100
1.3 @@ -5,7 +5,7 @@
1.4 from the simplify package but has had various details related to that package
1.5 removed.
1.6
1.7 -Copyright (C) 2006, 2007 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2006, 2007, 2008 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This program is free software; you can redistribute it and/or modify it under
1.11 the terms of the GNU General Public License as published by the Free Software
1.12 @@ -33,6 +33,7 @@
1.13 functionality of the micropython package may be accessed.
1.14 """
1.15
1.16 +from micropython.common import *
1.17 import micropython.ast
1.18 import micropython.inspect
1.19 import micropython.table
1.20 @@ -42,11 +43,12 @@
1.21 except NameError:
1.22 from sets import Set as set
1.23
1.24 -InspectError = micropython.inspect.InspectError
1.25 -
1.26 class Importer:
1.27
1.28 - "An import machine, searching for and loading modules."
1.29 + """
1.30 + An import machine, searching for and loading modules. In addition, this
1.31 + class supports the generation of a program image.
1.32 + """
1.33
1.34 def __init__(self, path=None, verbose=0):
1.35
2.1 --- a/micropython/ast.py Sat Feb 09 19:37:38 2008 +0100
2.2 +++ b/micropython/ast.py Sun Feb 10 21:20:38 2008 +0100
2.3 @@ -3,7 +3,7 @@
2.4 """
2.5 Translate the AST of a Python program into a more interpretable representation.
2.6
2.7 -Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk>
2.8 +Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk>
2.9
2.10 This program is free software; you can redistribute it and/or modify it under
2.11 the terms of the GNU General Public License as published by the Free Software
2.12 @@ -21,6 +21,7 @@
2.13
2.14 import micropython.inspect
2.15 from micropython.rsvp import *
2.16 +from micropython.common import *
2.17 import compiler.ast
2.18 from compiler.visitor import ASTVisitor
2.19 try:
2.20 @@ -28,11 +29,7 @@
2.21 except NameError:
2.22 from sets import Set as set
2.23
2.24 -class TranslateError(Exception):
2.25 -
2.26 - "A translation error."
2.27 -
2.28 - pass
2.29 +class TranslateError(ProcessingError): pass
2.30
2.31 class Label:
2.32
2.33 @@ -128,7 +125,7 @@
2.34 # Visitor methods.
2.35
2.36 def default(self, node, *args):
2.37 - raise TranslateError, node.__class__
2.38 + raise TranslateError(self.module.full_name(), node, "Node class %r is not supported." % node.__class__)
2.39
2.40 def dispatch(self, node, *args):
2.41 return ASTVisitor.dispatch(self, node, *args)
2.42 @@ -148,14 +145,14 @@
2.43 elif isinstance(unit, micropython.inspect.Module):
2.44 self.new_op(AttrInstruction(unit.module_attributes()[name]))
2.45 else:
2.46 - raise TranslateError, "Program unit %r has no local %r" % (unit, name)
2.47 + raise TranslateError(self.module.full_name(), node, "Program unit %r has no local %r" % (unit, name))
2.48
2.49 elif scope == "global":
2.50 globals = self.module.module_attributes()
2.51 if globals.has_key(name):
2.52 self.new_op(AttrInstruction(globals[name]))
2.53 else:
2.54 - raise TranslateError, "Module %r has no attribute %r" % (self.module, name)
2.55 + raise TranslateError(self.module.full_name(), node, "Module %r has no attribute %r" % (self.module, name))
2.56
2.57 else:
2.58 builtins = micropython.inspect.builtins.module_attributes()
2.59 @@ -240,7 +237,26 @@
2.60 # Optimise where the target is known now.
2.61
2.62 if target is not None:
2.63 - pos = self.paramtable.table[target.full_name()][arg.name]
2.64 +
2.65 + # Find the parameter table entry for the target.
2.66 +
2.67 + target_name = target.full_name()
2.68 +
2.69 + # Look for a callable with the precise target name.
2.70 +
2.71 + try:
2.72 + table_entry = self.paramtable.table[target_name]
2.73 +
2.74 + # Where no callable is present, check to see if it is a
2.75 + # class name and find the initialiser instead.
2.76 +
2.77 + except KeyError:
2.78 + if self.objtable.table.has_key(target_name):
2.79 + table_entry = self.paramtable.table[target_name + ".__init__"]
2.80 + else:
2.81 + raise
2.82 +
2.83 + pos = table_entry[arg.name]
2.84 self.new_op(StoreFrame(pos))
2.85
2.86 # Otherwise, generate the code needed to obtain the details of
2.87 @@ -251,7 +267,10 @@
2.88 # Combine the target details with the name to get the location.
2.89 # See the access method on the List class.
2.90
2.91 - paramindex = self.paramtable.get_index(arg.name)
2.92 + try:
2.93 + paramindex = self.paramtable.get_index(arg.name)
2.94 + except ValueError:
2.95 + raise TranslateError(self.module.full_name(), node, "No parameter definition exists for %r." % arg.name)
2.96
2.97 self.new_op(StoreFrameIndex(paramindex))
2.98
2.99 @@ -271,7 +290,7 @@
2.100
2.101 def visitClass(self, node):
2.102 unit = self.unit
2.103 - self.unit = node.cls
2.104 + self.unit = node.unit
2.105 self.dispatch(node.code)
2.106 self.unit = unit
2.107
2.108 @@ -319,8 +338,8 @@
2.109
2.110 # Only store the name when visiting this node from outside.
2.111
2.112 - if self.unit is self.module:
2.113 - self.new_op(LoadConst(node.function))
2.114 + if self.unit is not node.unit:
2.115 + self.new_op(LoadConst(node.unit))
2.116 self._visitName(node, (StoreName, StoreAttr))
2.117
2.118 # Visiting of the code occurs when get_code is invoked on this node.
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/micropython/common.py Sun Feb 10 21:20:38 2008 +0100
3.3 @@ -0,0 +1,37 @@
3.4 +#!/usr/bin/env python
3.5 +
3.6 +"""
3.7 +Common classes.
3.8 +
3.9 +Copyright (C) 2007, 2008 Paul Boddie <paul@boddie.org.uk>
3.10 +
3.11 +This program is free software; you can redistribute it and/or modify it under
3.12 +the terms of the GNU General Public License as published by the Free Software
3.13 +Foundation; either version 3 of the License, or (at your option) any later
3.14 +version.
3.15 +
3.16 +This program is distributed in the hope that it will be useful, but WITHOUT
3.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
3.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
3.19 +details.
3.20 +
3.21 +You should have received a copy of the GNU General Public License along with
3.22 +this program. If not, see <http://www.gnu.org/licenses/>.
3.23 +"""
3.24 +
3.25 +class ProcessingError(Exception):
3.26 +
3.27 + "A processing error."
3.28 +
3.29 + def __init__(self, unit_name, node, message):
3.30 + self.unit_name = unit_name
3.31 + self.node = node
3.32 + self.message = message
3.33 +
3.34 + def __repr__(self):
3.35 + return "ProcessingError in %r at line %d: %s" % (self.unit_name, self.node.lineno, self.message)
3.36 +
3.37 + def __str__(self):
3.38 + return repr(self)
3.39 +
3.40 +# vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/micropython/inspect.py Sat Feb 09 19:37:38 2008 +0100
4.2 +++ b/micropython/inspect.py Sun Feb 10 21:20:38 2008 +0100
4.3 @@ -45,6 +45,7 @@
4.4 each such object is defined.
4.5 """
4.6
4.7 +from micropython.common import *
4.8 import compiler.ast
4.9 from compiler.visitor import ASTVisitor
4.10 try:
4.11 @@ -52,13 +53,7 @@
4.12 except NameError:
4.13 from sets import Set as set
4.14
4.15 -class InspectError(Exception):
4.16 -
4.17 - "An inspection error."
4.18 -
4.19 - def __init__(self, node, message):
4.20 - self.node = node
4.21 - self.message = message
4.22 +class InspectError(ProcessingError): pass
4.23
4.24 class AtLeast:
4.25
4.26 @@ -166,7 +161,7 @@
4.27 if not self.namespace.has_key(name):
4.28 self.globals.add(name)
4.29 else:
4.30 - raise InspectError, "Name %r is global and local in %r" % (name, self)
4.31 + raise InspectError(self.full_name(), self.node, "Name %r is global and local in %r" % (name, self))
4.32
4.33 def get_assignments(self, name):
4.34 if self.assignments.has_key(name):
4.35 @@ -425,7 +420,7 @@
4.36 if name not in self.argnames and not self.has_key(name):
4.37 self.globals.add(name)
4.38 else:
4.39 - raise InspectError, "Name %r is global and local in %r" % (name, self)
4.40 + raise InspectError(self.full_name(), self.node, "Name %r is global and local in %r" % (name, self))
4.41
4.42 def parameters(self):
4.43
4.44 @@ -624,7 +619,7 @@
4.45 # Visitor methods.
4.46
4.47 def default(self, node, *args):
4.48 - raise InspectError, node.__class__
4.49 + raise InspectError(self.full_name(), node, "Node class %r is not supported." % node.__class__)
4.50
4.51 def dispatch(self, node, *args):
4.52 return ASTVisitor.dispatch(self, node, *args)
4.53 @@ -693,15 +688,17 @@
4.54 expr = self.dispatch(base)
4.55 if isinstance(expr, Attr):
4.56 if expr.assignments != 1:
4.57 - raise InspectError(node, "Base class %r for %r in %r is not constant." % (base, cls, self))
4.58 + raise InspectError(self.full_name(), node,
4.59 + "Base class %r for %r in %r is not constant." % (base, cls, self))
4.60 else:
4.61 cls.add_base(expr.value)
4.62 else: # if expr is None:
4.63 - raise InspectError(node, "Base class %r for %r in %r is not found: it may be hidden in some way." % (base, cls, self))
4.64 + raise InspectError(self.full_name(), node,
4.65 + "Base class %r for %r in %r is not found: it may be hidden in some way." % (base, cls, self))
4.66
4.67 # Make a back reference from the node for code generation.
4.68
4.69 - node.cls = cls
4.70 + node.unit = cls
4.71
4.72 # Make an entry for the class.
4.73
4.74 @@ -745,7 +742,8 @@
4.75
4.76 def visitFrom(self, node):
4.77 if self.importer is None:
4.78 - raise InspectError(node, "Please use the micropython.Importer class for code which uses the 'from' statement.")
4.79 + raise InspectError(self.full_name(), node,
4.80 + "Please use the micropython.Importer class for code which uses the 'from' statement.")
4.81
4.82 module = self.importer.load(node.modname, 1)
4.83
4.84 @@ -787,7 +785,7 @@
4.85
4.86 # Make a back reference from the node for code generation.
4.87
4.88 - node.function = function
4.89 + node.unit = function
4.90
4.91 self.namespaces.append(function)
4.92
4.93 @@ -843,7 +841,8 @@
4.94
4.95 def visitImport(self, node):
4.96 if self.importer is None:
4.97 - raise InspectError(node, "Please use the micropython.Importer class for code which uses the 'import' statement.")
4.98 + raise InspectError(self.full_name(), node,
4.99 + "Please use the micropython.Importer class for code which uses the 'import' statement.")
4.100
4.101 for name, alias in node.names:
4.102 if alias is not None:
5.1 --- a/test.py Sat Feb 09 19:37:38 2008 +0100
5.2 +++ b/test.py Sun Feb 10 21:20:38 2008 +0100
5.3 @@ -14,9 +14,8 @@
5.4 #m = i.load_from_file("micropython/__init__.py")
5.5 else:
5.6 m = i.load_from_file(sys.argv[1])
5.7 -except micropython.InspectError, exc:
5.8 - print "At line", exc.node.lineno, "the following error occurred:"
5.9 - print exc.message
5.10 +except micropython.ProcessingError, exc:
5.11 + print repr(exc)
5.12 else:
5.13 i.vacuum()
5.14 ot = i.get_object_table()