3 RS = Array{Int64, 1}(1024)
6 PS = Array{Int64, 1}(1024)
15 primitives = Array{Expr,1}()
16 memory = Array{Int64,1}(64*1024)
28 # Stack manipulation macros
30 function pushRS(val::Int64)
42 function pushPS(val::Int64)
54 # Primitive creation functions
56 function defPrim(name::AbstractString, expr::Expr)
63 memory[HERE] = length(name); HERE += 1
64 memory[HERE:(HERE+length(name)-1)] = [Int(c) for c in name]; HERE += length(name)
66 push!(primitives, expr)
67 memory[HERE] = -length(primitives)
74 defVar(name::AbstractString, var::Expr) = defPrim(name, Expr(:call, :pushPS, var))
75 defConst(name::AbstractString, val::Int64) = defPrim(name, Expr(:call, :pushPS, :val))
77 # Threading Primitives
79 NEXT = defPrim("NEXT", :(begin
86 DOCOL = defPrim("DOCOL", :(begin
92 EXIT = defPrim("EXIT", :(begin
98 # Basic forth primitives
100 DROP = defPrim("DROP", :(begin
105 SWAP = defPrim("SWAP", :(begin
106 PS[PSP], PS[PSP-1] = PS[PSP-1], PS[PS]
110 DUP = defPrim("DUP", :(begin
115 LIT = defPrim("LIT", :(begin
123 STORE = defPrim("!", :(begin
130 FETCH = defPrim("@", :(begin
136 ADDSTORE = defPrim("+!", :(begin
139 memory[addr] += toAdd
143 SUBSTORE = defPrim("-!", :(begin
146 memory[addr] -= toSub
153 defVar("STATE", :STATE)
154 defVar("HERE", :HERE)
155 defVar("LATEST", :LATEST)
156 defVAR("BASE", :BASE)
160 defConst("VERSION", 1)
161 defConst("DOCOL", DOCOL)
165 TOR = defPrim(">R", :(pushRS(popPS())))
166 FROMR = defPrim("R>", :(pushPS(popRS())))
167 RSPFETCH = defPrim("RSP@", :(pushPS(RSP)))
168 RSPSTORE = defPrim("RSP!", :(RSP = popPS()))
169 RDROP = defPrim("RDROP", :(popRS()))
173 PSPFETCH = defPrim("PSP@", :(pushPS(PSP)))
174 PSPSTORE = defPrim("PSP!", :(PSP = popPS()))
180 eval(primitives[-memory[jmp]])