4 ms·
Here’s a sketch of how you could write a toy lisp in forth: Let’s use “(* 4 (+ 5 6))” as an example input string. 1. Drop ( and ): “* 4 + 5 6”. 2. Tokenize o
by celoyd 16y ago
Here’s a sketch of how you could write a toy lisp in forth:
Let’s use “(* 4 (+ 5 6))” as an example input string.
1. Drop ( and ): “* 4 + 5 6”.
2. Tokenize on whitespace and reverse the tokens: 6 5 + 4 *
3. This is forth. Execute it.
There’s at least one class of bug here (that “(- a b)” != “b a -”)), but it shows a sense in which they’re very similar languages. From 40,000 feet, lisp could be called a syntax hack to add variadic functions to a backwards dialect of forth.
To put it another way, forth really is trivially tree-structured just like lisp, but the trees are implicit in the number of arguments the functions take. This allows tricks that depend on the AST being easy to work with, just like lisp macros.
- rsaarelm 16y agoYou can also do `(+ 1 2 3)` in Lisp, but `3 2 1 +` doesn't evaluate to 6 in Forth.
- michaelcampbell 16y agoThat's lower than the 40000 feet the person to whom you were replying was flying; in his post he mentions specifically variadic functions.
- Someone 16y agoThat is not forth. Instead (leaving out a bit of compile-time/run-time distinction magic): - define [+] something like: : [+] 0 swap 0 do + loop ; - define (+ as a word that pushes the address of [+] and the stack pointer onto the retun stack - similarly define [-] and (- , [* ] and (* , and [/] and (/ - define ) as a word that takes the current stack pointer, pops that saved stack pointer from the return stack and subtracts it from it (this puts the number of arguments to [+] on the stack, pops the address of [+] from the return stack and calls it. With that, "(* 4 (+ 5 6 ) )" becomes executable forth.