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 function defVar(name::AbstractString, var::Expr)
75 defPrim(name, Expr(:call, :pushPS, var))
78 # Threading Primitives
80 NEXT = defPrim("NEXT", :(begin
87 DOCOL = defPrim("DOCOL", :(begin
93 EXIT = defPrim("EXIT", :(begin
99 # Basic forth primitives
101 DROP = defPrim("DROP", :(begin
105 SWAP = defPrim("SWAP", :(begin
106 PS[PSP], PS[PSP-1] = PS[PSP-1], PS[PS]
109 DUP = defPrim("DUP", :(begin
113 LIT = defPrim("LIT", :(begin
120 STORE = defPrim("!", :(begin
126 FETCH = defPrim("@", :(begin
131 ADDSTORE = defPrim("+!", :(begin
134 memory[addr] += toAdd
137 SUBSTORE = defPrim("-!", :(begin
140 memory[addr] -= toSub
146 defVar("STATE", :STATE)
147 defVar("HERE", :HERE)
148 defVar("LATEST", :LATEST)
150 defVAR("BASE", :BASE)
160 eval(primitives[-memory[jmp]])