# HG changeset patch # User Paul Boddie # Date 1244402279 -7200 # Node ID db8334eb671f795d40455ca2d49f9d73ae574c9e # Parent 424eacd023949bf57af80772eb5577b8fb4c7087 Added generation of some built-in classes and functions where docstrings are provided. Added an implementation of the xrange class. Added a test of xrange and modified existing tests. Fixed augmented assignment code generation. diff -r 424eacd02394 -r db8334eb671f lib/builtins.py --- a/lib/builtins.py Sun Jun 07 20:04:16 2009 +0200 +++ b/lib/builtins.py Sun Jun 07 21:17:59 2009 +0200 @@ -207,9 +207,39 @@ pass class xrange(object): - def __init__(self, start_or_end, end=None, step=1): pass - def __iter__(self): pass - def next(self): pass + + "Implementation of xrange." + + def __init__(self, start_or_end, end=None, step=1): + + "Initialise the xrange with the given 'start_or_end', 'end' and 'step'." + + if end is None: + self.start = 0 + self.end = start_or_end + else: + self.start = start_or_end + self.end = end + + self.step = step + self.current = self.start + + def __iter__(self): + + "Return an iterator, currently self." + + return self + + def next(self): + + "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 + + current = self.current + self.current += self.step + return current # Exceptions and warnings. diff -r 424eacd02394 -r db8334eb671f micropython/__init__.py --- a/micropython/__init__.py Sun Jun 07 20:04:16 2009 +0200 +++ b/micropython/__init__.py Sun Jun 07 21:17:59 2009 +0200 @@ -144,7 +144,7 @@ # Omit built-in function code where requested. - if suppress_builtins: + if suppress_builtins and not obj.astnode.doc is not None: continue # Generate the instantiator/initialiser. @@ -175,7 +175,7 @@ # Omit built-in function code where requested. - if suppress_builtins: + if suppress_builtins and not obj.astnode.doc is not None: pass # Append the function code to the image. @@ -256,7 +256,7 @@ # Set the code location only where the code has been # generated. - if not with_builtins and item.module.name == "__builtins__": + if not with_builtins and item.module.name == "__builtins__" and item.astnode.doc is None: item.code_location = item.full_name() # Skip any defaults for named functions. @@ -298,7 +298,8 @@ elif isinstance(item, micropython.data.Class): assert item.instance_template_location == len(self.raw_code) - self.raw_code += item.as_raw(objtable, paramtable, with_builtins or item.module.name != "__builtins__") + self.raw_code += item.as_raw(objtable, paramtable, + with_builtins or item.module.name != "__builtins__" or item.astnode.doc is not None) assert item.location == len(self.raw_code) - 1 elif isinstance(item, micropython.data.Const): @@ -311,7 +312,7 @@ # Check the code location only where the code has been generated. - assert (not with_builtins and item.module.name == "__builtins__") or \ + assert (not with_builtins and item.module.name == "__builtins__" and item.astnode.doc is None) or \ item.name is not None and item.code_location == len(self.raw_code) + len(item.defaults) or \ item.name is None and item.code_location == len(self.raw_code) diff -r 424eacd02394 -r db8334eb671f micropython/ast.py --- a/micropython/ast.py Sun Jun 07 20:04:16 2009 +0200 +++ b/micropython/ast.py Sun Jun 07 21:17:59 2009 +0200 @@ -518,7 +518,7 @@ # Find the augmented assignment method and attempt to use it. aug_method, (left_method, right_method) = augassign_methods[node.op] - temp_out = self._generateOpMethod(node, temp1, temp2, aug_method, use_binary_block, end_block) + temp_out = self._generateOpMethod(node, temp1, temp2, aug_method, use_binary_block, use_binary_block, end_block) self.discard_temp(temp_out) # NOTE: Will re-use the same storage. # Where no such method exists, use the binary operator methods. diff -r 424eacd02394 -r db8334eb671f tests/cond_if_else.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/cond_if_else.py Sun Jun 07 21:17:59 2009 +0200 @@ -0,0 +1,12 @@ +#!/usr/bin/env python + +a = 0 + +if a: + b = 1 +else: + b = 2 + +result_2 = b + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r 424eacd02394 -r db8334eb671f tests/for.py --- a/tests/for.py Sun Jun 07 20:04:16 2009 +0200 +++ b/tests/for.py Sun Jun 07 21:17:59 2009 +0200 @@ -1,7 +1,9 @@ #!/usr/bin/env python -for i in range(0, 100, 10): - for j in range(0, 10): +for i in xrange(0, 100, 10): + for j in xrange(0, 10): k = i + j +result_99 = k + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 424eacd02394 -r db8334eb671f tests/xrange.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/xrange.py Sun Jun 07 21:17:59 2009 +0200 @@ -0,0 +1,8 @@ +#!/usr/bin/env python + +xr = xrange(0, 10, 1) +result_0 = xr.start +result_10 = xr.end +result_1 = xr.step + +# vim: tabstop=4 expandtab shiftwidth=4