1.1 --- a/rsvplib.py Wed Sep 30 00:24:41 2009 +0200
1.2 +++ b/rsvplib.py Wed Sep 30 00:53:07 2009 +0200
1.3 @@ -47,6 +47,9 @@
1.4 cls = self.machine._get_class("__builtins__", "IndexError")
1.5 self.index_error = cls.location
1.6 self.index_error_instance = cls.instance_template_location
1.7 + cls = self.machine._get_class("__builtins__", "basestring")
1.8 + self.str_class = cls.location
1.9 + self.str_instance = cls.instance_template_location
1.10
1.11 self.tuple_class = self.machine.tuple_class
1.12 self.type_error_instance = self.machine.type_error_instance
1.13 @@ -88,6 +91,95 @@
1.14
1.15 self.machine.result = addr, addr
1.16
1.17 + def builtins_logical_op(self, operand_class, op, true_if_incompatible):
1.18 + frame = self.local_sp_stack[-1]
1.19 +
1.20 + # Get operands addresses.
1.21 +
1.22 + left_context, left = self.frame_stack[frame]
1.23 + right_context, right = self.frame_stack[frame + 1]
1.24 +
1.25 + # Test operand suitability.
1.26 + # NOTE: Handle comparisons of incompatible types more appropriately.
1.27 +
1.28 + if not (self.machine._CheckInstance(left, operand_class) and self.machine._CheckInstance(right, operand_class)):
1.29 + if true_if_incompatible:
1.30 + self.machine.result = self.true_constant, self.true_constant
1.31 + else:
1.32 + self.machine.result = self.false_constant, self.false_constant
1.33 + return
1.34 +
1.35 + # NOTE: Assume single location for data.
1.36 +
1.37 + left_data = left + 1
1.38 + right_data = right + 1
1.39 +
1.40 + # Test the data.
1.41 + # NOTE: The data is considered ready to use.
1.42 +
1.43 + if op(self.machine.load(left_data), self.machine.load(right_data)):
1.44 + self.machine.result = self.true_constant, self.true_constant
1.45 + else:
1.46 + self.machine.result = self.false_constant, self.false_constant
1.47 +
1.48 + # Operators.
1.49 + # Although this takes a short-cut by using the operator module, testing is
1.50 + # still performed on the operands to ensure that they qualify for these
1.51 + # native operations.
1.52 +
1.53 + def builtins_int_add(self):
1.54 + return self.builtins_int_arithmetic_op(operator.add)
1.55 +
1.56 + def builtins_int_sub(self):
1.57 + return self.builtins_int_arithmetic_op(operator.sub)
1.58 +
1.59 + def builtins_int_pow(self):
1.60 + return self.builtins_int_arithmetic_op(operator.pow)
1.61 +
1.62 + def builtins_int_lt(self):
1.63 + return self.builtins_logical_op(self.int_class, operator.lt, 0)
1.64 +
1.65 + def builtins_int_le(self):
1.66 + return self.builtins_logical_op(self.int_class, operator.le, 0)
1.67 +
1.68 + def builtins_int_gt(self):
1.69 + return self.builtins_logical_op(self.int_class, operator.gt, 0)
1.70 +
1.71 + def builtins_int_ge(self):
1.72 + return self.builtins_logical_op(self.int_class, operator.ge, 0)
1.73 +
1.74 + def builtins_int_eq(self):
1.75 + return self.builtins_logical_op(self.int_class, operator.eq, 0)
1.76 +
1.77 + def builtins_int_ne(self):
1.78 + return self.builtins_logical_op(self.int_class, operator.ne, 1)
1.79 +
1.80 + def builtins_str_lt(self):
1.81 + return self.builtins_logical_op(self.str_class, operator.lt, 0)
1.82 +
1.83 + def builtins_str_le(self):
1.84 + return self.builtins_logical_op(self.str_class, operator.le, 0)
1.85 +
1.86 + def builtins_str_gt(self):
1.87 + return self.builtins_logical_op(self.str_class, operator.gt, 0)
1.88 +
1.89 + def builtins_str_ge(self):
1.90 + return self.builtins_logical_op(self.str_class, operator.ge, 0)
1.91 +
1.92 + def builtins_str_eq(self):
1.93 + return self.builtins_logical_op(self.str_class, operator.eq, 0)
1.94 +
1.95 + def builtins_str_ne(self):
1.96 + return self.builtins_logical_op(self.str_class, operator.ne, 1)
1.97 +
1.98 + def builtins_int_and(self):
1.99 + return self.builtins_int_arithmetic_op(operator.and_)
1.100 +
1.101 + def builtins_int_or(self):
1.102 + return self.builtins_int_arithmetic_op(operator.or_)
1.103 +
1.104 + # Specific operator methods.
1.105 +
1.106 def builtins_int_bool(self):
1.107 frame = self.local_sp_stack[-1]
1.108
1.109 @@ -144,67 +236,7 @@
1.110
1.111 self.machine.result = addr, addr
1.112
1.113 - def builtins_int_op(self, op, true_if_incompatible):
1.114 - frame = self.local_sp_stack[-1]
1.115 -
1.116 - # Get operands addresses.
1.117 -
1.118 - left_context, left = self.frame_stack[frame]
1.119 - right_context, right = self.frame_stack[frame + 1]
1.120 -
1.121 - # Test operand suitability.
1.122 - # NOTE: Support other types.
1.123 - # NOTE: Handle comparisons of incompatible types more appropriately.
1.124 -
1.125 - if not (self.machine._CheckInstance(left, self.int_class) and self.machine._CheckInstance(right, self.int_class)):
1.126 - if true_if_incompatible:
1.127 - self.machine.result = self.true_constant, self.true_constant
1.128 - else:
1.129 - self.machine.result = self.false_constant, self.false_constant
1.130 - return
1.131 -
1.132 - # NOTE: Assume single location for data.
1.133 -
1.134 - left_data = left + 1
1.135 - right_data = right + 1
1.136 -
1.137 - # Test the data.
1.138 - # NOTE: The data is considered ready to use.
1.139 -
1.140 - if op(self.machine.load(left_data), self.machine.load(right_data)):
1.141 - self.machine.result = self.true_constant, self.true_constant
1.142 - else:
1.143 - self.machine.result = self.false_constant, self.false_constant
1.144 -
1.145 - def builtins_int_add(self):
1.146 - return self.builtins_int_arithmetic_op(operator.add)
1.147 -
1.148 - def builtins_int_sub(self):
1.149 - return self.builtins_int_arithmetic_op(operator.sub)
1.150 -
1.151 - def builtins_int_lt(self):
1.152 - return self.builtins_int_op(operator.lt, 0)
1.153 -
1.154 - def builtins_int_le(self):
1.155 - return self.builtins_int_op(operator.le, 0)
1.156 -
1.157 - def builtins_int_gt(self):
1.158 - return self.builtins_int_op(operator.gt, 0)
1.159 -
1.160 - def builtins_int_ge(self):
1.161 - return self.builtins_int_op(operator.ge, 0)
1.162 -
1.163 - def builtins_int_eq(self):
1.164 - return self.builtins_int_op(operator.eq, 0)
1.165 -
1.166 - def builtins_int_ne(self):
1.167 - return self.builtins_int_op(operator.ne, 1)
1.168 -
1.169 - def builtins_int_and(self):
1.170 - return self.builtins_int_arithmetic_op(operator.and_)
1.171 -
1.172 - def builtins_int_or(self):
1.173 - return self.builtins_int_arithmetic_op(operator.or_)
1.174 + # Various built-in methods.
1.175
1.176 def builtins_bool_bool(self):
1.177 frame = self.local_sp_stack[-1]
1.178 @@ -431,6 +463,7 @@
1.179 "__builtins__.int.__add__" : builtins_int_add,
1.180 "__builtins__.int.__radd__" : builtins_int_add, # NOTE: To be made distinct.
1.181 "__builtins__.int.__sub__" : builtins_int_sub,
1.182 + "__builtins__.int.__pow__" : builtins_int_pow,
1.183 "__builtins__.int.__iadd__" : builtins_int_add,
1.184 "__builtins__.int.__bool__" : builtins_int_bool,
1.185 "__builtins__.int.__neg__" : builtins_int_neg,
1.186 @@ -450,6 +483,12 @@
1.187 "__builtins__.list.append" : builtins_list_append,
1.188 "__builtins__.tuple.__len__" : builtins_tuple_len,
1.189 "__builtins__.tuple.__getitem__" : builtins_tuple_getitem,
1.190 + "__builtins__.basestring.__lt__" : builtins_str_lt,
1.191 + "__builtins__.basestring.__le__" : builtins_str_le,
1.192 + "__builtins__.basestring.__gt__" : builtins_str_gt,
1.193 + "__builtins__.basestring.__ge__" : builtins_str_ge,
1.194 + "__builtins__.basestring.__eq__" : builtins_str_eq,
1.195 + "__builtins__.basestring.__ne__" : builtins_str_ne,
1.196
1.197 # Native initialisers:
1.198