1.1 --- a/micropython/ast.py Sat Feb 09 19:37:38 2008 +0100
1.2 +++ b/micropython/ast.py Sun Feb 10 21:20:38 2008 +0100
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Translate the AST of a Python program into a more interpretable representation.
1.6
1.7 -Copyright (C) 2007 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 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 @@ -21,6 +21,7 @@
1.13
1.14 import micropython.inspect
1.15 from micropython.rsvp import *
1.16 +from micropython.common import *
1.17 import compiler.ast
1.18 from compiler.visitor import ASTVisitor
1.19 try:
1.20 @@ -28,11 +29,7 @@
1.21 except NameError:
1.22 from sets import Set as set
1.23
1.24 -class TranslateError(Exception):
1.25 -
1.26 - "A translation error."
1.27 -
1.28 - pass
1.29 +class TranslateError(ProcessingError): pass
1.30
1.31 class Label:
1.32
1.33 @@ -128,7 +125,7 @@
1.34 # Visitor methods.
1.35
1.36 def default(self, node, *args):
1.37 - raise TranslateError, node.__class__
1.38 + raise TranslateError(self.module.full_name(), node, "Node class %r is not supported." % node.__class__)
1.39
1.40 def dispatch(self, node, *args):
1.41 return ASTVisitor.dispatch(self, node, *args)
1.42 @@ -148,14 +145,14 @@
1.43 elif isinstance(unit, micropython.inspect.Module):
1.44 self.new_op(AttrInstruction(unit.module_attributes()[name]))
1.45 else:
1.46 - raise TranslateError, "Program unit %r has no local %r" % (unit, name)
1.47 + raise TranslateError(self.module.full_name(), node, "Program unit %r has no local %r" % (unit, name))
1.48
1.49 elif scope == "global":
1.50 globals = self.module.module_attributes()
1.51 if globals.has_key(name):
1.52 self.new_op(AttrInstruction(globals[name]))
1.53 else:
1.54 - raise TranslateError, "Module %r has no attribute %r" % (self.module, name)
1.55 + raise TranslateError(self.module.full_name(), node, "Module %r has no attribute %r" % (self.module, name))
1.56
1.57 else:
1.58 builtins = micropython.inspect.builtins.module_attributes()
1.59 @@ -240,7 +237,26 @@
1.60 # Optimise where the target is known now.
1.61
1.62 if target is not None:
1.63 - pos = self.paramtable.table[target.full_name()][arg.name]
1.64 +
1.65 + # Find the parameter table entry for the target.
1.66 +
1.67 + target_name = target.full_name()
1.68 +
1.69 + # Look for a callable with the precise target name.
1.70 +
1.71 + try:
1.72 + table_entry = self.paramtable.table[target_name]
1.73 +
1.74 + # Where no callable is present, check to see if it is a
1.75 + # class name and find the initialiser instead.
1.76 +
1.77 + except KeyError:
1.78 + if self.objtable.table.has_key(target_name):
1.79 + table_entry = self.paramtable.table[target_name + ".__init__"]
1.80 + else:
1.81 + raise
1.82 +
1.83 + pos = table_entry[arg.name]
1.84 self.new_op(StoreFrame(pos))
1.85
1.86 # Otherwise, generate the code needed to obtain the details of
1.87 @@ -251,7 +267,10 @@
1.88 # Combine the target details with the name to get the location.
1.89 # See the access method on the List class.
1.90
1.91 - paramindex = self.paramtable.get_index(arg.name)
1.92 + try:
1.93 + paramindex = self.paramtable.get_index(arg.name)
1.94 + except ValueError:
1.95 + raise TranslateError(self.module.full_name(), node, "No parameter definition exists for %r." % arg.name)
1.96
1.97 self.new_op(StoreFrameIndex(paramindex))
1.98
1.99 @@ -271,7 +290,7 @@
1.100
1.101 def visitClass(self, node):
1.102 unit = self.unit
1.103 - self.unit = node.cls
1.104 + self.unit = node.unit
1.105 self.dispatch(node.code)
1.106 self.unit = unit
1.107
1.108 @@ -319,8 +338,8 @@
1.109
1.110 # Only store the name when visiting this node from outside.
1.111
1.112 - if self.unit is self.module:
1.113 - self.new_op(LoadConst(node.function))
1.114 + if self.unit is not node.unit:
1.115 + self.new_op(LoadConst(node.unit))
1.116 self._visitName(node, (StoreName, StoreAttr))
1.117
1.118 # Visiting of the code occurs when get_code is invoked on this node.