• Aucun résultat trouvé

Evaluating Code in a Workspace Window

Dans le document til Smalltalk (Page 106-115)

IIiiiIiIJ cl.assWorkspace

3.5 EVALUATING SMALLTALK EXPRESSIONS

3.5.1 Evaluating Code in a Workspace Window

To evaluate a Smalltalk expression, type it into a Workspace window, select it for evaluation (using the normal method for selecting text), and evaluate it by selccting either the do it or print it commands.

do it print it

Evaluates the sclected cxpression (or sequence of expressions).

Evaluates the selectcd expression (or sequcnce of expressions) and prints a representation of the object returncd by the evaluation. The result returned is highlighted.

Clear the Workspace window (select everything and use cut). Now type in the following expression:

32 + 17 *2

Select the expression and choose 'print it' from the Workspace menu.

The expccted result, 98, is printed (see Fig. 3.19). Note how thc rcsult remains highlighted so thatitis already selected to be delctcd if required. What happens if we try do itinstead of print it?

Delete the result(98)of the previous evaluation.

Reselect the expression:32 +17

*

2 Choose 'do it' from the Workspace menu.

I' II ItIll I

~jl

~~~:

I'J.~ ~

::~;

":~

I

.:::

":f

.:~

'.:: .:.::.

':~::

Figure 3.19 Evaluation of expression '32+17

*

2'.

This time no result is printed. The expression was evaluated but the resulting object was thrown away. Operation do it is most useful in cases where an expression is evaluated for its side-effect rather than its result. For example, a side-effect of evaluating an expression might be a graphical operation such as drawing a spiral on the display screen with a Smalltalk pen.

Experiment. Type in other expressions for evaluation. Suggestions include

5 factorial.

50factorial.

DatedateAndTimeNow.

Pennew mandala:30diameter:360.

Transcriptshow:'this text will be written in the Transcript window';cr.

RectanglefromUser.

Note that if you select more than one expression for evaluation at the same time, only the result of evaluating the last expression willbeprinted.

3.5.2 Evaluating Existing Smalltalk Code

The selection and evaluation of Smalltalk code is not restricted to workspace windows. These operations can be performed from any text window. The System Workspace window (see Fig. 3.20), for example, contains a set of commonly used Smalltalk expressions for accessing files, system maintenance, and querying the system. By providing these in the workspace, users can quickly select and evaluate them without having to type or remember the rarely used messages.

3.5.3 Compilation Errors

When an expression is selected for evaluation, it is first compiled and then executed. The compiler detects any syntactic errors in the expression. Visual feedback is provided when Chapter 3 An Introduction to the Smalltalk User Interface 91

such errors occur by inserting an error message at the point in the expression where the error was discovered.

Type in the following Smalltalk code and evaluate it.

IsumI sumf-O.

1to:100do:[:iI sumf-sum +il.

sum

Now, make a deliberate error - remove the period following sum f- 0 and evaluate the code again. Note how the error message is placed at the source ofthe error (see Fig. 3.20) and remains highlighted so that it can be removed easily.

sum ~ 0

Nothing more expected

->

sum ~ sum +

iJ.

"*""" ...

'1 to: 10 do: [:i

I

Figure 3.20 Syntax errors.

The message 'nothing more expected~' indicates that the expression sumf -0 was complete and that 1 to: ••. is not a valid continuation of that expression.

For some errors, Smalltalk can help in correcting the error.

Correct the original error.

Now make the following deliberate error:

Remove the declaration of the temporary variableI sum I

Reevaluate the code.

The first occurrence of sum is highlightedto indicate where the undeclared variable error was discovered, and a menu is displayed (as shown in Fig. 3.21). The menu contains possible corrective actions that can be invoked. The first four entries offer the choice of defining sum as a temp (temporary variable), class var (class variable), global (global variable), or undeclared. In our case, the correct action would be to select temp and let the evaluation continue. Invoking correct it invokes the Smalltalk spelling corrector.

Assuming that you have misspelled sum, the spelling corrector searches through the system

for names that bear a resemblance to sum, displays them in a menu, and allows the programmer to select an alternative spelling.

III

to

O.

1 to: 100 do: [d

I

sum to sum +

iJ.

sum,

class var global undeclared

correct It abort Figure 3.21 Undeclared variable menu.

I

~

~.

;~

..

J~

I

Make other deliberate errors; e.g.,

1. Remove the closing] bracket.

2. Replace 'sum' by 'sam' in 'sum.-0.' (Then try invoking the spel-ling corrector to change it back.)

3.6 QUmlNG FROM SMALLTALK

Activate the System Menu and choose Quit.

A menu will appear offering 3 choices (see Fig. 3.22).

Save, then quit

Quit, without sa\'ing

Continue

Takes a snapshot of the current state of the Smalltalk system and then quits. This snapshot or Smalltalk image can subsequently be reloaded to continue the Smalltalk session.

Quits from the Smalltalk system without saving any of the modifications made to the system during this Smalltalk session.

Cancels the quit operation and continues the Smalltalk session.

Choose Quit, without saving to exit the system and avoid saving the changes you made; i.e., to discard all modifications made to the Smalltalk system during your session.

Choose 'Quit. without saving' from the Quit menu.

Chapter 3 An Introduction to the Smalltalk User Interface 93

I

sum

I

sum +- 0,

1 to: 100 do: [:i

I

sum +- sum + i].

sum.

...~~

~

l*m:immmmmmmmmmmmmmmm!,m'I",,,,,,mlm"I:::~m:-:::.I·:::::m::@d·::~~

Figure 3.22 Quit menu.

3.7 SUMMARY

This chapter has provided a first introduction to the Smalltalk user interface. In particular, we have discussed the following:

• the 'ideal' Smalltalk three-buttoned mouse,

• the use of pop-up menus,

• common system, yellow, and blue menu entries,

• window manipulation commands such as open, close, frame, and so on,

• prompter and confumer windows,

• text editing commands,

• commands for expression evaluation, and

• how to exit from Smalltalk.

3.8 EXERCISES

Some of the exercises that follow involve the evaluation of Smalltalk code. When a semantic error is discovered during the evaluation of Smalltalk code, execution halts and a notifier window is displayed. The notifier displays a message describing the cause of the error.Ifyou are not familiar with the use ofnotifiers and debuggers to debug Smalltalk code.

we suggest that, until these topics are fully covered in Chapter 5,you simply close the notifier window (select close from the blue button menu) and debug your code manually.

1. Type in the Smalltalk code fragments generated from exercises 1-4 of Chapter 2 and execute them.

2. Change the layout of the windows on the display by resizing and moving all visible windows.

3. Move the contents of the system workspace into the system transcript.

Can the transcript hold it all?

4. Create several small workspaces in-side of (or on top of) a larger one.

Then activate the larger workspace.

Can you make the smaller work-spaces visible again?

5. While a piece of code is executing in one window. can you activate a sec-ond window? Try evaluating 120 fac-torial.

6. What do the yellow button menu commands accept and cancel do in a Workspace window? Try typing in some text to a workspace. selecting accept. typing in some additional text. and finally selecting cancel.

7. The delete key (sometimes, the backspace key) is used to delete a text selection. What happens if delete is used without selecting text?

8. What happens if "do it" or "print it" are selected when no text selec-tion has been made?

3.9 GLOSSARY

pointing device operations

selection A fundamental interaction sequence used for many tasks within the Smalltalk environment; e.g., to indicate the point at which text, when typed from the keyboard, should be inserted, or to change the active window. Two activities are involved in making a selection: first. moving the cur-sor on the screen to the desired position, and second. confirming the position by clicking the red button.

mouse interaction terminology single clicking The process of pressing and

then immediately releasing a mouse but-ton.

double clicking The process of pressing and then immediately releasing a mouse button twice in quick succession.

I 'ql

9. Smalltalk systems provide alternative

"control key" sequences for perform-ing tasks such as editperform-ing, under-lining, changing fonts, and changing emphasis, and also provide short cuts to avoid typing often used symbols.

For example. on the Macintosh, both ParcPlace™ and Apple Smalltalk sup-port the standard keyboard equiva-lents Ctrl x, Ctrl c, Ctrl v. and Ctrl z for cut. copy, paste, and undo respec-tively.

Determine the effect of the control key sequences Ctrl t and Ctrl f in your Smalltalk system. Do the same for the following list. In each case, a section of text should be selected within a text window before typing the control key sequence.

Ctrl O. Ctrl 1. Ctrl 2, ... Ctrl 9, Ctrl -, Ctrl b. Ctrl w,

Ctrl Shift -. Ctrl Shift b.

Ctrl [, Ctrl (. Ctrl <. Ctrl ", Ctrl •

extended selection A two part selection pro-cess used, for example. to select the start and end points of a text selection, the top left and bottom right-hand corners of a frame for a window. or to activate and choose a selection from a pop-up menu.

dragging The process of moving the mouse with the mouse button depressed. Used to perform an extended selection operation where a two part selection process is re-quired.

Chapter 3 An Introduction to the Smalltalk User Interface 95

menus

System Menu Allows the programmer to choose one of several global system ac-tions such as quitting Smalltalk, opening new windows, saving (or taking a snap-shot of) the current state of the Smalltalk system, and restoring (or redrawing) the display.

Blue Button Menu A pop-up menu that al-lows the programmer to choose one of several actions to manipulate the selected window. These include moving, framing, collapsing. and closing the window.

mouse buttons

red button Mouse button used to select in-formation.

yellow button Mouse button used to activate a menu for editing the contents of a win-dow. The cursor must be within the boundaries of the desired window when the button is depressed.

Smalltalkwindows

Workspace Workspace windows used as scratchpad areas where fragments of Smalltalk code can be entered, stored, edited, and evaluated.

System Workspace A Workspace window that acts as a repository for Small talk expressions (or expression templates) that perform common Smalltalk tasks. Prevents the programmer from having to remember and retype often used expressions.

System Browser A window for carrying pro-gramming activities like viewing the Smalltalk class library, modifying exist-ing classes and methods, and addexist-ing new classes.

blue button operations (window menu}

new label Command used to change the name in the window's label tab. A prompter appears to query the user about the new name.

Yellow Button Menu A pop-up menu that al-lows the programmer to choose one of several actions to be performed on the contents of the selected window. The menu choices depend on the type of window. For text windows, it typically includes text editing operations such as cut, copy. and paste. and commands to evaluate Smalltalk code.

blue button Mouse button used to activate a menu for manipulating the window itself.

The cursor must be within the boundaries of the desired window when the button is depressed.

System Transcript A window primarily used by the Smalltalk system and by program-mers as a notice board to display error in-formation or print messages describing the progress of a Smalltalk activity.

Confirmer A window used to request a 'yes' or 'no' type of answer to some question.

Confirmers are most often used to ask the user to confirm whether or not a request for some undoable action should be carried out.

Prompter A window that requires a "fill in the blank" response from a user - used to extract textual information in response to a user command.

under Command used to select a window that is under the active window and the cursor.

The obscured window is made visible and also becomes the active window.

move Command used to relocate a window on the screen. A selection operation is re-quired to specify the new location of the window. The window is not resized by this operation.

frame Command used torelocate and resize a window. As with opening a new window from the system menu, an extended se-lection operation is required to specify the top left and bottom right comers of the window frame. Thus the window is both resized and repositioned.

text editing terminology

text Insertion point The point (indicated by a caret) within an active text window where text will be inserted either by a paste op-eration or by typing characters on the keyboard.

yellow button textediting commands again An editing operation that repeats the

last replace, copy, or cut operation.

Can be used after a replace to repeat the operation on the next occurrence of the replaced text. With the shift key de-pressed, all occurrences are replaced. Can be used after a cut or copy operation to find and select the next occurrence of the text that was cut or copied.

undo An editing operation that reverses the effects of the last edit command. For ex-ample, undo can be used after a cut to paste back the deleted text or after a paste to delete the pasted text and restore the original text (if any).

yellow button evaluation commands do it Evaluates the currently selected text

without displaying the result.

collapse Command used to collapse a window so that only its label remains on the screen. The label may be placed at any de-sired location on the screen. A window that has been collapsed may be subse-quently reopened by selecting frame from the window menu.

close Command used to remove a window from the screen permanently. All record of the workspace window is lost from the system. The window cannot be subse-quently restored.

text selection The process of selecting a re-gion of text for subsequent manipulation.

Carried out using an extended selection operation.

copy Makes a copy of (or remembers) the cur-rently selected text.

cut Deletes the currently selected text.

paste Pastes a copy of the text from the most recent cut or cop y operation so that it replaces the selected text in the active window. If there is no selected text to re-place, the text is inserted following the current insertion point. Paste operations may be repeated to paste the same text into a document more than once.

print it Evaluates the currently selected text and displays the result.

Chapter 3 An Introduction to the Smalltalk User Interface

4

Dans le document til Smalltalk (Page 106-115)