7 ms·
Forth is written as a syntax tree in exactly the manner of Lisp. There are two differences, neither of which affects the fact that you express the AST directly:
by thaumasiotes 9y ago
Forth is written as a syntax tree in exactly the manner of Lisp. There are two differences, neither of which affects the fact that you express the AST directly:
1. Lisp is written preorder; Forth is written postorder.
2. The number of children a node in your Lisp AST has is marked explicitly, by parentheses, whereas the number of children a node in your Forth AST has is defined externally.
There is no difference between a Lisp-style
(+ (* 3 5 8) (+ 4 1))
and a Forth-style
3 5 8 * * 4 1 + +
except that the * in Forth-style can't take a variable number of arguments. But if you added parens to Forth, it could:
((3 5 8 *) (4 1 +) +)
- white-flame 9y agoor take the number of args as another parameter. Assuming N* multiplies N items from the stack: 3 5 8 3 N* 4 1 + + The equivalent of (+ (* 3 5 8) 4 1) could be 3 5 8 3 N* 4 1 3 N+
- kazinator 9y agoThat isn't a syntax tree. > the number of children a node in your Forth AST has is defined externally and that's basically why. Here's a 1000 node AST: foo / \ Where are the rest of the nodes? Oh, they are defined externally.