X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=src%2Fforth.jl;h=0835e4be721f193689993ad7912baae19a3f9e67;hb=2268c3eaeb5a3a885ee4d63d64ef454549039a22;hp=766bd4e1cdf2f3ff24685ab186a75188103f8202;hpb=4b2f52917ce00e4af92d2a3a86e199d21748da5b;p=forth.jl.git diff --git a/src/forth.jl b/src/forth.jl index 766bd4e..0835e4b 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -36,18 +36,16 @@ primNames = Array{ASCIIString,1}() # Built-in variables nextVarAddr = 1 -RSP0 = nextVarAddr; nextVarAddr += 1 -PSP0 = nextVarAddr; nextVarAddr += 1 -HERE = nextVarAddr; nextVarAddr += 1 +H = 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[H] = TIB + size_TIB # location of bottom of dictionary +mem[LATEST] = 0 # no previous definition -DICT = mem[HERE] # Save bottom of dictionary as constant +DICT = mem[H] # Save bottom of dictionary as constant # VM registers type Reg @@ -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) @@ -583,7 +583,7 @@ PSPFETCH = defPrimWord("PSP@", () -> begin end) PSPSTORE = defPrimWord("PSP!", () -> begin - PSP = popPS() + reg.PSP = popPS() return NEXT end) @@ -604,7 +604,14 @@ end) sources = Array{Any,1}() currentSource() = sources[length(sources)] -EOF_CFA = defConst("EOF", 4) +EOF = defPrimWord("\x04", () -> begin + close(pop!(sources)) + if !isempty(sources) + return NEXT + else + return 0 + end +end) EMIT = defPrimWord("EMIT", () -> begin print(Char(popPS())) @@ -622,7 +629,7 @@ EXPECT = defPrimWord("EXPECT", () -> begin putString(line[1:mem[SPAN]], addr) else mem[SPAN] = 1 - mem[addr] = EOF + mem[addr] = 4 # eof end return NEXT @@ -737,8 +744,8 @@ TRACE = defPrimWord("TRACE", () -> begin end) COMMA = defPrimWord(",", () -> begin - mem[mem[HERE]] = popPS() - mem[HERE] += 1 + mem[mem[H]] = popPS() + mem[H] += 1 return NEXT end) @@ -756,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]) @@ -769,8 +776,8 @@ WORD = defPrimWord("WORD", () -> begin mem[TOIN] += 1 end - countAddr = mem[HERE] - addr = mem[HERE]+1 + countAddr = mem[H] + addr = mem[H]+1 # Start reading in word count = 0 @@ -797,7 +804,7 @@ PARSE = defPrimWord("PARSE", () -> begin delim = popPS() # Chew up initial occurrences of delim - addr = mem[HERE] + addr = mem[H] # Start reading input stream count = 0 @@ -868,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]) @@ -900,6 +912,37 @@ HEADER = defPrimWord("HEADER", () -> begin return NEXT end) +CREATE = defWord("CREATE", + [LIT, 32, WORD, HEADER, + LIT, DOVAR, COMMA, + EXIT]); + +DODOES = defPrim(() -> begin + pushRS(reg.IP) + reg.IP = popPS() + return NEXT +end, name="DODOES") + +BDOES = defPrimWord("(DOES>)", () -> begin + pushPS(mem[LATEST]) + callPrim(mem[TOCFA]) + cfa = popPS() + println(cfa) + + mem[cfa] = defPrim(eval(:(() -> begin + pushPS($(mem[H])) + return DODOES + end)), name="doesPrim") + + mem[mem[H]] = LIT; mem[H] += 1 + mem[mem[H]] = cfa+1; mem[H] += 1 + + return NEXT +end, flags=F_IMMED) + +DOES = defWord("DOES>", + [BDOES, EXIT]) + LBRAC = defPrimWord("[", () -> begin mem[STATE] = 0 return NEXT @@ -987,16 +1030,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 @@ -1038,11 +1073,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() @@ -1052,11 +1087,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()