rev 491 - in trunk: . docs/tutorial pr/tutorial

SVN User <[email protected]> Sun, 16 May 2004 13:59:05 -0400
Newsgroups gmane.comp.lang.prothon.cvs
Message-ID <[email protected]>
Author: mark
Date: 2004-05-16 13:59:02 -0400 (Sun, 16 May 2004)
New Revision: 491

Added:
   trunk/docs/tutorial/tutorial7.htm
   trunk/pr/tutorial/tut10.pr
Modified:
   trunk/STATUS.txt
   trunk/docs/tutorial/index.htm
   trunk/docs/tutorial/tutorial4.htm
   trunk/docs/tutorial/tutorial5.htm
   trunk/docs/tutorial/tutorial6.htm
Log:
added expressions

Modified: trunk/STATUS.txt
===================================================================
--- trunk/STATUS.txt	2004-05-15 05:44:17 UTC (rev 490)
+++ trunk/STATUS.txt	2004-05-16 17:59:02 UTC (rev 491)
@@ -1,6 +1,8 @@
 
 ----------------------- TO-DO (highest priority first) ------------------------
 
+--- check dict keys for mutability bit
+
 --- nesting  /* .. */ comments 
 
 --- Add properties methods support (__get__,  __set__, __delete__)  rename protoList

Modified: trunk/docs/tutorial/index.htm
===================================================================
--- trunk/docs/tutorial/index.htm	2004-05-15 05:44:17 UTC (rev 490)
+++ trunk/docs/tutorial/index.htm	2004-05-16 17:59:02 UTC (rev 491)
@@ -158,16 +158,16 @@
                 <ul>
                   <li><a href="tutorial6.htm#attrkey">Attribute Keys</a> 
                   <li><a href="tutorial6.htm#symbol">Symbols</a> 
-                  <li><a href="tutorial6.htm#period">Period Operator</a>
+                  <li><a href="tutorial6.htm#period">Period Operator</a> 
                   <li><a href="tutorial6.htm#attrass">Attribute Assignment &amp; 
                     Del </a> 
                 </ul>
               <li><a href="tutorial6.htm#vsw">5.2 Variables, Scope, &amp; With 
                 </a> 
                 <ul>
-                  <li><a href="tutorial6.htm#main">Main Module</a>
+                  <li><a href="tutorial6.htm#main">Main Module</a> 
                   <li><a href="tutorial6.htm#attr">Variable Scope</a> 
-                  <li><a href="tutorial6.htm#with">With Statement</a>
+                  <li><a href="tutorial6.htm#with">With Statement</a> 
                 </ul>
               <li><a href="tutorial6.htm#funcdef">5.3 Functions &amp; Def Statement</a> 
                 <ul>
@@ -177,17 +177,27 @@
                   <li><a href="tutorial6.htm#pos">Positional Params</a> 
                   <li><a href="tutorial6.htm#defval">Default Values</a> 
                   <li><a href="tutorial6.htm#namematch">Keyword Params</a> 
-                  <li><a href="tutorial6.htm#starvar">Variable Parameters</a>
+                  <li><a href="tutorial6.htm#starvar">Variable Parameters</a> 
                   <li><a href="tutorial6.htm#actstarvar">Variable Arguments</a> 
                   <li><a href="tutorial6.htm#sampleparms">Sample Parameter Passing</a> 
                   <li><a href="tutorial6.htm#localscope">Local Scope</a> 
                   <li><a href="tutorial6.htm#passref">Pass By Reference</a> 
                   <li><a href="tutorial6.htm#noneobj">None Object</a> 
-                  <li><a href="tutorial6.htm#rtrnstmt">Return Statement</a>
+                  <li><a href="tutorial6.htm#rtrnstmt">Return Statement</a> 
                 </ul>
-              
-            </ul>
-        </ul>
+              <li><a href="tutorial7.htm#expr">5.4 Expressions</a>
+                <ul>
+                  <li><a href="tutorial7.htm#exprfunc">Operators = Functions</a> 
+                  <li><a href="tutorial7.htm#numfunc">Numeric Functions</a> 
+                  <li><a href="tutorial7.htm#numdp">Numbers Ending in Period</a> 
+                  <li><a href="tutorial7.htm#underbar">Trailing Underbar Symbols</a> 
+                  <li><a href="tutorial7.htm#oplist">Operator List</a> 
+                  <li><a href="tutorial7.htm#samplexpr">Sample Expressions</a> 
+                  <li><a href="tutorial7.htm#opchain">Chained Comparisons</a> 
+                  <li><a href="tutorial7.htm#shortckt">Short-Circuit Expr Eval</a> 
+                  <li><a href="tutorial7.htm#andorres">And-Or Result Objects</a>
+                </ul>
+        </ul></ul>
       </td>
     </tr>
   </table>
Modified: trunk/docs/tutorial/tutorial4.htm
===================================================================
--- trunk/docs/tutorial/tutorial4.htm	2004-05-15 05:44:17 UTC (rev 490)
+++ trunk/docs/tutorial/tutorial4.htm	2004-05-16 17:59:02 UTC (rev 491)
@@ -1,6 +1,6 @@
 <html>
 <head>
-<title>Prothon Tutorial - 2.7</title>
+<title>Prothon Tutorial - 3.4</title>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 </head>
 
Modified: trunk/docs/tutorial/tutorial5.htm
===================================================================
--- trunk/docs/tutorial/tutorial5.htm	2004-05-15 05:44:17 UTC (rev 490)
+++ trunk/docs/tutorial/tutorial5.htm	2004-05-16 17:59:02 UTC (rev 491)
@@ -1,6 +1,6 @@
 <html>
 <head>
-<title>Prothon Tutorial - 2.7</title>
+<title>Prothon Tutorial - 4.0</title>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 </head>
 
@@ -57,21 +57,22 @@
           that always returns only True or False, then you should end the name 
           in a question mark also.</p>
         <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="bool"></a></font>There 
-          is one very special function called obj.bool_?() that can be called 
-          on any object of any type and that type decides whether that object 
-          should be considered True or False. For example, the numeric types consider 
-          the number zero to be false and all other numbers to be True (just like 
-          the C language). Likewise, lists and strings are considered False is 
-          they are empty and True otherwise. Each type can conveniently define 
-          what is True or False for it's type.</p>
+          is one very special &quot;question&quot; function called obj.bool_?() 
+          that can be called on any object of any type and that type decides whether 
+          that object should be considered True or False. For example, the numeric 
+          types consider the number zero to be False and all other numbers to 
+          be True (just like the C language). Likewise, lists and strings are 
+          considered False is they are empty and True otherwise. Each type can 
+          conveniently define what is True or False for it's objects.</p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="smplcode"></a></font>Let's 
           look at how you use the if-elif-else statement. This example shows the 
           different parts of the statement. The &quot;if&quot; expression is always 
-          tested. If its expression returns true then its block is executed and 
+          tested. If its expression returns True then its block is executed and 
           all the other parts are skipped over. The &quot;elif&quot; expression 
           is evaluated if all the expressions before return False and then its 
           block is executed if it returns True just like the &quot;if&quot;. The 
-          &quot;else&quot; block is executed if all expression before return false.</p>
+          &quot;else&quot; block is executed if all expressions before return 
+          False.</p>
         <pre># Prothon source file tut4.pr
 
 if True:
Modified: trunk/docs/tutorial/tutorial6.htm
===================================================================
--- trunk/docs/tutorial/tutorial6.htm	2004-05-15 05:44:17 UTC (rev 490)
+++ trunk/docs/tutorial/tutorial6.htm	2004-05-16 17:59:02 UTC (rev 491)
@@ -25,7 +25,7 @@
         Page</a></td>
       <td width="183" valign="middle" align="center" height="25"><a href="index.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="tutorial7.htm">Next 
         Page</a></td>
     </tr>
   </table>
@@ -90,18 +90,23 @@
           etc</p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="with"></a></font>Prothon 
           has a special &quot;with&quot; statement just to set the current scope 
-          object. This is convenient for when you are about to work on one object 
-          extensively and want to save typing and to concentrate on that object. 
-          Later you will see something &quot;with&quot; is very useful for. Here 
-          is an example for now:</p>
-        <pre>>>> x = [1]
-[1]
+          object. The &quot;with&quot; statement has the form &quot;with name: 
+          body&quot;. Name is the object that will temporarily become the scope 
+          object. Body is a code body just like those in an &quot;if&quot; or 
+          &quot;for&quot; statement, except that the body in a &quot;with&quot; 
+          statement always executes just once. It is not conditional or a loop.</p>
+        <p>The &quot;with&quot; statement is convenient for when you are about 
+          to work on one object extensively and want to save typing and to concentrate 
+          on that object. Later you will see something &quot;with&quot; is very 
+          useful for. Here is an example for now:</p>
+        <pre>>>> x = [1,2]     # x is an ordinary list object
+[1, 2]
 >>> with x:
-...    a=1
+...    a = 'T'    # add attribute a to object x
 ...
->>> print x.a
-1
->>></pre>
+>>> print x.a     # attr a is not related to list data [1,2]
+T                 # list is stored in binary data of x
+>>>               # not in the attributes of x</pre>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="funcdef"></a>5.3 
           Functions &amp; the Def Statement</font></p>
         <p>A function allows you to factor out code to reuse in multiple places 
@@ -118,10 +123,13 @@
           the variable that will hold the new function object being created, just 
           as if the name was on the left side of an assignment statement (name 
           = function). The body is the body of code that is executed just as in 
-          the &quot;if&quot;, &quot;for&quot;, and other statements. Params is 
+          the &quot;if&quot;, &quot;for&quot;, and other statements, except that 
+          this code is not executed until the function is called. When the &quot;def&quot; 
+          statement executes this block is just saved away for later. Params is 
           the list of variable names that the incoming parameters will be assigned 
           to when the function is first started. These are called the formal parameters 
-          as opposed to the actual parameters that are in the function call. </p>
+          as opposed to the actual parameters, or arguments, that are in the function 
+          call. </p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="funcform"></a></font>Function 
           calls look similar to the definition of a function. They have the form 
           &quot;name(params)&quot; where name is the variable holding the function 
@@ -140,37 +148,38 @@
           has some very sophisticated methods for matching the actual parameters 
           to the formal parameters. The simplest method is positional matching. 
           If func is defined as func(x,y,z) and is called as func(1,2,3) then 
-          x=1, y=2, and z=3 after the function starts. </p>
+          x = 1, y = 2, and z = 3 after the function starts. </p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="defval"></a></font>The 
           next param passing trick is the use of default values, also known as 
           optional params. If the formal param in the function definition has 
           the form &quot;name = value&quot;, then that param can be missing in 
           the actual params and the default value will be used instead. If the 
-          func is defined as func(x, y=11, z=12) and it is called as func(1,2) 
-          then x=1, y=2, and z=12 inside the function. Note that the formal params 
-          with defaults must follow the params without defaults to avoid ambiguities.</p>
+          func is defined as func(x, y = 11, z = 12) and it is called as func(1, 
+          2) then x = 1, y = 2, and z = 12 inside the function. Note that the 
+          formal params with defaults must follow the params without defaults 
+          to avoid ambiguities.</p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="namematch"></a></font>The 
           third param technique is to match the formal and actual params by keyword 
           instead of by position. To do this, you use the form &quot;name = value&quot; 
           in the actual param in the function call. This allows you to place the 
           actual params in any order and it makes the call self-documenting. If 
-          the func is defined as func(x,y,z) and it is called as func(y=1, z=2, 
-          x=3), then x=3, y=1, and z=2 inside the function.</p>
+          the func is defined as func(x, y, z) and it is called as func(y = 1, 
+          z = 2, x = 3), then x = 3, y = 1, and z = 2 inside the function.</p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="starvar"></a></font>Finally, 
           there is a very powerful technique that allows a variable number of 
           parameters to be passed to or accepted into a function. If a formal 
           parameter list ends in &quot;*var&quot; then all actual positional parameters 
-          &quot;left over&quot; that haven't been assigned to formal positional 
-          parameters are put into a list named var. Likewise, if the formal list 
-          ends with a &quot;**var&quot; then all the given actual named parameters 
+          &quot;left over&quot;, that haven't been assigned to formal positional 
+          parameters, are put into a list named var. Likewise, if the formal list 
+          ends with a &quot;**var&quot; then all the given actual keyword parameters 
           that don't match any parameters in the formal list are put into a dictionary 
           named var with the name as the key and the actual value as the value. 
-          If you put both &quot;*var1, **var2&quot; into the formal list then 
-          all paramaters are guaranteed to be captured. </p>
+          If you put both &quot;*var1&quot; and &quot;**var2&quot; into the formal 
+          list then all actual parameters are guaranteed to be captured. </p>
         <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="actstarvar"></a></font>In 
           the other direction, you can pass *var1 and **var2 as actual parameters 
           in a call where var is a list of actual positional parameters and var2 
-          is a dictionary of named actual parameters. This allows you to calculate 
+          is a dictionary of actual keyword parameters. This allows you to calculate 
           an arbitrary number of parameters to pass to a function. </p>
         <p>Note that combining these two directions allows you to accept every 
           possible set of parameters in a function by using the definition func1(*args, 
@@ -266,7 +275,7 @@
         Page</a></td>
       <td width="183" valign="middle" align="center" height="25"><a href="index.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="tutorial7.htm">Next 
         Page</a></td>
     </tr>
   </table>
Added: trunk/docs/tutorial/tutorial7.htm
===================================================================
--- trunk/docs/tutorial/tutorial7.htm	2004-05-15 05:44:17 UTC (rev 490)
+++ trunk/docs/tutorial/tutorial7.htm	2004-05-16 17:59:02 UTC (rev 491)
@@ -0,0 +1,155 @@
+<html>
+<head>
+<title>Prothon Tutorial - 5.4</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="tutorial6.htm">Previous 
+        Page</a></td>
+      <td width="183" valign="middle" align="center" height="25"><a href="index.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="630" border="0" cellpadding="0" cellspacing="0" bgcolor="#ffffee" height="1707">
+    <tr> 
+      <td height="3515" valign="top"> 
+        <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="expr"></a>5.4 
+          Expressions</font></p>
+        <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="exprfunc"></a></font>We 
+          have been using expressions all along, such as when we say 1+2, but 
+          how can we say &quot;everything is an object&quot; when an expression 
+          looks nothing like an object? The answer is that expressions are made 
+          up of operators like + which are actually functions in disguise and 
+          we now know that functions are objects. The Prothon interpreter (actually 
+          the parser) quickly changes the simple expression &quot;x = 1+2&quot; 
+          into the Prothon statement &quot;x = 1.add_(2)&quot; by replacing the 
+          addition operator + into &quot;.add_()&quot;.</p>
+        <p align="left"><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="numfunc"></a></font>And 
+          numbers (which are objects also of course) can have functions called 
+          on them:</p>
+        <pre>>>> 48.chr()   # chr() changes integer 48 into string &quot;0&quot;
+0
+>>> 1.add_(2)  # same as 1 + 2
+3
+>>></pre>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="numdp"></a></font>We 
+          now know why floating point numbers cannot end with a decimal point 
+          like &quot;1.&quot; and that you must add a zero so that it becomes 
+          &quot;1.0&quot;. If you ended it with the period it would get confused 
+          with the period that sperates numbers from function names.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="underbar"></a></font>You 
+          might wonder why the add_ function has the underbar symbol ( _ ) hanging 
+          precariously off the end of the name. This is a special convention in 
+          Prothon that means the function is for internal use and not normally 
+          to be seen. It also allows the Prothon programmer to use the function 
+          name &quot;add&quot; and not accidently replace the internal version, 
+          which might cause problems. This means that you should not create function 
+          names that end with an underbar, unless you know what you are doing. 
+          We will cover one or two special exceptions later.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="oplist"></a></font>Here 
+          are the Prothon operators:</p>
+        <pre>    +   *    These work on numbers, strings, and lists
+    -        Subtract (binary operator) or negative (unary operator)
+    /  //    The single / returns a float and // returns an integer
+    %  **    % is modulo, ** means "raise to the power of"
+    &lt;&lt; &gt;&gt;       Left shift and right shift
+    ~  &amp;  |  ^  Bitwise negate, and, or, &amp; exclusive or (xor)
+    &lt;  &lt;=  &gt;  &gt;=  !=  ==  Comparison
+    &quot;is&quot;  &quot;is not&quot;        Identity (exact same object)
+    &quot;in&quot;  &quot;not in&quot;        Membership (left is in right collection)
+    &quot;not&quot;  &quot;and&quot;  &quot;or&quot;    Boolean </pre>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="samplexpr"></a></font>Here 
+          are some sample expressions:</p>
+        <pre>>>> 12/5
+2.4
+>>> 12//5
+2
+>>> 12%5
+2
+>>> 3 < 4
+True
+>>> 3 is 3
+False
+>>> x = 3
+3
+>>> y = x
+3
+>>> x is y
+True
+>>> 3 in [1,2,3]
+True</pre>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="opchain"></a></font>Several 
+          of the operators have special behaviour worth noting. The comparison 
+          operators can be chained together in an intuitive way. &quot;1 &lt; 
+          2 &lt; 3&quot; would normally not work in the intuitive way because 
+          it would be interpreted as &quot;(1 &lt; 2) &lt; 3&quot; which would 
+          evaluate to &quot;True &lt; 3&quot; which would give an error. Prothon 
+          goes to the trouble to change &quot;1 &lt; 2 &lt; 3&quot; to the expression 
+          &quot;(1 &lt; 2) and (2 &lt; 3)&quot; which is the accurate intuitive 
+          meaning. Note that if you have &quot;f1() &gt; f2() &gt; f3()&quot; 
+          that f2() is called only once even though it is changed to &quot;(f1() 
+          &gt; f2()) and (f2() &gt; f3())&quot;.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="shortckt"></a></font>The 
+          boolean operators &quot;and&quot; and &quot;or&quot; have the special 
+          behaviour that only part of the expression is evaluated if not all of 
+          it is needed. For example, while evaluating the expression &quot;True 
+          or func()&quot; func() will not be called because the result of the 
+          &quot;or&quot; is already known when the True is seen on the left side. 
+          This is sometimes called &quot;short-circuiting&quot;. Likewise, in 
+          the expression &quot;False and func()&quot;, func() will not be called.</p>
+        <p><font face="Verdana, Arial, Helvetica, sans-serif" size="+2"><a name="andorres"></a></font>Another 
+          feature of &quot;and&quot; and &quot;or&quot; is that when the result 
+          of the expression is &quot;true&quot;, the actual result object will 
+          be one of the inputs (remember that most objects evaluate to True). 
+          This allows &quot;gating&quot; of values. The result of &quot;True and 
+          99&quot; is 99. The result of &quot;False or 'abc'&quot; is 'abc'. This 
+          allows you to do this trick, which should look familiar to you C programmers 
+          as the &quot;x ? y : z&quot; construct:</p>
+        <pre># Prothon source file tut10.pr
+
+def sel(q, a, b):
+    return (q and a) or ((not q) and b)
+    
+print sel(True,  1, 2)
+print sel(False, 1, 2)</pre>
+        <p>Which prints out this result:</p>
+        <pre>XXX</pre>
+        <p>&nbsp;</p>
+      </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="tutorial6.htm">Previous 
+        Page</a></td>
+      <td width="183" valign="middle" align="center" height="25"><a href="index.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/tut10.pr
===================================================================
--- trunk/pr/tutorial/tut10.pr	2004-05-15 05:44:17 UTC (rev 490)
+++ trunk/pr/tutorial/tut10.pr	2004-05-16 17:59:02 UTC (rev 491)
@@ -0,0 +1,8 @@
+
+# Prothon source file tut10.pr
+
+def select(sel, a, b):
+    return (sel and a) or ((not sel) and b)
+    
+print select(True,  1, 2)
+print select(False, 1, 2)
\ No newline at end of file