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

  1.                        CEnvi Demo Manual, Chapter 3:
  2.                       Cmm versus C, for C Programmers
  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. 3.  Cmm versus C: The Cmm language for C programmers
  28.  
  29.  
  30.           This chapter is for those who already know how to program in the
  31.           C language.  This chapter describes only those elements of Cmm
  32.           that differ from standard C, and so if you don't already
  33.           understand C, then this shouldn't have much meaning for you.
  34.           Non-C programmers should instead look at the previous chapter.
  35.  
  36.           Since it is assumed that readers of this chapter are already
  37.           knowledgeable in C, only those aspects of Cmm that differ from C
  38.           are described here.  If it's not mentioned here, then assume that
  39.           Cmm behavior will be standard C.
  40.  
  41.           Deviations from C are a result of these two harmonious Cmm
  42.           directives: Convenience and Safety.  Cmm is different from C
  43.           where the change makes Cmm more convenient for small programs,
  44.           command-line code, or scripting files, or if unaltered C rules
  45.           encourage coding that is potentially unsafe.
  46.  
  47. 3.1.  C Minus Minus
  48.  
  49.           Cmm is "C minus minus" where the minuses are Type Declarations
  50.           and Pointers.  If you already know C and can remember to forget
  51.           these two aspects of C (I repeat, no Type Declarations and no
  52.           Pointers) then you know Cmm.  If you were to take C code, and
  53.           delete all the lines, code-words, and symbols that either declare
  54.           data types or explicitly point to data, then you would be left
  55.           with Cmm code; and although you would be removing bytes of source
  56.           code, you would not be removing capabilities.
  57.  
  58.           All of the details below that compare Cmm against C follow from
  59.           the general rule:
  60.             *Cmm is C minus Type Declarations and minus Pointers.
  61.  
  62. 3.2.  Data Types
  63.  
  64.           The only recognized data types are Float, Long, and Byte.  The
  65.           words "Float", "Long", and "Byte" do not appear in Cmm source
  66.           code; instead, the data types are determined by context.  For
  67.           instance 6 is a Long, 6.6 is a Float, and '6' is a Byte.  Byte is
  68.           unsigned, and the other types are signed.
  69.  
  70. 3.3.  Automatic Type Declaration
  71.  
  72.           There are no type declarators and no type casting.  Types are
  73.           determined from context.  If the code says "i=6" then i is a
  74.           Long, unless a previous statement has indicated otherwise.
  75.  
  76.           For instance, this C code:
  77.               int Max(int a,int b)
  78.               {
  79.                 int result;
  80.                 result = ( a < b ) ? b : a ;
  81.                 return result;
  82.               }
  83.           could become this Cmm code:
  84.               Max(a,b)
  85.               {
  86.                 result = ( a < b ) ? b : a ;
  87.                 return result;
  88.               }
  89.  
  90. 3.4.  Array Representation
  91.  
  92.           Arrays are used in Cmm much like they are in C, except that they
  93.           are stored differently: a first-order array (e.g., a string) is
  94.           stored in consecutive bytes in memory, but arrays of arrays are
  95.           not in consecutive memory locations.  The C declaration "char
  96.           c[3][3];" would state that there are nine consecutive bytes in
  97.           memory.  In Cmm a similar statement such as "c[2][2] = 'A'" would
  98.           tell you that there are (at least) three arrays of characters,
  99.           and the third array of arrays has (at least) three characters in
  100.           it; and although the characters in c[0] are in consecutive bytes,
  101.           and the characters in c[1] are in consecutive bytes, the two
  102.           arrays c[0] and c[1] are not necessarily adjacent in memory.
  103.  
  104. 3.4.1   Array Arithmetic
  105.  
  106.           When one array is assigned to the other, as in:
  107.               foo = "cat";
  108.               goo = foo;
  109.           then both variables define the same array and start at the same
  110.           offset 0.  In this case, if foo[2] is changed then you will find
  111.           that goo[2] has also been changed.
  112.  
  113.           Integer addition and subtraction can also be performed on arrays.
  114.           Array addition or subtraction sets where the array is based.  By
  115.           altering the previous code segment to:
  116.               foo = "cat";
  117.               goo = foo + 1;
  118.           goo and foo would now be arrays containing the same data, except
  119.           that now goo is based one element further, and foo[2] is now the
  120.           same data as goo[1].
  121.  
  122.           To demonstrate:
  123.               foo = "cat";  // foo[0] is 'c', foo[1] = 'a'
  124.               goo = foo + 1;// goo[0] is 'a', goo[-1] = 'c'
  125.               goo[0] = 'u'; // goo[0] is 'u', foo[1] = 'u', foo is "cut"
  126.               goo++;        // goo[0] is 't', goo[-2] = 'c'
  127.               goo[-2] = 'b' // goo[0] is 't', foo[0] = 'b', foo is "but"
  128.  
  129. 3.4.2   Automatic Array Allocation
  130.  
  131.           Arrays are dynamic, and any index, (positive or negative) into an
  132.           array is always valid.  If an element of an array is referred to,
  133.           then the Cmm must see to it that such an element will exist.  For
  134.           instance if the first statement in the Cmm source code is "foo[4]
  135.           = 7;" then the Cmm interpreter will make an array of 5 integers
  136.           referred to by the variable foo.  If a statement further on
  137.           refers to "foo[6]" then the Cmm interpreter will grow foo, if it
  138.           has to, to ensure that the element foo[6] exists.  This works
  139.           with negative indices as well.  When you refer to foo[-10], then
  140.           foo is grown in the other direction if it needs to be, but foo[4]
  141.           will still refer to that "7" you put there earlier.  Arrays can
  142.           reach any dimension order, so that foo[6][7][34][-1][4] is a
  143.           valid value.
  144.  
  145. 3.5.  Structures
  146.  
  147.           Structures are created dynamically, and their elements are not
  148.           necessarily contiguous in memory.  When CEnvi comes across the
  149.           statement 'foo.animal = "dog"' it creates a structure element of
  150.           foo that is referred to as "animal" and is an array of
  151.           characters, and this "animal" variable is thereafter carried
  152.           around with "foo" (much like a stem variable in REXX).  The
  153.           resulting code looks very much like regular C code, except that
  154.           there is not a separate structure definition anywhere.
  155.  
  156.  
  157.           This C code:
  158.  
  159.               struct Point {
  160.                 int Row;
  161.                 int Column;
  162.               };
  163.  
  164.               struct Square {
  165.                 struct Point BottomLeft;
  166.                 struct Point TopRight;
  167.               };
  168.  
  169.               void main()
  170.               {
  171.                 struct Square sq;
  172.                 int Area;
  173.                 sq.BottomLeft.Row = 1;
  174.                 sq.BottomLeft.Column = 15;
  175.                 sq.TopRight.Row = 82;
  176.                 sq.TopRight.Column = 120;
  177.                 Area = AreaOfASquare(sq);
  178.               }
  179.  
  180.               int AreaOfASquare(struct Square s)
  181.               {
  182.                 int width, height;
  183.                 width = s.TopRight.Column - s.BottomLeft.Column + 1;
  184.                 height = s.TopRight.Row - s.BottomLeft.Row + 1;
  185.                 return( length * height );
  186.               }
  187.  
  188.           can be changed into the equivalent Cmm code simply be removing
  189.           declaration lines, resulting in:
  190.  
  191.               main()
  192.               {
  193.                 sq.BottomLeft.Row = 1;
  194.                 sq.BottomLeft.Column = 15;
  195.                 sq.TopRight.Row = 82;
  196.                 sq.TopRight.Column = 120;
  197.                 Area = AreaOfASquare(sq);
  198.               }
  199.  
  200.               int AreaOfASquare(s)
  201.               {
  202.                 width = s.TopRight.Column - s.BottomLeft.Column + 1;
  203.                 height = s.TopRight.Row - s.BottomLeft.Row + 1;
  204.                 return( length * height );
  205.               }
  206.  
  207.           Structures can be passed, returned, and modified just as any
  208.           other variable.  Of course structures and arrays are independent,
  209.           so you could very well have the statement "foo[8].animal.forge[3]
  210.           = bil.bo".
  211.  
  212. 3.6.  Passing Variables by Reference
  213.  
  214.           By default, LValues in Cmm are passed to functions by reference,
  215.           and so if the function alters a variable then the variable in the
  216.           calling function is altered as well IF IT IS AN LVALUE.  So
  217.           instead of this C code:
  218.  
  219.               main() {
  220.                 .
  221.                 .
  222.                 .
  223.                 CQuadrupleInPlace(&i);
  224.                 .
  225.                 .
  226.                 .
  227.               }
  228.  
  229.               void CQuadrupleInPlace(int *j)
  230.               {
  231.                 *j *= 4;
  232.               }
  233.  
  234.           the Cmm version would be:
  235.  
  236.               main() {
  237.                 .
  238.                 .
  239.                 .
  240.                 QuadrupleInPlace(i);
  241.                 .
  242.                 .
  243.                 .
  244.               }
  245.  
  246.               void QuadrupleInPlace(j)
  247.               {
  248.                 j *= 4;
  249.               }
  250.  
  251.           If the rare circumstance arises that you want to pass a copy of
  252.           an LValue to a function, instead of passing the variable by
  253.           reference, then you can use the Cmm "copy-of" operator "=".
  254.           foo(=i) can be interpreted as saying "pass a value equal to i,
  255.           but not i itself"; so that "foo(=i) ... foo(j) { j *= 4; }" would
  256.           not change the value at i.
  257.  
  258.           Note that for this Cmm version, the following calls to
  259.           QuadrupleInPlace() would be valid, but no value will have changed
  260.           upon return from QuadrupleInPlace() because an LValue is not
  261.           being passed:
  262.               QuadrupleInPlace(8);
  263.               QuadrupleInPlace(i+1);
  264.               QuadrupleInPlace(=1);
  265.  
  266. 3.7.  Data Pointers(*) and Addresses(&)
  267.  
  268.           No pointers.  None.  The "*" symbol NEVER means "pointer" and the
  269.           "&" symbol never means "address".  This may at first cause
  270.           seasoned C programmers to gasp in disbelief, but it turns out to
  271.           be not such a big deal, and these two operators are seldom
  272.           missed, after considering these two rules:
  273.               1) "*var" can be replaced in all instances by "var[0]"
  274.               2) variables (if LValues) are passed by reference
  275.  
  276. 3.8.  Case Statements
  277.  
  278.           Case statements in a switch statement may be a constant, a
  279.           variable, or any other statement that can be evaluated to a
  280.           variable.  So you might see this Cmm code:
  281.  
  282.               switch(i) {
  283.                 case 4:
  284.                 case foe():
  285.                 case sqrt(foe()):
  286.                 case (PILLBOX * 3 - 2):
  287.                 default:
  288.               }
  289.  
  290. 3.9.  Initialization: Code external to functions
  291.  
  292.           All code that is not inside any function block is interpreted
  293.           before main() is called.  So the following Cmm code:
  294.  
  295.               printf("hey there ");
  296.  
  297.               main()
  298.               {
  299.                 printf("ho there ");
  300.               }
  301.  
  302.               printf("hi there ");
  303.  
  304.           would result in the output "hey there hi there ho there ".
  305.  
  306. 3.10. Unnecessary tokens
  307.  
  308.           If symbols are redundant, then they are usually unnecessary in
  309.           Cmm.  This allows for more flexibility in the non-C-trained and
  310.           also lets more code get in the tiny space available on the
  311.           command line.  Besides, I got tired of my compiler saying
  312.           "missing semi-colon"; What good is a semi-colon if it doesn't
  313.           tell the compiler anything new?  So you can have the statement
  314.           "foo()" as well as "foo();".  It certainly doesn't hurt to have
  315.           the semi-colon there, especially when it can clarify a "return;"
  316.           statement, for example, but it isn't necessary.  Similarly, "("
  317.           and ")" are often unnecessary, so you may have "while a < b a++"
  318.           as a complete statement.
  319.  
  320. 3.11. #include
  321.  
  322.           The #include statement has been enhanced for reading source
  323.           snippets from within other types of files.  So we have
  324.  
  325.               #Include <filespec,Extension,Prefix,HeaderLine,FooterLine>
  326.  
  327.           where filespec is the same as in C's #include <filespec>
  328.           statement, Extension is a file extension (such as BAT) that may
  329.           be added to the filespec (so batch files can say #include
  330.           <%0,bat>", Prefix specifies that only those lines in filespec
  331.           that begin with Prefix will be source, and HeaderLine and
  332.           FooterLine specify that source will be read only from sections of
  333.           filespec between HeaderLine and FooterLine.  If a full path is
  334.           not specified then CEnvi searches for the file in various paths
  335.           in this order:
  336.             *Search the current directory.
  337.             *If the code is run from a *.cmm source file, then search in
  338.               the source directory for the *.cmm file.
  339.             *If this is the Windows version of CEnvi, searches all the
  340.               files in the CMMPATH profile value (from WIN.INI in the
  341.               [CEnvi] section).
  342.             *Search all directories in the CMMPATH environment variable.
  343.             *Search the directory that CEnvi.exe is executed from.
  344.             *Search all directories from the PATH environment variable.
  345.  
  346.           In CEnvi a file will not be included more than once, and so if it
  347.           has already been included, a second (or third) #include statement
  348.           will have no effect.
  349.  
  350. 3.12. Macros
  351.  
  352.           Function macros are not supported.  Since speed is not of primary
  353.           importance, a macro gains little over a function call, and so any
  354.           macros may simply become functions.
  355.  
  356.           Token replacement macros ("#define NULL 0") are supported in Cmm.
  357.  
  358. 3.13. Back-quote strings
  359.  
  360.           The back-quote character (`), also known as a "back-tick" or
  361.           "grave accent", can be used in Cmm in place of a double quote (")
  362.           to specify a string where escape sequences are not to be
  363.           translated.  So, for example, here are two ways to describe the
  364.           same file name:
  365.               "c:\\autoexec.bat"  // traditional method
  366.               `c:\autoexec.bat`   // alternative method
  367.  
  368. 3.14. Converting existing C code to Cmm
  369.  
  370.           Converting existing C code to Cmm, should you choose to do so, is
  371.           mostly a process of deleting unnecessary text.  You search on
  372.           type declarations, such as "int", "float", "struct", "char",
  373.           "[]", etc... and then delete these declaration strings.  For
  374.           instance, these instances of C code (or C++ code) on the left can
  375.           be replaced by the Cmm code on the right:
  376.  
  377.                    C                               Cmm     
  378.               ----------                      -------------
  379.  
  380.               int i;    ................... i (or nothing at all)
  381.  
  382.               int foo = 3; ................ foo = 3;
  383.  
  384.               struct {    ................... /* nothing */
  385.                 int row;
  386.                 int col;
  387.               }
  388.  
  389.               char name[] = "George"; ..... name = "George";
  390.  
  391.               int go(int a,char *s,int &c)    go(a,s,c)
  392.               int zoo[] = { 1, 2, 3 }; .... zoo = { 1, 2, 3 };
  393.  
  394.           The next step in converting C to Cmm is to search for the address
  395.           and pointer operators ("*", "&").  If the '&' and '*' work
  396.           together so that the address of a variable is passed to a
  397.           function, then both of these operators become unnecessary because
  398.           Cmm passes lvars by reference.  If there are still "*" found then
  399.           they are usually referring to the zeroth value of a pointer
  400.           address, and so can be replaced with [0], as in "*foo = 4"
  401.           replaced by "foo[0] = 4".  Finally, the "->" operator for
  402.           structures must be replaced by "." either because the structure
  403.           is now being passed by referenced or because the element of the
  404.           structure is being referred to by its array index: "foo->row" may
  405.           need to become "foo[0].row".
  406.