The Mops Terminal Lesson 1 Contents

The Mops window that opens when you doubleclick the PowerMops application normally appears like this:

PowerMops.png The Mops window is divided through the middle. The lower half is a "Terminal" interface, which we will be discussing here. Comparing the Mops Terminal to the OSX Terminal, the Mops Terminal is limited to the Mops system rather than the POSIX commands of the OS X Terminal application.

The lines of information beginning with "Room in data area ..." are optional and provide info about PowerMops' memory availability. The appearance of this information is controlled by the "Free Space" item of the Show Menu.

As well as the Apple menu, you will see five menu titles. You won't be using all the menus right away, but acquaint yourself now with the contents of each menu.

The Apple menu contains the usual items. The File menu is much like the File menu in other applications, but with a special item (Load), which you'll use later for loading text files containing your program code.

The Edit menu has the usual items - cut, copy, paste and clear (delete), along with their keystrokes. Notice that Show>HFS Paths menu is identical to the OSX Terminal's $PATH command.

The remaining menus, List, Show and Utilities, contain many operations that will be useful in the writing and debugging of Mops programs.

Learn more about the menu below.

Sending Commands
For now, ignore the top part of the Mops window. In the lower part, you will see some information about how much memory is available in different areas—we'll explain these values below.

Below these lines, you will see a flashing cursor, like you might see in a text editor. Type something, and you will see that this window is indeed a text editing window. You may type and edit text here, as in any text editor.

Just for fun, type or select-drag this text into the lower half of the PowerMops window:

8 SPACES

then press the «enter» key. Eight space characters (the length of the command) are programmatically typed into the next line. If you pressed the «return» key this will not work, nothing happened except a lineending character was typed. On space-saving Apple keyboards with no number pad, shift-return types the «enter» character. The «enter» key is discussed in detail below.
Now try this one:

36 emit CR

The dollar ($) character appears, since that is ASCII character number 36. The EMIT word tells Mops to PRINT an ASCII character. Notice that Mops does not care about the case of its own words. This gives you flexibility in your programming to identify words by case. We also used the word CR to programmatically put the cursor in the next line to avoid confusion with our exercises.

You can use multiple commands on one line:


36 EMIT  8 SPACES  38 EMIT  CR
Forth words and values must be separated by at least one space to be recognized as a word. Space characters are therefore delimiters. In the above example, we used two spaces to "clump" our commands for better readability.

About the «enter» Key
As we saw in the previous lesson, the use of the Enter key has a specialized use in Mops. In most text editors, Enter is treated the same as Return. At the hardware level, Enter and Return have different scan codes, but the Enter key has been dropped on many keyboards. In Mops, however, Enter causes Mops commands to be executed.

We said earlier that Mops behaves like a dictionary. When you opened the PowerMops Application, the Mac automatically loaded the basic Mops vocabulary into its memory. Each time you type a word—any group of text characters—and press «enter», Mops searches through its dictionary for that word and carries out whatever instructions are associated with it. If the word you type is not in the current Mops dictionary, a message appears on the screen to advise you that Mops could not find the word. We'll try it in a moment.

If you're familiar with another computer language, note that in Mops, as in other Forths, we use the word "word" in a different way from normal computer terminology. A Mops "word" is any group of text characters, terminated by white space (a space, tab or carriage return). In C you might say "if(a>b)foo(bar);" but in Mops that would be one word, because it contained no white space. Characters which are punctuation or special characters in other languages can be part of a Mops word, since the only thing that terminates a word is a white space character.

Now we'll demonstrate the difference between the Return and Enter keys.

First type a nonsensical word and press Return

gnarly «return»

The line just sits there in the Mops window, exactly as if you'd typed it into a text editor. Nothing else happens. Now try it again, but this time use «enter»:

gnarly 

Error # -13 : undefined word
gnarly
       ^
Current object: TW    Class:
Stack: empty
Return stack: Depth 42
 9511662 $9122EE ?NOTFOUND
 9502140 $90FDBC NUMBER
 9522950 $914F06 INTERPRET

A few things happened here. Since you used «enter», Mops tried to interpret "gnarly" as a command. The message coming back from Mops indicated that the word you typed in was undefined. This means that the name was not found in the dictionary—in that split second, Mops compared the name against over 1100 words in the Mops dictionary.

And finally, the flashing cursor appears below the message lines, indicating that Mops is waiting for you to type another command.

Now try typing the number 999, and then press «enter» . This time no message will appear from Mops—it has simply accepted the number you typed in. Where is it? It has been placed on a stack. You'll be able to see it in the upper part of the Mops window, which is a display of the stack:

Stack: depth 1
999  $    3E7
Now type the number 888 and press «enter». You will see that it is placed on top of the 999:
Stack: depth 2
888  $    378
999  $    3E7
The stack depth is now two, since there are two numbers there. The numbers to the left are decimal, and the numbers to the right are the same numbers in hex.

You can now change the numeric base that Mops is using. Here you will change the base from decimal (the base that Mops starts in) to hexadecimal (base 16, hex for short). Type

hex «enter»

You have set your default base system to 16. To display the top number in hex, type . and press «enter». The top number (888) is taken off the stack and displayed in the Mops window as 378.

Any new numbers you enter are now assumed to be hex numbers. If you type in 3E7 «enter», it is displayed on the right side with the decimal 999 on the left. (If you are not very familiar with hex numbers, we give a fuller description in lesson 13). To change back to decimal, simply type

decimal «enter»

Now try the same thing, typing HEX and DECIMAL in upper case. The results should be exactly the same. That's because Mops makes no distinction between upper and lower case letters when it comes to words in its dictionary. Internally, everything is converted to upper case.

Now type an undefined word again. The error message lines will again appear, but also notice that the stack becomes empty. This is another part of the Mops error handling—the stack is emptied on an error.

The Menu Bar
PowerMops has a simple set of menus, yet the features built into them make writing code, compiling, and debugging rather easy. When you start PowerMops, a "front end" brings in the basic Mops menus - Apple, PowerMops, File, Edit, List, Show, and Utilities.

To help you understand the functions of each menu selection, we'll describe the action of each menu item. We'll also explain the built-in utilities, which can make you more productive in program creation and debugging. QuickEdit is a dedicated editor to interface with PowerMops.

PowerMops Menu
About PowerMops… Displays the Mops version number and the date that version was released.
Quit This is the equivalent of the Mops command bye. All files are immediately closed and the application quits.
File Menu
Load… Allows you to load text source files on top of the Mops dictionary currently in memory (the same as issuing the "// filename" command from the Mops prompt). The standard file dialog box appears, from which you can select the file to load. As the source file loads, it is compiled by Mops. If your program requires the loading of several text files, the files must be loaded in the proper order (so that dependent words and classes are loaded after the words or classes they depend on). The word NEED makes it easy to ensure everything is loaded in the right order.
Save Dictionary Copies to a disk a compiled image of a program you have in memory. It saves the image to a file with the same filename as is shown at the top of the Mops window. For this reason, Save should be used with care. If you have added code to Mops.dic and wish to save the image as a separate application, then use the Save As… selection, below; otherwise, your Mops.dic file will contain your additions.
Save Dictionary As… Lets you copy to a disk a compiled image of a program you have in memory (you are prompted for a new filename, and you may save to a different disk, if you like). For example, when Mops.dic was originally built, its compiled image was saved with this command. To start a program saved in this manner, double-click the appropriate Mops document icon from the desktop, just as you start Mops.dic. It is recommended that you save programs in this manner only after their source code has been sufficiently debugged. Until then, you'll want to take advantage of the interactivity of the Mops interpreter while debugging source files by maintaining the code as source files and Loading them to test how well the program runs.
Edit Menu
Cut, Copy, Paste, Clear and Select All These perform all the customary editing functions in the Mops window.
HFS Paths... Opens an editing window for the file searching paths. This is actually easier to do in QuickEdit. See a similar item in the Show menu.
Preferences This does nothing for me (as of Mops 6.1). Probably not yet implemented.
List Menu
Words Presents a running list of all words in the current dictionary, starting with the word most recently defined (i.e., highest in memory). The name field of each dictionary entry is displayed along with the hex address of the name field. To pause the list, press any key once. To restart the list, press the Spacebar; to cancel the list display, press any key other than the Spacebar.
Objects… Opens a QuickEdit list of all objects defined in the current dictionary in memory. Objects are listed alphabetically with its class in the right column.
Classes… Opens a QuickEdit list of all classes defined in the current dictionary in memory. The classes are listed alphabetically with parent class(es) in the right column.
Show Menu
HFS Paths Displays in the Mops window the current paths which will be searched when you ask Mops to open a file.
Free Space Displays in the Mops window the amount of memory available for new dictionary entries, as well as the condition of the heap. A typical listing is:
Room in data area of dictionary:                200000
Room in code area of dictionary:               1499996
Distance to top of mainData range (neg is OK):   48460
Distance to top of mainCode range:             -352680
Total heap (no purge):                         20971520
Largest block (purge):                         20971520
The Total heap figure is the current available heap if you do nothing to purge modules from it. The Largest block figure represents the largest amount of heap available in a contiguous block if you purged all extraneous blocks from the heap.
Show Module Status Lists all modules defined in the dictionary in memory, and indicates which one(s) are currently on the heap by printing their load addresses. Modules are locked while executing to prevent their being removed from the heap at an inopportune time. A typical listing is shown below:
PATHSMOD       modHdl    FA0D4 -> 804800 
seg#      10 
flags     2 
install?  false
Utilities Menu
Echo During Load Displays every line of text from a source file as it is being loaded and compiled into Mops. Use this feature in the early stages of program development to aid you in discovering exactly where your bugs are cropping up. By following the load, line-by-line, you can see exactly where Mops runs into trouble and stops the load. Once your code is sufficiently debugged, you can turn off echo to speed up loading. This selection is identical to the Mops command +echo . If you select Echo During Load, a check mark appears next to the menu listing. Selecting it again toggles the feature. You can pause an echoed load by hitting a key, while quiet loads do not pause to permit type-ahead.
Clear Stack Clears the stack. This command is also available in QuickEdit.
Clear Window Clears all text from the Mops window. This command is also available in QuickEdit.
Install The use of Install is explained in Installing an Application.
Purge Modules Clears the heap of all modules loaded by your program.

 

Previous Chapter Contents Next Chapter
This page online:  http://PowerMops.com/MopsManual/Lessons/Chapter1.html