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 function defWord(name::AbstractString, wordAddrs::Array{Int64,1}; flags::Int64=0)
159 createHeader(name, flags)
162 mem[mem[HERE]] = DOCOL
165 for wordAddr in wordAddrs
166 mem[mem[HERE]] = wordAddr
173 # Threading Primitives
175 NEXT = defPrim("NEXT", () -> begin
182 DOCOL = defPrim("DOCOL", () -> begin
188 EXIT = defPrim("EXIT", () -> begin
194 # Basic forth primitives
196 DROP = defPrim("DROP", () -> begin
201 SWAP = defPrim("SWAP", () -> begin
209 DUP = defPrim("DUP", () -> begin
214 OVER = defPrim("OVER", () -> begin
216 pushPS(mem[reg.PSP-1])
220 ROT = defPrim("ROT", () -> begin
230 NROT = defPrim("-ROT", () -> begin
240 TWODROP = defPrim("2DROP", () -> begin
246 TWODUP = defPrim("2DUP", () -> begin
255 TWOSWAP = defPrim("2SWAP", () -> begin
267 QDUP = defPrim("?DUP", () -> begin
276 INCR = defPrim("1+", () -> begin
282 DECR = defPrim("1-", () -> begin
288 ADD = defPrim("+", () -> begin
295 SUB = defPrim("-", () -> begin
302 MUL = defPrim("*", () -> begin
309 DIVMOD = defPrim("/MOD", () -> begin
318 LIT = defPrim("LIT", () -> begin
326 STORE = defPrim("!", () -> begin
333 FETCH = defPrim("@", () -> begin
339 ADDSTORE = defPrim("+!", () -> begin
346 SUBSTORE = defPrim("-!", () -> begin
356 defExistingVar("HERE", HERE)
357 defExistingVar("LATEST", LATEST)
358 defExistingVar("PSP0", PSP0)
359 defExistingVar("RSP0", RSP0)
360 STATE = defNewVar("STATE", 0)
361 BASE = defNewVar("BASE", 10)
365 defConst("VERSION", 1)
366 defConst("DOCOL", DOCOL)
367 defConst("DICT", DICT)
368 F_IMMED = defConst("F_IMMED", 128)
369 F_HIDDEN = defConst("F_HIDDEN", 256)
370 F_LENMASK = defConst("F_LENMASK", 127)
374 TOR = defPrim(">R", () -> begin
379 FROMR = defPrim("R>", () -> begin
384 RSPFETCH = defPrim("RSP@", () -> begin
389 RSPSTORE = defPrim("RSP!", () -> begin
394 RDROP = defPrim("RDROP", () -> begin
401 PSPFETCH = defPrim("PSP@", () -> begin
406 PSPSTORE = defPrim("PSP!", () -> begin
414 NUMTIB = defNewVar("#TIB", 0)
415 TOIN = defNewVar(">IN", 0)
417 KEY = defPrim("KEY", () -> begin
418 if mem[TOIN] >= mem[NUMTIB]
421 mem[NUMTIB] = length(line)
422 mem[TIB:(TIB+mem[NUMTIB]-1)] = [Int64(c) for c in collect(line)]
425 pushPS(mem[TIB + mem[TOIN]])
431 EMIT = defPrim("EMIT", () -> begin
436 WORD = defPrim("WORD", () -> begin
458 if c == ' ' || c == '\t'
469 mem[wordAddr + offset] = Int64(c)
475 if c == ' ' || c == '\t' || c == '\n'
488 NUMBER = defPrim("NUMBER", () -> begin
493 s = ASCIIString([Char(c) for c in mem[wordAddr:(wordAddr+wordLen-1)]])
496 pushPS(parse(Int64, s, mem[BASE]))
499 pushPS(1) # Error indication
505 # Dictionary searches
507 FIND = defPrim("FIND", () -> begin
511 word = ASCIIString([Char(c) for c in mem[wordAddr:(wordAddr+wordLen-1)]])
516 lenAndFlags = mem[latest+1]
517 len = lenAndFlags & F_LENMASK
518 hidden = (lenAndFlags & F_HIDDEN) == F_HIDDEN
520 if hidden || len != wordLen
526 thisWord = ASCIIString([Char(c) for c in mem[thisAddr:(thisAddr+len-1)]])
538 TOCFA = defPrim(">CFA", () -> begin
541 lenAndFlags = mem[addr+1]
542 len = lenAndFlags & F_LENMASK
544 pushPS(addr + 2 + len)
549 TODFA = defWord(">DFA", [TOCFA, INCR, EXIT])
554 while (jmp = callPrim(jmp)) != 0 end
559 function coredump(startAddr::Int64; count::Int64 = 16, cellsPerLine::Int64 = 8)
560 chars = Array{Char,1}(cellsPerLine)
564 if i%cellsPerLine == 0
568 print("\t$(mem[addr]) ")
570 if (mem[addr]>=32 && mem[addr]<176)
571 chars[i%cellsPerLine + 1] = Char(mem[addr])
573 chars[i%cellsPerLine + 1] = '.'
576 if i%cellsPerLine == cellsPerLine-1
577 println(string("\t", ASCIIString(chars)))
583 count = reg.PSP - mem[PSP0]
587 for i in (mem[PSP0]+1):reg.PSP
592 println("Parameter stack empty")