10.8 Using commandline arguments. 4tH 4tH Forth compiler
Reklama
Reklama
CHAPTER 10. INTEGER SEGMENT AND CODE SEGMENT app 0 th @ app 1 th @ + out !
Nothing to it..
123
10.8
Using commandline arguments
A host program can also transfer an array of strings to the 4tH environment. Usually, commandline arguments will be transferred this way, although any string array with the correct format can be used. If so, you will probably find it in the documentation of the host program.
If you are familiar with C, the concept is probably quite easy to understand. There are two words, ’ARGN’ and ’ARGS’. ’ARGN’ will leave the number of commandline arguments on the stack. The commandline arguments itself are numbered from 0 to (ARGN - 1), e.g.
argn 0> if argn 0 do i args type cr loop then
\ test if there are
\ any arguments
\ loop through them
\ print them
First, we test if there are any commandline arguments. Second, if that is the case we loop through them with ’ARGN’ as upper limit. Why? Since "ARGN 1- ARGS" is always the last valid commandline argument!
Third, when ’ARGS’ executes, it takes a number from the stack as index. Then it leaves the address of the Character Segment (where it is temporarily stored, usually PAD) and its count on the stack.
Using the expression "TYPE CR" we can print that string. Because it is already stored in the Character Segment we can treat it like any other string. Remember, that if you don’t save it anywhere else it won’t last long!
10.9
The layout of the Variable Area
There are special words that allow you to get information about the layout of the Variable
Area. They are called ’VARS’, ’APP’, ’FIRST’ and ’LAST’.
’VARS’ is the address of the very first variable. Before that is the Stack Area and other variables you are not allowed to touch. ’APP’ is the address of the first application variable.
All variables before that are 4tHs own built-in variables. ’FIRST’ is the address of the first user-variable, a variable you defined yourself in your 4tH program. ’LAST’ is the address of the last accessible variable, so last ?
will never fail. The first question that will pop in your mind is, what can I do with them.
Well, you can use it to see how many variables there are of a certain kind, so you can prevent runtime errors:
." number of 4tH variables: " app vars - . cr
." number of application variables: " first app - . cr
." number of user variables: " last first - 1+ . cr
Stiahnuť
Reklama