X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=src%2Fforth.jl;h=fb7121c27d13851b98c445e2daf7db1000219e4c;hb=87b17b0ee12c90f028186761fbb2bd5f6b4008eb;hp=a233743809fa806017a4ce0313d8d454fc51114d;hpb=366db53c2e77d243c744c11f68b8bdeb575c86e6;p=forth.jl.git diff --git a/src/forth.jl b/src/forth.jl index a233743..fb7121c 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -36,16 +36,14 @@ primNames = Array{ASCIIString,1}() # Built-in variables nextVarAddr = 1 -RSP0 = nextVarAddr; nextVarAddr += 1 -PSP0 = nextVarAddr; nextVarAddr += 1 HERE = nextVarAddr; nextVarAddr += 1 LATEST = nextVarAddr; nextVarAddr += 1 -mem[RSP0] = nextVarAddr # bottom of RS -mem[PSP0] = mem[RSP0] + size_RS # bottom of PS -TIB = mem[PSP0] + size_PS # address of terminal input buffer -mem[HERE] = TIB + size_TIB # location of bottom of dictionary -mem[LATEST] = 0 # no previous definition +RSP0 = nextVarAddr # bottom of RS +PSP0 = RSP0 + size_RS # bottom of PS +TIB = PSP0 + size_PS # address of terminal input buffer +mem[HERE] = TIB + size_TIB # location of bottom of dictionary +mem[LATEST] = 0 # no previous definition DICT = mem[HERE] # Save bottom of dictionary as constant @@ -56,7 +54,7 @@ type Reg IP::Int64 # Instruction pointer W::Int64 # Working register end -reg = Reg(mem[RSP0], mem[PSP0], 0, 0) +reg = Reg(RSP0, PSP0, 0, 0) # Stack manipulation functions @@ -66,8 +64,8 @@ type ReturnStackUnderflow <: Exception end Base.showerror(io::IO, ex::ParamStackUnderflow) = print(io, "Parameter stack underflow.") Base.showerror(io::IO, ex::ReturnStackUnderflow) = print(io, "Return stack underflow.") -getRSDepth() = reg.RSP - mem[RSP0] -getPSDepth() = reg.PSP - mem[PSP0] +getRSDepth() = reg.RSP - RSP0 +getPSDepth() = reg.PSP - PSP0 function ensurePSDepth(depth::Int64) if getPSDepth() begin end) RSPSTORE = defPrimWord("RSP!", () -> begin - RSP = popPS() + reg.RSP = popPS() return NEXT end) @@ -584,7 +583,7 @@ PSPFETCH = defPrimWord("PSP@", () -> begin end) PSPSTORE = defPrimWord("PSP!", () -> begin - PSP = popPS() + reg.PSP = popPS() return NEXT end) @@ -764,7 +763,7 @@ NUMTIB, NUMTIB_CFA = defNewVar("#TIB", 0) TOIN, TOIN_CFA = defNewVar(">IN", 0) QUERY = defWord("QUERY", - [TIB_CFA, LIT, 80, EXPECT, + [TIB_CFA, LIT, 160, EXPECT, SPAN_CFA, FETCH, NUMTIB_CFA, STORE, LIT, 0, TOIN_CFA, STORE, EXIT]) @@ -876,11 +875,16 @@ PROMPT = defPrimWord("PROMPT", () -> begin end) QUIT = defWord("QUIT", - [RSP0_CFA, RSPSTORE, + [LIT, 0, STATE_CFA, STORE, + LIT, 0, NUMTIB_CFA, STORE, + RSP0_CFA, RSPSTORE, QUERY, INTERPRET, PROMPT, BRANCH,-4]) +ABORT = defWord("ABORT", + [PSP0_CFA, PSPSTORE, QUIT]) + INCLUDE = defPrimWord("INCLUDE", () -> begin pushPS(32) callPrim(mem[WORD]) @@ -984,7 +988,7 @@ function run(;initialize=true) jmp = NEXT while jmp != 0 try -# println("Entering prim $(getPrimName(jmp))") + #println("Entering prim $(getPrimName(jmp))") jmp = callPrim(jmp) catch ex @@ -995,16 +999,8 @@ function run(;initialize=true) close(pop!(sources)) end - # Want backtrace in here eventually - println("reg.W: $(reg.W) reg.IP: $(reg.IP)") - print("PS: "); printPS() - print("RS: "); printRS() - - mem[STATE] = 0 - mem[NUMTIB] = 0 - reg.PSP = mem[PSP0] - reg.RSP = mem[RSP0] - reg.IP = QUIT + 1 + # QUIT + reg.IP = ABORT + 1 jmp = NEXT end end @@ -1046,11 +1042,11 @@ function dump(startAddr::Int64; count::Int64 = 100, cellsPerLine::Int64 = 10) end function printPS() - count = reg.PSP - mem[PSP0] + count = reg.PSP - PSP0 if count > 0 print("<$count>") - for i in (mem[PSP0]+1):reg.PSP + for i in (PSP0+1):reg.PSP print(" $(mem[i])") end println() @@ -1060,11 +1056,11 @@ function printPS() end function printRS() - count = reg.RSP - mem[RSP0] + count = reg.RSP - RSP0 if count > 0 print("<$count>") - for i in (mem[RSP0]+1):reg.RSP + for i in (RSP0+1):reg.RSP print(" $(mem[i])") end println()