1.1 --- a/micropython/trans.py Sat May 23 01:46:46 2009 +0200
1.2 +++ b/micropython/trans.py Sat May 23 02:46:04 2009 +0200
1.3 @@ -44,6 +44,14 @@
1.4
1.5 self.new_op(MakeObject(n + 1))
1.6
1.7 + def make_exception(self, name, node):
1.8 +
1.9 + "Make an exception of the given 'name' using 'node'."
1.10 +
1.11 + # NOTE: Reserving only one attribute.
1.12 +
1.13 + self.make_object(self.get_builtin_class(name, node), 1)
1.14 +
1.15 # Name-related methods.
1.16
1.17 def get_scope(self, name):
1.18 @@ -676,7 +684,7 @@
1.19 self.new_op(DropFrame())
1.20 self.new_op(LoadResult())
1.21
1.22 - self.load_builtin("TypeError", node)
1.23 + self.make_exception("TypeError", node)
1.24 self.new_op(StoreException())
1.25 self.new_op(RaiseException())
1.26
1.27 @@ -933,7 +941,7 @@
1.28 # Raise a TypeError.
1.29
1.30 self.set_block(type_error_block)
1.31 - self.load_builtin("TypeError", node)
1.32 + self.make_exception("TypeError", node)
1.33 self.new_op(StoreException())
1.34 self.new_op(RaiseException())
1.35
1.36 @@ -1018,7 +1026,7 @@
1.37 # Raise a TypeError.
1.38
1.39 self.set_block(type_error_block)
1.40 - self.load_builtin("TypeError", node)
1.41 + self.make_exception("TypeError", node)
1.42 self.new_op(StoreException())
1.43 self.new_op(RaiseException())
1.44
2.1 --- a/rsvp.py Sat May 23 01:46:46 2009 +0200
2.2 +++ b/rsvp.py Sat May 23 02:46:04 2009 +0200
2.3 @@ -121,22 +121,31 @@
2.4
2.5 # Constants.
2.6
2.7 - self.attr_error = objlist.access("__builtins__", "AttributeError").get_value().location
2.8 - self.type_error = objlist.access("__builtins__", "TypeError").get_value().location
2.9 - self.index_error = objlist.access("__builtins__", "IndexError").get_value().location
2.10 + cls = self._get_class("__builtins__", "AttributeError")
2.11 + self.attr_error = cls.location
2.12 + self.attr_error_instance = cls.instance_template_location
2.13 + cls = self._get_class("__builtins__", "TypeError")
2.14 + self.type_error = cls.location
2.15 + self.type_error_instance = cls.instance_template_location
2.16 + cls = self._get_class("__builtins__", "IndexError")
2.17 + self.index_error = cls.location
2.18 + self.index_error_instance = cls.instance_template_location
2.19
2.20 # Native class constants.
2.21
2.22 - cls = objlist.access("__builtins__", "int")
2.23 - self.int_class_location = cls and cls.get_value() and cls.get_value().location
2.24 - self.int_instance_location = cls and cls.get_value() and cls.get_value().instance_template_location
2.25 - cls = objlist.access("__builtins__", "list")
2.26 - self.list_instance_location = cls and cls.get_value() and cls.get_value().instance_template_location
2.27 + cls = self._get_class("__builtins__", "int")
2.28 + self.int_class = cls.location
2.29 + self.int_instance = cls.instance_template_location
2.30 + cls = self._get_class("__builtins__", "list")
2.31 + self.list_instance = cls.instance_template_location
2.32
2.33 # Debugging attributes.
2.34
2.35 self.breakpoints = set()
2.36
2.37 + def _get_class(self, module, name):
2.38 + return self._objlist.access(module, name).get_value()
2.39 +
2.40 # Debugging methods.
2.41
2.42 def dump(self):
2.43 @@ -245,6 +254,14 @@
2.44 except EmptyPCStack:
2.45 pass
2.46
2.47 + print "Execution terminated",
2.48 + if self.exception is not None:
2.49 + context, ref = self.exception
2.50 + print "with exception:", self.load(ref)
2.51 + print "at address:", self.load(ref + 1)
2.52 + else:
2.53 + print "successfully."
2.54 +
2.55 def execute(self):
2.56
2.57 "Execute code in the memory at the current PC address."
2.58 @@ -395,7 +412,8 @@
2.59 else:
2.60 self.value = self.load(ref + offset)
2.61 else:
2.62 - self.exception = self.attr_error
2.63 + exc = self._MakeObject(2, self.attr_error_instance)
2.64 + self.exception = exc, exc
2.65 return self.RaiseException()
2.66
2.67 def LoadAttrIndexContext(self):
2.68 @@ -407,7 +425,8 @@
2.69 loaded_context, loaded_ref = self.load(offset) # offset is address of class attribute
2.70 self.value = ref, loaded_ref
2.71 else:
2.72 - self.exception = self.attr_error
2.73 + exc = self._MakeObject(2, self.attr_error_instance)
2.74 + self.exception = exc, exc
2.75 return self.RaiseException()
2.76
2.77 def LoadAttrIndexContextCond(self):
2.78 @@ -422,7 +441,8 @@
2.79 else:
2.80 self.value = self.load(ref + offset)
2.81 else:
2.82 - self.exception = self.attr_error
2.83 + exc = self._MakeObject(2, self.attr_error_instance)
2.84 + self.exception = exc, exc
2.85 return self.RaiseException()
2.86
2.87 def StoreAttrIndex(self):
2.88 @@ -432,12 +452,14 @@
2.89 attr_index, class_attr, offset = element
2.90 if attr_index == self.operand:
2.91 if class_attr:
2.92 - self.exception = self.type_error
2.93 + exc = self._MakeObject(2, self.type_error_instance)
2.94 + self.exception = exc, exc
2.95 return self.RaiseException()
2.96 else:
2.97 self.save(ref + offset, self.source)
2.98 else:
2.99 - self.exception = self.attr_error
2.100 + exc = self._MakeObject(2, self.attr_error_instance)
2.101 + self.exception = exc, exc
2.102 return self.RaiseException()
2.103
2.104 # NOTE: LoadAttrIndexContext is a possibility if a particular attribute can always be overridden.
2.105 @@ -468,7 +490,8 @@
2.106 if param_index == self.operand:
2.107 self.frame_stack[frame + offset + 1] = self.source # add 1 to skip the context always generated
2.108 else:
2.109 - self.exception = self.type_error
2.110 + exc = self._MakeObject(2, self.type_error_instance)
2.111 + self.exception = exc, exc
2.112 return self.RaiseException()
2.113
2.114 def LoadCallable(self):
2.115 @@ -527,21 +550,24 @@
2.116 if nlocals > 0:
2.117 self_context, self_ref = self.frame_stack[frame + 1]
2.118 if not self._CheckInstance(self_ref, context_context):
2.119 - raise Exception, "CheckFrame %r (%r vs. %r)" % (self.operand, self.load(self_ref), self.load(context_context))
2.120 - #self.exception = self.type_error
2.121 - #return self.RaiseException()
2.122 + #raise Exception, "CheckFrame %r (%r vs. %r)" % (self.operand, self.load(self_ref), self.load(context_context))
2.123 + exc = self._MakeObject(2, self.type_error_instance)
2.124 + self.exception = exc, exc
2.125 + return self.RaiseException()
2.126 else:
2.127 - raise Exception, "CheckFrame %r (no self argument)" % self.operand
2.128 - #self.exception = self.type_error
2.129 - #return self.RaiseException()
2.130 + #raise Exception, "CheckFrame %r (no self argument)" % self.operand
2.131 + exc = self._MakeObject(2, self.type_error_instance)
2.132 + self.exception = exc, exc
2.133 + return self.RaiseException()
2.134
2.135 # Test the frame size.
2.136 # NOTE: Raise a proper exception here.
2.137
2.138 if not ((nargs - ndefaults) <= nlocals and (nlocals <= nargs or has_star)):
2.139 - raise Exception, "CheckFrame %r (%r <= %r <= %r)" % (self.operand, nargs - ndefaults, nlocals, nargs)
2.140 - #self.exception = self.type_error
2.141 - #return self.RaiseException()
2.142 + #raise Exception, "CheckFrame %r (%r <= %r <= %r)" % (self.operand, nargs - ndefaults, nlocals, nargs)
2.143 + exc = self._MakeObject(2, self.type_error_instance)
2.144 + self.exception = exc, exc
2.145 + return self.RaiseException()
2.146
2.147 def FillDefaults(self):
2.148 # NOTE: Make the get_operand method of the instruction provide the
2.149 @@ -617,6 +643,9 @@
2.150 self.exception = self.value[1]
2.151
2.152 def RaiseException(self):
2.153 + # NOTE: Adding the program counter as the first attribute.
2.154 + self.save(self.exception[1] + 1, self.pc)
2.155 + # Jumping to the current handler.
2.156 return self.handler_stack[-1]
2.157
2.158 def PushHandler(self):
2.159 @@ -696,8 +725,9 @@
2.160
2.161 # Test operand suitability.
2.162
2.163 - if not self._CheckInstance(left, self.int_class_location) and self._CheckInstance(right, self.int_class_location):
2.164 - self.exception = self.type_error
2.165 + if not self._CheckInstance(left, self.int_class) and self._CheckInstance(right, self.int_class):
2.166 + exc = self._MakeObject(2, self.type_error_instance)
2.167 + self.exception = exc, exc
2.168 return self.RaiseException()
2.169
2.170 # NOTE: Assume single location for data.
2.171 @@ -707,7 +737,7 @@
2.172
2.173 # Make a new object.
2.174
2.175 - addr = self._MakeObject(2, self.int_instance_location)
2.176 + addr = self._MakeObject(2, self.int_instance)
2.177
2.178 # Store the result.
2.179 # NOTE: The data is considered ready to use.
2.180 @@ -728,8 +758,9 @@
2.181
2.182 # Test operand suitability.
2.183
2.184 - if not self._CheckInstance(left, self.int_class_location):
2.185 - self.exception = self.type_error
2.186 + if not self._CheckInstance(left, self.int_class):
2.187 + exc = self._MakeObject(2, self.type_error_instance)
2.188 + self.exception = exc, exc
2.189 return self.RaiseException()
2.190
2.191 # NOTE: Assume single location for data.
2.192 @@ -753,8 +784,9 @@
2.193
2.194 # Test operand suitability.
2.195
2.196 - if not self._CheckInstance(left, self.int_class_location):
2.197 - self.exception = self.type_error
2.198 + if not self._CheckInstance(left, self.int_class):
2.199 + exc = self._MakeObject(2, self.type_error_instance)
2.200 + self.exception = exc, exc
2.201 return self.RaiseException()
2.202
2.203 # NOTE: Assume single location for data.
2.204 @@ -763,7 +795,7 @@
2.205
2.206 # Make a new object.
2.207
2.208 - addr = self._MakeObject(2, self.int_instance_location)
2.209 + addr = self._MakeObject(2, self.int_instance)
2.210
2.211 # Store the result.
2.212 # NOTE: The data is considered ready to use.
2.213 @@ -791,7 +823,7 @@
2.214 args_context, args = self.frame_stack[frame]
2.215 header = self.load(args)
2.216
2.217 - list = self._MakeObject(header.size, self.list_instance_location)
2.218 + list = self._MakeObject(header.size, self.list_instance)
2.219 for i in range(1, header.size):
2.220 self.save(list + i, self.load(args + i))
2.221
2.222 @@ -816,7 +848,8 @@
2.223 elif item_pos < 0 and item_pos >= -nelements:
2.224 item_pos = nelements + item_pos
2.225 else:
2.226 - self.exception = self.index_error
2.227 + exc = self._MakeObject(2, self.index_error_instance)
2.228 + self.exception = exc, exc
2.229 return self.RaiseException()
2.230
2.231 self.result = self.load(obj + 1 + item_pos)