home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / CMMTUTOR.DOC < prev    next >
Text File  |  1996-02-20  |  45KB  |  1,044 lines

  1.                       CEnvi Demo Manual, Chapter 2:
  2.                            Cmm Language Tutorial
  3.  
  4.                            CEnvi version 2.11
  5.                             20 February 1996
  6.  
  7.                  Copyright 1993, Nombas, All Rights Reserved.
  8.           Published by Nombas, 64 Salem Street, MEDFORD MA 02155 USA
  9.           
  10.              VOICE (617) 391-6595     EMAIL: nombas@nombas.com
  11.              BBS   (617) 391-3718     WWW: http://www.nombas.com
  12.              FAX   (617) 391-3842
  13.  
  14.          Thank you for trying this shareware version of CEnvi from Nombas.
  15.  
  16.                           _______
  17.                      ____|__     |                (R)
  18.                   --|       |    |-------------------
  19.                     |   ____|__  |  Association of
  20.                     |  |       |_|  Shareware
  21.                     |__|   o   |    Professionals
  22.                   -----|   |   |---------------------
  23.                        |___|___|    MEMBER
  24.  
  25.  
  26.  
  27. 2.  The Cmm Language: Tutorial for Non-C Programmers
  28.  
  29.           The information in this chapter is geared toward those who are
  30.           not familiar with the C programming language.  C programmers
  31.           should jump ahead to the next chapter: Cmm versus C.  This
  32.           section is an introduction to and description of the Cmm
  33.           programming language.
  34.  
  35.           If you can write a batch, script, or macro file, or if you can
  36.           remember what "y = x + 1" means from your algebra class, then
  37.           you're ready to take on Cmm.  Really.  Cmm contains only
  38.           variables, mathematics symbols (remember algebra), and these few
  39.           statements: IF, ELSE, DO, WHILE, FOR, SWITCH, CASE, BREAK,
  40.           DEFAULT, CONTINUE, GOTO, and RETURN.
  41.  
  42.           This section is an abbreviation of the Cmm Language Tutorial
  43.           chapter in the CEnvi registered manual.  The CEnvi registered
  44.           manual goes into much more depth, has many more examples, and
  45.           follows a step-by-step tutorial to create a simple text editor
  46.           with CEnvi.
  47.  
  48. 2.1.  Your first Cmm program
  49.  
  50.           Before going into a description of Cmm, let's first make sure
  51.           that CEnvi is working properly.  With a text editor (e.g., EDIT
  52.           for DOS, E for OS/2, or NOTEPAD for Windows) create the file
  53.           HELLO.CMM and enter this text:
  54.  
  55.               // Hello.cmm: My first Cmm program
  56.               Count = 1; /* Count is how many Cmm programs I've written */
  57.               printf("Hello world. This is my %dst Cmm program.\n",Count);
  58.               printf("Press any key to quit...");
  59.               getch();
  60.  
  61.           You have now written a Cmm program named "HELLO.CMM".  Don't be
  62.           concerned if you do not yet understand the HELLO.CMM program; it
  63.           should become clear as Cmm is defined.  Now execute this program
  64.           (for DOS or OS/2 you would enter "CENVID HELLO.CMM" or "CENVI2 HELLO.CMM"
  65.           and for Windows you need may use the File Manager and double-click 
  66.           on the file name).  You should get this output:
  67.  
  68.               Hello world. This is my 1st Cmm program.
  69.               Press any key to quit...
  70.  
  71.           If this program will execute, then you are ready to go on to
  72.           learn about Cmm.  If it did not execute then consult the CEnvi
  73.           installation section in the first chapter of this shareware
  74.           manual.
  75.  
  76. 2.2.  Cmm comments
  77.  
  78.           Comments are used in Cmm code to explain what the code does, but
  79.           the comment itself does nothing.  Comments are very useful in
  80.           programming.  A comment takes nothing away from the execution of
  81.           a program, but adds immeasurably to the readability of the source
  82.           code.
  83.  
  84.           In Cmm, any text on a line following two slash characters (//) is
  85.           considered a comment and so is ignored by the Cmm interpreter.
  86.           Likewise, anything between a slash-asterisk (/*) and an
  87.           asterisk-slash (*/) is a comment (this type of comment may extend
  88.           over many lines).  In the HELLO.CMM program there is a "//"
  89.           comment on the first line and a "/* blah blah */" comment on the
  90.           second line.
  91.  
  92. 2.3.  Cmm primary data types
  93.  
  94.           There are three principal data types in Cmm:
  95.             *Byte: A character (e.g., 'D') or a whole number between 0 and
  96.               255, inclusive
  97.             *Integer: A whole number value; this is the most common numeric
  98.               data type (examples: 0, -1000, 567, 4335600)
  99.             *Float: floating point numbers; any number containing a decimal
  100.               point (examples: 0.567, 3.14159, -5.456e-12)
  101.  
  102.           Cmm determines the data type of a number by how it is used; for
  103.           example, in the HELLO.CMM program the "1" in the second line is
  104.           an integer because that is the default type for numbers without a
  105.           decimal point.
  106.  
  107. 2.3.1   Escape Sequences for Characters
  108.  
  109.           Certain characters are represented with a multi-character
  110.           sequence beginning with a backslash (\).  These are called escape
  111.           sequences, and have the following meanings:
  112.               \a      Audible bell
  113.               \b      Backspace
  114.               \f      Formfeed
  115.               \n      Newline
  116.               \r      Carriage return
  117.               \t      Tab
  118.               \v      Vertical tab
  119.               \'      Single quote
  120.               \"      Double quote
  121.               \\      Backslash character
  122.               \###    Octal number  // ex: '\033' is escape character
  123.                                     // ex: \0 is null character
  124.               \x##    Hex number    // '\x1B' is escape character
  125.  
  126. 2.4.  Cmm Variables
  127.  
  128.           A Cmm variable is a symbol that may be assigned data.  The
  129.           assignment of data is usually performed by the equal sign (=), as
  130.           in the line "Count = 1" in HELLO.CMM.  After variables have been
  131.           assigned, they can be treated as their data type.  So, after
  132.           these statements:
  133.               Count = 1               // assign count as the integer 1
  134.               Count = Count + 2       // same as: Count = 1 + 2
  135.           Count would now have the value 3.
  136.  
  137. 2.5.  Cmm Expressions, Statements, and Blocks
  138.  
  139.           A Cmm "expression" or "statement" is any sequence of code that
  140.           perform a computation or take an action (e.g., "Count=1",
  141.           "(2+4)*3").  Cmm code is executed one statement at a time in the
  142.           order it is read.  A Cmm program is a series of statements
  143.           executed sequentially, one at a time.  Each line of HELLO.CMM,
  144.           for example, follows the previous line as it is written and as it
  145.           is executed.
  146.  
  147.           A statement usually ends in a semicolon (;) (this is required in
  148.           C, and still a good idea in Cmm to improve readability).  Each
  149.           program statement is usually written on a separate line to make
  150.           the code easy to read.
  151.  
  152.           Expressions may be grouped to effect the sequence of processing;
  153.           expressions inside parentheses are processed first.  Notice that:
  154.               4 * 7 - 5 * 3       // 28 - 15 = 13
  155.           has the same meaning, due to algebraic operator precedence, as:
  156.               (4 * 7) - (5 * 3)   // 28 - 15 = 13
  157.           but has a different meaning than:
  158.               4 * (7 - 5) * 3     // 4 * 2 * 3 = 8 * 3 = 24
  159.           which is still different from:
  160.               4 * (7 - (5 * 3))   // 4 * (7 - 15) = 4 * -8 = -32
  161.  
  162.           A "block" is a group of statements enclosed in curly braces ({})
  163.           to show that they are all a group and so are treated as one
  164.           statement.  For example, HELLO.CMM may be rewritten as:
  165.               // Hello.cmm: My first Cmm program
  166.               Count = 1; /* Count is how many Cmm programs I've written */
  167.               printf("Hello world. This is my %dst Cmm program.\n",Count);
  168.               {
  169.                 // this block tells the user we're done, and quits
  170.                 printf("Press any key to quit...");
  171.                 getch();
  172.               }
  173.           The indentation of statements is not necessary, but is useful for
  174.           readability.
  175.  
  176. 2.6.  Cmm Mathematical Operators
  177.  
  178.           Cmm code usually contains some mathematical operations, such as
  179.           adding numbers together, multiplying, dividing, etc.  These are
  180.           written in a natural way, such as "2 + 3" when you want to add
  181.           two and three.  The next few subsections define the recognized
  182.           operators within the comments of sample code:
  183.  
  184. 2.6.1   Basic Arithmetic
  185.  
  186.               //  "="  assignment: sets a variable's value
  187.               i = 2;      // i is 2
  188.  
  189.               //  "+"  addition: adds two numbers
  190.               i = i + 3;  // i is now (2+3) or 5
  191.  
  192.               //  "-"  subtraction: subtracts a number
  193.               i = i - 3;  // i is now (5-3) or 2 again
  194.  
  195.               //  "*"  multiplication:
  196.               i = i * 5;  // i is now (2*5) or 10
  197.  
  198.               //  "/"  division:
  199.               i = i / 3;  // i is now (10/3) or 3 (no remainder)
  200.  
  201.               //  "%"  remainder: remainder after division
  202.               i = 10;
  203.               i = i % 3;  // i is now (10%3) or 1
  204.  
  205. 2.6.2   Assignment Arithmetic
  206.  
  207.           Each of the above operators can combined with the assignment
  208.           operator (=) as a shortcut.  This automatically assumes that the
  209.           assigned variable is the first variable in the arithmetic
  210.           operation.  Using assignment arithmetic operators, the above code
  211.           could be simplified as follows:
  212.  
  213.               //  "="  assignment: sets a variable's value
  214.               i = 2;      // i is 2
  215.  
  216.               //  "+="  assign addition: adds number
  217.               i += 3;     // i is now (2+3) or 5
  218.  
  219.               //  "-="  assign subtraction: subtracts a number
  220.               i -= 3;     // i is now (5-3) or 2 again
  221.  
  222.               //  "*="  assign multiplication:
  223.               i *= 5;     // i is now (2*5) or 10
  224.  
  225.               //  "/="  assign divide:
  226.               i /= 3;     // i is now (10/3) or 3 (no remainder)
  227.  
  228.               //  "%="  assign remainder: remainder after division
  229.               i = 10;
  230.               i %= i % 3; // i is now (10%3) or 1
  231.  
  232. 2.6.3   Auto-Increment (++) and Auto-Decrement (--)
  233.  
  234.           Other arithmetic shortcuts are the auto-increment (++) and
  235.           auto-decrement (--) operators.  These operators add or subtract 1
  236.           (one) from the value to which they are applied.  ("i++" is a
  237.           shortcut for "i+=1", which is itself shortcut for "i=i+1").
  238.           These operators can be used before (prefix) or after (postfix)
  239.           their variables.  If used before, then the variable is altered
  240.           before it is used in the statement; if used after, then the
  241.           variable is altered after it is used in the statement.  This is
  242.           demonstrated by the following code sequence:
  243.  
  244.               i = 4;    // i is initialized to 4
  245.               j = ++i;  // j is 5, i is 5 (i was incremented before use)
  246.               j = i++;  // j is 5, i is 6 (i was incremented after use)
  247.               j = --i;  // j is 5, i is 5 (i was decremented before use)
  248.               j = i--;  // j is 5, i is 4 (i was decremented after use)
  249.  
  250. 2.7.  Cmm Arrays and Strings
  251.  
  252.           An "array" is a group of individual data elements.  Each
  253.           individual item in the array is then called an "array element".
  254.           An element of an array is itself a variable, much like any other
  255.           variable.  Any particular element of the array is selected by
  256.           specifying the element's offset in the array.  This offset is an
  257.           integer in square brackets ([]).  For example:
  258.  
  259.               prime[0] = 2;
  260.               prime[1] = 3;
  261.               prime[2] = 5;
  262.               month[0] = "January";
  263.               month[1] = "February";
  264.               month[2] = "March";
  265.  
  266.           An array in Cmm does not need to be pre-defined for size or data
  267.           type, as it does in other languages.  Any array extends from
  268.           minus infinity to plus infinity (within reasonable computer
  269.           memory limits).  The data type for the array is the type of the
  270.           data first assigned to it.
  271.  
  272.           If Cmm code begins with:
  273.               foo[5] = 7;
  274.               foo[2] = -100;
  275.               foo[-5] = 4;
  276.           then foo is an array of integers and the element at index 7 is a
  277.           5, at index 2 is -100, and at index -5 is 4.  "foo[5]" can be
  278.           used in the code anywhere that a variable could be used.
  279.  
  280. 2.7.1   Array Initialization
  281.  
  282.           Arrays can be initialized by initializing specific elements, as
  283.           in:
  284.               foo[5] = 7; foo[2] = -100; foo[-5] = 4;
  285.           or by enclosing all the initial statements in curly braces, which
  286.           will cause the array to start initializing at element 0, as in:
  287.               foo = { 0, 0, -100, 0, 0, 7 };
  288.  
  289. 2.7.2   Strings
  290.  
  291.           Strings are arrays of bytes that end in the null-byte (zero).
  292.           "String" is just a shorthand way to specify this array of bytes.
  293.           A string is most commonly specified simply by writing text within
  294.           two quote characters (e.g., "I am a string.")
  295.  
  296.           If this statement were present in the Cmm code:
  297.               animal = "dog";
  298.           it would be identical to this statement:
  299.               animal = { 'd', 'o', 'g', 0 };
  300.           and in both cases the value at animal[0] is 'd', at animal[2] is
  301.           'g', and at animal[3] is 0.
  302.  
  303.           Escape sequences encountered in strings will be translated into
  304.           their byte value.  So you'll often see strings such as "\aHello
  305.           world\n" where the "\a" means to beep and the "\n" means to put a
  306.           newline character at the end of the string.  Cmm provides the
  307.           back-quote (`), also known as known as the "back-tick" or "grave
  308.           accent", as an alternative quote character to indicate that
  309.           escape sequences are not to be translated.  So, for example, here
  310.           are two ways to describe the same file name:
  311.               "c:\\autoexec.bat"    // traditional method
  312.               `c:\autoexec.bat`   // alternative method
  313.  
  314. 2.7.3   Array Arithmetic
  315.  
  316.           When one array is assigned to the other, as in:
  317.               foo = "cat";
  318.               goo = foo;
  319.           then both variables define the same array and start at the same
  320.           offset 0.  In this case, if foo[2] is changed then you will find
  321.           that goo[2] has also been changed.
  322.  
  323.           Integer addition and subtraction can also be performed on arrays.
  324.           Array addition or subtraction sets where the array is based.  By
  325.           altering the previous code segment to:
  326.               foo = "cat";
  327.               goo = foo + 1;
  328.           goo and foo would now be arrays containing the same data, except
  329.           that now goo is based one element further, and foo[2] is now the
  330.           same data as goo[1].
  331.  
  332.           To demonstrate:
  333.               foo = "cat";    // foo[0] is 'c', foo[1] = 'a'
  334.               goo = foo + 1;// goo[0] is 'a', goo[-1] = 'c'
  335.               goo[0] = 'u'; // goo[0] is 'u', foo[1] = 'u', foo is "cut"
  336.               goo++;        // goo[0] is 't', goo[-2] = 'c'
  337.               goo[-2] = 'b' // goo[0] is 't', foo[0] = 'b', foo is "but"
  338.  
  339. 2.7.4   Multi-Dimensional Arrays: Arrays of Arrays
  340.  
  341.           An array element is a variable, and so if the type of that
  342.           element's variable is itself an array, then you have an array of
  343.           arrays.  A statement such as:
  344.               goo[4][2] = 5;
  345.           indicates that goo is an array of arrays, and that element 2 of
  346.           element 4 is the integer 5.
  347.  
  348.           Multi-dimensional arrays might be useful for programming a
  349.           tic-tac-toe game.  Each row and column of the 3x3 board could be
  350.           represented with a row, column element containing 'X', 'O' or ' '
  351.           (blank) depending on the character at that location.  For
  352.           example, a horizontal 'X' win in the middle row could be
  353.           initialized like this:
  354.               board[1][0] = 'X';
  355.               board[1][1] = 'X';
  356.               board[1][2] = 'X';
  357.  
  358.           Note that a string is an array, and so anytime you make an array
  359.           of strings you are defining an array of arrays.
  360.  
  361. 2.8.  Cmm Structures
  362.  
  363.           A "structure" is a collection of named variables that belong
  364.           together as a whole.  Each of the named variables in a structure
  365.           is called a member of that structure and can be any data type
  366.           (integer, float, array, another structure, array of structures,
  367.           etc.).  These structure members are associated with the structure
  368.           by using a period between the structure variable and the member
  369.           name.
  370.  
  371.           A simple and useful example of a structure is to specify a point
  372.           on the screen.  A point is made up of a row and column, and so
  373.           you may specify:
  374.               place.row = 12;   // set to row 12
  375.               place.col = 20;   // set at column 20
  376.               place.row--;        // move up one row to row 11
  377.  
  378.           Two structures can be compared for equality or inequality, and
  379.           they may be assigned, but no other arithmetic operations can be
  380.           performed on a structure.
  381.  
  382. 2.9.  printf() and getch()
  383.  
  384.           Although although the Cmm "function" has not yet been defined, it
  385.           is useful to use two functions already, printf() and getch(), for
  386.           learning more about Cmm programming.  Unfortunately, printf() is
  387.           about as complicated as a function can get.
  388.  
  389. 2.9.1   printf() Syntax
  390.  
  391.           printf() prints to the screen, providing useful feedback on your
  392.           programming.  The basic syntax of printf() is this:
  393.  
  394.               printf(FormatString,data1,data2,data3,...);
  395.  
  396.           FormatString is the string that will be printed to the screen.
  397.           When FormatString contains a percent character (%) followed by
  398.           another character, then instead of printing those characters
  399.           printf() will print the next data item (data1, data2, data3,
  400.           etc...).  The way the data will be presented is determined by the
  401.           characters immediately following the percent sign (%).  There are
  402.           many such data formats, as described fully in the Registered
  403.           User's Manual (or any C programming manual), but here's few to
  404.           start with:
  405.             *%%  print a percentage sign
  406.             *%d  print a decimal integer
  407.             *%X  print a hexadecimal integer
  408.             *%c  print a character
  409.             *%f  print a floating-point value
  410.             *%E  print a floating-point value in scientific format
  411.             *%s  print a string
  412.  
  413.           There are also special characters that cause unique screen
  414.           behavior when they are printed, some of which are:
  415.             *\n  goto beginning of next line
  416.             *\t  tab
  417.             *\a  alarm: audible beep
  418.             *\r  carriage-return without a line feed
  419.             *\"  print the quote character
  420.             *\\  print a backslash
  421.  
  422.           Here are some example printf() statements:
  423.               printf("Hello world. This is my %dst Cmm program.\n",Count);
  424.               printf("%c %d %dlips",'I',8,2);
  425.               printf("%d decimal is the same as %X in hexadecimal",n,n);
  426.               printf("My name is: %s\n","Mary");
  427.  
  428. 2.9.2   getch() Syntax
  429.  
  430.           getch() (GET CHaracter) waits for any key to be pressed.  The
  431.           program stops executing at the getch() statement until a key is
  432.           pressed.  getch() is especially useful in Windows because once a
  433.           program is completed its windows will go away.  So getch() might
  434.           be placed at the end of the program to keep the screen visible
  435.           until a key is pressed.
  436.  
  437. 2.9.3   Using printf() and getch() to Learn Cmm
  438.  
  439.           You can now experiment with what you are learning by using the
  440.           printf() and getch() statements.
  441.  
  442.           If, during program execution, you want to know that you're about
  443.           to execute an exciting piece of code, then before those
  444.           interesting lines of code you may want to write:
  445.  
  446.               printf("Here comes the good stuff.  Press any key...");
  447.               getch();
  448.  
  449.           You can display values at any point in your program with a
  450.           printf() statement.  This short TEST.CMM program tests how "*="
  451.           works:
  452.  
  453.               // TEST.CMM - test the *= operator
  454.               var1 = 4;
  455.               var2 = 5;
  456.               printf("the numbers are %d and %d\n",var1,var2);
  457.               var1 *= 7;
  458.               var2 *= 7;
  459.               printf("after *= 7 the values are %d and %d\n",var1,var2);
  460.               getch();
  461.  
  462. 2.10. Bit Operators
  463.  
  464.           Cmm contains many operators for operating on the binary bits in a
  465.           byte or an integer.  If you're not familiar with hexadecimal
  466.           numbering (digits '0' to 'F'), you may want to refer to the CEnvi
  467.           Registered User's Manual.
  468.  
  469.               i = 0x146;
  470.  
  471.               //  "<<"  shift left: shift all bits left by value
  472.               //  "<<=" assignment shift left
  473.               i <<= 2;    // shift i (0x146) left 2, so i = 0x518
  474.  
  475.               //  ">>"  shift right: shift all bits right by value
  476.               //  ">>=" assignment shift right
  477.               i >>= 3;    // shift i (0x518) right 3, so i = 0xA3
  478.  
  479.               //  "&"   bitwise AND
  480.               //  "&="  assignment bitwise and
  481.               i &= 0x35;  // and i (0xA3) with 0x35 so i = 0x21
  482.  
  483.               //  "|"   bitwise OR
  484.               //  "|="  assignment bitwise OR
  485.               i |= 0xC5;    // OR i (0x21) with 0xC4 so i = 0xE5
  486.  
  487.               //  "^"   bitwise XOR: exlusive OR
  488.               //  "^="  assignment bitwise XOR
  489.               i ^= 0xF329 // XOR i (0xE5) with so i = 0xF3CC
  490.  
  491.               //  "~"   Bitwise complement: (turn 0 bits to 1, and 1 to 0)
  492.               i = ~i;     // complement i (0xF3CC) so i = 0xFFFF0C33 (OS/2)
  493.  
  494. 2.11. Logical Operators and Conditional Expressions
  495.  
  496.           A conditional expression is evaluated to be TRUE or FALSE, where
  497.           FALSE means zero, and TRUE means anything that is not FALSE
  498.           (i.e., not zero).  A variable or any other expression by itself
  499.           can be TRUE or FALSE (i.e., non-zero or zero).  With logic
  500.           operators, these expressions can be combined to make encompassing
  501.           TRUE/FALSE decisions.  The logic operators are:
  502.  
  503.             * "!"   NOT: opposite of decision
  504.               !TRUE               // FALSE
  505.               !FALSE              // TRUE
  506.               !('D')              // FALSE
  507.               !(5-5)              // TRUE
  508.  
  509.             * "&&"  AND: TRUE if and only if both expressions are true
  510.               TRUE && FALSE       // FALSE
  511.               TRUE && TRUE        // TRUE
  512.               0 && (foo+2)        // FALSE: (foo+2) is not evaluated
  513.               !(5-5) && 4         // TRUE
  514.  
  515.             * "||"  OR: TRUE if either expression is TRUE
  516.               TRUE || FALSE       // TRUE: doesn't even check FALSE
  517.               TRUE || (4+2)       // TRUE: (4+2) is not evaluated
  518.               FALSE || (4+2)      // TRUE: (4+2) is evaluated
  519.               FALSE || FALSE      // FALSE
  520.               0 || 0.345          // TRUE
  521.               !(5-3) || 4         // TRUE
  522.  
  523.             * "=="  EQUALITY: TRUE if both values equal, else FALSE
  524.               5 == 5              // TRUE
  525.               5 == 6              // FALSE
  526.  
  527.             * "!="  INEQUALITY: TRUE if both values not equal, else FALSE
  528.               5 != 5              // FALSE
  529.               5 != 6              // TRUE
  530.  
  531.             * "<"   LESS THAN
  532.               5 < 5               // FALSE
  533.               5 < 6               // TRUE
  534.  
  535.             * ">"   GREATER THAN
  536.               5 > 5               // FALSE
  537.               5 > 6               // FALSE
  538.               5 > -1              // TRUE
  539.  
  540.             * "<="  LESS THAN OR EQUAL TO
  541.               5 <= 5              // TRUE
  542.               5 <= 6              // TRUE
  543.               5 <= -1             // FALSE
  544.  
  545.             * ">="  GREATER THAN OR EQUAL TO
  546.               5 >= 5              // TRUE
  547.               5 >= 6              // FALSE
  548.               5 >= -1             // TRUE
  549.  
  550. 2.12. Flow Decisions: Loops, Conditions, and Switches
  551.  
  552.           This section describes how Cmm statements can control the flow of
  553.           your program.  You'll notice code sections are often indented,
  554.           when blocks belong together; this makes the code much easier to
  555.           read.
  556.  
  557. 2.12.1  IF - Execute statement (or block) if conditional expression is TRUE
  558.  
  559.               if ( goo < 10 ) {
  560.                 printf("goo is smaller than 10\n");
  561.               }
  562.  
  563. 2.12.2  ELSE - Execute statement (or block) if IF block was not executed
  564.               if ( goo < 10 ) {
  565.                 printf("goo is smaller than 10\n");
  566.               } else {
  567.                 printf("goo is not smaller than 10\n");
  568.               }
  569.  
  570.           ELSE can also be combined with IF to find one in a series of
  571.           values:
  572.  
  573.               if ( goo < 10 ) {
  574.                 printf("goo is less than 10\n");
  575.                 if ( goo < 0 ) {
  576.                   printf("goo is negative; of course it's less than 10\n");
  577.                 }
  578.               } else if ( goo > 10 ) {
  579.                 printf("goo is greater than 10\n");
  580.               } else {
  581.                 printf("goo is 10\n");
  582.               }
  583.  
  584. 2.12.3  WHILE - Execute block while conditional expression is TRUE
  585.  
  586.               str = "WHO ARE YOU?";
  587.               while ( str[0] != 'A' ) {
  588.                 printf("%s\n",str);
  589.                 str++;
  590.               }
  591.               printf("%s\n",str);
  592.  
  593.           The output of the above code would be:
  594.               WHO ARE YOU?
  595.               HO ARE YOU?
  596.               O ARE YOU?
  597.                ARE YOU?
  598.               ARE YOU?
  599.  
  600. 2.12.4  DO ... WHILE - Execute block, and then test for conditional
  601.  
  602.           This is different from the WHILE statement in that the block is
  603.           executed at least once, before the condition is tested.
  604.  
  605.               do {
  606.                 value++;
  607.                 printf("value = %d\n",value);
  608.               } while( value < 100 );
  609.  
  610.           The code used to demonstrate the WHILE statement might be better
  611.           written this way to avoid the extra printf():
  612.  
  613.               str = "WHO ARE YOU?";
  614.               do {
  615.                 printf("%s\n",str);
  616.               } while ( str++[0] != 'A' );
  617.  
  618. 2.12.5  FOR - initialize, test conditional, then loop
  619.  
  620.           "for" is a combination of statements of this format:
  621.               for ( initialization; conditional; loop_expression )
  622.                 statement
  623.           The initialization is performed first.  Then the conditional is
  624.           tested.  If the conditional is TRUE (or if there is no
  625.           conditional expression) then statement is executed, and then the
  626.           loop_expression is executed, and so on back to testing the
  627.           conditional.  If the conditional is FALSE then the program
  628.           continues beyond statement.  The "for" statement is a shortcut
  629.           for this form of WHILE:
  630.               initialization;
  631.               while ( conditional ) {
  632.                 statement;
  633.                 loop_expression;
  634.               }
  635.  
  636.           The above code demonstrating the WHILE statement would be
  637.           rewritten this way if you preferred to use the FOR statement:
  638.  
  639.               for ( str = "WHO ARE YOU?"; str[0] != 'A'; str++ )
  640.                 printf("%s\n",str);
  641.               printf("%s\n",str);
  642.  
  643.  
  644. 2.12.6  BREAK and CONTINUE
  645.  
  646.           "break" terminates the nearest loop or case statement.
  647.  
  648.           "continue" jumps to the test condition in the nearest DO or WHILE
  649.           loop, and jumps to the loop_expression in the nearest FOR loop.
  650.  
  651. 2.12.7  SWITCH, CASE, and DEFAULT
  652.  
  653.           The SWITCH statement follows this format:
  654.  
  655.               switch( switch_expression ) {
  656.                 case exp1:  statement1
  657.                 case exp2:  statement2
  658.                 case exp3:  statement3
  659.                 case exp4:  statement4
  660.                 case exp5:  statement5
  661.                 .
  662.                 .
  663.                 .
  664.                 default: default_statement
  665.               }
  666.  
  667.           switch_expression is evaluated, and then it is compared to all of
  668.           the case expressions (expr1, expr2, expr3, etc...) until a match
  669.           is found (i.e., switch_expression == expr? ).  The statements
  670.           following that match are then executed until the end of the
  671.           switch block is reached or until a BREAK statement exits the
  672.           switch block.  If no match is found, the DEFAULT: statement is
  673.           executed if there is one.
  674.  
  675.           For example, this code handles the variable "key", which is
  676.           assumed to be the value of a key that was just pressed on the
  677.           keyboard:
  678.  
  679.               switch ( key ) {
  680.                 case '0':
  681.                 case '1':
  682.                 case '2':
  683.                 case '3':
  684.                 case '4':
  685.                 case '5':
  686.                 case '6':
  687.                 case '7':
  688.                 case '8':
  689.                 case '9:
  690.                   printf("A digit was pressed\n");
  691.                   break;
  692.                 case '\r';
  693.                   printf("You pressed RETURN\n");
  694.                 default:
  695.                   printf("I am not prepared for the key you pressed\n");
  696.                   break;
  697.               }
  698.  
  699. 2.12.8  GOTO and Labels
  700.  
  701.           You may jump to any location within a function block (see
  702.           functions) by using the GOTO statement.  The syntax is:
  703.               goto LABEL;
  704.           where LABEL is an identifier followed by a colon (:).
  705.  
  706.               if ( a < 0 ) {
  707.                 BadGoingsOn:      // this is a label
  708.                 printf("\aThe value for a is very bad.\n");
  709.                 abort();          // see function library for abort()
  710.               } else if ( 1000 < a )
  711.                 goto BadGoingsOn;
  712.  
  713.           GOTOs should be used sparingly, for they often make it difficult
  714.           to track program flow.
  715.  
  716. 2.13. Conditional Operator ?:
  717.  
  718.           The conditional operator is a strange-looking statement that is
  719.           simply a useful shortcut.  The syntax is this:
  720.  
  721.               test_expression ? expression_if_true : expression_if_false
  722.  
  723.           First, test_expression is evaluated.  If test_expression is
  724.           non-zero (TRUE) then expression_if_true is evaluated and the
  725.           value of the entire expression replaced by the value of
  726.           expression_if_true.  If test_expression is FALSE, then
  727.           expression_if_false is evaluated and the value of the entire
  728.           expression is that of expression_if_false.
  729.  
  730.           For example:
  731.               foo = ( 5 < 6 ) ? 100 : 200;  // foo is set to 100
  732.               printf("User's name is %s\n",NULL==name ? "unknown" : name);
  733.  
  734. 2.14. Functions
  735.  
  736.           Functions in Cmm can perform any action of any complexity, and
  737.           yet they are used in statements as easily as variables.  It is
  738.           because of the flexibility and simplicity of functions that Cmm
  739.           needs so few statements.  Any action can take place in a
  740.           function, and yet the function is treated by the calling code
  741.           simply as the data type that the function returns.
  742.  
  743. 2.14.1  Function definition
  744.  
  745.           A function takes a form such as this:
  746.  
  747.               FunctionName(Parameter1,Parameter2,Parameter3)
  748.               {
  749.                 statements...
  750.                 return result;
  751.               }
  752.  
  753.           where the function name is followed by a list of input parameters
  754.           in parentheses (there can be any number of input parameters and
  755.           they can be named with any variable names).
  756.  
  757.           A call is made to a function in this format:
  758.  
  759.               FunctionName(Value1,Value2,Value3)
  760.  
  761.           So any call to FunctionName will be result in the execution of
  762.           FunctionName() with the parameters passed, and then be equivalent
  763.           to whatever value FunctionName() returns.
  764.  
  765. 2.14.2  Function RETURN Statement
  766.  
  767.           The return statement causes a function to return to the code that
  768.           initially called the function.  The calling code will receive the
  769.           value that the function returned.  Here are function examples:
  770.  
  771.               foo(a,b)    // return a times b
  772.               {
  773.                 return( a * b );
  774.               }
  775.  
  776.               foo(a,b)    // return a times b
  777.               {
  778.                 return a * b;
  779.               }
  780.  
  781.               foo(a,b)    // return the minimum value of a and b
  782.               {
  783.                 if ( a < b )
  784.                   result = a;
  785.                 else
  786.                   result = b;
  787.                 return(result);
  788.               }
  789.  
  790.               foo(a,b)    // return a structure with members .min and .max
  791.               {           // for the smaller and larger of a and b
  792.                 if ( a < b ) {
  793.                   bounds.min = a;
  794.                   bounds.max = b; 
  795.                 } else {
  796.                   bounds.min = b;
  797.                   bounds.max = a;
  798.                 }
  799.                 return( bounds );
  800.               }
  801.  
  802.           If no value is returned, or if the end of the function block is
  803.           reached without a return statement, then no value is returned and
  804.           the function is a "void" type.  void functions return no value to
  805.           the calling code.  These examples demonstrate some void-returning
  806.           functions:
  807.  
  808.               foo(a,b)    // set a = b squared. No return value.
  809.               {
  810.                 a = b * b;
  811.                 return;
  812.               }
  813.  
  814.               foo(a,b)    // set a = b squared. No return value.
  815.               {
  816.                 a = b * b;
  817.               }
  818.  
  819. 2.14.3  Function Example
  820.  
  821.           The use of functions should gain clarity with this example.  This
  822.           code demonstrates a function that multiplies two integers
  823.           (although multiplying integers could more easily be performed
  824.           with the multiply (*) operator):
  825.  
  826.               num1 = 4;
  827.               num2 = 6;
  828.  
  829.               // use the standard method of multiplying numbers
  830.               printf("%d times %d is %d\n",num1,num2,num1*num2);
  831.  
  832.               // now call our function to do the same thing
  833.               printf("%d times %d is %d\n",num1,num2,Multiply(num1,num2));
  834.  
  835.                 // declare a function that multiplies two integers.  Notice
  836.                 // that the integers are defined as i and j, so that's how
  837.                 // they'll be called in this function, no matter what they
  838.                 // were named in the calling code.  This function will
  839.                 // return i added to itself j times.
  840.               Multiply(i,j)
  841.               {
  842.                 total = 0;
  843.                 for ( count = 0; count < j; count++ )
  844.                   total += i;
  845.                 return(total);  // caller will receive this value
  846.               }
  847.  
  848.           When executed, the above code will print:
  849.               4 times 6 is 24
  850.               4 times 6 is 24
  851.  
  852.           This example demonstrated several features of Cmm functions.
  853.           Notice that in calling printf() the parameter "num1*num2" is not
  854.           passed to printf, but the result of "num1*num2" is passed to
  855.           printf.  Likewise, "Multiply(num1,num2)" is not a parameter to
  856.           printf(); instead, printf receives the return value of Multiply()
  857.           as its parameter.
  858.  
  859. 2.14.4  Function Recursion
  860.  
  861.           When a function calls itself, or calls some other function that
  862.           calls itself, then it is known as a recursive function.
  863.           Recursion is permitted in Cmm, as it is in C.  Each call into a
  864.           function is independent of any other call to that function (see
  865.           section on variable scope).
  866.  
  867.           The following function, factor(), factors a number.  Factoring is
  868.           a natural candidate for recursion because it is a repetitive
  869.           process where the result of one factor is then itself factored
  870.           according to the same rules.
  871.  
  872.               factor(i)   // recursive function to print all factors of i,
  873.               {           // and return the number of factors in i
  874.                  if ( 2 <= i ) {
  875.                     for ( test = 2; test <= i; test++ ) {
  876.                        if ( 0 == (i % test) ) {
  877.                           // found a factor, so print this factor then call
  878.                           // factor() recursively to find the next factor
  879.                           printf(" %d",test);
  880.                           return( 1 + factor(i/test) );
  881.                        }
  882.                     }
  883.                  }
  884.                  // if this point was reached, then factor not found
  885.                  return( 0 );
  886.               }
  887.  
  888. 2.15. Variable Scope
  889.  
  890.           A variable in Cmm is either global or local.  A global variable
  891.           is one that is referenced outside of any function, making it
  892.           visible and accessible to all functions.  A local variable is one
  893.           that is only referenced inside of a function, and so it only has
  894.           meaning and value within the function that declares it.  Note
  895.           that two identically-named local variables in different functions
  896.           are not the same variable.  Also, each instance of a recursive
  897.           function has its own set of local variables.  In other words, a
  898.           variable that is not referenced outside of a function only has
  899.           meaning (and that meaning is unique) while the code for that
  900.           function is executing.
  901.  
  902.           In the following sample code, "number" is the only global
  903.           variable, the two "result" variables in Quadruple() and Double()
  904.           are completely independent, and the "result" variable in one call
  905.           Double() is unique to each call to Double():
  906.  
  907.               number = Quadruple(3);
  908.  
  909.               Quadruple(i)
  910.               {
  911.                 result = Double(Double(i));
  912.                 return(result);
  913.               }
  914.  
  915.               Double(j)
  916.               {
  917.                 result = j + j;
  918.                 return(result);
  919.               }
  920.  
  921.           Note that variables that are all in uppercase letter are
  922.           considered global and refer to environment variables.  See the
  923.           chapter on CEnvi specifics for more on environment variables.
  924.  
  925. 2.16. #define
  926.  
  927.           "#define" is used to replace a token (a variable or a function
  928.           name) with other characters.  The structure is:
  929.               #define token replacement
  930.           This results in all subsequent occurrences of "token" to be
  931.           replaced with "replacement".  For example:
  932.               #define HI  "Hello world!"
  933.               printf(HI);
  934.           would result in the following output:
  935.               Hello world!
  936.           because HI was replaced with "Hello world!"
  937.  
  938.           A common use of #define is to define constant numeric or string
  939.           values that might possibly change, so that in the future only the
  940.           #define at the top of the file needs to be altered.  For
  941.           instance, if you write screen routines for a 25-line monitor, and
  942.           then later decide to make it a 50-line monitor, you're better off
  943.           altering
  944.               #define ROW_COUNT   25
  945.           to
  946.               #define ROW_COUNT   50
  947.           and using ROW_COUNT in your code (or ROW_COUNT-1, ROW_COUNT+2,
  948.           ROW_COUNT/2, etc...) than if you had to search for every instance
  949.           of the numbers 25, 24, 26, etc...
  950.  
  951.           CEnvi has a large default list of tokens that have already been
  952.           #define'd, such as TRUE, FALSE, and NULL.  More of these are
  953.           listed in the chapter on the Internal Function Library.
  954.  
  955. 2.17. #include
  956.  
  957.           Cmm code is executed from one stream of source code.  Sometimes
  958.           it is useful to break the program into different files or to
  959.           access commonly used code without recreating that code again and
  960.           again in every new program written.  This is what "#include" is
  961.           for: it allows code from another file to be included in a
  962.           program.
  963.  
  964.           The #include syntax is:
  965.  
  966.               #include <Name[,Ext[,Prefix[,BeginCode[,EndCode]]]]>
  967.               where:
  968.               Name - Name of the file to include
  969.               Ext - Extension name to add to Name if not already there
  970.               Prefix - Will only include code from Name on lines that begin
  971.                        with this string
  972.               BeginCode - Will start including code from Name following
  973.                           this line of text
  974.               EndCode - Will stop including code when this line is read
  975.  
  976.           The quote characters (' or ") may be used in place of < and >.
  977.           The only field that is required by Cmm is Name (this is the field
  978.           used in C's #include), which is the name of the file to include.
  979.           All other fields are optional, and can be left blank if unused.
  980.  
  981.           To include all of C:\CMM\LIB.cmm
  982.               #include <c:\CMM\LIB.cmm>
  983.           To include from dog.bat between the lines GOTO END and :END
  984.               #include 'dog,bat,,GOTO END,:END'
  985.           To include lots of little files into one big one program
  986.               #include <screen.cmm>
  987.               #include <keyboard.cmm>
  988.               #include <init.cmm>
  989.               #include <comm.cmm>
  990.  
  991.           In CEnvi a file will not be included more than once, and so if it
  992.           has already been included, a second (or third) #include statement
  993.           will have no effect.
  994.  
  995. 2.18. Initialization
  996.  
  997.           All code that is outside of any function is part of the global
  998.           initialization function.  This code is executed first, before
  999.           calling any function (unless a function is called from this
  1000.           initialization code).
  1001.  
  1002.           Any variables referenced in the initialization section are
  1003.           global.
  1004.  
  1005. 2.19. main(ArgCount,ArgStrings)
  1006.  
  1007.           After the initialization has been performed, the main() function
  1008.           is called and is given the argument input to the Cmm program.
  1009.           The first parameter to main (ArgCount) is the number of arguments
  1010.           provided to the program.  ArgStrings is an array of those input
  1011.           arguments, which are always strings.  The first argument is
  1012.           always the name of the source code, or of CEnvi.exe if there is
  1013.           no source file.
  1014.  
  1015.           The value returned from main() should be an integer, and is the
  1016.           exit code (ERRORLEVEL for DOS) returned to the operating system
  1017.           from your program (see also exit() in the internal library).
  1018.  
  1019.           This code file TEST.CMM:
  1020.               printf("I am initializing\n");
  1021.               main(argc,argv)
  1022.               {
  1023.                 printf("This program is called %s.\n",argv[0]);
  1024.                 printf("There are %d input arguments, as follows:\n",argc);
  1025.                 for ( i = 0; i < argc; i++ )
  1026.                   printf("\t%s\n",argv[i]);
  1027.                 return(0);
  1028.               }
  1029.               printf("still initializing\n");
  1030.           When executed in this way:
  1031.               cenvi test.cmm I am 4 "years old" today
  1032.           would result in this output:
  1033.               I am initializing
  1034.               still initializing
  1035.               This program is called test.CMM.
  1036.               There are 6 input arguments, as follows:
  1037.                       test.CMM
  1038.                       I
  1039.                       am
  1040.                       4
  1041.                       years old
  1042.                       today
  1043.           and would return 0 to the operating system.
  1044.