Strip comments from a string: Difference between revisions

m
Line 622:
Or Concatenate the functions together as we did here.
 
Tested with Swift Forth on OS/X, GForth on Windows
 
<LANG FORTH>\ Rosetta Code Strip Comment
: LASTCHAR ( addr len -- addr len c) 2DUP + 1- C@ ;
 
: LASTCHAR ( addr len -- addr len c) 2DUP + 1- C@ ;
 
: COMMENT? ( char -- ? ) S" #;" ROT SCAN NIP ; \ is char '#' or ';'
 
: -COMMENT ( addr len -- addr len') \ removes # or ; comments
BEGIN 1-
LASTCHAR COMMENT? 0= BEGIN
LASTCHAR COMMENT? 0=
WHILE \ while not a comment char...
1- \ reduce length by 1
REPEAT
1- ; \ remove 1 more (the comment char)
 
\ -TRAILING is resident in desktop Forth systems like Swift Forth
\ shown here for demonstration
: -TRAILING ( adr len -- adr len') \ remove trailing spaces
BEGIN 1-
LASTCHAR BL = BEGIN
WHILE LASTCHAR \ while lastcharBL = blank char
WHILE \ while lastchar = blank
1- \ reduce length by 1
REPEAT ;
1+ ;
 
: COMMENT-STRIP ( addr len -- addr 'len) -COMMENT -TRAILING ;</LANG>
 
Anonymous user