Problem with Forth and Lisp is that you can freely start to improve language, so it sort of destroys the usefulness in a company of full of Salarymen and Office Droids.
Here is Grok's scheme to make Forth more readable:
Below is a Forth implementation of a simple parser that transforms
(. (+ 1 2)) into 1 2 + . and executes it. This assumes the input is
provided as a string or entered interactively, and the parser outputs
the transformed Forth code to the input stream for execution.
\ Buffer to store transformed output
20 CONSTANT BUF-SIZE
CREATE BUF BUF-SIZE ALLOT
VARIABLE BUF-PTR
: INIT-BUF ( -- )
BUF BUF-SIZE ERASE
0 BUF-PTR ! ;
: ADD-TO-BUF ( c -- )
BUF-PTR @ BUF-SIZE < IF
BUF BUF-PTR @ + C!
1 BUF-PTR +!
ELSE
." Buffer overflow" ABORT
THEN ;
: PARSE-EXPR ( addr u -- )
INIT-BUF
BEGIN
DUP 0> WHILE
OVER C@ CASE
'(' OF DROP 1 /STRING ENDCASE \ Skip (
')' OF DROP 1 /STRING ENDCASE \ Skip )
'.' OF BL ADD-TO-BUF '.' ADD-TO-BUF BL ADD-TO-BUF
DROP 1 /STRING ENDCASE
'+' OF BL ADD-TO-BUF '+' ADD-TO-BUF BL ADD-TO-BUF
DROP 1 /STRING ENDCASE
BL OF BL ADD-TO-BUF DROP 1 /STRING ENDCASE \ Handle spaces
ELSE
DUP ADD-TO-BUF \ Copy number characters
DROP 1 /STRING
ENDCASE
REPEAT
2DROP ;
: EVAL-EXPR ( addr u -- )
PARSE-EXPR
BUF BUF-PTR @ EVALUATE ; \ Execute the transformed string
\ Example usage
: TEST ( -- )
S" (. (+ 1 2))" EVAL-EXPR ;
TEST
Did you actually test that code? Never mind the tiny buffer size or possible hallucinations, but just from looking at the "parser", it does seem to do nothing more than copy the expected tokens in the same order it reads them on input, ignoring any parentheses.
This CAN'T possibly work, not even for the example input. Maybe if it reversed the string, but unless I've forgotten completely how to read Forth it doesn't do even that.
And for some reason this kind of garbage is apparently the future of programming, and people seem compelled to post it everywhere...
This was kinda joke. I have done it in real life, but that was in 1976. And the mechanism was totally different, more Lisp-like, but could not make Grok to do it, so I suggested simple pre-parser.
What I really wanted was a "pascal-type" tokenizer and second stack for commands. "(" means push next and ")" means pop and execute. In this kind of system (if (< a 1) (setq a (+ a 2))) is totally valid without much overhead and about 187% more readable than Forth.
And yes I know it is not Lisp, perse.
Also Lisp isn't just about a prefix syntax, it's the whole eval/apply and lists.
Bloody Grok does not seem tobe able to make generic parser, so that Forth whitespace requirements can be ignored. When said I want "Pascal-type" tokenizer, it started to print Pascal-language compiler, which was amazingly complex and long. I cannot imagine many humans can produce that much Forth and survive.