7 size_RS = 1024 # Return stack size
8 size_PS = 1024 # Parameter stack size
9 size_TIB = 1096 # Terminal input buffer size
11 # The mem array constitutes the memory of the VM. It has the following geography:
13 # mem = +-----------------------+
14 # | Built-in Variables |
15 # +-----------------------+
17 # +-----------------------+
19 # +-----------------------+
20 # | Terminal Input Buffer |
21 # +-----------------------+
23 # +-----------------------+
25 # Note that all words (user-defined, primitive, variables, etc) are included in
28 # Simple linear addressing is used with one exception: references to primitive code
29 # blocks, which are represented as anonymous functions, appear the negative index
30 # into the primitives array which contains only these functions.
32 mem = Array{Int64,1}(size_mem)
33 primitives = Array{Function,1}()
38 RSP0 = nextVarAddr; nextVarAddr += 1
39 PSP0 = nextVarAddr; nextVarAddr += 1
40 HERE = nextVarAddr; nextVarAddr += 1
41 LATEST = nextVarAddr; nextVarAddr += 1
43 mem[RSP0] = nextVarAddr # bottom of RS
44 mem[PSP0] = mem[RSP0] + size_RS # bottom of PS
45 TIB = mem[PSP0] + size_PS # address of terminal input buffer
46 mem[HERE] = TIB + size_TIB # location of bottom of dictionary
47 mem[LATEST] = 0 # no previous definition
49 DICT = mem[HERE] # Save bottom of dictionary as constant
53 RSP::Int64 # Return stack pointer
54 PSP::Int64 # Parameter/data stack pointer
55 IP::Int64 # Instruction pointer
56 W::Int64 # Working register
57 X::Int64 # Extra register
59 reg = Reg(mem[RSP0], mem[PSP0], 0, 0, 0)
63 type StackUnderflow <: Exception end
65 getRSDepth() = reg.RSP - mem[RSP0]
66 getPSDepth() = reg.PSP - mem[PSP0]
68 function ensurePSDepth(depth::Int64)
70 throw(StackUnderflow())
74 function ensureRSDepth(depth::Int64)
76 throw(StackUnderflow())
80 function pushRS(val::Int64)
92 function pushPS(val::Int64)
93 mem[reg.PSP += 1] = val
104 # Primitive creation and calling functions
106 function createHeader(name::AbstractString, flags::Int64)
107 mem[mem[HERE]] = mem[LATEST]
108 mem[LATEST] = mem[HERE]
111 mem[mem[HERE]] = length(name) + flags; mem[HERE] += 1
112 mem[mem[HERE]:(mem[HERE]+length(name)-1)] = [Int(c) for c in name]; mem[HERE] += length(name)
115 function defPrim(name::AbstractString, f::Function; flags::Int64=0)
116 createHeader(name, flags)
119 mem[mem[HERE]] = -length(primitives)
122 return -length(primitives)
125 callPrim(addr::Int64) = primitives[-addr]()
127 function defExistingVar(name::AbstractString, varAddr::Int64; flags::Int64=0)
128 defPrim(name, eval(:(() -> begin
134 function defNewVar(name::AbstractString, initial::Int64; flags::Int64=0)
135 createHeader(name, flags)
137 varAddr = mem[HERE] + 1
138 push!(primitives, eval(:(() -> begin
142 mem[mem[HERE]] = -length(primitives); mem[HERE] += 1
144 mem[mem[HERE]] = initial; mem[HERE] += 1
149 function defConst(name::AbstractString, val::Int64; flags::Int64=0)
150 defPrim(name, eval(:(() -> begin
158 # Threading Primitives
160 NEXT = defPrim("NEXT", () -> begin
167 DOCOL = defPrim("DOCOL", () -> begin
173 EXIT = defPrim("EXIT", () -> begin
179 # Basic forth primitives
181 DROP = defPrim("DROP", () -> begin
186 SWAP = defPrim("SWAP", () -> begin
194 DUP = defPrim("DUP", () -> begin
199 OVER = defPrim("OVER", () -> begin
201 pushPS(mem[reg.PSP-1])
205 ROT = defPrim("ROT", () -> begin
215 NROT = defPrim("-ROT", () -> begin
225 TWODROP = defPrim("2DROP", () -> begin
231 TWODUP = defPrim("2DUP", () -> begin
240 TWOSWAP = defPrim("2SWAP", () -> begin
252 QDUP = defPrim("?DUP", () -> begin
261 LIT = defPrim("LIT", () -> begin
269 STORE = defPrim("!", () -> begin
276 FETCH = defPrim("@", () -> begin
282 ADDSTORE = defPrim("+!", () -> begin
289 SUBSTORE = defPrim("-!", () -> begin
299 defExistingVar("HERE", HERE)
300 defExistingVar("LATEST", LATEST)
301 defExistingVar("PSP0", PSP0)
302 defExistingVar("RSP0", RSP0)
303 STATE = defNewVar("STATE", 0)
304 BASE = defNewVar("BASE", 10)
308 defConst("VERSION", 1)
309 defConst("DOCOL", DOCOL)
310 defConst("DICT", DICT)
311 F_IMMED = defConst("F_IMMED", 100)
312 F_HIDEN = defConst("F_HIDDEN", 1000)
316 TOR = defPrim(">R", () -> begin
321 FROMR = defPrim("R>", () -> begin
326 RSPFETCH = defPrim("RSP@", () -> begin
331 RSPSTORE = defPrim("RSP!", () -> begin
336 RDROP = defPrim("RDROP", () -> begin
343 PSPFETCH = defPrim("PSP@", () -> begin
348 PSPSTORE = defPrim("PSP!", () -> begin
356 NUMTIB = defNewVar("#TIB", 0)
357 TOIN = defNewVar(">IN", 0)
359 KEY = defPrim("KEY", () -> begin
360 if mem[TOIN] >= mem[NUMTIB]
363 mem[NUMTIB] = length(line)
364 mem[TIB:(TIB+mem[NUMTIB]-1)] = [Int64(c) for c in collect(line)]
367 pushPS(mem[TIB + mem[TOIN]])
373 EMIT = defPrim("EMIT", () -> begin
378 WORD = defPrim("WORD", () -> begin
400 if c == ' ' || c == '\t'
411 mem[wordAddr + offset] = Int64(c)
417 if c == ' ' || c == '\t'
430 #NUMBER = defPrim("NUMBER", (reg) -> begin
436 #function runVM(reg::Reg)
438 # while (jmp = callPrim(reg, jmp)) != 0 end
443 function coredump(startAddr::Int64; count::Int64 = 16, cellsPerLine::Int64 = 8)
444 chars = Array{Char,1}(cellsPerLine)
448 if i%cellsPerLine == 0
452 print("\t$(mem[addr]) ")
454 if (mem[addr]>=32 && mem[addr]<176)
455 chars[i%cellsPerLine + 1] = Char(mem[addr])
457 chars[i%cellsPerLine + 1] = '.'
460 if i%cellsPerLine == cellsPerLine-1
461 println(string("\t", ASCIIString(chars)))
467 count = reg.PSP - mem[PSP0]
471 for i in (mem[PSP0]+1):reg.PSP
476 println("Parameter stack empty")