1.1 --- a/rsvp.py Sat May 30 20:30:28 2009 +0200
1.2 +++ b/rsvp.py Sun May 31 18:44:40 2009 +0200
1.3 @@ -53,6 +53,7 @@
1.4 """
1.5
1.6 from micropython.program import DataObject # for creating "nice" new objects
1.7 +import operator
1.8
1.9 class IllegalInstruction(Exception):
1.10 pass
1.11 @@ -650,6 +651,9 @@
1.12 def StoreException(self):
1.13 self.exception = self.value[1]
1.14
1.15 + def ClearException(self):
1.16 + self.exception = None
1.17 +
1.18 def RaiseException(self):
1.19 # NOTE: Adding the program counter as the first attribute.
1.20 self.save(self.exception + 1, self.pc)
1.21 @@ -734,8 +738,9 @@
1.22 right_context, right = self.frame_stack[frame + 1]
1.23
1.24 # Test operand suitability.
1.25 + # NOTE: Support other types.
1.26
1.27 - if not self._CheckInstance(left, self.int_class) and self._CheckInstance(right, self.int_class):
1.28 + if not (self._CheckInstance(left, self.int_class) and self._CheckInstance(right, self.int_class)):
1.29 self.exception = self._MakeObject(2, self.type_error_instance)
1.30 return self.RaiseException()
1.31
1.32 @@ -814,6 +819,50 @@
1.33
1.34 self.result = addr, addr
1.35
1.36 + def builtins_int_op(self, op, true_if_incompatible):
1.37 + frame = self.local_sp_stack[-1]
1.38 +
1.39 + # Get operands addresses.
1.40 +
1.41 + left_context, left = self.frame_stack[frame]
1.42 + right_context, right = self.frame_stack[frame + 1]
1.43 +
1.44 + # Test operand suitability.
1.45 + # NOTE: Support other types.
1.46 + # NOTE: Handle comparisons of incompatible types more appropriately.
1.47 +
1.48 + if not (self._CheckInstance(left, self.int_class) and self._CheckInstance(right, self.int_class)):
1.49 + if true_if_incompatible:
1.50 + self.result = self.true_constant, self.true_constant
1.51 + else:
1.52 + self.result = self.false_constant, self.false_constant
1.53 + return
1.54 +
1.55 + # NOTE: Assume single location for data.
1.56 +
1.57 + left_data = left + 1
1.58 + right_data = right + 1
1.59 +
1.60 + # Test the data.
1.61 + # NOTE: The data is considered ready to use.
1.62 +
1.63 + if op(self.load(left_data), self.load(right_data)):
1.64 + self.result = self.true_constant, self.true_constant
1.65 + else:
1.66 + self.result = self.false_constant, self.false_constant
1.67 +
1.68 + def builtins_int_lt(self):
1.69 + return self.builtins_int_op(operator.lt, 0)
1.70 +
1.71 + def builtins_int_gt(self):
1.72 + return self.builtins_int_op(operator.gt, 0)
1.73 +
1.74 + def builtins_int_eq(self):
1.75 + return self.builtins_int_op(operator.eq, 0)
1.76 +
1.77 + def builtins_int_ne(self):
1.78 + return self.builtins_int_op(operator.ne, 1)
1.79 +
1.80 def builtins_bool_bool(self):
1.81 frame = self.local_sp_stack[-1]
1.82
1.83 @@ -867,6 +916,10 @@
1.84 "__builtins__.int.__add__" : builtins_int_add,
1.85 "__builtins__.int.__bool__" : builtins_int_bool,
1.86 "__builtins__.int.__neg__" : builtins_int_neg,
1.87 + "__builtins__.int.__lt__" : builtins_int_lt,
1.88 + "__builtins__.int.__gt__" : builtins_int_gt,
1.89 + "__builtins__.int.__eq__" : builtins_int_eq,
1.90 + "__builtins__.int.__ne__" : builtins_int_ne,
1.91 "__builtins__.bool.__bool__" : builtins_bool_bool,
1.92 "__builtins__.list" : builtins_list_new,
1.93 "__builtins__.list.__getitem__" : builtins_list_getitem,