JimBoyd wrote:
The general idea is to place the string ';COMMENT' in the editors find buffer and use SEEK , a component of F , the editor find word, to seek to that point. It would then copy the editors edit pointer R# to >IN
It should work like this:
Code:
0: // TEST OF COMMENT:
1: : SOMEWORD
2: <SOME FORTH STUFF> COMMENT: THIS IS THE START OF A MULTI-LINE COMMENT
3: AND IT CONTINUES TO THIS LINE
4: AND THIS ONE
5: AND EVEN THIS ONE ;COMMENT
6: <REST OF DEFINITION> ;
7:
I had not given this further thought as something had come up. I was just fleshing out an idea here.
Using SEEK from the editor vocabulary is not the best way to go. Using MATCH is the way to go. MATCH takes the address and length of a string, the main string, and the address and length of a sub-string to find in the main string. The non-standard word 'STREAM , address of stream, will provide the main string. This word returns the address of the current position in the text stream and the length of the stream remaining. It can be defined in Ans Forth like this:
Code:
: 'STREAM ( ADR CNT -- )
SOURCE >IN @ /STRING ;
Here is the definition for COMMENT:
Code:
: COMMENT: ( -- )
'STREAM " ;COMMENT" COUNT MATCH
0= ABORT" NO ;COMMENT"
>IN +! ; IMMEDIATE
and what it compiles to:
Code:
SEE COMMENT:
COMMENT: IMMEDIATE
31486 6915 'STREAM
31488 6746 (") ;COMMENT
31499 5139 COUNT
31501 13960 MATCH
31503 4348 0=
31505 7734 (ABORT") NO ;COMMENT
31519 2113 >IN
31521 4859 +!
31523 2930 EXIT
39
OK
The word " , when compiling, will compile the word (") and an inline counted string. If the string ';COMMENT' is not found, COMMENT: aborts with an error message. If the string is found, the text position pointer, >IN is adjusted to point to the first character after the string.
Note that ';COMMENT' does not need to be defined. It is just a text string to be sought.
Here is an example of using COMMENT:
Code:
// TEST OF MULTI LINE COMMENTS
: POINTLESS
CR .S
COMMENT:
THIS DEFINITION IS, AS IT'S NAME
INDICATES, QUITE POINTLESS.
IT ONLY EXISTS TO DEMONSTRATE
MULTI LINE COMMENTS.
;COMMENT
CR .AS ;
and what POINTLESS compiles to:
Code:
SEE POINTLESS
POINTLESS
31558 7024 CR
31560 16278 .S
31562 7024 CR
31564 16349 .AS
31566 2930 EXIT
10
OK
I said that ';COMMENT' did not need to be defined. It can be defined as a no-op for convenience. Here is an example of a multi-line comment keeping a word from being defined.
Code:
SCR# 8473 2 281
0: // ANOTHER MULTI LINE COMMENT TEST
1: // COMMENT OUT THE FOLLOWING WORD
2: COMMENT:
3: CODE NUTATE ( N1 N2 N3 -- N3 N2 N1 )
4: 0 ,X LDA 4 ,X LDY
5: 0 ,X STY 4 ,X STA
6: 1 ,X LDA 5 ,X LDY
7: 1 ,X STY 5 ,X STA
8: NEXT JMP END-CODE
9: ;COMMENT
A:
B:
C:
D:
E:
F:
Suppose I want to compile NUTATE and test it. Since I defined the word ;COMMENT as a no-op and Fleet Forth has LINELOAD , I can.
Code:
0 FH LOAD OK
LATEST ID. POINTLESS OK
3 0 FH LINELOAD OK
LATEST ID. NUTATE OK
3 0 FH LINELOAD begins loading the current screen at line 3. This skips over COMMENT: .
IamRob wrote:
I don't know if that would work, as how would the interpreter know if ;COMMENT itself is not part of the comment, since commented words are skipped over and not interpreted.
The string ';COMMENT' is not interpreted. The interpreter does not care because the interpreter takes no part in this except for finding and executing the immediate word COMMENT: . It is COMMENT: which searches for the string ';COMMENT' in the text stream and repositions the text stream pointer >IN . After COMMENT: executes, the interpreter resumes with the current position in the text stream pointing just past the string ';COMMENT' . Any string could have been used. I could have made the search string 'MANTICORE' in the definition of COMMENT: and that is the string it would have used for the end of a multi-line comment.
Quote:
How about this instead. The same as the parenthesis works, the interpreter searches for the ending parenthesis to signal the end of a comment, one can use the backslash or dbl-forward-slash to signify the end of the comment as well. But there would be no need to use an ending back-slash as that is the same as using the parenthesis.
In the Forth-83 standard, and possibly others, the words which parse the text stream take a single ASCII character as a delimiter. WORD usually takes a blank (ASCII code $20), the word " takes the ASCII character " , and the words ( and .( take the ASCII character ) for their delimiters.
If you are going to use more than one character for your delimiter, that still involves a string search.
Quote:
Some computers have the ability to use the backslash character, so a backslash can signify to ignore all text to the end of the line and a dbl-forward-slash can signify to ignore all comments until another dbl-forward-slash is encountered, even across multiple lines.
Some computers, but not all. My Fleet Forth for the Commodore 64 is not the only Forth for the Commodore 64 to use the double forward slash // for end of line comments. Scott Ballantyne's Blazin' Forth for the Commodore 64 released in 1985 also used the double forward slash for an end of line comment.
Since the double forward slash is used in some Forths as an alias for the backslash, how about using a different double character for multi-line comments, such as the double carat '^^'?
Having said that, I think that having the same token for the start of a multi-line comment as the end of a multi-line comment is problematic. They are not visually distinct. If there is only one multi-line comment in a screen, this would not be a problem. With the possibility of larger screens (32 lines? maybe more?) there is the possibility of more than one multi-line comment. If the same tokens were used for multi-line comments in source in plain old text files, the problem could be worse. There would be no block boundaries and the file size could be large enough that there may be several multi-line comments.