We begin with a digression ... Reference is made to the tiny-c system library several times below. Another important feature of tiny-c is the tiny-c shell. If you run the interpreter without a command tail, a default .ipl ("initial program load") file is loaded which contains, in addition to the tiny-c system library, a shell program which then runs and gives you a tiny-c prompt, tc>. The commands you may issue at the prompt are documented in tiny-c shell. If you do supply a command tail when running the interpreter, then you will not get a tiny-c prompt, you will not have access to any of the functions in the tiny-c system library (unless you include one or more of them in your .ipl) and the function that appears in brackets ([ ]) at the beginning of your .ipl (see below for an example of a simple .ipl) will automatically run. After testing applications under the shell, I sometimes convert them to an .ipl. You gain resources (space for variables and functions) applications may need by doing this (because they are no longer used by the shell).
Example: Here's what happens when you run it. Notice the command tail hello.ipl.
One more thing and then we'll get to the main course! Take it from an experienced tiny-c programmer, there are a few "gotchas" you'll be happy I've cataloged. See tiny-c gotchas. That's it for the digression. On to tiny-c basic language constructs! Please refer to the program below when reading this material. A screen shot which shows this program in action may be seen here.
1.
This is an example of a function invocation (i.e. call). The hilo function (which is defined in the tiny-c system library) takes 1 argument, a character variable or character literal. hilo '1' causes all text that subsequent pn, ps and pl statements sent to the terminal to be "bright." hilo '0' sets things back to "dim." 2.
This is a comment in tiny-c. 3.
Another example of a function invocation (i.e. call). Here, pl takes 1 argument, a "string literal" or a character array (see below). pl is defined in the tiny-c system library. It prints its argument on a new line. 4.
tiny-c supports character variables and integer variables. You may use a semicolon when you want to put more than 1 statement on a line. Variables must be declared before they are used. All variables are initialized to 0 (if integer) or ASCII 0 (NUL) (if character). 5.
This shows one of the 2 looping constructs of tiny-c (the other is the while statement). Note the important idea illustrated here, namely that of a block of statements enclosed by brackets ([ ]). How the for statement works will be discussed shortly (see 7. below).
In the while statement, condition is tested and, if true (i.e. not 0), the statements in the block are executed. This test/execute when true sequence continues until condition is found to be false (i.e. 0), whereupon the loop is exited. 6.
This illustrates the if statement. Note the two operators used here. The unary ! is the "not" operator. If the expression is true, ! will make it false (and vice versa). The binary operator % is the modulus (remainder) operator. For example, 5%2 equals 1 because 5/2 equals 2 with a remainder of 1. Precedence rules force us to put parentheses around the inner expression because ! has higher precedence than %. See tiny-c appendices for tiny-c's operators and their precedence. The purpose of this line, by the way, is to display a new line and "Press Enter ... " (w/o the quotes) every 20 lines, forcing the viewer to strike a key to proceed. 7. The important "assignment" statement is illustrated by all of the for statements. Assignment statements cause what's on the right hand side of an equals sign (=) to be "assigned" (i.e. copied) into what's on the left hand side of an equals sign. That is, they are not statements that something is equal to something else but rather instructions which cause values to be copied from one place in computer memory to another. tiny-c for statements contain three clauses inside their parentheses. The 1st is an initialization statement, which is usually a simple assignment statement. The 2nd is a condition which is tested to see if the loop should be exited. The 3rd is an increment or decrement statement which takes place after the instructions in the block of statements of the for statement have been carried out. Note the use of the unary operator ++ in our example program's for statements. ++a is equivalent to a=a+1, another example of an assignment statement. 8. Not illustrated by our program are the return statement, break statement and if-else statement. tiny-c functions can return values. For example:
At a tiny-c prompt you could load and run the above.
The number 25 would be printed on the screen. The break statement may be used inside loops to cause early exits under desired conditions. Here's an example of the if-else construct.
If condition is true (i.e. not 0), blah;blah takes place. Otherwise, foo;foo takes place. 9. An important "data structure" supported by tiny-c (which is not illustrated by our example program) is the array. Arrays are declared like so:
str is a "character array," or "string." avail is an array of integers.
would print "thread" (w/o the quotes). strcpy is a tiny-c system library function which takes two string arguments and copies the second to the first. Here's another example which involves arrays.
Note how parentheses are used to specify which occurrence of the avail array is to be assigned a value. Very complex things can be accomplished using arrays. The binary tree program tree.ipl uses arrays and sorts data entered via keyboard or file.
|