2.1 --- a/micropython/ast.py Wed Sep 30 00:24:41 2009 +0200
2.2 +++ b/micropython/ast.py Wed Sep 30 00:53:07 2009 +0200
2.3 @@ -1009,7 +1009,17 @@
2.4 self.new_op(ClearException())
2.5 self.drop_exception_blocks()
2.6
2.7 - def visitTryFinally(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "TryFinally")
2.8 + def visitTryFinally(self, node):
2.9 +
2.10 + """
2.11 + Add finally handler, potentially as an exception handler.
2.12 + Generate body, potentially changing return statements so that they do
2.13 + not return immediately.
2.14 + Generate handler, removing the handler from the active handler list,
2.15 + adding instructions which raise active exceptions.
2.16 + """
2.17 +
2.18 + raise TranslationNotImplementedError(self.module.full_name(), node, "TryFinally")
2.19
2.20 def visitWhile(self, node):
2.21 exit_block = self.new_block()
3.1 --- a/rsvplib.py Wed Sep 30 00:24:41 2009 +0200
3.2 +++ b/rsvplib.py Wed Sep 30 00:53:07 2009 +0200
3.3 @@ -47,6 +47,9 @@
3.4 cls = self.machine._get_class("__builtins__", "IndexError")
3.5 self.index_error = cls.location
3.6 self.index_error_instance = cls.instance_template_location
3.7 + cls = self.machine._get_class("__builtins__", "basestring")
3.8 + self.str_class = cls.location
3.9 + self.str_instance = cls.instance_template_location
3.10
3.11 self.tuple_class = self.machine.tuple_class
3.12 self.type_error_instance = self.machine.type_error_instance
3.13 @@ -88,6 +91,95 @@
3.14
3.15 self.machine.result = addr, addr
3.16
3.17 + def builtins_logical_op(self, operand_class, op, true_if_incompatible):
3.18 + frame = self.local_sp_stack[-1]
3.19 +
3.20 + # Get operands addresses.
3.21 +
3.22 + left_context, left = self.frame_stack[frame]
3.23 + right_context, right = self.frame_stack[frame + 1]
3.24 +
3.25 + # Test operand suitability.
3.26 + # NOTE: Handle comparisons of incompatible types more appropriately.
3.27 +
3.28 + if not (self.machine._CheckInstance(left, operand_class) and self.machine._CheckInstance(right, operand_class)):
3.29 + if true_if_incompatible:
3.30 + self.machine.result = self.true_constant, self.true_constant
3.31 + else:
3.32 + self.machine.result = self.false_constant, self.false_constant
3.33 + return
3.34 +
3.35 + # NOTE: Assume single location for data.
3.36 +
3.37 + left_data = left + 1
3.38 + right_data = right + 1
3.39 +
3.40 + # Test the data.
3.41 + # NOTE: The data is considered ready to use.
3.42 +
3.43 + if op(self.machine.load(left_data), self.machine.load(right_data)):
3.44 + self.machine.result = self.true_constant, self.true_constant
3.45 + else:
3.46 + self.machine.result = self.false_constant, self.false_constant
3.47 +
3.48 + # Operators.
3.49 + # Although this takes a short-cut by using the operator module, testing is
3.50 + # still performed on the operands to ensure that they qualify for these
3.51 + # native operations.
3.52 +
3.53 + def builtins_int_add(self):
3.54 + return self.builtins_int_arithmetic_op(operator.add)
3.55 +
3.56 + def builtins_int_sub(self):
3.57 + return self.builtins_int_arithmetic_op(operator.sub)
3.58 +
3.59 + def builtins_int_pow(self):
3.60 + return self.builtins_int_arithmetic_op(operator.pow)
3.61 +
3.62 + def builtins_int_lt(self):
3.63 + return self.builtins_logical_op(self.int_class, operator.lt, 0)
3.64 +
3.65 + def builtins_int_le(self):
3.66 + return self.builtins_logical_op(self.int_class, operator.le, 0)
3.67 +
3.68 + def builtins_int_gt(self):
3.69 + return self.builtins_logical_op(self.int_class, operator.gt, 0)
3.70 +
3.71 + def builtins_int_ge(self):
3.72 + return self.builtins_logical_op(self.int_class, operator.ge, 0)
3.73 +
3.74 + def builtins_int_eq(self):
3.75 + return self.builtins_logical_op(self.int_class, operator.eq, 0)
3.76 +
3.77 + def builtins_int_ne(self):
3.78 + return self.builtins_logical_op(self.int_class, operator.ne, 1)
3.79 +
3.80 + def builtins_str_lt(self):
3.81 + return self.builtins_logical_op(self.str_class, operator.lt, 0)
3.82 +
3.83 + def builtins_str_le(self):
3.84 + return self.builtins_logical_op(self.str_class, operator.le, 0)
3.85 +
3.86 + def builtins_str_gt(self):
3.87 + return self.builtins_logical_op(self.str_class, operator.gt, 0)
3.88 +
3.89 + def builtins_str_ge(self):
3.90 + return self.builtins_logical_op(self.str_class, operator.ge, 0)
3.91 +
3.92 + def builtins_str_eq(self):
3.93 + return self.builtins_logical_op(self.str_class, operator.eq, 0)
3.94 +
3.95 + def builtins_str_ne(self):
3.96 + return self.builtins_logical_op(self.str_class, operator.ne, 1)
3.97 +
3.98 + def builtins_int_and(self):
3.99 + return self.builtins_int_arithmetic_op(operator.and_)
3.100 +
3.101 + def builtins_int_or(self):
3.102 + return self.builtins_int_arithmetic_op(operator.or_)
3.103 +
3.104 + # Specific operator methods.
3.105 +
3.106 def builtins_int_bool(self):
3.107 frame = self.local_sp_stack[-1]
3.108
3.109 @@ -144,67 +236,7 @@
3.110
3.111 self.machine.result = addr, addr
3.112
3.113 - def builtins_int_op(self, op, true_if_incompatible):
3.114 - frame = self.local_sp_stack[-1]
3.115 -
3.116 - # Get operands addresses.
3.117 -
3.118 - left_context, left = self.frame_stack[frame]
3.119 - right_context, right = self.frame_stack[frame + 1]
3.120 -
3.121 - # Test operand suitability.
3.122 - # NOTE: Support other types.
3.123 - # NOTE: Handle comparisons of incompatible types more appropriately.
3.124 -
3.125 - if not (self.machine._CheckInstance(left, self.int_class) and self.machine._CheckInstance(right, self.int_class)):
3.126 - if true_if_incompatible:
3.127 - self.machine.result = self.true_constant, self.true_constant
3.128 - else:
3.129 - self.machine.result = self.false_constant, self.false_constant
3.130 - return
3.131 -
3.132 - # NOTE: Assume single location for data.
3.133 -
3.134 - left_data = left + 1
3.135 - right_data = right + 1
3.136 -
3.137 - # Test the data.
3.138 - # NOTE: The data is considered ready to use.
3.139 -
3.140 - if op(self.machine.load(left_data), self.machine.load(right_data)):
3.141 - self.machine.result = self.true_constant, self.true_constant
3.142 - else:
3.143 - self.machine.result = self.false_constant, self.false_constant
3.144 -
3.145 - def builtins_int_add(self):
3.146 - return self.builtins_int_arithmetic_op(operator.add)
3.147 -
3.148 - def builtins_int_sub(self):
3.149 - return self.builtins_int_arithmetic_op(operator.sub)
3.150 -
3.151 - def builtins_int_lt(self):
3.152 - return self.builtins_int_op(operator.lt, 0)
3.153 -
3.154 - def builtins_int_le(self):
3.155 - return self.builtins_int_op(operator.le, 0)
3.156 -
3.157 - def builtins_int_gt(self):
3.158 - return self.builtins_int_op(operator.gt, 0)
3.159 -
3.160 - def builtins_int_ge(self):
3.161 - return self.builtins_int_op(operator.ge, 0)
3.162 -
3.163 - def builtins_int_eq(self):
3.164 - return self.builtins_int_op(operator.eq, 0)
3.165 -
3.166 - def builtins_int_ne(self):
3.167 - return self.builtins_int_op(operator.ne, 1)
3.168 -
3.169 - def builtins_int_and(self):
3.170 - return self.builtins_int_arithmetic_op(operator.and_)
3.171 -
3.172 - def builtins_int_or(self):
3.173 - return self.builtins_int_arithmetic_op(operator.or_)
3.174 + # Various built-in methods.
3.175
3.176 def builtins_bool_bool(self):
3.177 frame = self.local_sp_stack[-1]
3.178 @@ -431,6 +463,7 @@
3.179 "__builtins__.int.__add__" : builtins_int_add,
3.180 "__builtins__.int.__radd__" : builtins_int_add, # NOTE: To be made distinct.
3.181 "__builtins__.int.__sub__" : builtins_int_sub,
3.182 + "__builtins__.int.__pow__" : builtins_int_pow,
3.183 "__builtins__.int.__iadd__" : builtins_int_add,
3.184 "__builtins__.int.__bool__" : builtins_int_bool,
3.185 "__builtins__.int.__neg__" : builtins_int_neg,
3.186 @@ -450,6 +483,12 @@
3.187 "__builtins__.list.append" : builtins_list_append,
3.188 "__builtins__.tuple.__len__" : builtins_tuple_len,
3.189 "__builtins__.tuple.__getitem__" : builtins_tuple_getitem,
3.190 + "__builtins__.basestring.__lt__" : builtins_str_lt,
3.191 + "__builtins__.basestring.__le__" : builtins_str_le,
3.192 + "__builtins__.basestring.__gt__" : builtins_str_gt,
3.193 + "__builtins__.basestring.__ge__" : builtins_str_ge,
3.194 + "__builtins__.basestring.__eq__" : builtins_str_eq,
3.195 + "__builtins__.basestring.__ne__" : builtins_str_ne,
3.196
3.197 # Native initialisers:
3.198