Forth Amendment

In which I comment on things

Define words ( and ) in Forth. Make ( read words until ) and do nothing with them. This is how Forth implements comments.

I didn’t quite follow the prescription above. I found that gforth will happily parse things like 3 ( 4 hgfd) . and (in this case) return 3. So it is not a case of just ignoring words until you get the ) but ignoring characters until you see the ) character.

My implementation endows InputParser with a comment character, which if not nil causes it to discard all characters in its input stream up to and including the comment character. It then emits ) to close the comment. The words (and \ simply set an appropriate comment character and the word ) sets the comment character to nil.

The code is tagged blog-1061.

You will notice from the commits the I have made some other changes. For example, I added code in to allow me to dump the contents of the dictionary for debugging purposes. I’ve also finally made words case insensitive, so you can now type dup or DUP or dUp which is in line with many Forth implementations.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.