home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Interactive Guide
/
c-cplusplus-interactive-guide.iso
/
c_ref
/
csource1
/
cenvid
/
cmmtutor.doc
< prev
next >
Wrap
Text File
|
1993-12-21
|
51KB
|
1,175 lines
CEnvi Shareware Manual, Chapter 2:
Cmm Language Tutorial
CEnvi unregistered version 1.008
21 December 1993
CEnvi Shareware User's Manual
Copyright 1993, Nombas, All Rights Reserved.
Published by Nombas, P.O. Box 875, Medford, MA 02155 USA
(617)391-6595
Thank you for trying this shareware version of CEnvi from Nombas,
a member of the Association of Shareware Professionals (ASP).
2. The Cmm Language: Tutorial for Non-C Programmers
The information in this chapter is geared toward those who are
not familiar with the C programming language. C programmers
should jump ahead to the next chapter: Cmm versus C. This
section is an introduction to and description of the Cmm
programming language.
If you can write a batch, script, or macro file, or if you can
remember what "y = x + 1" means from your algebra class, then
you're ready to take on Cmm. Really. Cmm contains only
variables, mathematics symbols (remember algebra), and these few
statements: IF, ELSE, DO, WHILE, FOR, SWITCH, CASE, BREAK,
DEFAULT, CONTINUE, GOTO, and RETURN.
This section is an abbreviation of the Cmm Language Tutorial
chapter in the CEnvi registered manual. The CEnvi registered
manual goes into much more depth, has many more examples, and
follows a step-by-step tutorial to create a simple text editor
with CEnvi.
2.1. Your first Cmm program
Before going into a description of Cmm, let's first make sure
that CEnvi is working properly. With a text editor (e.g., EDIT
for DOS, E for OS/2, or NOTEPAD for Windows) create the file
HELLO.CMM and enter this text:
// Hello.cmm: My first Cmm program
Count = 1; /* Count is how many Cmm programs I've written */
printf("Hello world. This is my %dst Cmm program.\n",Count);
printf("Press any key to quit...");
getch();
You have now written a Cmm program named "HELLO.CMM". Don't be
concerned if you do not yet understand the HELLO.CMM program; it
should become clear as Cmm is defined. Now execute this program
(for DOS or OS/2 you would enter "CENVI HELLO.CMM" and for
Windows you need may use the File Manager and double-click on the
file name). You should get this output:
Hello world. This is my 1st Cmm program.
Press any key to quit...
If this program will execute, then you are ready to go on to
learn about Cmm. If it did not execute then consult the CEnvi
installation section in the first chapter of this shareware
manual.
2.2. Cmm comments
Comments are used in Cmm code to explain what the code does, but
the comment itself does nothing. Comments are very useful in
programming. A comment takes nothing away from the execution of
a program, but adds immeasurably to the readability of the source
code.
In Cmm, any text on a line following two slash characters (//) is
considered a comment and so is ignored by the Cmm interpreter.
Likewise, anything between a slash-asterisk (/*) and an
asterisk-slash (*/) is a comment (this type of comment may extend
over many lines). In the HELLO.CMM program there is a "//"
comment on the first line and a "/* blah blah */" comment on the
second line.
2.3. Cmm primary data types
There are three principal data types in Cmm:
*Byte: A character (e.g., 'D') or a whole number between 0 and
255, inclusive
*Integer: A whole number value; this is the most common numeric
data type (examples: 0, -1000, 567, 4335600)
*Float: floating point numbers; any number containing a decimal
point (examples: 0.567, 3.14159, -5.456e-12)
Cmm determines the data type of a number by how it is used; for
example, in the HELLO.CMM program the "1" in the second line is
an integer because that is the default type for numbers without a
decimal point.
2.3.1 Escape Sequences for Characters
Certain characters are represented with a multi-character
sequence beginning with a backslash (\). These are called escape
sequences, and have the following meanings:
\a Audible bell
\b Backspace
\f Formfeed
\n Newline
\r Carriage return
\t Tab
\v Vertical tab
\' Single quote
\" Double quote
\\ Backslash character
\### Octal number // ex: '\033' is escape character
// ex: \0 is null character
\x## Hex number // '\x1B' is escape character
2.4. Cmm Variables
A Cmm variable is a symbol that may be assigned data. The
assignment of data is usually performed by the equal sign (=), as
in the line "Count = 1" in HELLO.CMM. After variables have been
assigned, they can be treated as their data type. So, after
these statements:
Count = 1 // assign count as the integer 1
Count = Count + 2 // same as: Count = 1 + 2
Count would now have the value 3.
2.5. Cmm Expressions, Statements, and Blocks
A Cmm "expression" or "statement" is any sequence of code that
perform a computation or take an action (e.g., "Count=1",
"(2+4)*3"). Cmm code is executed one statement at a time in the
order it is read. A Cmm program is a series of statements
executed sequentially, one at a time. Each line of HELLO.CMM,
for example, follows the previous line as it is written and as it
is executed.
A statement usually ends in a semicolon (;) (this is required in
C, and still a good idea in Cmm to improve readability). Each
program statement is usually written on a separate line to make
the code easy to read.
Expressions may be grouped to effect the sequence of processing;
expressions inside parentheses are processed first. Notice that:
4 * 7 - 5 * 3 // 28 - 14 = 13
has the same meaning, do to algebraic operator precedence, as:
(4 * 7) - (5 * 3) // 28 - 15 = 13
but has a different meaning than:
4 * (7 - 5) * 3 // 4 * 2 * 3 = 8 * 3 = 24
which is still different from:
4 * (7 - (5 * 3)) // 4 * (7 - 15) = 4 * -8 = -32
A "block" is a group of statements enclosed in curly braces ({})
to show that they are all a group and so are treated as one
statement. For example, HELLO.CMM may be rewritten as:
// Hello.cmm: My first Cmm program
Count = 1; /* Count is how many Cmm programs I've written */
printf("Hello world. This is my %dst Cmm program.\n",Count);
{
// this block tells the user we're done, and quits
printf("Press any key to quit...");
getch();
}
The indentation of statements is not necessary, but is useful for
readability.
2.6. Cmm Mathematical Operators
Cmm code usually contains some mathematical operations, such as
adding numbers together, multiplying, dividing, etc. These are
written in a natural way, such as "2 + 3" when you want to add
two and three. The n