Calculator mode. 4tH 4tH Forth compiler
Реклама
Реклама
CHAPTER 4. A GUIDED TOUR
4.9
Calculator mode
60
Startup 4tH and enter the editor. We’re going to show you this baby can do a lot more than just editing:
OK
.( Hello world!) cr
Hello world!
OK
Hey, that is a lot like the very first program we ran! Yes, it is. You can enter a subset of the 4tH language at the editor prompt, so you can test simple programs like this without getting into the ”edit-compile-run” cycle. You can even make some simple calculations:
OK
23 45 + .
68 OK
Simple? Aren’t the operators and operands entered in the wrong order? No, they aren’t.
4tH uses Reverse Polish Notation, which is also used by HP calculators and the Unix ”dc” command. 4tH has even eight built-in variables in which you can store numbers:
23 45 + A. !
OK
A. ?
68 OK
It even understands binary, hexadecimal and octal numbers:
23 45 + binary .
1000100 OK
1000011111 hex FACE octal 765 + + decimal .
65250 OK
This is called the ”calculator mode” and you don’t have to do anything if you want to use it. It is part of the editor command set. You can mix editor commands and calculations
9 as you like. Nice extra, isn’t it?
4.10
Epilogue
This concludes our tour of the 4tH interactive mode. We hope we’ve shown you what you can do with it and how to use it. Of course, you don’t have to use 4tH’s interactive mode. It will happily reside and cooperate with existing external IDE’s, editors and the like. But if memory is tight and you have nothing else, 4tH will prove to be a completely selfcontained environment.
If you’re still wondering what you can do with Forth and 4tH in particular, let me tell you this: if you worked your way through this tour, you’ve been working with Forth all the time. The entire editor is a 4tH program, embedded in the 4tH executable, taking up less than 3 KB. It is run by the very same interpreter as your initial ”Hello world!” program.
Have fun!
9
The full calculator command set is listed in the ”Editor reference guide”.
Chapter 5
Frequently asked questions
Q
UESTION
:
A
NSWER
:
Why has exec_4th() this enormous switch() statement? Why wasn’t a structure used with pointers to functions?
That one was built too, but it proved to be upto four times slower on all platforms. Which is perfectly understandable, because every time you evaluate a token, you have to take the overhead of calling a function into account.
Q
UESTION
:
A
NSWER
:
Why are the tokens of exec_4th() listed in a random order?
We have statistically analyzed which tokens are used more often. They are up front. Some C-compilers generate a jumptable. Others generate a repeated "if .. elif .. endif" construction. Compilants produced by the latter perform better when tokens are ordered this way.
Q
UESTION
:
A
NSWER
:
Do I have to use 4tHs builtin editor?
No. You can use any editor you like. 4tH will happily compile all vanilla
MS-DOS, MS-Windows and Unix text files and most block files as well.
Just use your favorite editor to create your source and compile it at the command line. Note the editor is capable of exporting vanilla text files.
Q
UESTION
:
A
NSWER
:
I get ”I/O error” all the time. What am I doing wrong?
If you’re working with Windows and you’re trying to compile some example programs, type ’S’ and enter the relative path to the example file, e.g. ”examples/romans.4th”. If you’re working on some other
Operating System, be sure you’ve also installed the library files. Now go to the directory where you have installed them, e.g. ”cd /home/john/4th/lib” and type ”cd ..”. From this working directory use the relative or absolute path to your source file when you compile it. Be sure the path of the environment variable DIR4TH is correct.
61
CHAPTER 5. FREQUENTLY ASKED QUESTIONS 62
Q
UESTION
:
A
NSWER
:
When I use 4tHs builtin editor I get ”Cannot open file” all the time. What am I doing wrong?
Probably nothing. 4tH just informs you it cannot load the file you issued at the main menu or the command line. This is always the case when you start a new file.
Q
UESTION
:
A
NSWER
:
When I try to use 4tHs builtin editor or run 4tsH I get ”Bad object” all the time. What am I doing wrong?
You’re probably running 4tH under a 64 bit operating system. The only thing you can do is to recompile 4tH (see sections 25.7 and 25.8).
Q
UESTION
:
A
NSWER
:
When I try to load a screen file or execute an .hx file, 4tH doesn’t seem to take the DIR4TH environment variable into account.
The DIR4TH environment variable is used by the compiler when it tries to pull all source files from their different locations at compiletime. Note that it only reads files. When the editor would start writing files where you don’t expect it to, things might get very dangerous. The .hx files are executables and your Operating System already offers several ways to find them.
Q
UESTION
:
A
NSWER
:
When I open up the editor in 4tH, it takes most 4tH code like an actual
Forth compiler, but not my colon definitions. Why?
The 4tH editor mimics Forth, that’s true. But it is actually a Forth like environment on top of 4tH. It may seem like you’re working on a Forth prompt, but you’re not. You can use the editor only for editing or some quick calculations, but if you want to use the full capability of 4tH, you’re stuck to the menu.
Q
UESTION
:
A
NSWER
:
Can I load shared libraries with 4tH and call the external functions defined there?
No. 4tH is (almost) entirely composed of ANSI-C and since ANSI-C doesn’t define loading and using shared libraries you can’t. Furthermore, it would violate 4tH’s ”uncrashable” design objective, since it is impossible to trap all possible errors when you call external functions. However, if you know C, it shouldn’t be too difficult to add that functionality yourself.
Q
UESTION
:
A
NSWER
:
When I compile a 4tH program, I get messages like: ”Word 381:
Undefined name
” . How do I know where that is?
4tH is a single pass compiler and keeps the partial compilant in memory for you to examine. Simply decompile it by pressing ”D” or add the ”d” option to the command line. Section 11.22 will give you all the details.
Part II
Primer
63
Chapter 6
Introduction
Don’t you hate it? You’ve just got a new programming language and you’re trying to write your first program. You want to use a certain feature (you know it’s got to be there) and you can’t find it in the manual.
I’ve had that experience many times. So when I wrote 4tH I promised myself, that would not happen to 4tH-users. In this manual you will find many short features on all kind of topics. How to input a number from the keyboard, what a cell is, etc.
I hope this will enable you to get quickly on your way. If it didn’t, email me at ’[email protected]’. You will not only get an answer, but you will help future 4tH users as well.
You can use this manual two ways. You can either just get what you need or work your way through. Every section builds on the knowledge you obtained in the previous sections.
All sections are grouped into levels. We advise you to use what you’ve learned after you’ve worked your way through a level.
There are six levels. First, 4tH fundamentals. It assumes a working knowledge of programming and covers the basics. Second, 4tH arrays. We’ll try to explain to you what an address is and teach you basic string handling.
Third, 4tHs Character Segment. We’ll explain you how it is laid out and what you can do with it. Fourth, 4tHs Integer Segment and Code Segment. We’ll explain you how it is laid out and what you can do with it.
Finally, advanced programming techniques. First the builtin facilities 4tH offers and after that the extra features the 4tH library offers. We’ll teach you how to program multilevel exits, write interpreters, use jump-tables, emulate floating point calculation and a lot more!!
I don’t think it is enough to teach you Forth, from which 4tH was derived, but you can always get a good textbook on Forth, like "Starting Forth" by Leo Brodie. Have fun!
64
Изтегляне
Реклама