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 # Handy functions for adding/retrieving strings to/from memory.
106 getString(addr::Int64, len::Int64) = ASCIIString([Char(c) for c in mem[addr:(addr+len-1)]])
107 function putString(str::ASCIIString, addr::Int64)
108 mem[addr:(addr+length(str)-1)] = [Int64(c) for c in str]
111 # Primitive creation and calling functions
113 function createHeader(name::AbstractString, flags::Int64)
114 mem[mem[HERE]] = mem[LATEST]
115 mem[LATEST] = mem[HERE]
118 mem[mem[HERE]] = length(name) | flags; mem[HERE] += 1
119 putString(name, mem[HERE]); mem[HERE] += length(name)
122 function defPrim(name::AbstractString, f::Function; flags::Int64=0)
123 createHeader(name, flags)
125 codeWordAddr = mem[HERE]
127 mem[codeWordAddr] = -length(primitives)
133 callPrim(addr::Int64) = primitives[-addr]()
135 function defExistingVar(name::AbstractString, varAddr::Int64; flags::Int64=0)
136 defPrim(name, eval(:(() -> begin
142 function defNewVar(name::AbstractString, initial::Int64; flags::Int64=0)
143 createHeader(name, flags)
145 varAddr = mem[HERE] + 1
146 push!(primitives, eval(:(() -> begin
150 mem[mem[HERE]] = -length(primitives); mem[HERE] += 1
152 mem[mem[HERE]] = initial; mem[HERE] += 1
157 function defConst(name::AbstractString, val::Int64; flags::Int64=0)
158 defPrim(name, eval(:(() -> begin
166 function defWord(name::AbstractString, wordAddrs::Array{Int64,1}; flags::Int64=0)
167 createHeader(name, flags)
170 mem[mem[HERE]] = mem[DOCOL]
173 for wordAddr in wordAddrs
174 mem[mem[HERE]] = wordAddr
181 # Threading Primitives (inner interpreter)
183 NEXT = defPrim("NEXT", () -> begin
189 DOCOL = defPrim("DOCOL", () -> begin
195 EXIT = defPrim("EXIT", () -> begin
200 # Basic forth primitives
202 DROP = defPrim("DROP", () -> begin
207 SWAP = defPrim("SWAP", () -> begin
215 DUP = defPrim("DUP", () -> begin
220 OVER = defPrim("OVER", () -> begin
222 pushPS(mem[reg.PSP-1])
226 ROT = defPrim("ROT", () -> begin
236 NROT = defPrim("-ROT", () -> begin
246 TWODROP = defPrim("2DROP", () -> begin
252 TWODUP = defPrim("2DUP", () -> begin
261 TWOSWAP = defPrim("2SWAP", () -> begin
273 QDUP = defPrim("?DUP", () -> begin
282 INCR = defPrim("1+", () -> begin
288 DECR = defPrim("1-", () -> begin
294 INCR2 = defPrim("2+", () -> begin
300 DECR2 = defPrim("2-", () -> begin
306 ADD = defPrim("+", () -> begin
313 SUB = defPrim("-", () -> begin
320 MUL = defPrim("*", () -> begin
327 DIVMOD = defPrim("/MOD", () -> begin
336 EQU = defPrim("=", () -> begin
339 pushPS(a==b ? -1 : 0)
343 NEQU = defPrim("<>", () -> begin
346 pushPS(a!=b ? -1 : 0)
350 LT = defPrim("<", () -> begin
357 GT = defPrim(">", () -> begin
364 LE = defPrim("<=", () -> begin
367 pushPS(a<=b ? -1 : 0)
371 GE = defPrim(">=", () -> begin
374 pushPS(a>=b ? -1 : 0)
378 ZEQU = defPrim("0=", () -> begin
379 pushPS(popPS() == 0 ? -1 : 0)
383 ZNEQU = defPrim("0<>", () -> begin
384 pushPS(popPS() != 0 ? -1 : 0)
388 ZLT = defPrim("0<", () -> begin
389 pushPS(popPS() < 0 ? -1 : 0)
393 ZGT = defPrim("0>", () -> begin
394 pushPS(popPS() > 0 ? -1 : 0)
398 ZLE = defPrim("0<=", () -> begin
399 pushPS(popPS() <= 0 ? -1 : 0)
403 ZGE = defPrim("0>=", () -> begin
404 pushPS(popPS() >= 0 ? -1 : 0)
408 AND = defPrim("AND", () -> begin
415 OR = defPrim("OR", () -> begin
422 XOR = defPrim("XOR", () -> begin
429 INVERT = defPrim("INVERT", () -> begin
436 LIT = defPrim("LIT", () -> begin
444 STORE = defPrim("!", () -> begin
451 FETCH = defPrim("@", () -> begin
457 ADDSTORE = defPrim("+!", () -> begin
464 SUBSTORE = defPrim("-!", () -> begin
474 defExistingVar("HERE", HERE)
475 defExistingVar("LATEST", LATEST)
476 defExistingVar("PSP0", PSP0)
477 defExistingVar("RSP0", RSP0)
478 STATE = defNewVar("STATE", 0)
479 BASE = defNewVar("BASE", 10)
483 defConst("VERSION", 1)
484 defConst("DOCOL", DOCOL)
485 defConst("DICT", DICT)
486 F_IMMED = defConst("F_IMMED", 128)
487 F_HIDDEN = defConst("F_HIDDEN", 256)
488 F_LENMASK = defConst("F_LENMASK", 127)
492 TOR = defPrim(">R", () -> begin
497 FROMR = defPrim("R>", () -> begin
502 RSPFETCH = defPrim("RSP@", () -> begin
507 RSPSTORE = defPrim("RSP!", () -> begin
512 RDROP = defPrim("RDROP", () -> begin
519 PSPFETCH = defPrim("PSP@", () -> begin
524 PSPSTORE = defPrim("PSP!", () -> begin
532 NUMTIB = defNewVar("#TIB", 0)
533 TOIN = defNewVar(">IN", 0)
535 KEY = defPrim("KEY", () -> begin
536 if mem[TOIN] >= mem[NUMTIB]
539 mem[NUMTIB] = length(line)
543 pushPS(mem[TIB + mem[TOIN]])
549 EMIT = defPrim("EMIT", () -> begin
554 WORD = defPrim("WORD", () -> begin
576 if c == ' ' || c == '\t'
587 mem[wordAddr + offset] = Int64(c)
593 if c == ' ' || c == '\t' || c == '\n'
606 NUMBER = defPrim("NUMBER", () -> begin
611 s = getString(wordAddr, wordLen)
614 pushPS(parse(Int64, s, mem[BASE]))
617 pushPS(1) # Error indication
623 # Dictionary searches
625 FIND = defPrim("FIND", () -> begin
629 word = getString(wordAddr, wordLen)
634 while (latest = mem[latest]) > 0
635 lenAndFlags = mem[latest+1]
636 len = lenAndFlags & F_LENMASK
637 hidden = (lenAndFlags & F_HIDDEN) == F_HIDDEN
639 if hidden || len != wordLen
644 thisWord = getString(thisAddr, len)
656 TOCFA = defPrim(">CFA", () -> begin
659 lenAndFlags = mem[addr+1]
660 len = lenAndFlags & F_LENMASK
662 pushPS(addr + 2 + len)
667 TODFA = defWord(">DFA", [TOCFA, INCR, EXIT])
671 CREATE = defPrim("CREATE", () -> begin
675 word = getString(wordAddr, wordLen)
677 createHeader(word, 0)
682 COMMA = defPrim(",", () -> begin
683 mem[mem[HERE]] = popPS()
689 LBRAC = defPrim("[", () -> begin
694 RBRAC = defPrim("]", () -> begin
699 HIDDEN = defPrim("HIDDEN", () -> begin
701 mem[addr] = mem[addr] $ F_HIDDEN
705 HIDE = defWord("HIDE",
715 LATEST, FETCH, HIDDEN,
719 SEMICOLON = defWord(";",
721 LATEST, FETCH, HIDDEN,
723 EXIT], flags=F_IMMED)
725 IMMEDIATE = defPrim("IMMEDIATE", () -> begin
726 lenAndFlagsAddr = mem[LATEST] + 1
727 mem[lenAndFlagsAddr] = mem[lenAndFlagsAddr] $ F_IMMED
731 TICK = defWord("'", [WORD, FIND, TOCFA, EXIT])
735 BRANCH = defPrim("BRANCH", () -> begin
736 reg.IP += mem[reg.IP]
740 ZBRANCH = defPrim("0BRANCH", () -> begin
742 reg.IP += mem[reg.IP]
752 LITSTRING = defPrim("LITSTRING", () -> begin
765 while (jmp = callPrim(jmp)) != 0 end
770 function dump(startAddr::Int64; count::Int64 = 100, cellsPerLine::Int64 = 10)
771 chars = Array{Char,1}(cellsPerLine)
775 if i%cellsPerLine == 0
779 print("\t$(mem[addr]) ")
781 if (mem[addr]>=32 && mem[addr]<128)
782 chars[i%cellsPerLine + 1] = Char(mem[addr])
784 chars[i%cellsPerLine + 1] = '.'
787 if i%cellsPerLine == cellsPerLine-1
788 println(string("\t", ASCIIString(chars)))
794 count = reg.PSP - mem[PSP0]
798 for i in (mem[PSP0]+1):reg.PSP
803 println("Parameter stack empty")
808 count = reg.RSP - mem[RSP0]
812 for i in (mem[RSP0]+1):reg.RSP
817 println("Return stack empty")