# HG changeset patch # User Paul Boddie # Date 1244408159 -7200 # Node ID c93dcf98ca1b3b9cf4e1d01a78fc7e6507e343f6 # Parent db8334eb671f795d40455ca2d49f9d73ae574c9e Changed the code body location initialisation in the code generation activity to set either a specific location or a symbolic reference to a native function or class (employing an instantiator). Fixed the "for" loop iterator storage and exception handling. Made xrange raise a StopIteration instance, short of adding class detection into RaiseException. Made code generation produce instantiators for StopIteration and BaseException, with the latter also providing attribute/size information in the builtins module. Added more integer operations in the RSVP implementation. diff -r db8334eb671f -r c93dcf98ca1b lib/builtins.py --- a/lib/builtins.py Sun Jun 07 21:17:59 2009 +0200 +++ b/lib/builtins.py Sun Jun 07 22:55:59 2009 +0200 @@ -1,9 +1,10 @@ #!/usr/bin/env python """ -Simple built-in classes and functions. +Simple built-in classes and functions. Objects which provide code which shall +always be compiled should provide docstrings. -Copyright (C) 2005, 2006, 2007, 2008 Paul Boddie +Copyright (C) 2005, 2006, 2007, 2008, 2009 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -235,7 +236,7 @@ "Return the next item or raise a StopIteration exception." if self.step < 0 and self.current <= self.end or self.step > 0 and self.current >= self.end: - raise StopIteration + raise StopIteration() current = self.current self.current += self.step @@ -244,6 +245,9 @@ # Exceptions and warnings. class BaseException(object): + + "Implementation of BaseException." + def __init__(self, *args): self.args = args @@ -277,7 +281,7 @@ class RuntimeError(Exception): pass class RuntimeWarning(Warning): pass class StandardError(Exception): pass -class StopIteration(Exception): pass +class StopIteration(Exception): "Implementation of StopIteration." class SyntaxError(Exception): pass class SyntaxWarning(Warning): pass class SystemError(Exception): pass diff -r db8334eb671f -r c93dcf98ca1b micropython/__init__.py --- a/micropython/__init__.py Sun Jun 07 21:17:59 2009 +0200 +++ b/micropython/__init__.py Sun Jun 07 22:55:59 2009 +0200 @@ -211,7 +211,6 @@ # Position the objects. pos = 0 - current_function = None for item in self.code: @@ -221,14 +220,6 @@ item.location = pos pos += len(item.code) - # Set code body information on functions, assuming that the - # first block is for argument checks. - - if current_function is not None: - current_function.code_body_location = pos - - current_function = None - # Other multi-location objects. elif isinstance(item, ( @@ -269,14 +260,8 @@ else: item.code_location = pos - current_function = item - elif isinstance(item, micropython.data.Const): pos += len(item.raw_data()) - current_function = None - - else: - current_function = None else: pos += 1 @@ -323,6 +308,15 @@ else: self.raw_code.append(item) + # Set the code body location for items now that the code blocks have + # been positioned. + + if isinstance(item, (micropython.data.Class, micropython.data.Function)): + if not with_builtins and item.module.name == "__builtins__" and item.astnode.doc is None: + item.code_body_location = item.full_name() + else: + item.code_body_location = item.get_body_block().location + # Fix the module locations. for module in self.importer.modules_ordered: diff -r db8334eb671f -r c93dcf98ca1b micropython/ast.py --- a/micropython/ast.py Sun Jun 07 21:17:59 2009 +0200 +++ b/micropython/ast.py Sun Jun 07 22:55:59 2009 +0200 @@ -757,7 +757,10 @@ self._doCallFunc(temp_target, target) self._endCallFunc(temp_target, target, temp_context) - temp_iterator = self.optimiser.optimise_temp_storage() + # Use a long-lasting temporary storage slot, since any result from the + # __iter__ method will not remain around for long. + + temp_iterator = self.get_temp() # In the loop... @@ -806,7 +809,6 @@ # After the handler, clear the exception. self.set_block(end_handler_block) - self.new_op(ClearException()) # Assign to the target. @@ -826,13 +828,19 @@ # Produce the "else" section. if node.else_ is not None: - self.set_block(exit_block) - + self.set_block(else_block) + self.new_op(ClearException()) self.dispatch(node.else_) - # After the loop... + # After the loop... + + self.set_block(exit_block) - self.set_block(exit_block) + else: + # After the loop... + + self.set_block(exit_block) + self.new_op(ClearException()) # Compilation duties... diff -r db8334eb671f -r c93dcf98ca1b micropython/data.py --- a/micropython/data.py Sun Jun 07 21:17:59 2009 +0200 +++ b/micropython/data.py Sun Jun 07 22:55:59 2009 +0200 @@ -556,6 +556,8 @@ self.location = None self.code_location = None + self.code_body_location = None # corresponds to the instantiator + self.instantiator = None self.instance_template_location = None # for creating instances at run-time @@ -967,6 +969,7 @@ self.location = None self.code_location = None + self.code_body_location = None # Program-related details. diff -r db8334eb671f -r c93dcf98ca1b micropython/rsvp.py --- a/micropython/rsvp.py Sun Jun 07 21:17:59 2009 +0200 +++ b/micropython/rsvp.py Sun Jun 07 22:55:59 2009 +0200 @@ -136,7 +136,7 @@ return "%s(%r) # %r" % (self.__class__.__name__, self.get_operand(), name(self.attr)) def get_operand(self): - return self.attr.get_body_block().location + return self.attr.code_body_location Target = TargetInstruction diff -r db8334eb671f -r c93dcf98ca1b rsvp.py --- a/rsvp.py Sun Jun 07 21:17:59 2009 +0200 +++ b/rsvp.py Sun Jun 07 22:55:59 2009 +0200 @@ -875,9 +875,15 @@ def builtins_int_lt(self): return self.builtins_int_op(operator.lt, 0) + def builtins_int_le(self): + return self.builtins_int_op(operator.le, 0) + def builtins_int_gt(self): return self.builtins_int_op(operator.gt, 0) + def builtins_int_ge(self): + return self.builtins_int_op(operator.ge, 0) + def builtins_int_eq(self): return self.builtins_int_op(operator.eq, 0) @@ -936,10 +942,13 @@ native_functions = { "__builtins__.int.__add__" : builtins_int_add, "__builtins__.int.__radd__" : builtins_int_add, # NOTE: To be made distinct. + "__builtins__.int.__iadd__" : builtins_int_add, "__builtins__.int.__bool__" : builtins_int_bool, "__builtins__.int.__neg__" : builtins_int_neg, "__builtins__.int.__lt__" : builtins_int_lt, + "__builtins__.int.__le__" : builtins_int_le, "__builtins__.int.__gt__" : builtins_int_gt, + "__builtins__.int.__ge__" : builtins_int_ge, "__builtins__.int.__eq__" : builtins_int_eq, "__builtins__.int.__ne__" : builtins_int_ne, "__builtins__.bool.__bool__" : builtins_bool_bool,