# HG changeset patch # User Paul Boddie # Date 1242583273 -7200 # Node ID 526a20aebd06cef85371f20dc54b672892a186ab # Parent 626da7ae6d5e57f79746300195c0615d8b30d7f0 Fixed unary operator code generation. Added subscript support. Added RSVP implementations of list.__getitem__ and int.__neg__. Added a test of subscripts and a currently unused test of star parameter unpacking. diff -r 626da7ae6d5e -r 526a20aebd06 lib/builtins.py --- a/lib/builtins.py Sun May 17 00:58:27 2009 +0200 +++ b/lib/builtins.py Sun May 17 20:01:13 2009 +0200 @@ -228,12 +228,11 @@ class FloatingPointError(Exception): pass class FutureWarning(Warning): pass class GeneratorExit(Exception): pass -class IndexError(Exception): pass -class IOError(Exception): pass class ImportError(Exception): pass class ImportWarning(Warning): pass class IndentationError(Exception): pass class IndexError(Exception): pass +class IOError(Exception): pass class KeyError(Exception): pass class KeyboardInterrupt(Exception): pass class LookupError(Exception): pass @@ -336,6 +335,7 @@ AttributeError StopIteration TypeError +IndexError list tuple diff -r 626da7ae6d5e -r 526a20aebd06 micropython/ast.py --- a/micropython/ast.py Sun May 17 00:58:27 2009 +0200 +++ b/micropython/ast.py Sun May 17 20:01:13 2009 +0200 @@ -441,7 +441,13 @@ def visitSlice(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Slice") - def visitSubscript(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Subscript") + def visitSubscript(self, node): + self.dispatch(node.expr) + self._startCallFunc() + self._generateAttr(node, "__getitem__", self.attribute_load_instructions) + temp, target = self._generateCallFunc(node.subs, node) + self._doCallFunc(temp, target) + self._endCallFunc(temp, target) def visitTuple(self, node): self._generateSequence("tuple", node) diff -r 626da7ae6d5e -r 526a20aebd06 micropython/inspect.py --- a/micropython/inspect.py Sun May 17 00:58:27 2009 +0200 +++ b/micropython/inspect.py Sun May 17 20:01:13 2009 +0200 @@ -745,7 +745,9 @@ visitSub = _visitBinary - visitSubscript = OP + def visitSubscript(self, node): + self.importer.use_name("__getitem__") + self.OP(node) def visitTryExcept(self, node): self.dispatch(node.body) diff -r 626da7ae6d5e -r 526a20aebd06 micropython/trans.py --- a/micropython/trans.py Sun May 17 00:58:27 2009 +0200 +++ b/micropython/trans.py Sun May 17 20:01:13 2009 +0200 @@ -924,11 +924,11 @@ self._endCallFuncArgs(1) self._doCallFunc(temp_method) self._endCallFunc(temp_method) - self.new_op(Jump(end_block)) # Store the result. temp_out = self.get_temp() + self.new_op(Jump(end_block)) # Raise a TypeError. diff -r 626da7ae6d5e -r 526a20aebd06 rsvp.py --- a/rsvp.py Sun May 17 00:58:27 2009 +0200 +++ b/rsvp.py Sun May 17 20:01:13 2009 +0200 @@ -123,6 +123,7 @@ self.attr_error = objlist.access("__builtins__", "AttributeError").get_value().location self.type_error = objlist.access("__builtins__", "TypeError").get_value().location + self.index_error = objlist.access("__builtins__", "IndexError").get_value().location # Native class constants. @@ -707,6 +708,37 @@ else: self.result = self.false_constant, self.false_constant + def builtins_int_neg(self): + frame = self.local_sp_stack[-1] + + # Get operands addresses. + + left_context, left = self.frame_stack[frame] + + # Test operand suitability. + + if not self._CheckInstance(left, self.int_class_location): + self.exception = self.type_error + return self.RaiseException() + + # NOTE: Assume single location for data. + + left_data = left + 1 + + # Make a new object. + + addr = self._MakeObject(2, self.int_instance_location) + + # Store the result. + # NOTE: The data is considered ready to use. + + self.save(addr + 1, -self.load(left_data)) + + # Return the new object. + # Introduce object as context for the new object. + + self.result = addr, addr + def builtins_bool_bool(self): frame = self.local_sp_stack[-1] @@ -729,14 +761,40 @@ self.result = list, list + def builtins_list_getitem(self): + frame = self.local_sp_stack[-1] + + # Get operands addresses. + + obj_context, obj = self.frame_stack[frame] + item_context, item = self.frame_stack[frame + 1] + + header = self.load(obj) + nelements = header.size - 1 + + # NOTE: Assume single location for data. + + item_pos = self.load(item + 1) + if item_pos >= 0 and item_pos < nelements: + pass + elif item_pos < 0 and item_pos >= -nelements: + item_pos = nelements + item_pos + else: + self.exception = self.index_error + return self.RaiseException() + + self.result = self.load(obj + 1 + item_pos) + def builtins_object_init(self): pass native_functions = { "__builtins__.int.__add__" : builtins_int_add, "__builtins__.int.__bool__" : builtins_int_bool, + "__builtins__.int.__neg__" : builtins_int_neg, "__builtins__.bool.__bool__" : builtins_bool_bool, "__builtins__.list" : builtins_list_new, + "__builtins__.list.__getitem__" : builtins_list_getitem, "__builtins__.object.__init__" : builtins_object_init, } diff -r 626da7ae6d5e -r 526a20aebd06 tests/call_func_star_parameter_unpack.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/call_func_star_parameter_unpack.py Sun May 17 20:01:13 2009 +0200 @@ -0,0 +1,9 @@ +#!/usr/bin/env python + +def f(*args): + a, b, c = args + return c + +x = f(1, 2, 3) + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 626da7ae6d5e -r 526a20aebd06 tests/subscripts.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/subscripts.py Sun May 17 20:01:13 2009 +0200 @@ -0,0 +1,8 @@ +#!/usr/bin/env python + +l = [1, 2, 3] +x = l[0] +y = l[1] +z = l[-1] + +# vim: tabstop=4 expandtab shiftwidth=4