rev 504 - in trunk: docs/tutorial pr/tutorial

SVN User <[email protected]> Tue, 18 May 2004 19:19:02 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-05-18 19:18:58 -0400 (Tue, 18 May 2004)
New Revision: 504

Added:
   trunk/docs/tutorial/tutorial10.htm
   trunk/pr/tutorial/tut13.pr
   trunk/pr/tutorial/tut14.pr
   trunk/pr/tutorial/tut15.pr
Modified:
   trunk/docs/tutorial/index.htm
   trunk/docs/tutorial/outline2.htm
   trunk/docs/tutorial/tutorial9.htm
Log:
added scope chains and Outer keyword to tutorial

Modified: trunk/docs/tutorial/index.htm
===================================================================
--- trunk/docs/tutorial/index.htm	2004-05-18 20:40:38 UTC (rev 503)
+++ trunk/docs/tutorial/index.htm	2004-05-18 23:18:58 UTC (rev 504)
@@ -33,17 +33,17 @@
     </tr>
   </table>
   <br>
-  <table width="630" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffee" height="1094">
+  <table width="630" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffee">
     <tr> 
-      <td height="1649" valign="top">
+      <td valign="top"> 
         <p>&nbsp;</p>
         <p align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="+2">Prothon 
           Tutorial </font></b></p>
         <p align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif">Version 
           0.1, Build 500</font></b></p>
-        <p align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif">Mark 
-          Hahn</font></b></p>
         <p align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif">5/18/2004</font></b></p>
+        <p align="center"><b><font face="Verdana, Arial, Helvetica, sans-serif">by 
+          Mark Hahn</font></b></p>
         <p>This tutorial is an attempt to present all documentation about the Prothon 
           language and its implementation in a tutorial format. This is an unusual 
           way to present complete documentation on a language, but Prothon is 
@@ -56,10 +56,14 @@
           you are running. </p>
         <p>Even parts of the Prothon language as fundamental as keywords and syntax 
           are subject to change until approximately July of 2004.</p>
-        <p>Known problems in tutorial: </p>
-        <p>List comprehensions are missing. Local variable scope searching, &quot;outer&quot; 
-          and &quot;caller&quot; keywords are missing.</p>
-        <p>This tutorial is copyright Mark Hahn, 2004, all rights reserved. </p>
+        <p>Like Prothon itself, the tutorial is not finished. The following topics 
+          are not covered yet: packages, import, modules, list comprehensions, 
+          &quot;caller&quot; 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>
+        <p align="center"><font size="-1">This tutorial is copyright Mark Hahn, 
+          2004, all rights reserved. </font></p>
+        <p>&nbsp;</p>
       </td>
     </tr>
   </table>
Modified: trunk/docs/tutorial/outline2.htm
===================================================================
--- trunk/docs/tutorial/outline2.htm	2004-05-18 20:40:38 UTC (rev 503)
+++ trunk/docs/tutorial/outline2.htm	2004-05-18 23:18:58 UTC (rev 504)
@@ -87,11 +87,32 @@
               <li><a href="tutorial9.htm#mulinh">6.7 Multiple Inheritance</a> 
                 <ul>
                   <li><a href="tutorial9.htm#miwarn">Dangers of Multiple Inheritance</a> 
-                  <li><a href="tutorial9.htm#miex">Example</a>
+                  <li><a href="tutorial9.htm#miex">Example</a> 
                 </ul>
             </ul>
+          <li>7. <a href="tutorial10.htm#vsm">Variable Scope &amp; Module Structure</a> 
+            <ul>
+              <li><a href="tutorial10.htm#lvasc">7.1 Local Var Access &amp; Scope 
+                Chain </a> 
+                <ul>
+                  <li><a href="tutorial10.htm#locscobj">Local Scope Object</a>
+                  <li><a href="tutorial10.htm#lschain">Local Scope Chain</a>
+                  <li><a href="tutorial10.htm#blcks">Blocks Create Scopes</a> 
+                  <li><a href="tutorial10.htm#noglobal">No Python Global</a> 
+                  <li><a href="tutorial10.htm#object">Chain Ends at Object</a>
+                </ul>
+              
+              <li><a href="tutorial10.htm#slvok">7.2 Setting Local Vars &amp; 
+                Outer Keyword</a> 
+                <ul>
+                  <li><a href="tutorial10.htm#lvasc">Assignment Sets Scope</a> 
+                  <li><a href="tutorial10.htm#outer">Outer Keyword</a> 
+                  <li><a href="tutorial10.htm#closure">Closures</a>
+                </ul>
+              
+            </ul>
         </ul>
-      </td>
+        </td>
     </tr>
   </table>
 
Added: trunk/docs/tutorial/tutorial10.htm
===================================================================
--- trunk/docs/tutorial/tutorial10.htm	2004-05-18 20:40:38 UTC (rev 503)
+++ trunk/docs/tutorial/tutorial10.htm	2004-05-18 23:18:58 UTC (rev 504)
@@ -0,0 +1,179 @@
+<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">&nbsp;<a href="http://prothon.org">Prothon 
+        Home</a></td>
+      <td width="194" valign="middle" align="center" height="25"><a href="tutorial9.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="3165" valign="top"> 
+        <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="vsm"></a>7.0 
+          Variable Scopes &amp; Module Structure</font></p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="lvasc"></a>7.1 
+          Local Var Access &amp; Scope Chain</font></p>
+        <p>As we learned is section 5, all variables are attributes of scope objects. 
+          In section 6 we concentrated on the &quot;self&quot; scope object in 
+          methods. We learned how attribute lookup in an object goes through a 
+          chain of prototype links. In a &quot;self&quot; scope object this is 
+          the whole story when looking up a &quot;self&quot; variable like &quot;self.width&quot;.. 
+        </p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="locscobj"></a></font>When 
+          a variable has no prefix like the &quot;self&quot; keyword, such as 
+          in a simple variable like &quot;x&quot;, the variable uses the &quot;local&quot; 
+          scope object. Attribute lookup in the &quot;local&quot; scope object 
+          is not as simple as in the &quot;self&quot; scope object.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="lschain"></a></font>Attribute 
+          lookup starts in the &quot;local&quot; scope object as usual, but when 
+          it fails to find the attribute, instead of going down the prototype 
+          chain, it goes down a different scope chain, the &quot;local scope chain&quot;. 
+          This is a chain of pointers to each of the surrounding &quot;local&quot; 
+          scopes in the source file. This is easier to show in an example than 
+          to describe in words:</p>
+        <pre># Prothon source file tut13.pr
+ 
+a = 1    # this is outermost scope-1
+ 
+with a:
+    b = 2    # This is second scope-2 inside scope-1
+    
+    def func():
+        c = 3    # this is third scope-3 inside scope-2
+        
+        print a, b, c
+        
+a.func() # this prints "1 2 3"</pre>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="blcks"></a></font>As 
+          you can see, a nested block from a &quot;object&quot;, &quot;with&quot;, 
+          or &quot;def&quot; statement block will create a new scope. Blocks from 
+          any other statements such as &quot;if&quot;, &quot;for&quot;, etc. will 
+          not create scopes. The way to remember this is that the &quot;object&quot;, 
+          &quot;with&quot;, and &quot;def&quot; blocks change the scope of the 
+          variables inside so they create new scope objects. </p>
+        <p>Inside func there is an unamed scope object that we will call &quot;scope-3&quot; 
+          here. It has a pointer to &quot;scope-2&quot; and &quot;scope-2&quot; 
+          has a pointer to &quot;scope-1&quot;. These pointers to local scopes 
+          make up the &quot;local scope chain&quot;. When an attribute such as 
+          &quot;a&quot; is refernced in func(), it will be looked up in &quot;scope-1&quot; 
+          and not found, then looked up in &quot;scope-2&quot; and then looked 
+          up in &quot;scope-1&quot; and finally found.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="noglobal"></a></font>In 
+          the language Python, which Prothon is heavily based on, &quot;scope-1&quot; 
+          is called &quot;global&quot;. Prothon has no such thing as global. Python's 
+          &quot;global&quot; is just the last scope in the &quot;local scope chain&quot; 
+          in Prothon.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="object"></a></font>What 
+          happens when it isn't found in scope-1? In that case the search continues 
+          in the prototype chain of scope-1, which eventually ends up at Object, 
+          just like the &quot;self&quot; search. This means that all searches 
+          end up at Object. This is important because built-in functions and methods 
+          like Len(str) and Cmp(a,b) are stored in Object.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="slvok"></a>7.2 
+          Setting Local Vars &amp; Outer Keyword</font></p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="lvasc"></a></font>The 
+          last section concerned read access only. In other words access on the 
+          right side of assignment statements. If you noticed, in the tut13 example, 
+          each of the variable's (a, b, or c) scope locations was determined by 
+          where it was set on the left side on an assignment statement. If you 
+          wanted to change the value of scop-1's variable &quot;a&quot; from inside 
+          of func(), and you just used the statement &quot;a = 9&quot;, you would 
+          actually create a new local variable &quot;a&quot; inside of scope-3 
+          in func(), not change &quot;a&quot; in scope-1.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="outer"></a></font>In 
+          order to solve this problem, Prothon has a special keyword named &quot;outer&quot; 
+          that when prefixed to a variable name, tells the interpreter to skip 
+          over the first local scope when looking for that attribute and also 
+          to only use existing attributes when assigning values, never create 
+          new attributes. Here is an example of it's usage:</p>
+        <pre>
+# Prothon source file tut14.pr
+ 
+a = 1    # this is outermost scope-1
+ 
+with a:
+    b = 2    # This is second scope-2 inside scope-1
+    
+    def func():
+        a = 11
+        b = 22
+        c = 33    # this is third scope-3 inside scope-2
+        print a, b, c       
+        
+        print outer.a, outer.b    # accessing outer variables
+        
+        outer.a = 99              # modifying outer variables
+
+        
+a.func()
+print a</pre>
+        <p>This is the output:</p>
+        <pre>11 22 33 
+1 2 
+99</pre>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="closure"></a></font>Outer 
+          can also be used to access the local scope objects of functions that 
+          are not even running any more. This feature is called a &quot;closure&quot; 
+          and requires no special syntax in Prothon. Accessing a closure variable 
+          doesn't even require using the &quot;outer&quot; keyword.</p>
+        <pre># Prothon source file tut15.pr
+ 
+def getFunc():
+    counter = 0
+    
+    def func():
+        outer.counter += 1
+        print counter      # outer not needed for access
+        
+    return func
+ 
+func = getFunc()
+
+# func has closure with local scope object from getFunc
+
+func()    # prints 1
+func()    # prints 2
+func()    # prints 3
+func()    # prints 4</pre>
+      </td>
+    </tr>
+  </table>
+  <table width="630" border="0" cellpadding="0" cellspacing="0">
+    <tr> 
+      <td width="114" height="25" valign="top">&nbsp;<a href="http://prothon.org">Prothon 
+        Home</a></td>
+      <td width="194" valign="middle" align="center" height="25"><a href="tutorial9.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>
Modified: trunk/docs/tutorial/tutorial9.htm
===================================================================
--- trunk/docs/tutorial/tutorial9.htm	2004-05-18 20:40:38 UTC (rev 503)
+++ trunk/docs/tutorial/tutorial9.htm	2004-05-18 23:18:58 UTC (rev 504)
@@ -25,7 +25,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="tutorial10.htm">Next 
         Page</a></td>
     </tr>
   </table>
@@ -237,7 +237,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="tutorial10.htm">Next 
         Page</a></td>
     </tr>
   </table>
Added: trunk/pr/tutorial/tut13.pr
===================================================================
--- trunk/pr/tutorial/tut13.pr	2004-05-18 20:40:38 UTC (rev 503)
+++ trunk/pr/tutorial/tut13.pr	2004-05-18 23:18:58 UTC (rev 504)
@@ -0,0 +1,14 @@
+
+# Prothon source file tut13.pr
+ 
+a = 1    # this is outermost scope-1
+ 
+with a:
+    b = 2    # This is second scope-2 inside scope-1
+    
+    def func():
+        c = 3    # this is third scope-3 inside scope-2
+        
+        print a, b, c
+        
+a.func() # this prints "1 2 3"
\ No newline at end of file

Added: trunk/pr/tutorial/tut14.pr
===================================================================
--- trunk/pr/tutorial/tut14.pr	2004-05-18 20:40:38 UTC (rev 503)
+++ trunk/pr/tutorial/tut14.pr	2004-05-18 23:18:58 UTC (rev 504)
@@ -0,0 +1,21 @@
+
+# Prothon source file tut14.pr
+ 
+a = 1    # this is outermost scope-1
+ 
+with a:
+    b = 2    # This is second scope-2 inside scope-1
+    
+    def func():
+        a = 11
+        b = 22
+        c = 33    # this is third scope-3 inside scope-2
+        print a, b, c       
+        
+        print outer.a, outer.b    # accessing outer variables
+        
+        outer.a = 99              # modifying outer variables
+
+        
+a.func()
+print a
\ No newline at end of file

Added: trunk/pr/tutorial/tut15.pr
===================================================================
--- trunk/pr/tutorial/tut15.pr	2004-05-18 20:40:38 UTC (rev 503)
+++ trunk/pr/tutorial/tut15.pr	2004-05-18 23:18:58 UTC (rev 504)
@@ -0,0 +1,20 @@
+
+# Prothon source file tut15.pr
+ 
+def getFunc():
+    counter = 0
+    
+    def func():
+        outer.counter += 1
+        print counter      # outer not needed for access
+        
+    return func
+ 
+func = getFunc()
+
+# func has closure with local scope object from getFunc
+
+func()    # prints 1
+func()    # prints 2
+func()    # prints 3
+func()    # prints 4