Moved some input stream words to library.
[forth.jl.git] / src / lib_4_comments.4th
diff --git a/src/lib_4_comments.4th b/src/lib_4_comments.4th
new file mode 100644 (file)
index 0000000..1c0be39
--- /dev/null
@@ -0,0 +1,22 @@
+\ Parenthetic comments
+
+: ( IMMEDIATE
+        1               \ allowed nested parens by keeping track of depth
+        BEGIN
+                >IN @ #IB @ >= IF      \ End of TIB?
+                        QUERY-INPUT    \ Get next line
+                THEN
+
+                IB >IN @ + @ 1 >IN +!
+                DUP [CHAR] ( = IF       \ open paren?
+                        DROP            \ drop the open paren
+                        1+              \ depth increases
+                ELSE
+                        [CHAR] ) = IF   \ close paren?
+                                1-      \ depth decreases
+                        THEN
+                THEN
+        DUP 0= UNTIL            \ continue until we reach matching close paren, depth 0
+        DROP            \ drop the depth counter
+;
+