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", 128)
312 F_HIDDEN = defConst("F_HIDDEN", 256)
313 F_LENMASK = defConst("F_LENMASK", 127)
317 TOR = defPrim(">R", () -> begin
322 FROMR = defPrim("R>", () -> begin
327 RSPFETCH = defPrim("RSP@", () -> begin
332 RSPSTORE = defPrim("RSP!", () -> begin
337 RDROP = defPrim("RDROP", () -> begin
344 PSPFETCH = defPrim("PSP@", () -> begin
349 PSPSTORE = defPrim("PSP!", () -> begin
357 NUMTIB = defNewVar("#TIB", 0)
358 TOIN = defNewVar(">IN", 0)
360 KEY = defPrim("KEY", () -> begin
361 if mem[TOIN] >= mem[NUMTIB]
364 mem[NUMTIB] = length(line)
365 mem[TIB:(TIB+mem[NUMTIB]-1)] = [Int64(c) for c in collect(line)]
368 pushPS(mem[TIB + mem[TOIN]])
374 EMIT = defPrim("EMIT", () -> begin
379 WORD = defPrim("WORD", () -> begin
401 if c == ' ' || c == '\t'
412 mem[wordAddr + offset] = Int64(c)
418 if c == ' ' || c == '\t' || c == '\n'
431 NUMBER = defPrim("NUMBER", () -> begin
436 s = ASCIIString([Char(c) for c in mem[wordAddr:(wordAddr+wordLen-1)]])
439 pushPS(parse(Int64, s, mem[BASE]))
442 pushPS(1) # Error indication
448 # Dictionary searches
450 FIND = defPrim("FIND", () -> begin
454 word = ASCIIString([Char(c) for c in mem[wordAddr:(wordAddr+wordLen-1)]])
459 lenAndFlags = mem[latest+1]
460 len = lenAndFlags & F_LENMASK
461 hidden = (lenAndFlags & F_HIDDEN) == F_HIDDEN
463 if hidden || len != wordLen
469 thisWord = ASCIIString([Char(c) for c in mem[thisAddr:(thisAddr+len-1)]])
481 TOCFA = defPrim(">CFA", () -> begin
484 lenAndFlags = mem[addr+1]
485 len = lenAndFlags & F_LENMASK
487 pushPS(addr + 1 + len)
493 #function runVM(reg::Reg)
495 # while (jmp = callPrim(reg, jmp)) != 0 end
500 function coredump(startAddr::Int64; count::Int64 = 16, cellsPerLine::Int64 = 8)
501 chars = Array{Char,1}(cellsPerLine)
505 if i%cellsPerLine == 0
509 print("\t$(mem[addr]) ")
511 if (mem[addr]>=32 && mem[addr]<176)
512 chars[i%cellsPerLine + 1] = Char(mem[addr])
514 chars[i%cellsPerLine + 1] = '.'
517 if i%cellsPerLine == cellsPerLine-1
518 println(string("\t", ASCIIString(chars)))
524 count = reg.PSP - mem[PSP0]
528 for i in (mem[PSP0]+1):reg.PSP
533 println("Parameter stack empty")