13.9 Menus. 4tH 4tH Forth compiler
Werbung
Werbung
CHAPTER 13. SPECIAL LIBRARIES 213
Note that only the use of integers is supported in these interpreters. If you want floating point support you have to include ZEN floating point support
11 before including interprt.4th, either: include lib/zenfloat.4th
include lib/zenfpio.4th
include lib/interprt.4th
Or: include lib/zenfloat.4th
include lib/zenans.4th
include lib/fpin.4th
include lib/fpout.4th
include lib/interprt.4th
The difference between both form is that the former only supports floating point entry in the scientific form. Consequently, the latter is more suited for humans like you and me.
Don’t let anybody ever tell you you can’t make interactive applications with 4tH. As you have seen, you can with very little effort.
13.9
Menus
The menu is a well-known user interface on the console. Typically, it displays a list of options from which the user can choose. 4tH allows you to create such menus with very little effort. First, include the appropriate library file: include lib/menu.4th
Each option needs his own word. Note that an option - any option - can’t take nor leave any items on the stack:
: helloworld ." Hello world!" cr ;
: calculate ." 1 + 1 = " 1 1 + . cr ;
: bye ." Goodbye!" quit cr ;
Now we have to define our menu. The first item is the title of the menu: create MyMenu
," My beautiful program"
All subsequent entries are options. Each option consists of a description and an execution token. The menu is terminated by ”NULL”: create MyMenu
," My first little programs"
," Hello world" ’ helloworld ,
," 1 + 1" ’ calculate ,
," Exit"
NULL ,
’ bye ,
11
See section 12.21
CHAPTER 13. SPECIAL LIBRARIES
Now all we have to do is to bind it to the main menu and run it:
MyMenu MainMenu
RunMenu
214
That’s it! But what if we want to nest menus? Well, a nested menu is simply an option which opens up a new menu. For each additional menu we need to do two things. First, define a variable: variable NextMenu
The variable will hold the address of the menu table you will declare later on. Second, define a word that allows us to enter the menu:
: >NextMenu NextMenu >Menu ;
That particular word is needed when we add an extra option to the main menu, which gives the user access to the sub menu we’re about to define: create MyMenu
," My first little programs"
," Hello world"
," 1 + 1"
," More programs" ’ >NextMenu ,
," Exit" ’ bye ,
NULL ,
’ helloworld ,
’ calculate ,
Next, we have to define the sub menu itself: create MyNextMenu
," More little programs"
," Bottles of beer" ’ bottlebeer ,
," FooBar" ’ foobar ,
," Return to main" ’ >MainMenu ,
NULL ,
Hey, where did ”>MainMenu” come from? Well, it’s part of the library, so it’s always at your disposal. And it’s included in this menu, because otherwise the user would be stuck in this menu and wouldn’t be able to return to the main menu. Finally, you have to add the menu by feeding the table and the variable you declared to ”AddMenu”:
MyNextMenu NextMenu AddMenu
Done! Next time you execute ”RunMenu”, you’ll see have an extra option giving you access to the sub menu you just created. BTW, You can have as many menus as you like and can nest them as deep as you want.
Another nice feature is that when an error occurs during execution, the menu will display the appropriate message and simply return. In other words, it won’t bomb out, no matter how bad a programmer you are.
Herunterladen
Werbung