Category : C Source Code
Archive   : TC_MAN.ZIP
Filename : SEC___12.RNO

 
Output of file : SEC___12.RNO contained in archive : TC_MAN.ZIP
.ls 2
.he 'SECTION 1.2''V.005'
.fo ''-#-''
SECTION 1.2 EDITING A TINY-C PROGRAM
.sp 2
The program you are talking to when you start tiny-c is called
the tiny-c Shell. It is a command language processing program which
lets you create, edit, run, debug, save, and restore
programs written in the tiny-c programming language.
.sp 1
In this section you will learn how to use the tiny-c Shell. There
are only twelve commands that the Shell understands so this
really isn't much of an undertaking.
.sp 2
FUNDAMENTALS OF THE SHELL
.sp 2
The tiny-c Shell announces its presence by printing the tiny-c prompt:
.sp 1
.in 10
tc>
.sp 1
.in 0
indicating it is ready for you to command it what to do.
You can now type lines of your tiny-c
program or give commands for the Shell to execute.
.sp 1
Any line you type must end with a carriage return; the Shell does nothing with
your input line until the carriage return is given.
.sp 1
After you hit carriage return, the Shell will either enter the line into your
program or execute the command. Then it gives you another
.sp 1
.in 10
tc>
.in 0
.sp 1
and awaits another line or command.
.sp 1
If you mistype before giving a carriage return, the backspace
key (the key right above the carriage return key marked with a
single arrow pointing to the left)
kills the most recently typed character. You can enter it several
times to kill several characters.
.sp 1
If you mistype a line so hopelessly that you want to do the whole
line over, then the Esc key kills the whole line.
It must be given before you hit carriage return however. You
cannot give it several times to kill several lines. It will delete
only the line which you have started, for which you have not
yet given a carriage return.
.sp 1
If you have typed a carriage return and still want to make a change,
this can be done. You must explicitly delete the line, using the
delete command described below. Then you can re-enter
the line.
.sp 1
Do not type in line numbers at the beginning of each line. tiny-c
does not use them. In fact, it does not even tolerate them.
.sp 1
To indent, use tabs or spaces. The use of indentation to show the
number of logical conditions in effect at each point in a program can greatly
improve its readability.
.sp 1
Now you've probably asked yourself, "How does the Shell tell the
difference between a command and a line of text?" Good question
... here's the answer:
.sp 2
.nf
.ce 2
A COMMAND ALWAYS BEGINS WITH A PERIOD, A PLUS,
OR A MINUS. ANYTHING ELSE IS A TEXT LINE.
.sp 2
.fi
We will cover Shell commands later; first, we will go over text lines. To do
this we need the concepts of PROGRAM BUFFER, LINE ZERO, and CURRENT LINE.
.sp 1
As you enter program lines, they
are collected into an area of the computer's memory called the
PROGRAM BUFFER. Think of the program buffer as a series of text
lines. As you enter new text lines, they go into the program buffer.
Each new line can be appended to the end of all the lines that

are already in the buffer. Or it may go at the beginning of
all the lines that are already there. Or it may go between lines
that are already in the buffer.
.sp 1
When you start out, the program buffer contains only one line.
It is called LINE ZERO. Line
zero has no text; it's just a carriage return. No matter what else you put
in the program buffer line zero is always there and is always just a
carriage return.
.sp 1
There is one particular line in the program buffer which you should
think of as the CURRENT LINE. It is where you are located ... no,
where your attention is focused ... no, actually where the Shell's
attention is focused ... in the program buffer.
Initially the current line is line zero. You can always display the
current line by giving the print command:
.sp 1
.in 10
tc>.p
.sp 1
.in 0
The `tc>' is the prompt printed by the Shell. The `.p' is the print
command typed in by you. The current line is printed on the screen
immediately after you type `.p' and hit return.
.sp 2
ENTERING TEXT LINES
.sp 2
Now, where do new text lines go? Here's the rule used by the Shell:
.sp 2
.nf
.ce 2
A TEXT LINE IS ENTERED INTO THE PROGRAM BUFFER
IMMEDIATELY AFTER THE CURRENT LINE. THE
NEWLY ENTERED LINE BECOMES THE CURRENT LINE.
.sp 2
.fi
As you recall, when you start off line zero is the current line.
Let's say you type a text line. According to this rule, it will
go into the program buffer after line 0 as line 1. It also becomes the
current line. You type a second
text line. It goes in after the current line, that is line 1, and becomes
line 2. Now line 2 is current. If all you do keep on entering text
lines, they will each go into the program buffer one after the other. This
is what you'd expect, no?
.sp 1
There are commands described below which make any line in the program
buffer the current line. As we have seen, whenever you enter a series of
text lines, each is inserted
one after the other below the current line. Thus
by moving around you can enter
text lines anywhere in the program buffer. You will discover
that this text line entry rule is simple, natural, and powerful.
.sp 2
THE SHELL COMMANDS
.sp 2
In the description of the Shell commands below,
an `n' represents an integer without any leading
plus or minus sign; for example, 5 and but neither +5 nor -5.
Exactly one blank must separate an integer `n' from preceeding characters.
.nf
.in 3
.sp 1
tc>.p Print the current line on the screen
.sp 1
tc>.p n Print `n' lines starting with the current line.
.in +11
The last line printed becomes the current line.
.in -11
.sp 1
tc>.d Delete the current line. Make the line BEFORE
.in +11
it the current line.
.in -11
.sp 1
tc>.d n Delete `n' lines starting with the current line.
.in +11
Make the line BEFORE the first deleted line
the current line.
.in -11
.sp 1
tc>+ Move down one line and make the line AFTER the
.in +11
present current line the new current line.
.in -11
.sp 1
tc>+n Move down `n' lines; the new current line is the
.in +11
present current line plus `n'.
.in -11
.sp 1
tc>- Move up one line; make the line BEFORE the
.in +11
present current line the new current line.
.in -11
.sp 1
tc>-n Move up `n' lines; the new current line is the
.in +11
present current line minus `n'.
.in -11
.sp 1
tc>.n Make the n-th line in the program buffer the
.in +11
current line; in other words go to line `n'
.in -11
.sp 1
tc>.l text
.in +11
.fi
Starting with the line AFTER the current line,
and proceeding to the end of the program buffer
if necessary, locate a line containing `text'.
If found, print the line, and make it current.
If not found, print "?", and leave the current
line unchanged. There must be one blank between
the "l" and the first character of `text'. A "^"
as the first character of
`text' means the text must begin the line. A "^"
as the last character of `text' means the text
must end the line. The text may contain blanks.
.nf
.in -11
.sp 1
tc>.l Same as above but using the same text as given
.in +11
in the previous locate or change command.
.in -11
.sp 1
tc>.c text newtext
.in +11
.sp 1
.fi
In the current line the first occurrence of
`text' is replaced by `newtext'. If `text' does not
occur in the current line, then no change is made.
In either case the resulting line is printed and
the current line remains the current line.
.sp 1
As shown, there are exactly two blanks in the command, one after the
`c', and one between `text' and `newtext'. In this form, no blanks
can be used in `text' or `newtext', because a blank is the delimiter
that separates them. However, since you may want to manipulate text
containing blanks, any character can be used as the delimiter.
.in 0
.sp 1
For example, the command can be given as:
.nf
.sp 1
.in 3
tc>.c/text/newtext
.in +11
.sp 1
.fi
In this case, blanks can be used in the texts, but /'s may not be used.
.in 0
.sp 1
An optional delimiter is permitted at the end of `newtext':
.nf
.in 3
.sp 1
tc>.c/text/newtext/
.sp 1
.in 0
Either `text' or `newtext' can be empty.
.in 3
.sp 1
tc>.c//newtext/
.sp 1
.in +11
will insert `newtext' at the beginning of the line.
.sp 1
.in -11
tc>.c/text//
.in +11
.sp 1
will erase `text' from the current line.
.sp 1
.in 0
.fi
Finally, when making a series of
identical changes, you need not retype the texts over and over.
.in 3
.sp 1
tc>.c Makes a change on the current line substituting
.in +11
.fi
for the `text' given in the most recent change or locate command
`newtext' given in the most recent change command. The carriage return
must be immediately after the `c'.
.in 0
This is all you can do with the change command. What are some other
commands you can give the Shell?
.in 3
.sp 1
.nf
tc>./ Prints the current line number, the total number of
.in +11
.fi
lines, the total number of characters used in the program buffer, and the
total number of characters unused. This reminds you where you
are in the program buffer and tells you something about how full the
buffer is.
.nf
.in -11
.sp 1
tc>.r filename
.in +11
.fi
.sp 1
Reads the file named `filename' from the disk, putting what is read AFTER
the last line. The current line is not changed. Read is like
the BASIC LOAD command.
.nf
.in -11
.sp 1
tc>.w filename
.in +11
.fi
.sp 1
Writes all lines in the program buffer to the disk, giving the name
`filename' to what is written. Write is like the BASIC SAVE command.
.nf
.in 0
.sp 2
RUNNING A tiny-c PROGRAM
.sp 2
.fi
How do you run a tiny-c program? Remember the "Do it!" dot that
we used to run `hello_world'?
.sp 2
.nf
.ce 3
A COMMAND STARTING WITH A PERIOD FOLLOWED
BY AT LEAST TWO ALPHABETIC CHARACTERS IS
EXECUTED IMMEDIATELY AS A TINY-C STATEMENT.
.sp 2
.fi
We will learn about tiny-c statements in the next chapter but you have
probably already guessed that a function call is a perfectly legal
tiny-c statement. Therefore, to execute a tiny-c function
you just mention its name after a period.
Now you know the whole truth about running `hello_world'. When
you entered
.sp 1
.in 10
tc>.hello_world
.in 0
.sp 1
what you really did, perhaps without even knowing it, is call the
`hello_world' function.
.sp 1
Can arguments be passed to functions you call with a period,
she asked in whispered tones. Is a worm icky?
Try
.sp 1
.in 10
tc>.pl "hello, world"
.sp 1
.in 0
What happened? Now, what when on to cause that to happen? You called
the `pl' system library function and passed it the argument
"hello, world", that's what went on.
.sp 1
The "Do it!" dot rule turns the tiny-c Shell into a handy-dandy personal
calculator. To add 7 and
11 and print the answer, call the `pn' (print_number) system library function
with the argument 7+11. Like this:
.sp 1
.in 10
tc>.pn 7+11
.in 0
.sp 1
Try it and see what happens. And now try some other arithmetic expressions.
.sp 1
The ramifications of the "Do it!" dot rule are really quite
surprising. As you build up generally useful functions in the program
buffer reflect on how they might be used in this immediate
execution mode. In addition to simply calling a function, you
will discover that you can also write one-line
tiny-c programs that are immediately executed by the "Do it!" dot rule.
Here's an example to whet your appetite:
.sp 1
.in 10
tc>.[char c;c='a'-1;while((c=c+1)<='z') putchar c]
.sp 1
.in 0
Don't worry if you don't understand it. Just tuck it away as a
little treat to come back to when you've learned a little more.
.sp 2
BUMPING THE TOP AND THE BOTTOM
.sp 2
Several commands can bump into the top or the bottom of the program buffer.
This is all right and in fact can be quite useful. For example, suppose
there are 50 or so lines in the buffer. Then
.sp 1
.nf
.in 10
tc>.0
tc>.p 999
.sp 1
.in 0
.fi
makes line zero current, then prints the whole buffer. When the print
command
bumps into the bottom of the buffer it stops. The
commands `.d', `+', `-', `.n', and `.l' can also bump into the top or
bottom. A convenient way to go to the last line in the program buffer is:
.in 10
.sp 1
tc>.999
.in 0
Issuing a
.in 10
.sp 1
tc>./
.sp 1
at this juncture will tell you how big your program is.
.sp 2
DELETING
.sp 2
Line zero cannot be deleted. To delete lines at the top, go to
line 1, then give the a delete command that gets rid of the
number of lines you want to discard.
Remember that delete moves the current line up, not down. In this way new
lines you type right after giving a delete will replace the deleted lines
you deleted. Neat foot work, eh?
.sp 2
LINE NUMBERS
.sp 2
When lines are inserted or deleted, lines further down in the buffer
immediately get different line numbers. So `.n' is not the best way to
hop to your favorite line landmark. The best way is to use the `.l'
command to locate it.
.sp 1
Recall the `hello_world' function:
.sp 1
.nf
.in 10
hello_world [
.in +4
pl "Hello, world"
.in -4
]
.in 0
.sp 1
.fi
The command:
.in 10
.sp 1
tc>.2
.in 0
.sp 1
makes the second line - the line with `pl' on it - the current line.
But if edits are made to `hello_world', then this line may or may not
continue to be the second line. For this reason, locate is a more
powerful program buffer navigation tool.
.sp 2
USING LOCATE AND CHANGE
.sp 2
Locating all lines with a given text is done like this:
.sp 1
.nf
.in 10
tc>.0
tc>.l world
.in 0
.sp 1
The first command hops to line 0. This is always safe since line
zero never changes. (It's always that carriage return at the
beginning of the program, remember?) The next command will match line 1 in
the `hello_world' function since "world" is part
of the name of the function `hello_world'. If you then say
.sp 1
.in 10
tc>.l
.in 0
.sp 1
.fi
you will match line 2 since "world" appears in "Hello, world".
In a longer program, continuing to enter
`.l' will locate each subsequent instance of the text you are seeking.
In this particular case, of course, you're out of "world"'s and any
more `.l''s will bump the bottom and earn you a "?".
.sp 1
Making a common change throughout the buffer is just as easy. To change
"world" to "galaxy", you would enter
.sp 1
.nf
.in 10
tc>.0
tc>.l world
.in 0
.sp 1
This will make line 1 current. Now you type
.in 10
tc>.c world galaxy
.in 0
.sp 1
"hello_world" in line 1 is now changed to "hello_galaxy". Continue with
.sp 1
.in 10
tc>.l
.in 0
.sp 1
.fi
to find the next instance of "world". This will make line 2 current since
it contains "world". Change "world" to "galaxy" by typing:
.sp 1
.in 10
tc>.c
.in 0
.sp 1
and resume with:
.sp 1
.in 10
tc>.l
.in 0
.sp 1
.fi
to find the next occurrence of "world".
.sp 1
You can continue in this fashion, changing lines selectively, until you
again bump the bottom. When you do, you will have changed all appearances
of "world" to "galaxy".
.sp 2
ERRORS
.sp 2
Sometimes ... just sometimes ... when you run a tiny-c program an error
will be detected. When this happens
the program halts and the Shell resumes control of the situation.
Your program text is intact
and you can edit it or restart it or write it to a
disk. The Shell prints three lines of helpful information about what
happened and then gives you its prompt:
.nf
.sp 1
17 -- err 26
text of bad line
<
tc>
.sp 1
.fi
The first line shows the line number on which the error occurred
and the error number. Consult Appendix A for the explanation that goes
with each error number.
The second line is the text of the bad line. Immediately
above or to the left of the < on the third line
is where the problem was discovered. This may or
may not be the real problem depending on the logic of the program.
.sp 2
SAMPLE SESSION
.sp 2
Type
.sp 1
.nf
.in 10
tc>.1
tc>.d 999
.in 0
.sp 1
.fi
This clears out the program buffer. Convince yourself of this by typing
.sp 1
.in 10
tc>./
.sp 1
.in 0
The response should be
.in 10
.sp 1
0 0 0 12000
.in 0
.sp 1
which means that there are 0 lines in the program buffer, the current line
is 0, you have used 0 characters, and there are 12000 characters left.
That's a clean slate, my friend.
.sp 1
Now type in the following transcript just as it appears. I know it sounds
as exciting as Bingo night in Cleveland but please do it.
In the next section we're going discuss the program you're
entering in detail and you really do need to get some experience with
the Shell.
.sp 1
A warning: 22 lines into this exercise you will see
.in 10
.sp 1
tc>.r random
.sp 1
.in 0
What you are doing here is causing the file RANDOM. to be read in from
the disk and appended to the end of what you have typed in so far. Be
sure that your tiny-c disk still inserted or tiny-c won't be able to find
this file.
.sp 2
.nf
tc>/* Guess a number between 1 andq100
tc>.c/q/ /
/* Guess a number between 1 and 100
tc>guessnum[
tc> int guess, number
tc> number=random(1,100)
tc> pl "guess a number between 1 and 100"
tc> pl "tyype in your guess now"
tc> while(guess != number) [
tc> guess = gn
tc> if(guess == number)pl "right!"
tc> if(guess tc> number)plq"too high"
tc> if(guess < number)pl "too low"
tc> pl"";pl""
tc> ] /* end of game loop
tc>] /* end of program
tc>./
14 14 380 11620
tc>.1
/* Guess a number between 1 and 100
tc>.p 99
/* Guess a number between 1 and 100
guessnum[
int guess, number
number=random(1,100)
pl "guess a number between 1 and 100"
pl "tyype in your guess now"
while(guess != number)[
guess = gn
if(guess == number)pl "right!"
if(guess tc> number)plq"too high"
if(guess < number)pl "too low"
pl"";pl""
] /* end of game loop
] /* end of program
tc>.r random
292
14 25 672 11328
tc>.p
] /* end of program
tc>+
/* random -- generates a random number between little and big
tc>.p 99
/* random -- generates a random number between little and big
random int little,big [
int range
if(last==0)last=seed=99
range=big-little+1
last=last*seed
if(last<0)last=-last
return little + (last/8)%range
]
int seed,last
tc>.guessnum

guess a number between 1 and 100
tyype in your guess now50


10 --- err 26
if(guess tc> number)plq"too high"
<
tc>.c/q/ /
if(guess tc> number)pl "too high"
tc>.0

tc>.l yy
pl "tyype in your guess now"
tc>.c/yy/y/
pl "type in your guess now"
tc>.guessnum
guess a number between 1 and 100
type in your guess now50
too high
25
too low
37
too high
27
too high
26
right!
tc>.w guess
2 25 671 11329
671
tc>


  3 Responses to “Category : C Source Code
Archive   : TC_MAN.ZIP
Filename : SEC___12.RNO

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/