Well, I've had a look at your TurboForth site (nice!) and cobbled together a grammar plus lexer. Does this look like Forth to you, especially the lexer?
For the grammar I'll have to clean up the whitespace handling (that includes assembly and BASIC) before I dare putting out a new plugin version.
Whoa! Fantastic! Yes this looks very promising indeed. I'll have a look at this tomorrow (Thursday) as I'm out playing guitar tonight. I'll add the other families of words to the files for you. We'll also get Lee to add any words from fbForth, too if you don't mind.
You're quite right... There is a little bit of syntax. Here's the rules:
"Words" (in other languages called "functions") are defined as follows:
- Shall be preceded by a colon and one or more whitespace (including tab)
- Shall include a name
- Shall include 0 or more references to (pre-existing) words already defined, each separated by one or more whitespace characters or a new line.
- Shall be terminated with a semi-colon
: my-word ( some comment)
: my-word ( some comment ) som-word another-word yet-another-word ;
- A comment shall begin with the word (
- Note that ( is a word, and therefore may have one or more whitespace characters either side of it
- A comment is terminated with a closing ) character (not a word)
- A comment may include one or more ( characters
- A comment may not inlude a ) character (since the ) character terminates the comment)
- If end-of-line is reached without a terminating ) character, the comment is assumed closed
: some-word ( this is an in-line comment) some more code ;
( here is another in line comment, all by itself )
( here is an other )
(this is not a comment - there is no space after the open ( character)
( this is a comment the dup at the end is valid even though there is no trailing space)dup
( normally it would be written like this ) dup ( i.e. it would have a space after the closing bracket, but both are valid )
- Line comments may begin anywhere on a line.
- The begin with the word \ which may have one or more white space characters before and after it
- When a line comment is detected, everything up to the end of the line is ignored
\ this is a comment
\ this is a comment
3 constant LIVES \ define a constant for the number of lives
\this is not a comment (there is no such word as \this)
\ \\\\\ this \\ is \ a valid \ comment (because the entire line is ignored)
- The words S" and ." define strings.
- A string is terminated with a closing " character
- The " character may not appear in the string
- Whitespace between the opening S" word and the string are included in the string
S" This is a string" ( note space between the WORD S" and the start of the string - at least one space required (to identify the word S")
S" This is also a string"
." This is a string"
." This is also a string"
I think that's about it. The secret is to know that words and numbers MUST be separated by at least one whitespace character. That's how Forth identifies them:
SWAP DUP DROP - 3 separate words - all valid
SwapDupDrop - the system would look for a word called SwapDupDrop - if it's defined, it's valid.
Also, a word can have any name (except for a blank space):
: '@@#@]][-=*&^% some-code ;
This is perfectly valid.
So is this (if you are a masochist):
: 1 99 ;
Now, 1 + 1 gives you 108. This is because Forth looks for words in its dictionary first. If it doesn't find a matching word, only then does it look to see if it's a valid number.
Edited by Willsy, Wed Aug 12, 2015 7:14 AM.