18.2.1 Back quoted strings. 4tH 4tH Forth compiler
Oglas
Oglas
CHAPTER 18. PREPROCESSOR MANUAL
18.2
Macros
360
A macro starts with the word :MACRO and is delimited by a semi-colon like a normal definition. A macro may span several lines and may contain anything you want, including conditional and loop statements. Macros may be nested, but they may not contain certain directives.
This ANS Forth definition is impossible to define in 4tH:
: STEP 8 POSTPONE LITERAL POSTPONE +LOOP ;
In the preprocessor you can define it like this:
:MACRO STEP 8 +LOOP ;
Macros are expanded by the preprocessor which means that every time it finds ”STEP” it will be replaced by ”8 +LOOP”.
18.2.1
Back quoted strings
You can’t print a semi-colon inside a macro, because it will be interpreted to be the end of the macro. In order to defeat the interpreter you can use back quoted strings. Back quoted strings are printed verbatim, so this will work:
:macro extra_stack : dip over swap ‘ ;‘ ;
You can even define entire strings:
:macro nul? dup 0= ‘ abort" Missing string"‘ ;
Note that back quoted strings are only interpreted within macros. Outside macros they are ignored.
18.2.2
Registers
The preprocessor has 4 global registers, which can only be used inside macro definitions.
Take this definition:
:MACRO MONTH @1@ @2@ CREATE #1# #2# , DOES> @C . ‘ ;‘ ;
MONTH SEPTEMBER 9
Will expand to:
CREATE SEPTEMBER 9 , DOES> @C . ;
The commands @1@ through @4@ parse a word and store it in register 1 through 4. The commands #1# though #4# print the string stored there. Registers retain their values between calls, e.g.:
Preuzimanje
Oglas