home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 February / psl_9403.zip / psl_9403 / DOS / UT_SYSTM / CENVI2.ZIP / CMMTUTOR.DOC < prev    next >
Text File  |  1993-11-17  |  50KB  |  1,137 lines

  1.                     CEnvi Shareware Manual, Chapter 2:
  2.                            Cmm Language Tutorial
  3.  
  4.  
  5.                      CEnvi unregistered version 1.007
  6.                              17 November 1993
  7.  
  8.                        CEnvi Shareware User's Manual
  9.  
  10.           Copyright 1993, Nombas, All Rights Reserved.
  11.           Published by Nombas, P.O. Box 875, Medford, MA 02155 USA
  12.           (617)391-6595
  13.  
  14.           Thank you for trying this shareware version of CEnvi from Nombas,
  15.           a member of the Association of Shareware Professionals (ASP).
  16.  
  17. 2.  The Cmm Language: Tutorial for Non-C Programmers
  18.  
  19.           The information in this chapter is geared toward those who are
  20.           not familiar with the C programming language.  C programmers
  21.           should jump ahead to the next chapter: Cmm versus C.  This
  22.           section is an introduction to and description of the Cmm
  23.           programming language.
  24.  
  25.           If you can write a batch, script, or macro file, or if you can
  26.           remember what "y = x + 1" means from your algebra class, then
  27.           you're ready to take on Cmm.  Really.  Cmm contains only
  28.           variables, mathematics symbols (remember algebra), and these few
  29.           statements: IF, ELSE, DO, WHILE, FOR, SWITCH, CASE, BREAK,
  30.           DEFAULT, CONTINUE, GOTO, and RETURN.
  31.  
  32.           This section is an abbreviation of the Cmm Language Tutorial
  33.           chapter in the CEnvi registered manual.  The CEnvi registered
  34.           manual goes into much more depth, has many more examples, and
  35.           follows a step-by-step tutorial to create a simple text editor
  36.           with CEnvi.
  37.  
  38. 2.1.  Your first Cmm program
  39.  
  40.           Before going into a description of Cmm, let's first make sure
  41.           that CEnvi is working properly.  With a text editor (e.g., EDIT
  42.           for DOS, E for OS/2, or NOTEPAD for Windows) create the file
  43.           HELLO.CMM and enter this text:
  44.  
  45.               // Hello.cmm: My first Cmm program
  46.               Count = 1; /* Count is how many Cmm programs I've written */
  47.               printf("Hello world. This is my %dst Cmm program.\n",Count);
  48.               printf("Press any key to quit...");
  49.               getch();
  50.  
  51.           You have now written a Cmm program named "HELLO.CMM".  Don't be
  52.           concerned if you do not yet understand the HELLO.CMM program; it
  53.           should become clear as Cmm is defined.  Now execute this program
  54.           (for DOS or OS/2 you would enter "CENVI HELLO.CMM" and for
  55.           Windows you need may use the File Manager and double-click on the
  56.           file name).  You should get this output:
  57.  
  58.               Hello world. This is my 1st Cmm program.
  59.               Press any key to quit...
  60.  
  61.  
  62.           If this program will execute, then you are ready to go on to
  63.           learn about Cmm.  If it did not execute then consult the CEnvi
  64.           installation section in the first chapter of this shareware
  65.           manual.
  66.  
  67. 2.2.  Cmm comments
  68.  
  69.           Comments are used in Cmm code to explain what the code does, but
  70.           the comment itself does nothing.  Comments are very useful in
  71.           programming.  A comment takes nothing away from the execution of
  72.           a program, but adds immeasurably to the readability of the source
  73.           code.
  74.  
  75.           In Cmm, any text on a line following two slash characters (//) is
  76.           considered a comment and so is ignored by the Cmm interpreter.
  77.           Likewise, anything between a slash-asterisk (/*) and an
  78.           asterisk-slash (*/) is a comment (this type of comment may extend
  79.           over many lines).  In the HELLO.CMM program there is a "//"
  80.           comment on the first line and a "/* blah blah */" comment on the
  81.           second line.
  82.  
  83. 2.3.  Cmm primary data types
  84.  
  85.           There are three principal data types in Cmm:
  86.             *Byte: A character (e.g., 'D') or a whole number between 0 and
  87.               255, inclusive
  88.             *Integer: A whole number value; this is the most common numeric
  89.               data type (examples: 0, -1000, 567, 4335600)
  90.             *Float: floating point numbers; any number containing a decimal
  91.               point (examples: 0.567, 3.14159, -5.456e-12)
  92.  
  93.           Cmm determines the data type of a number by how it is used; for
  94.           example, in the HELLO.CMM program the "1" in the second line is
  95.           an integer because that is the default type for numbers without a
  96.           decimal point.
  97.  
  98. 2.3.1   Escape Sequences for Characters
  99.  
  100.           Certain characters are represented with a multi-character
  101.           sequence beginning with a backslash (\).  These are called escape
  102.           sequences, and have the following meanings:
  103.               \a      Audible bell
  104.               \b      Backspace
  105.               \f      Formfeed
  106.               \n      Newline
  107.               \r      Carriage return
  108.               \t      Tab
  109.               \v      Vertical tab
  110.               \'      Single quote
  111.               \"      Double quote
  112.               \\      Backslash character
  113.               \###    Octal number  // ex: '\033' is escape character
  114.                                     // ex: \0 is null character
  115.               \x##    Hex number    // '\x1B' is escape character
  116.  
  117. 2.4.  Cmm Variables
  118.  
  119.           A Cmm variable is a symbol that may be assigned data.  The
  120.           assignment of data is usually performed by the equal sign (=), as
  121.           in the line "Count = 1" in HELLO.CMM.  After variables have been
  122.           assigned, they can be treated as their data type.  So, after
  123.           these statements:
  124.               Count = 1               // assign count as the integer 1
  125.               Count = Count + 2       // same as: Count = 1 + 2
  126.           Count would now have the value 3.
  127.  
  128. 2.5.  Cmm Expressions, Statements, and Blocks
  129.  
  130.           A Cmm "expression" or "statement" is any sequence of code that
  131.           perform a computation or take an action (e.g., "Count=1",
  132.           "(2+4)*3").  Cmm code is executed one statement at a time in the
  133.           order it is read.  A Cmm program is a series of statements
  134.           executed sequentially, one at a time.  Each line of HELLO.CMM,
  135.           for example, follows the previous line as it is written and as it
  136.           is executed.
  137.  
  138.           A statement usually ends in a semicolon (;) (this is required in
  139.           C, and still a good idea in Cmm to improve readability).  Each
  140.           program statement is usually written on a separate line to make
  141.           the code easy to read.
  142.  
  143.           Expressions may be grouped to effect the sequence of processing;
  144.           expressions inside parentheses are processed first.  Notice that:
  145.               4 * 7 - 5 * 3       // 28 - 14 = 13
  146.           has the same meaning, do to algebraic operator precedence, as:
  147.               (4 * 7) - (5 * 3)   // 28 - 15 = 13
  148.           but has a different meaning than:
  149.               4 * (7 - 5) * 3     // 4 * 2 * 3 = 8 * 3 = 24
  150.           which is still different from:
  151.               4 * (7 - (5 * 3))   // 4 * (7 - 15) = 4 * -8 = -32
  152.  
  153.           A "block" is a group of statements enclosed in curly braces ({})
  154.           to show that they are all a group and so are treated as one
  155.           statement.  For example, HELLO.CMM may be rewritten as:
  156.               // Hello.cmm: My first Cmm program
  157.               Count = 1; /* Count is how many Cmm programs I've written */
  158.               printf("Hello world. This is my %dst Cmm program.\n",Count);
  159.               {
  160.                 // this block tells the user we're done, and quits
  161.                 printf("Press any key to quit...");
  162.                 getch();
  163.               }
  164.           The indentation of statements is not necessary, but is useful for
  165.           readability.
  166.  
  167. 2.6.  Cmm Mathematical Operators
  168.  
  169.           Cmm code usually contains some mathematical operations, such as
  170.           adding numbers together, multiplying, dividing, etc.  These are
  171.           written in a natural way, such as "2 + 3" when you want to add
  172.           two and three.  The