en:pfw:substitution-phrases
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
en:pfw:substitution-phrases [2023-09-04 18:22] – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | en:pfw:substitution-phrases [2023-09-04 18:22] (current) – ↷ Seite von pfw:substitution-phrases nach en:pfw:substitution-phrases verschoben uho | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | {{pfw: | ||
+ | ====== Substitution phrases ====== | ||
+ | |||
+ | uh 2022-02-21 | ||
+ | |||
+ | ===== The idea of substitution phrases ===== | ||
+ | |||
+ | Sometimes you face a Forth system, that does not support a specific feature that you might want to use. For example you might want to compare two numbers (first and second) to find out whether or not the first number is less or equal than the second number. You would like to write | ||
+ | |||
+ | < | ||
+ | first second < | ||
+ | </ | ||
+ | |||
+ | If the the system you use does not define the word '' | ||
+ | |||
+ | * Instead of '' | ||
+ | * Or you can define the missing operator and make use of the substitution phrase in the definition:< | ||
+ | : <= ( n1 n2 -- f ) > 0= ; | ||
+ | </ | ||
+ | |||
+ | |||
+ | It is not always reasonable to make a new definition. Sometimes it is better to just use the substitution phrase as is in the program. | ||
+ | |||
+ | Anyway it is good to know about substitution phrases. | ||
+ | |||
+ | So here is the definition of a substition phrase | ||
+ | |||
+ | > **Substiution Phrase** | ||
+ | > | ||
+ | > A // | ||
+ | |||
+ | ===== Some substitution phrases ===== | ||
+ | |||
+ | Here are some substitution phrases that you can use and that might be used in programs that you encounter: | ||
+ | |||
+ | ^You can write | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |'' | ||
+ | |… | ||
+ | |||
+ | ==== Substitution phrases for control structures ==== | ||
+ | |||
+ | Some Forth systems do not support '' | ||
+ | |||
+ | Please have a look at the files | ||
+ | |||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
+ | for a discussion how substitute these. | ||
+ | |||
+ | ==== Possible pitfalls with substitution phrases vs. definitions. ==== | ||
+ | |||
+ | Sometimes you cannot just use a substitution phrase and pour them into a definition. When invoking a word it uses the return stack to store the return address of the caller, so execution can continue in the caller when the current word is done. So, if your execution phrase contains return stack operators (as the substitution phrase for '' | ||
+ | |||
+ | < | ||
+ | \ INCORRECT | ||
+ | |||
+ | : 2r> ( -- x1 x2 ) r> r> swap ; | ||
+ | |||
+ | \ INCORRECT | ||
+ | </ | ||
+ | |||
+ | You would need to define a //compiling word// and compile the appropriate words as if the substition phrase would have been used directly: | ||
+ | |||
+ | < | ||
+ | \ CORRECT | ||
+ | |||
+ | : 2r> ( -- x1 x2 ) postpone r> postpone r> postpone swap ; immediate | ||
+ | </ | ||
+ | |||
+ | If this in the end is simpler than using the substitution phrase directly, depends on the situation. | ||