aboutsummaryrefslogtreecommitdiff
path: root/amforth-6.5/examples/rec-double-paren.frt
diff options
context:
space:
mode:
Diffstat (limited to 'amforth-6.5/examples/rec-double-paren.frt')
-rw-r--r--amforth-6.5/examples/rec-double-paren.frt73
1 files changed, 73 insertions, 0 deletions
diff --git a/amforth-6.5/examples/rec-double-paren.frt b/amforth-6.5/examples/rec-double-paren.frt
new file mode 100644
index 0000000..254fa9e
--- /dev/null
+++ b/amforth-6.5/examples/rec-double-paren.frt
@@ -0,0 +1,73 @@
+\
+\ Purpose: temporarly switch off all actions until
+\ a delimiting word is found and executed.
+\ Useful to comment larger text parts.
+\
+\ (( switches to a limited command set and
+\ makes all words no-operations. Only words
+\ in a special wordlist are allowed for
+\ execution. )) is one of them and switches
+\ back to normal operation.
+\
+\ The recognizer switch survives REFILL's so
+\ multi line comments work too. This is an
+\ example for replacing the whole system
+\ recognizer stack with another one.
+\
+\ Author: Matthias Trute
+\ Date: Oct 14, 2016
+\ License: Public Domain
+\
+
+\ keep the previously active forth-recognizer stack
+variable old-f-rs
+wordlist constant comment-actions
+
+get-current
+comment-actions set-current
+
+\ only words in this wordlist are executed inside comments
+\ at least the )) is needed.
+
+\ switch back to the saved recognizer stack
+: ))
+ old-f-rs @ to forth-recognizer
+; immediate
+
+\ that's all for the comment actions
+set-current
+
+\ every word found is fine. Even the ones that are not found
+\ in the dictionary
+' noop dup dup recognizer: r:skip
+: rec:skip ( addr len -- r:skip ) 2drop r:skip ;
+
+\ search only in the comment-actions wordlist
+: rec:comment-actions ( addr len -- xt +/-1 r:word | r:fail )
+ comment-actions search-wordlist ( xt +/-1 | 0 )
+ ?dup if r:word else r:fail then
+;
+
+\ a simple two-element recognizer stack
+2 recognizer constant rs:comment
+' rec:skip ' rec:comment-actions 2 rs:comment set-recognizers
+
+\ save the current recognizer stack and
+\ switch over to the limited one
+: (( ( -- )
+ forth-recognizer old-f-rs !
+ rs:comment to forth-recognizer
+; immediate
+
+\ ------------- Test Cases ----------------
+\
+\ : rec:comment rs:comment do-recognizer ;
+\ T{ S" DUP" rec:comment -> r:skip }T
+\ T{ S" 1234" rec:comment -> r:skip }T
+
+\ the XT of )) is not important
+\ T{ S" ))" rec:comment rot drop -> 1 r:word }T
+\
+\ ------------------------------------------
+\ with nesting the [IF] [ELSE] [THEN] can be
+\ implemented likewise.