Tricks of the Trade

In this chapter, we'll cover a few last details on using Alice to your greatest advantage.

Expressions

An expression is anything you can fill into a Value or Condition placeholder. Expressions are stored in a special internal format (based on Polish notation), much different from the way that expressions are displayed on the screen. In essence, humans write expressions in a much different way than the computer does. This means that Alice has to make special allowances so that people can work the way they are used to.

For example, when you are typing in an expression, Alice does not do any error-checking until you have indicated that you're finished (by moving off the placeholder with TAB, [CTRL-[<-]], [ENTER], etc. or by typing whatever comes after the placeholder). This is the one time in which it is possible to create a syntax error in an Alice program -- you could enter an expression that contained an error and Alice wouldn't find the error until you finished typing the expression. At this point, Alice will issue an error message saying that the expression contains a mistake, and will highlight the entire expression so you won't forget there's a problem.

You can correct the problem right away or leave it for a while and come back later. However, you won't be able to run the program while the error is still there, since Alice believes it is pointless to run something that it knows is wrong.

To change an expression, you can select (highlight) the code in the usual way, CLIP it, then start again. You can also issue the EDIT command to change the expression. Before you do this, you must highlight the part of the expression that you want to edit. Once the correct part of the expression is highlighted, you can issue the EDIT command by pressing [ALT-E] or by choosing the Edit selection from the Changes Menu. Alice will then allow you to change the selected expression.

You may want to note that you can apply the EDIT command on small things without first highlighting them. Small things (what we call leaves) include strings, constants, comments and variable names. You can even position the cursor in the middle of a leaf and press the EDIT key. This will start you editing at the place you put the cursor.

When you are editing an expression, there are a few special keys to remember. We have already seen that backspace erases the character before the cursor. The [->] key moves the cursor left without erasing the character. If you move the cursor backwards, a few characters then start typing again, the material you type will be inserted in the middle of the expression.

If you are in the middle of an expression, the [<-] key moves the cursor to the right. Also, [CTRL-D] will erase the character that the cursor is resting on.

As always, when you are finished putting in the changes, you must indicate that you are done by moving off the expression (TAB, [CTRL-[<-]], [ENTER], etc.).

Load in the old ``squares'' program and move the cursor to the declaration of the variable i. Put the cursor right on the name i and press [ALT-E]. This lets you edit the name i. By pressing [CTRL-D] delete the i and replace it with j, then press TAB to indicate that you are finished editing. You will find that every reference to i in the program turns into a j. If you EDIT the name in a variable declaration, the name of that variable will change everywhere it appears in the program. This is the simplest way to change the name or the spelling of a variable. In fact, it's the only ``global search-and-replace'' that you are ever likely to need in program editing.

Adding Declarations

Often when you are entering the instructions of a program, you will find that you need to use a symbol that you have not yet declared. For example, you may be entering a for statement and find that you have not declared a variable that you can use as the for counter.

If you find you need to add a new declaration, all you have to do is type one of the keywords

label

const

type

var

procedure

function

followed by a space. You can do this no matter where you are in the program, provided that you are not currently typing in a comment or a string constant. Alice will immediately go to the appropriate declaration section, and you can then add the declaration you need.

To get back to where you were, choose the Former Cursor Loc selection from the Move Menu.

Comments

You can fill any Statement or Declaration placeholder with a comment instead of a normal statement or declaration. To do so, simply type a ``{'' when you are positioned on the placeholder. Alice will lay out a

{Comment}

template and you can fill the placeholder in the usual way.

Note that Alice does not let you place comments in the middle of instructions -- you can only have comments in predetermined places (e.g. the end of declarations) or on lines all by themselves. Furthermore, you cannot have a comment that extends over more than one line. If you have several lines of comments, each line must be enclosed in its own braces. These limitations are forced by the special internal format that Alice uses to store programs.

Calling Subprograms

When you are creating a function or procedure call, Alice tries to make your job easier by telling you the names of the parameters used in the call. For example, suppose you have declared a procedure with

procedure myproc(inchar:char; var outchar:char);

This procedure has two parameters, a value parameter named inchar and a var parameter named outchar. Now suppose that you want to call this procedure somewhere in this program, so you begin the procedure call with

myproc(

When Alice realizes that you are making a call to myproc, it will help you out by giving you the name of the first parameter. You will see Alice display

myproc(inchar:char,var outchar:char);

The placeholders that Alice uses for the procedure arguments are the declarations for the corresponding parameters. In this way, Alice reminds you of what each argument should be.

Adding an ELSE to an IF

Normally, if statements are created without an else part on the end. To add an else, just type else on an empty Statement placeholder inside the statements of the if. Alice will add the template

else begin
    Statement
    end;

on the end of the if, and delete the empty Statement placeholder.

DOWNTO FOR Statements

When you type the keyword for, Alice lays out the template

for variable := start to finish do begin
    Statement
    end;

In order to obtain a downto in place of the to, type downto instead of for to begin the statement. Alice will lay out the template

for variable := start downto finish do begin
    Statement
    end;

Another way of making this change is simply to type downto inside the for loop, in the same way that you add an else to an if.

You can also change normal fors to downtos from the Special Changes menu. First call up the Miscellaneous Menu by pressing [SHIFT-F9] and choose the Special Changes selection. This gives you the Special Changes Menu, commands for converting one type of statement into another. The for-to-downto conversion is one of the choices on this menu.

Logging Output Into a File

There are times when you want to save the output from a program in a file. To do this, summon the Run-Time Menu and choose the selection

Log Output to File

Once you have selected this, the output from any program you run will be saved in a file called ``log'' under your current working directory. The output will also be displayed on the screen as usual. To turn this off, summon the Run-Time Menu again and choose the selection

Don't Log Output to File

The first time you run a program with ``logging'' turned on, the current contents of ``log'' will be deleted and replaced with your program's output. If you keep ``logging'' turned on, output from subsequent programs will be APPENDED to the ``log'' file. This lets you capture the output from several programs by running them one after another.

If you want the output of a program to replace the current contents of ``log'' instead of being appended, turn logging off, then turn it back on again.

Note that once you have saved your output in the ``log'' file, you can print it out using the standard routines for sending a file to a printer (if you have a printer attached to your machine).

Alternate Command Format

All Alice commands can be issued by selecting them from a menu. Most commands can also be issued by typing the right key or key sequence (e.g. pressing [ALT-S] issues the SAVE command). Alice also offers a third technique for issuing commands. It works like this.

The [CTRL-X] key is called the command key. When you press it, Alice will print

>

on the bottom of the screen and wait for you to type a command. In this case, the command will be a single word standing for a standard Alice command. For example, press [CTRL-X] and type

save

then press [ENTER]. This issues the SAVE command. You can also say

save test.ap

and avoid having to go through the usual menu business to choose the save file.

You may find that you prefer this way to issue commands in a number of cases. For example, if you can't remember the key binding for something like HIDE and can't remember what command menu it's on, you may find it easier to press [CTRL-X] and type hide.

Alternate Indentation

By default, the end that ends a begin-end block is indented to the same degree as the statements in the block. For example, you have seen

while Condition do begin
    Statement
    end;

Alice can provide a second style of indentation for those who do not like the default indentation. In this style, the end lines up with the statement that began the begin-end block, as in

while Condition do begin
    Statement
end;

If you prefer this type of indentation, use the -i option on the command line that invokes Alice, as in

alice -i

In this style of indentation, the begin and end in a procedure or function template will line up with the statements of the subprogram, all indented from the subprogram heading.

Screen Shot dashi not available

Note that the indentation style is associated with the Alice session, not with files containing Alice Pascal programs. This means that you can create a program using the default indentation, then edit it later in a session that uses the -i indentation.

HIDE and REVEAL

The HIDE and REVEAL commands are not quite opposites of each other. If you HIDE a piece of text, you attach a

 ... {Comment} ...

to the code. When you reveal the hidden code, this comment will remain in your code. Thus you don't quite get back to things the way they were before the HIDE took place.

If you want to get rid of the comment that was created when the code was hidden, move the cursor to the opening brace of the comment and press [F10]. This should highlight the comment and the code that was hidden. Next, press [F3] to call up the Delete Menu and choose the Raise selection.