4 size_mem = 1000000 # 1 mega-int
7 size_RS = 1000 # Return stack size
8 size_PS = 1000 # Parameter stack size
9 size_TIB = 1000 # 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 as negative indicies
30 # into the primitives array which contains these functions.
32 mem = Array{Int64,1}(size_mem)
33 primitives = Array{Function,1}()
34 primNames = Array{ASCIIString,1}()
39 RSP0 = nextVarAddr; nextVarAddr += 1
40 PSP0 = nextVarAddr; nextVarAddr += 1
41 HERE = nextVarAddr; nextVarAddr += 1
42 LATEST = nextVarAddr; nextVarAddr += 1
44 mem[RSP0] = nextVarAddr # bottom of RS
45 mem[PSP0] = mem[RSP0] + size_RS # bottom of PS
46 TIB = mem[PSP0] + size_PS # address of terminal input buffer
47 mem[HERE] = TIB + size_TIB # location of bottom of dictionary
48 mem[LATEST] = 0 # no previous definition
50 DICT = mem[HERE] # Save bottom of dictionary as constant
54 RSP::Int64 # Return stack pointer
55 PSP::Int64 # Parameter/data stack pointer
56 IP::Int64 # Instruction pointer
57 W::Int64 # Working register
59 reg = Reg(mem[RSP0], mem[PSP0], 0, 0)
61 # Stack manipulation functions
63 type ParamStackUnderflow <: Exception end
64 type ReturnStackUnderflow <: Exception end
66 Base.showerror(io::IO, ex::ParamStackUnderflow) = print(io, "Parameter stack underflow.")
67 Base.showerror(io::IO, ex::ReturnStackUnderflow) = print(io, "Return stack underflow.")
69 getRSDepth() = reg.RSP - mem[RSP0]
70 getPSDepth() = reg.PSP - mem[PSP0]
72 function ensurePSDepth(depth::Int64)
74 throw(ParamStackUnderflow())
78 function ensureRSDepth(depth::Int64)
80 throw(ReturnStackUnderflow())
84 function pushRS(val::Int64)
96 function pushPS(val::Int64)
97 mem[reg.PSP += 1] = val
108 # Handy functions for adding/retrieving strings to/from memory.
110 getString(addr::Int64, len::Int64) = ASCIIString([Char(c) for c in mem[addr:(addr+len-1)]])
112 function putString(str::ASCIIString, addr::Int64)
113 mem[addr:(addr+length(str)-1)] = [Int64(c) for c in str]
116 # Primitive creation and calling functions
118 function defPrim(f::Function; name="nameless")
120 push!(primNames, replace(name, "\004", "EOF"))
122 return -length(primitives)
125 function callPrim(addr::Int64)
126 if addr >=0 || -addr>length(primitives)
127 error("Attempted to execute non-existent primitive at address $addr.")
132 getPrimName(addr::Int64) = primNames[-addr]
134 # Word creation functions
140 function createHeader(name::AbstractString, flags::Int64)
141 mem[mem[HERE]] = mem[LATEST]
142 mem[LATEST] = mem[HERE]
145 mem[mem[HERE]] = length(name) | flags; mem[HERE] += 1
146 putString(name, mem[HERE]); mem[HERE] += length(name)
149 function defPrimWord(name::AbstractString, f::Function; flags::Int64=0)
150 createHeader(name, flags)
152 codeWordAddr = mem[HERE]
153 mem[codeWordAddr] = defPrim(f, name=name)
159 function defWord(name::AbstractString, wordAddrs::Array{Int64,1}; flags::Int64=0)
160 createHeader(name, flags)
163 mem[mem[HERE]] = DOCOL
166 for wordAddr in wordAddrs
167 mem[mem[HERE]] = wordAddr
174 # Variable creation functions
176 function defExistingVar(name::AbstractString, varAddr::Int64; flags::Int64=0)
178 defPrimWord(name, eval(:(() -> begin
184 function defNewVar(name::AbstractString, initial::Int64; flags::Int64=0)
185 createHeader(name, flags)
187 codeWordAddr = mem[HERE]
188 varAddr = mem[HERE] + 1
190 mem[mem[HERE]] = DOVAR; mem[HERE] += 1
191 mem[mem[HERE]] = initial; mem[HERE] += 1
193 return varAddr, codeWordAddr
196 function defConst(name::AbstractString, val::Int64; flags::Int64=0)
197 createHeader(name, flags)
199 codeWordAddr = mem[HERE]
201 mem[mem[HERE]] = DOCON; mem[HERE] += 1
202 mem[mem[HERE]] = val; mem[HERE] += 1
207 # Threading Primitives (inner interpreter)
209 NEXT = defPrim(() -> begin
215 DOCOL = defPrim(() -> begin
221 DOVAR = defPrim(() -> begin
226 DOCON = defPrim(() -> begin
227 pushPS(mem[reg.W + 1])
231 EXIT = defPrimWord("EXIT", () -> begin
236 # Dictionary entries for core built-in variables, constants
238 HERE_CFA = defExistingVar("HERE", HERE)
239 LATEST_CFA = defExistingVar("LATEST", LATEST)
240 PSP0_CFA = defExistingVar("PSP0", PSP0)
241 RSP0_CFA = defExistingVar("RSP0", RSP0)
243 defConst("DOCOL", DOCOL)
244 defConst("DOCON", DOCON)
245 defConst("DOVAR", DOVAR)
247 defConst("DICT", DICT)
248 defConst("MEMSIZE", size_mem)
250 F_IMMED_CFA = defConst("F_IMMED", F_IMMED)
251 F_HIDDEN_CFA = defConst("F_HIDDEN", F_HIDDEN)
252 F_LENMASK_CFA = defConst("F_LENMASK", F_LENMASK)
254 # Basic forth primitives
256 DROP = defPrimWord("DROP", () -> begin
261 SWAP = defPrimWord("SWAP", () -> begin
269 DUP = defPrimWord("DUP", () -> begin
275 OVER = defPrimWord("OVER", () -> begin
277 pushPS(mem[reg.PSP-1])
281 ROT = defPrimWord("ROT", () -> begin
291 NROT = defPrimWord("-ROT", () -> begin
302 TWODROP = defPrimWord("2DROP", () -> begin
308 TWODUP = defPrimWord("2DUP", () -> begin
317 TWOSWAP = defPrimWord("2SWAP", () -> begin
329 TWOOVER = defPrimWord("2OVER", () -> begin
338 QDUP = defPrimWord("?DUP", () -> begin
347 INCR = defPrimWord("1+", () -> begin
353 DECR = defPrimWord("1-", () -> begin
359 INCR2 = defPrimWord("2+", () -> begin
365 DECR2 = defPrimWord("2-", () -> begin
371 ADD = defPrimWord("+", () -> begin
378 SUB = defPrimWord("-", () -> begin
385 MUL = defPrimWord("*", () -> begin
392 DIVMOD = defPrimWord("/MOD", () -> begin
401 TWOMUL = defPrimWord("2*", () -> begin
406 TWODIV = defPrimWord("2/", () -> begin
411 EQ = defPrimWord("=", () -> begin
414 pushPS(a==b ? -1 : 0)
418 NE = defPrimWord("<>", () -> begin
421 pushPS(a!=b ? -1 : 0)
425 LT = defPrimWord("<", () -> begin
432 GT = defPrimWord(">", () -> begin
439 LE = defPrimWord("<=", () -> begin
442 pushPS(a<=b ? -1 : 0)
446 GE = defPrimWord(">=", () -> begin
449 pushPS(a>=b ? -1 : 0)
453 ZE = defPrimWord("0=", () -> begin
454 pushPS(popPS() == 0 ? -1 : 0)
458 ZNE = defPrimWord("0<>", () -> begin
459 pushPS(popPS() != 0 ? -1 : 0)
463 ZLT = defPrimWord("0<", () -> begin
464 pushPS(popPS() < 0 ? -1 : 0)
468 ZGT = defPrimWord("0>", () -> begin
469 pushPS(popPS() > 0 ? -1 : 0)
473 ZLE = defPrimWord("0<=", () -> begin
474 pushPS(popPS() <= 0 ? -1 : 0)
478 ZGE = defPrimWord("0>=", () -> begin
479 pushPS(popPS() >= 0 ? -1 : 0)
483 AND = defPrimWord("AND", () -> begin
490 OR = defPrimWord("OR", () -> begin
497 XOR = defPrimWord("XOR", () -> begin
504 INVERT = defPrimWord("INVERT", () -> begin
511 LIT = defPrimWord("LIT", () -> begin
519 STORE = defPrimWord("!", () -> begin
526 FETCH = defPrimWord("@", () -> begin
532 ADDSTORE = defPrimWord("+!", () -> begin
539 SUBSTORE = defPrimWord("-!", () -> begin
549 TOR = defPrimWord(">R", () -> begin
554 FROMR = defPrimWord("R>", () -> begin
559 RFETCH = defPrimWord("R@", () -> begin
564 RSPFETCH = defPrimWord("RSP@", () -> begin
569 RSPSTORE = defPrimWord("RSP!", () -> begin
574 RDROP = defPrimWord("RDROP", () -> begin
581 PSPFETCH = defPrimWord("PSP@", () -> begin
586 PSPSTORE = defPrimWord("PSP!", () -> begin
593 WFETCH = defPrimWord("W@", () -> begin
598 WSTORE = defPrimWord("W!", () -> begin
605 sources = Array{Any,1}()
606 currentSource() = sources[length(sources)]
608 EOF = defPrimWord("\x04", () -> begin
617 EMIT = defPrimWord("EMIT", () -> begin
622 SPAN, SPAN_CFA = defNewVar("SPAN", 0)
623 EXPECT = defPrimWord("EXPECT", () -> begin
627 if !eof(currentSource())
628 line = chomp(readline(currentSource()))
629 mem[SPAN] = min(length(line), maxLen)
630 putString(line[1:mem[SPAN]], addr)
639 BASE, BASE_CFA = defNewVar("BASE", 10)
640 NUMBER = defPrimWord("NUMBER", () -> begin
642 wordLen = mem[wordAddr-1]
644 s = getString(wordAddr, wordLen)
646 pushPS(parse(Int64, s, mem[BASE]))
651 # Dictionary searches
653 FIND = defPrimWord("FIND", () -> begin
655 countedAddr = popPS()
656 wordAddr = countedAddr + 1
657 wordLen = mem[countedAddr]
658 word = lowercase(getString(wordAddr, wordLen))
663 while (latest = mem[latest]) > 0
664 lenAndFlags = mem[latest+1]
665 len = lenAndFlags & F_LENMASK
666 hidden = (lenAndFlags & F_HIDDEN) == F_HIDDEN
668 if hidden || len != wordLen
673 thisWord = lowercase(getString(thisAddr, len))
675 if lowercase(thisWord) == lowercase(word)
685 TOCFA = defPrimWord(">CFA", () -> begin
688 lenAndFlags = mem[addr+1]
689 len = lenAndFlags & F_LENMASK
691 pushPS(addr + 2 + len)
696 TOPFA = defWord(">PFA", [TOCFA, INCR, EXIT])
700 BRANCH = defPrimWord("BRANCH", () -> begin
701 reg.IP += mem[reg.IP]
705 ZBRANCH = defPrimWord("0BRANCH", () -> begin
707 reg.IP += mem[reg.IP]
717 LITSTRING = defPrimWord("LITSTRING", () -> begin
727 TYPE = defPrimWord("TYPE", () -> begin
730 str = getString(addr, len)
737 TRACE = defPrimWord("TRACE", () -> begin
738 println("reg.W: $(reg.W) reg.IP: $(reg.IP)")
739 print("PS: "); printPS()
740 print("RS: "); printRS()
747 COMMA = defPrimWord(",", () -> begin
748 mem[mem[HERE]] = popPS()
754 BTICK = defWord("[']",
755 [FROMR, DUP, INCR, TOR, FETCH, EXIT])
757 EXECUTE = defPrimWord("EXECUTE", () -> begin
762 TIB_CFA = defConst("TIB", TIB)
763 NUMTIB, NUMTIB_CFA = defNewVar("#TIB", 0)
764 TOIN, TOIN_CFA = defNewVar(">IN", 0)
766 QUERY = defWord("QUERY",
767 [TIB_CFA, LIT, 160, EXPECT,
768 SPAN_CFA, FETCH, NUMTIB_CFA, STORE,
769 LIT, 0, TOIN_CFA, STORE,
772 WORD = defPrimWord("WORD", () -> begin
775 # Chew up initial occurrences of delim
776 while (mem[TOIN]<mem[NUMTIB] && mem[TIB+mem[TOIN]] == delim)
780 countAddr = mem[HERE]
783 # Start reading in word
785 while (mem[TOIN]<mem[NUMTIB])
786 mem[addr] = mem[TIB+mem[TOIN]]
789 if (mem[addr] == delim)
798 mem[countAddr] = count
804 PARSE = defPrimWord("PARSE", () -> begin
807 # Chew up initial occurrences of delim
810 # Start reading input stream
812 while (mem[TOIN]<mem[NUMTIB])
813 mem[addr] = mem[TIB+mem[TOIN]]
816 if (mem[addr] == delim)
830 BYE = defPrimWord("BYE", () -> begin
835 STATE, STATE_CFA = defNewVar("STATE", 0)
837 INTERPRET = defWord("INTERPRET",
838 [LIT, 32, WORD, # Read next space-delimited word
840 DUP, FETCH, ZE, ZBRANCH, 3,
841 DROP, EXIT, # Exit if TIB is exhausted
843 STATE_CFA, FETCH, ZBRANCH, 31,
845 DUP, FIND, QDUP, ZBRANCH, 19,
849 DUP, TOCFA, SWAP, INCR, FETCH, LIT, F_IMMED, AND, ZBRANCH, 4,
850 # Immediate: Execute!
851 EXECUTE, BRANCH, -33,
853 # Not immediate: Compile!
856 # No word found, parse number
857 NUMBER, BTICK, LIT, COMMA, COMMA, BRANCH, -43,
860 DUP, FIND, QDUP, ZBRANCH, 7,
862 # Found word. Execute!
863 SWAP, DROP, TOCFA, EXECUTE, BRANCH, -54,
865 # No word found, parse number and leave on stack
870 PROMPT = defPrimWord("PROMPT", () -> begin
871 if (mem[STATE] == 0 && currentSource() == STDIN)
878 QUIT = defWord("QUIT",
879 [LIT, 0, STATE_CFA, STORE,
880 LIT, 0, NUMTIB_CFA, STORE,
881 RSP0_CFA, FETCH, RSPSTORE,
886 ABORT = defWord("ABORT",
887 [PSP0_CFA, FETCH, PSPSTORE, QUIT])
889 INCLUDE = defPrimWord("INCLUDE", () -> begin
893 wordLen = mem[wordAddr-1]
894 word = getString(wordAddr, wordLen)
896 push!(sources, open(word, "r"))
906 HEADER = defPrimWord("HEADER", () -> begin
908 wordLen = mem[wordAddr-1]
909 word = getString(wordAddr, wordLen)
911 createHeader(word, 0)
916 LBRAC = defPrimWord("[", () -> begin
921 RBRAC = defPrimWord("]", () -> begin
926 HIDDEN = defPrimWord("HIDDEN", () -> begin
928 mem[addr] = mem[addr] $ F_HIDDEN
932 HIDE = defWord("HIDE",
942 LATEST_CFA, FETCH, HIDDEN,
946 SEMICOLON = defWord(";",
948 LATEST_CFA, FETCH, HIDDEN,
950 EXIT], flags=F_IMMED)
952 IMMEDIATE = defPrimWord("IMMEDIATE", () -> begin
953 lenAndFlagsAddr = mem[LATEST] + 1
954 mem[lenAndFlagsAddr] = mem[lenAndFlagsAddr] $ F_IMMED
962 initFileName = nothing
964 initFileName = "lib.4th"
965 elseif isfile(Pkg.dir("forth/src/lib.4th"))
966 initFileName = Pkg.dir("forth/src/lib.4th")
969 function run(;initialize=true)
970 # Begin with STDIN as source
971 push!(sources, STDIN)
973 global initialized, initFileName
974 if !initialized && initialize
975 if initFileName != nothing
976 print("Including definitions from $initFileName...")
977 push!(sources, open(initFileName, "r"))
980 println("No library file found. Only primitive words available.")
984 # Start with IP pointing to first instruction of outer interpreter
987 # Primitive processing loop.
988 # Everyting else is simply a consequence of this loop!
992 #println("Entering prim $(getPrimName(jmp))")
996 showerror(STDOUT, ex)
999 while !isempty(sources) && currentSource() != STDIN
1000 close(pop!(sources))
1012 function dump(startAddr::Int64; count::Int64 = 100, cellsPerLine::Int64 = 10)
1013 chars = Array{Char,1}(cellsPerLine)
1015 lineStartAddr = cellsPerLine*div((startAddr-1),cellsPerLine) + 1
1016 endAddr = startAddr + count - 1
1018 q, r = divrem((endAddr-lineStartAddr+1), cellsPerLine)
1019 numLines = q + (r > 0 ? 1 : 0)
1025 for c in 1:cellsPerLine
1026 if i >= startAddr && i <= endAddr
1028 if mem[i]>=32 && mem[i]<128
1029 chars[c] = Char(mem[i])
1041 println("\t", ASCIIString(chars))
1046 count = reg.PSP - mem[PSP0]
1050 for i in (mem[PSP0]+1):reg.PSP
1055 println("Parameter stack empty")
1060 count = reg.RSP - mem[RSP0]
1064 for i in (mem[RSP0]+1):reg.RSP
1069 println("Return stack empty")
1073 DUMP = defPrimWord("DUMP", () -> begin
1077 dump(addr, count=count)