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

  1.                     CEnvi Shareware Manual, Chapter 3:
  2.                       Cmm versus C, for C Programmers
  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.  
  15.           Thank you for trying this shareware version of CEnvi from Nombas,
  16.           a member of the Association of Shareware Professionals (ASP).
  17.  
  18. 3.  Cmm versus C: The Cmm language for C programmers
  19.  
  20.           This chapter is for those who already know how to program in the
  21.           C language.  This chapter describes only those elements of Cmm
  22.           that differ from standard C, and so if you don't already
  23.           understand C, then this shouldn't have much meaning for you.
  24.           Non-C programmers should instead look at the previous chapter.
  25.  
  26.           Since it is assumed that readers of this chapter are already
  27.           knowledgeable in C, only those aspects of Cmm that differ from C
  28.           are described here.  If it's not mentioned here, then assume that
  29.           Cmm behavior will be standard C.
  30.  
  31.           Deviations from C are a result of these two harmonious Cmm
  32.           directives: Convenience and Safety.  Cmm is different from C
  33.           where the change makes Cmm more convenient for small programs,
  34.           command-line code, or scripting files, or if unaltered C rules
  35.           encourage coding that is potentially unsafe.
  36.  
  37. 3.1.  C Minus Minus
  38.  
  39.           Cmm is "C minus minus" where the minuses are Type Declarations
  40.           and Pointers.  If you already know C and can remember to forget
  41.           these two aspects of C (I repeat, no Type Declarations and no
  42.           Pointers) then you know Cmm.  If you were to take C code, and
  43.           delete all the lines, code-words, and symbols that either declare
  44.           data types or explicitly point to data, then you would be left
  45.           with Cmm code; and although you would be removing bytes of source
  46.           code, you would not be removing capabilities.
  47.  
  48.           All of the details below that compare Cmm against C follow from
  49.           the general rule:
  50.             *Cmm is C minus Type Declarations and minus Pointers.
  51.  
  52. 3.2.  Data Types
  53.  
  54.           The only recognized data types are Float, Long, and Byte.  The
  55.           words "Float", "Long", and "Byte" do not appear in Cmm source
  56.           code; instead, the data types are determined by context.  For
  57.           instance 6 is a Long, 6.6 is a Float, and '6' is a Byte.  Byte is
  58.           unsigned, and the other types are signed.
  59.  
  60. 3.3.  Automatic Type Declaration
  61.  
  62.           There are no type declarators and no type casting.  Types are
  63.           determined from context.  If the code says "i=6" then i is a
  64.           Long, unless a previous statement has indicated otherwise.
  65.  
  66.           For instance, this C code:
  67.               int Max(int a,int b)
  68.               {
  69.                 int result;
  70.                 result = ( a < b ) ? b : a ;
  71.                 return result;
  72.               }
  73.           could become this Cmm code:
  74.               Max(a,b)
  75.               {
  76.                 result = ( a < b ) ? b : a ;
  77.                 return result;
  78.               }
  79.  
  80. 3.4.  Array Representation
  81.  
  82.           Arrays are used in Cmm much like they are in C, except that they
  83.           are stored differently: a first-order array (e.g., a string) is
  84.           stored in consecutive bytes in memory, but arrays of arrays are
  85.           not in consecutive memory locations.  The C declaration "char
  86.           c[3][3];" would state that there are nine consecutive bytes in
  87.           memory.  In Cmm a similar statement such as "c[2][2] = 'A'" would
  88.           tell you that there are (at least) three arrays of characters,
  89.           and the third array of arrays has (at least) three characters in
  90.           it; and although the characters in c[0] are in consecutive bytes,
  91.           and the characters in c[1] are in consecutive bytes, the two
  92.           arrays c[0] and c[1] are not necessarily adjacent in memory.
  93.  
  94. 3.4.1   Array Arithmetic
  95.  
  96.           When one array is assigned to the other, as in:
  97.               foo = "cat";
  98.               goo = foo;
  99.           then both variables define the same array and start at the same
  100.           offset 0.  In this case, if foo[2] is changed then you will find
  101.           that goo[2] has also been changed.
  102.  
  103.           Integer addition and subtraction can also be performed on arrays.
  104.           Array addition or subtraction sets where the array is based.  By
  105.           altering the previous code segment to:
  106.               foo = "cat";
  107.               goo = foo + 1;
  108.           goo and foo would now be arrays containing the same data, except
  109.           that now goo is based one element further, and foo[2] is now the
  110.           same data as goo[1].
  111.  
  112.           To demonstrate:
  113.               foo = "cat";  // foo[0] is 'c', foo[1] = 'a'
  114.               goo = foo + 1;// goo[0] is 'a', goo[-1] = 'c'
  115.               goo[0] = 'u'; // goo[0] is 'u', foo[1] = 'u', foo is "cut"
  116.               goo++;        // goo[0] is 't', goo[-2] = 'c'
  117.               goo[-2] = 'b' // goo[0] is 't', foo[0] = 'b', foo is "but"
  118.  
  119. 3.4.2   Automatic Array Allocation
  120.  
  121.           Arrays are dynamic, and any index, (positive or negative) into an
  122.           array is always valid.  If an element of an array is referred to,
  123.           then the Cmm must see to it that such an element will exist.  For
  124.           instance if the first statement in the Cmm source code is "foo[4]
  125.           = 7;" then the Cmm interpreter will make an array of 5 integers
  126.           referred to by the variable foo.  If a statement further on
  127.           refers to "foo[6]" then the Cmm interpreter will grow foo, if it
  128.           has to, to ensure that the element foo[6] exists.  This works
  129.           with negative indices as well.  When you refer to foo[-10], then
  130.           foo is grown in the other direction if it needs to be, but foo[4]
  131.           will still refer to that "7" you put there earlier.  Arrays can
  132.           reach any dimension order, so that foo[6][7][34][-1][4] is a
  133.           valid value.
  134.  
  135. 3.5.  Structures
  136.  
  137.           Structures are created dynamically, and their elements are not
  138.           necessarily contiguous in memory.  When CEnvi comes across the
  139.           statement 'foo.animal = "dog"' it creates a structure element of
  140.           foo that is referred to as "animal" and is an array of
  141.           characters, and this "animal" variable is thereafter carried
  142.           around with "foo" (much like a stem variable in REXX).  The
  143.           resulting code looks very much like regular C code, except that
  144.           there is not a separate structure definition anywhere.
  145.  
  146.           This C code:
  147.  
  148.               struct Point {
  149.                 int Row;
  150.                 int Column;
  151.               };
  152.  
  153.               struct Square {
  154.                 struct Point BottomLeft;
  155.                 struct Point TopRight;
  156.               };
  157.  
  158.               void main()
  159.               {
  160.                 struct Square sq;
  161.                 int Area;
  162.                 sq.BottomLeft.Row = 1;
  163.                 sq.BottomLeft.Column = 15;
  164.                 sq.TopRight.Row = 82;
  165.                 sq.TopRight.Column = 120;
  166.                 Area = AreaOfASquare(sq);
  167.               }
  168.  
  169.               int AreaOfASquare(struct Square s)
  170.               {
  171.                 int width, height;
  172.                 width = s.TopRight.Column - s.BottomLeft.Column + 1;
  173.                 height = s.TopRight.Row - s.BottomLeft.Row + 1;
  174.                 return( length * height );
  175.               }
  176.  
  177.           can be changed into the equivalent Cmm code simply be removing
  178.           declaration lines, resulting in:
  179.  
  180.               main()
  181.               {
  182.                 sq.BottomLeft.Row = 1;
  183.                 sq.BottomLeft.Column = 15;
  184.                 sq.TopRight.Row = 82;
  185.                 sq.TopRight.Colum