2.1 --- a/micropython/rsvp.py Sun Jan 25 02:56:16 2009 +0100
2.2 +++ b/micropython/rsvp.py Mon Jan 26 01:25:30 2009 +0100
2.3 @@ -20,11 +20,12 @@
2.4 """
2.5
2.6 from micropython.data import Attr, Const
2.7 +from micropython.common import Block
2.8
2.9 def name(attr):
2.10 if isinstance(attr, Attr):
2.11 return attr.name or "<unnamed>"
2.12 - elif isinstance(attr, Const):
2.13 + elif isinstance(attr, (Block, Const)):
2.14 return attr
2.15 else:
2.16 return attr.full_name() or "<unnamed>"
3.1 --- a/rsvp.py Sun Jan 25 02:56:16 2009 +0100
3.2 +++ b/rsvp.py Mon Jan 26 01:25:30 2009 +0100
3.3 @@ -73,7 +73,7 @@
3.4
3.5 "A really simple virtual processor."
3.6
3.7 - def __init__(self, memory, objlist, paramlist, attr_error, type_error, pc=None, debug=0):
3.8 + def __init__(self, memory, objlist, paramlist, pc=None, debug=0):
3.9
3.10 """
3.11 Initialise the processor with a 'memory' (a list of values containing
3.12 @@ -81,8 +81,8 @@
3.13 """
3.14
3.15 self.memory = memory
3.16 - self.objlist = objlist
3.17 - self.paramlist = paramlist
3.18 + self.objlist = objlist.as_raw()
3.19 + self.paramlist = paramlist.as_raw()
3.20 self.pc = pc or 0
3.21 self.debug = debug
3.22
3.23 @@ -109,8 +109,12 @@
3.24
3.25 # Constants.
3.26
3.27 - self.attr_error = attr_error
3.28 - self.type_error = type_error
3.29 + self.attr_error = objlist.access("__builtins__", "AttributeError").value.location
3.30 + self.type_error = objlist.access("__builtins__", "TypeError").value.location
3.31 +
3.32 + # Native class constants.
3.33 +
3.34 + self.int_class = objlist.access("__builtins__", "int").value.location
3.35
3.36 # Debugging methods.
3.37
3.38 @@ -280,8 +284,11 @@
3.39 PC to 'next' afterwards; otherwise, set PC to 'addr'.
3.40 """
3.41
3.42 + # Trap library functions introduced through the use of strings instead
3.43 + # of proper locations.
3.44 +
3.45 if isinstance(addr, str):
3.46 - getattr(self, addr)()
3.47 + self.native_functions[addr](self)
3.48 return next
3.49 else:
3.50 self.push_pc(self.pc + 1)
3.51 @@ -319,10 +326,7 @@
3.52 def MakeObject(self):
3.53 size = self.operand
3.54 context, ref = self.value
3.55 - classcode, attrcode, codeaddr, codedetails, instance = self.load(ref)
3.56 - addr = self.new(size)
3.57 - # Set the header to resemble the class.
3.58 - self.save(addr, (classcode, attrcode, None, None, 1)) # NOTE: __call__ method not yet provided.
3.59 + addr = self._MakeObject(size, ref)
3.60 # Introduce null context for new object.
3.61 self.value = None, addr
3.62
3.63 @@ -438,7 +442,7 @@
3.64
3.65 # Check the details of the proposed context and the target's context.
3.66
3.67 - self._CheckInstance(ref, target_context)
3.68 + self.status = self._CheckInstance(ref, target_context)
3.69
3.70 def JumpWithFrame(self):
3.71 codeaddr, codedetails = self.callable
3.72 @@ -498,7 +502,7 @@
3.73 self.handler_stack.pop()
3.74
3.75 def CheckException(self):
3.76 - self._CheckInstance(self.exception, self.value[1])
3.77 + self.status = self.exception is not None and self._CheckInstance(self.exception, self.value[1])
3.78
3.79 def TestIdentity(self):
3.80 self.status = self.value[1] == self.source[1]
3.81 @@ -522,9 +526,55 @@
3.82
3.83 element = self.objlist[target_classcode + attrcode]
3.84 attr_index, class_attr, replace_context, offset = element
3.85 - if attr_index == attrcode:
3.86 - self.status = 1
3.87 - else:
3.88 - self.status = 0
3.89 + return attr_index == attrcode
3.90 +
3.91 + def _MakeObject(self, size, ref):
3.92 + classcode, attrcode, codeaddr, codedetails, instance = self.load(ref)
3.93 + addr = self.new(size)
3.94 + # Set the header to resemble the class.
3.95 + self.save(addr, (classcode, attrcode, None, None, 1)) # NOTE: __call__ method not yet provided.
3.96 + return addr
3.97 +
3.98 + # Native function implementations.
3.99 +
3.100 + def builtins_object_init(self):
3.101 + pass
3.102 +
3.103 + def builtins_int_init(self):
3.104 + pass
3.105 +
3.106 + def builtins_int_add(self):
3.107 + frame = self.local_sp_stack[-1]
3.108 +
3.109 + # Get operands addresses.
3.110 +
3.111 + left_context, left = self.frame_stack[frame]
3.112 + right_context, right = self.frame_stack[frame + 1]
3.113 +
3.114 + # Test operand suitability.
3.115 +
3.116 + if not self._CheckInstance(left, self.int_class) and self._CheckInstance(right, self.int_class):
3.117 + self.exception = self.type_error
3.118 + return self.RaiseException()
3.119 +
3.120 + # NOTE: Assume single location for data.
3.121 +
3.122 + left_data = left + 1
3.123 + right_data = right + 1
3.124 +
3.125 + # Make a new object.
3.126 +
3.127 + addr = self._MakeObject(2, self.int_class)
3.128 + self.save(addr + 1, self.load(left_data) + self.load(right_data))
3.129 +
3.130 + # Return the new object (with a null context).
3.131 +
3.132 + self.result = None, addr
3.133 +
3.134 + native_functions = {
3.135 + "__builtins__.object.__init__" : builtins_object_init,
3.136 + "__builtins__.int.__init__" : builtins_int_init,
3.137 + "__builtins__.int.__add__" : builtins_int_add,
3.138 + }
3.139
3.140 # vim: tabstop=4 expandtab shiftwidth=4
4.1 --- a/test.py Sun Jan 25 02:56:16 2009 +0100
4.2 +++ b/test.py Mon Jan 26 01:25:30 2009 +0100
4.3 @@ -37,12 +37,10 @@
4.4 pt = program.get_parameter_table()
4.5 objlist = ot.as_list()
4.6 paramlist = pt.as_list()
4.7 - attr_error = objlist.access("__builtins__", "AttributeError").value.location
4.8 - type_error = objlist.access("__builtins__", "TypeError").value.location
4.9 print "Getting raw image..."
4.10 rc = program.get_raw_image()
4.11 print "Initialising the machine..."
4.12 - rm = rsvp.RSVPMachine(rc, objlist.as_raw(), paramlist.as_raw(), attr_error, type_error, debug=debug)
4.13 + rm = rsvp.RSVPMachine(rc, objlist, paramlist, debug=debug)
4.14 rm.pc = program.code_location
4.15 return rm
4.16