Forth Interpreter

A Forth interpreter, written in MUSHCode.

Author: Ari@M*U*S*H
Category: Other
Commands: @create, @lock, @set.
Compatibility: PennMUSH.


Copy and paste the below code into a compatible MUSH or MUX.

MUSHCode for Forth Interpreter

@create Forth
@lock Forth==me
&DESCRIBE Forth=A Forth interpreter. Examine it for implementation details. You can use u\([num(me)]/FN_FORTH_EVAL\,<code>\) to run the interpreter. When execution finishes, the top value on the stack will be returned from the function. Here are the available words and syntax:%r%b'<x> quote the symbol <x> and push it on the stack%r%b+ pop two items from the stack, add them, and push the result%r%b. pop the last item from the stack and print it%r%bAPPLY pop a symbol from the stack and execute the function it names%r%bLIST pop an item N from the stack and then create a list from the last N items on the stack, in reverse order (FIFO)%r%bDUP push a copy of the top item of the stack%r%bPROC turn the list on the top of the stack into a procedure%r%bDEF pop a symbol from the stack and then pop a procedure to define for the symbol%r%rExample: u\([num(me)]/FN_FORTH_EVAL\,1 '+ 2 LIST PROC '1+ DEF 4 1+\) ==> 5
@set Forth/DESCRIBE=no_command visual prefixmatch public nearby
&FN_FORTH_EVAL Forth=letq(M,eval,[iter(%0,switch(%qM,eval,u(FN_FORTH_EXECUTE,%i0)), ,)][u(FN_FORTH_POP)])
&FN_FORTH_EXECUTE Forth=if(setr(0,grab(%qD,%0 *,|)),switch(first(rest(%q0)),PROC,u(FN_FORTH_EVAL,rest(rest(%q0))),u(FN_FORTH_PUSH,rest(%q0))),if(hasattrpval(me,FN_FORTH_WORD_%0),u(FN_FORTH_WORD_%0),u(FN_FORTH_PUSH,%0)))
&FN_FORTH_POP Forth=letq(0,first(%qS,|),[setq(S,rest(%qS,|))]%q0)
&FN_FORTH_PUSH Forth=setq(S,[switch(left(%0,1),',after(%0,'),%0)]|%qS)
&FN_FORTH_WORD_+ Forth=letq(0,u(FN_FORTH_POP),1,u(FN_FORTH_POP),u(FN_FORTH_PUSH,add(%q0,%q1)))
&FN_FORTH_WORD_. Forth=pemit(%#,u(FN_FORTH_POP))
&FN_FORTH_WORD_APPLY Forth=letq(0,u(FN_FORTH_POP),switch(first(%q0),PROC,u(FN_FORTH_EVAL,rest(%q0)),if(setr(1,grab(%qW,%q0 *,|)),u(FN_FORTH_EVAL,rest(%q10)),if(hasattrpval(me,FN_FORTH_WORD_%q0),u(FN_FORTH_WORD_%q0),u(FN_FORTH_PUSH,NO SUCH WORD [ucstr(%q0)]))))
&FN_FORTH_WORD_DEF Forth=setq(D,setunion(%qD,[u(FN_FORTH_POP)] [u(FN_FORTH_POP)],|)
&FN_FORTH_WORD_DUP Forth=u(FN_FORTH_PUSH,first(%qS,|))
&FN_FORTH_WORD_LIST Forth=letq(0,u(FN_FORTH_POP),1,,[iter(lnum(%q0),setq(1,[u(FN_FORTH_POP)][if(%q1,%b%q1)]), ,)][u(FN_FORTH_PUSH,%q1)])

look Forth