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

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