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 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