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)
161 mem[mem[HERE]] = DOCOL
164 for wordAddr in wordAddrs
165 mem[mem[HERE]] = wordAddr
170 # Threading Primitives
172 NEXT = defPrim("NEXT", () -> begin
179 DOCOL = defPrim("DOCOL", () -> begin
185 EXIT = defPrim("EXIT", () -> begin
191 # Basic forth primitives
193 DROP = defPrim("DROP", () -> begin
198 SWAP = defPrim("SWAP", () -> begin
206 DUP = defPrim("DUP", () -> begin
211 OVER = defPrim("OVER", () -> begin
213 pushPS(mem[reg.PSP-1])
217 ROT = defPrim("ROT", () -> begin
227 NROT = defPrim("-ROT", () -> begin
237 TWODROP = defPrim("2DROP", () -> begin
243 TWODUP = defPrim("2DUP", () -> begin
252 TWOSWAP = defPrim("2SWAP", () -> begin
264 QDUP = defPrim("?DUP", () -> begin
273 INCR = defPrim("1+", () -> begin
279 DECR = defPrim("1-", () -> begin
285 ADD = defPrim("+", () -> begin
292 SUB = defPrim("-", () -> begin
299 MUL = defPrim("*", () -> begin
306 DIVMOD = defPrim("/MOD", () -> begin
315 LIT = defPrim("LIT", () -> begin
323 STORE = defPrim("!", () -> begin
330 FETCH = defPrim("@", () -> begin
336 ADDSTORE = defPrim("+!", () -> begin
343 SUBSTORE = defPrim("-!", () -> begin
353 defExistingVar("HERE", HERE)
354 defExistingVar("LATEST", LATEST)
355 defExistingVar("PSP0", PSP0)
356 defExistingVar("RSP0", RSP0)
357 STATE = defNewVar("STATE", 0)
358 BASE = defNewVar("BASE", 10)
362 defConst("VERSION", 1)
363 defConst("DOCOL", DOCOL)
364 defConst("DICT", DICT)
365 F_IMMED = defConst("F_IMMED", 128)
366 F_HIDDEN = defConst("F_HIDDEN", 256)
367 F_LENMASK = defConst("F_LENMASK", 127)
371 TOR = defPrim(">R", () -> begin
376 FROMR = defPrim("R>", () -> begin
381 RSPFETCH = defPrim("RSP@", () -> begin
386 RSPSTORE = defPrim("RSP!", () -> begin
391 RDROP = defPrim("RDROP", () -> begin
398 PSPFETCH = defPrim("PSP@", () -> begin
403 PSPSTORE = defPrim("PSP!", () -> begin
411 NUMTIB = defNewVar("#TIB", 0)
412 TOIN = defNewVar(">IN", 0)
414 KEY = defPrim("KEY", () -> begin
415 if mem[TOIN] >= mem[NUMTIB]
418 mem[NUMTIB] = length(line)
419 mem[TIB:(TIB+mem[NUMTIB]-1)] = [Int64(c) for c in collect(line)]
422 pushPS(mem[TIB + mem[TOIN]])
428 EMIT = defPrim("EMIT", () -> begin
433 WORD = defPrim("WORD", () -> begin
455 if c == ' ' || c == '\t'
466 mem[wordAddr + offset] = Int64(c)
472 if c == ' ' || c == '\t' || c == '\n'
485 NUMBER = defPrim("NUMBER", () -> begin
490 s = ASCIIString([Char(c) for c in mem[wordAddr:(wordAddr+wordLen-1)]])
493 pushPS(parse(Int64, s, mem[BASE]))
496 pushPS(1) # Error indication
502 # Dictionary searches
504 FIND = defPrim("FIND", () -> begin
508 word = ASCIIString([Char(c) for c in mem[wordAddr:(wordAddr+wordLen-1)]])
513 lenAndFlags = mem[latest+1]
514 len = lenAndFlags & F_LENMASK
515 hidden = (lenAndFlags & F_HIDDEN) == F_HIDDEN
517 if hidden || len != wordLen
523 thisWord = ASCIIString([Char(c) for c in mem[thisAddr:(thisAddr+len-1)]])
535 TOCFA = defPrim(">CFA", () -> begin
538 lenAndFlags = mem[addr+1]
539 len = lenAndFlags & F_LENMASK
541 pushPS(addr + 2 + len)
546 TODFA = defWord(">DFA", [TOCFA, INCR1, EXIT])
549 #function runVM(reg::Reg)
551 # while (jmp = callPrim(reg, jmp)) != 0 end
556 function coredump(startAddr::Int64; count::Int64 = 16, cellsPerLine::Int64 = 8)
557 chars = Array{Char,1}(cellsPerLine)
561 if i%cellsPerLine == 0
565 print("\t$(mem[addr]) ")
567 if (mem[addr]>=32 && mem[addr]<176)
568 chars[i%cellsPerLine + 1] = Char(mem[addr])
570 chars[i%cellsPerLine + 1] = '.'
573 if i%cellsPerLine == cellsPerLine-1
574 println(string("\t", ASCIIString(chars)))
580 count = reg.PSP - mem[PSP0]
584 for i in (mem[PSP0]+1):reg.PSP
589 println("Parameter stack empty")