function defNewVar(name::AbstractString, initial::Int64; flags::Int64=0)
createHeader(name, flags)
+ codeWordAddr = mem[HERE]
varAddr = mem[HERE] + 1
push!(primitives, eval(:(() -> begin
pushPS($(varAddr))
mem[mem[HERE]] = initial; mem[HERE] += 1
- return varAddr
+ return varAddr, codeWordAddr
end
function defConst(name::AbstractString, val::Int64; flags::Int64=0)
# Built-in variables
-defExistingVar("HERE", HERE)
-defExistingVar("LATEST", LATEST)
-defExistingVar("PSP0", PSP0)
-defExistingVar("RSP0", RSP0)
-STATE = defNewVar("STATE", 0)
-BASE = defNewVar("BASE", 10)
+HERE_CFA = defExistingVar("HERE", HERE)
+LATEST_CFA = defExistingVar("LATEST", LATEST)
+PSP0_CFA = defExistingVar("PSP0", PSP0)
+RSP0_CFA = defExistingVar("RSP0", RSP0)
+STATE, STATE_CFA = defNewVar("STATE", 0)
+BASE, BASE_CFA = defNewVar("BASE", 10)
# Constants
# I/O
defConst("TIB", TIB)
-NUMTIB = defNewVar("#TIB", 0)
-TOIN = defNewVar(">IN", 0)
+NUMTIB, NUMTIB_CFA = defNewVar("#TIB", 0)
+TOIN, TOIN_CFA = defNewVar(">IN", 0)
KEY = defPrim("KEY", () -> begin
if mem[TOIN] >= mem[NUMTIB]
return mem[NEXT]
end)
+TELL = defPrim("TELL", () -> begin
+ len = popPS()
+ addr = popPS()
+ str = getString(addr, len)
+ print(str)
+ return mem[NEXT]
+end)
+
+# Outer interpreter
+
+INTERPRET = defPrim("INTERPRET", () -> begin
+
+ callPrim(mem[WORD])
+ callPrim(mem[FIND])
+
+ wordAddr = mem[reg.PSP]
+
+ if wordAddr>0
+ # Word in dictionary
+
+ lenAndFlags = mem[wordAddr+1]
+ callPrim(mem[TOCFA])
+ wordCFA = popPS()
+
+ else
+ # Not in dictionary
+ end
+
+ return mem[NEXT]
+end)
+
+QUIT = defWord("QUIT",
+ [RSP0_CFA, RSPSTORE,
+ INTERPRET,
+ BRANCH,-2])
+
#### VM loop ####
function runVM()
jmp = mem[NEXT]