rev 506 - in trunk: docs/tutorial pr/tutorial
SVN User <[email protected]> Wed, 19 May 2004 00:31:51 -0400
| Newsgroups | gmane.comp.lang.prothon.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: mark
Date: 2004-05-19 00:31:48 -0400 (Wed, 19 May 2004)
New Revision: 506
Added:
trunk/docs/tutorial/tutorial11.htm
trunk/pr/tutorial/tut16.pr
trunk/pr/tutorial/tut17.pr
Modified:
trunk/docs/tutorial/index.htm
trunk/docs/tutorial/tutorial10.htm
Log:
added generator to tutorial
Modified: trunk/docs/tutorial/index.htm
===================================================================
--- trunk/docs/tutorial/index.htm 2004-05-19 00:30:33 UTC (rev 505)
+++ trunk/docs/tutorial/index.htm 2004-05-19 04:31:48 UTC (rev 506)
@@ -57,10 +57,10 @@
<p>Even parts of the Prothon language as fundamental as keywords and syntax
are subject to change until approximately July of 2004.</p>
<p>Like Prothon itself, the tutorial is not finished. The following topics
- are not covered yet: packages, import, modules, list comprehensions,
- "caller" keyword, security system, add-on modules, files.
- Email <a href="mailto:[email protected]">me</a> about any things I've
- not covered that aren't on this list.</p>
+ are not covered yet: print statement, import, modules, packages, list
+ comprehensions, gen keyword, exceptions, security system, files, console
+ line editing. Email <a href="mailto:[email protected]">me</a> about any
+ things I've not covered that aren't on this list.</p>
<p align="center"><font size="-1">This tutorial is copyright Mark Hahn,
2004, all rights reserved. </font></p>
<p> </p>
Modified: trunk/docs/tutorial/tutorial10.htm
===================================================================
--- trunk/docs/tutorial/tutorial10.htm 2004-05-19 00:30:33 UTC (rev 505)
+++ trunk/docs/tutorial/tutorial10.htm 2004-05-19 04:31:48 UTC (rev 506)
@@ -25,16 +25,16 @@
Page</a></td>
<td width="183" valign="middle" align="center" height="25"><a href="outline2.htm">Tutorial
Outline</a></td>
- <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next
+ <td width="139" valign="middle" align="center" height="25"><a href="tutorial11.htm">Next
Page</a></td>
</tr>
</table>
<br>
<table width="629" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffee" height="1707">
<tr>
- <td height="3165" valign="top">
+ <td height="2569" valign="top">
<p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="vsm"></a>7.0
- Local Scopes & Module Structure</font></p>
+ Local Scopes</font></p>
<p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="lvasc"></a>7.1
Local Var Access & Scope Chain</font></p>
<p>As we learned is section 5, all variables are attributes of scope objects.
@@ -208,7 +208,7 @@
Page</a></td>
<td width="183" valign="middle" align="center" height="25"><a href="outline2.htm">Tutorial
Outline</a></td>
- <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next
+ <td width="139" valign="middle" align="center" height="25"><a href="tutorial11.htm">Next
Page</a></td>
</tr>
</table>
Added: trunk/docs/tutorial/tutorial11.htm
===================================================================
--- trunk/docs/tutorial/tutorial11.htm 2004-05-19 00:30:33 UTC (rev 505)
+++ trunk/docs/tutorial/tutorial11.htm 2004-05-19 04:31:48 UTC (rev 506)
@@ -0,0 +1,146 @@
+<html>
+<head>
+<title>Prothon Tutorial - 6.0</title>
+<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
+</head>
+
+<body bgcolor="#FFFFFF" text="#000000">
+<div align="center">
+ <table width="630" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFCC">
+ <tr>
+ <td height="121" width="109"><img src="../images/logo_left.jpg" width="109" height="135"></td>
+ <td valign="center" height="43">
+ <div align="center"><font size="5" face="Verdana, Arial, Helvetica, sans-serif">Prothon
+ Tutorial</font></div>
+ </td>
+ <td valign="center" height="121" width="106"><img src="../images/logo_right.jpg" width="109" height="135"></td>
+ </tr>
+ </table>
+ <br>
+ <table width="630" border="0" cellpadding="0" cellspacing="0">
+ <tr>
+ <td width="114" height="25" valign="top"> <a href="http://prothon.org">Prothon
+ Home</a></td>
+ <td width="194" valign="middle" align="center" height="25"><a href="tutorial10.htm">Previous
+ Page</a></td>
+ <td width="183" valign="middle" align="center" height="25"><a href="outline2.htm">Tutorial
+ Outline</a></td>
+ <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next
+ Page</a></td>
+ </tr>
+ </table>
+ <br>
+ <table width="629" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffee" height="1707">
+ <tr>
+ <td height="2569" valign="top">
+ <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="vsm"></a>8.0
+ Generators & Exceptions</font></p>
+ <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="lvasc"></a>8.1
+ Generator & Gen Keyword</font></p>
+ <p>In section <a href="tutorial5.htm#forstmt">4.2</a> we learned that
+ the "for" statement uses iterators to generate the sequence
+ of values for each loop, and that an iterator is simply a function that
+ responds to repeated calls of a method "next()" by returning
+ values until it signals the end of the sequence with an exception. </p>
+ <p>Prothon has a "gen" keyword to make a "gen" statement
+ that looks identical to the "def" statement with "def"
+ replaced by "gen": "gen name(params): body". So
+ "gen" defines a function-like object with a name, formal parameters,
+ and a code body. The difference is that the object created is always
+ an iterator, not a simple function. You can think of the word "generator"
+ as meaning that it "generates" an iterator, or that the iterator
+ it produces "generates" values. It works either way. For this
+ discussion we will use the second meaning, that it generates values
+ when it runs.</p>
+ <p>The generator has to use another keyword, "yield" in order
+ to do the actual generation of values. Yield is somewhat like the "return"
+ keyword in that it has an argument that is used as a return value when
+ "yield" is executed, but unlike "return" the function
+ execution is not terminated. Instead, the state of the function is stored
+ away, or "frozen" if you want to think of it that way so that
+ the function can resume execution at the statement after the yield later
+ when another value is needed.</p>
+ <p>So in it's simplest form, the "gen" is like a function that
+ runs until it hits a "yield" and then returns the "yield"
+ value. Then it is paused until "next()" is called on the iterator
+ again and then the function resumes execution until it hits the next
+ yield, returns the second value, pauses again, etc. This continues until
+ the function hits a return or runs off the end of the code block at
+ which time it signals that it is done.</p>
+ <pre># Prothon source file tut16.pr
+
+gen odds(max):
+ num = 1
+ while num < max:
+ yield num
+ num += 2
+
+for i in odds(10):
+ print i, # prints 1 3 5 7 9</pre>
+ <p>Prothon also allows you to call other functions with yields in them
+ to build up more complex code structure. When doing so, only the one
+ outermost "function" should use the "gen" keyword,
+ because it is the one producing an iterator, not being called. The others
+ are being called as normal. </p>
+ <pre># Prothon source file tut17.pr
+
+gen evenOdds(max):
+ def evens(max):
+ num = 0
+ while num < max:
+ yield num
+ num += 2
+
+ def odds(max):
+ num = 1
+ while num < max:
+ yield num
+ num += 2
+
+ evens(max)
+ odds(max)
+
+for i in evenOdds(10):
+ print i, # prints 0 2 4 6 8 1 3 5 7 9</pre>
+ <p>In order to explain how generators and the yield statement works, I
+ need to explain a bit about function calling in Prothon. Prothon is
+ "stackless". What this means is that the interpreter maintains
+ a data structure that holds the stack of Prothon objects, stack pointer,
+ code data, program counter, and various scope objects for each "execution
+ frame" totally seperate from any operating system stack. An execution
+ frame is the data needed by each scope of running code. Whenever a function
+ call happens, a new "execution frame" is created and put on
+ the list of running frames. When a function returns, that frame is usually
+ destroyed after the return value is copied from it.</p>
+ <p>When a yield statement is executed, things are done a bit differently.
+ A reference to the current execution frame is stored in the generator
+ object instead of being destroyed. If it was a normal function defined
+ by a "def" keyword, then the next one is stored. This is repeated
+ until one is stored that was created by a "gen" keyword instead
+ of a "def" keyword. At this point all the frames are stored
+ away in the generator object and the "yield" value is returned
+ via the "next()" call. </p>
+ <p>When the next "next()" call comes in, the generator object
+ (the iterator) takes the execution frames it has saved away and puts
+ them all back on the active frames list and restarts the code where
+ it left off. This sounds like it might be slow but in actuality it only
+ involves moving a reference pointer for each function and is very fast.</p>
+ <p> </p>
+ </td>
+ </tr>
+ </table>
+ <table width="630" border="0" cellpadding="0" cellspacing="0">
+ <tr>
+ <td width="114" height="25" valign="top"> <a href="http://prothon.org">Prothon
+ Home</a></td>
+ <td width="194" valign="middle" align="center" height="25"><a href="tutorial10.htm">Previous
+ Page</a></td>
+ <td width="183" valign="middle" align="center" height="25"><a href="outline2.htm">Tutorial
+ Outline</a></td>
+ <td width="139" valign="middle" align="center" height="25"><a href="index.htm">Next
+ Page</a></td>
+ </tr>
+ </table>
+</div>
+</body>
+</html>
Added: trunk/pr/tutorial/tut16.pr
===================================================================
--- trunk/pr/tutorial/tut16.pr 2004-05-19 00:30:33 UTC (rev 505)
+++ trunk/pr/tutorial/tut16.pr 2004-05-19 04:31:48 UTC (rev 506)
@@ -0,0 +1,11 @@
+
+# Prothon source file tut16.pr
+
+gen odds(max):
+ num = 1
+ while num < max:
+ yield num
+ num += 2
+
+for i in odds(10):
+ print i, # prints 1 3 5 7 9
Added: trunk/pr/tutorial/tut17.pr
===================================================================
--- trunk/pr/tutorial/tut17.pr 2004-05-19 00:30:33 UTC (rev 505)
+++ trunk/pr/tutorial/tut17.pr 2004-05-19 04:31:48 UTC (rev 506)
@@ -0,0 +1,21 @@
+
+# Prothon source file tut17.pr
+
+gen evenOdds(max):
+ def evens(max):
+ num = 0
+ while num < max:
+ yield num
+ num += 2
+
+ def odds(max):
+ num = 1
+ while num < max:
+ yield num
+ num += 2
+
+ evens(max)
+ odds(max)
+
+for i in evenOdds(10):
+ print i, # prints 0 2 4 6 8 1 3 5 7 9