18.2.3 Parsing strings. 4tH 4tH Forth compiler
Reklama
Reklama
CHAPTER 18. PREPROCESSOR MANUAL
:MACRO BEGIN-STRUCTURE @1@ STRUCT ;
:MACRO END-STRUCTURE END-STRUCT #1# ;
BEGIN-STRUCTURE point
1 CELLS +FIELD p.x
1 CELLS +FIELD p.y
END-STRUCTURE
Will expand to:
STRUCT
1 CELLS +FIELD p.x
1 CELLS +FIELD p.y
END-STRUCT point
361
18.2.3
Parsing strings
Strings can be parsed by using the $1$ through $4$ commands and store them in register
1 through 4. They always have to be followed by a delimiter, e.g.:
INCLUDE lib/escape.4th
:MACRO S\" $1$ " ‘ S" ‘ >1> |#| >>> " |#| S>ESCAPE ;
S\" \tAnd the winner is:\t\q4tH compiler\q!"
Will expand to:
S" \tAnd the winner is:\t\q4tH compiler\q!" S>ESCAPE
18.2.4
The string stack
If four registers are not enough, there is also a string stack you can use to store strings. This macro will read a string and push it on the string stack:
:MACRO BEGIN-STRUCTURE @1@ >1> STRUCT ;
This macro will pop the string from the string stack and print it:
:MACRO END-STRUCTURE END-STRUCT <2< #2# ;
So if you write code like this:
BEGIN-STRUCTURE point
FIELD: p.x
FIELD: p.y
END-STRUCTURE
It will be expanded to this:
STRUCT
FIELD: p.x
FIELD: p.y
END-STRUCT point
If you’re really short on registers, you can write a macro that saves the current value on the stack and restores it afterwards. Note that the string stack remains completely intact between calls. Just remember to keep it balanced at all times.
Stáhnout
Reklama