home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 035 / cenvi29.zip / CMMTUTOR.DOC < prev    next >
Text File  |  1994-11-29  |  44KB  |  1,033 lines

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