X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=src%2Fforth.jl;h=a115e48221200b5af56576cde1087f90ca66d325;hb=a4a274445e7ddaa07b595814dfeb9f6ddb0ce95a;hp=5ea12255a23e8e238721aeda0722729c7a1f3e56;hpb=1cee9829bacd68f8e39e4e56257558b3c8b119ca;p=forth.jl.git diff --git a/src/forth.jl b/src/forth.jl index 5ea1225..a115e48 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -1,45 +1,35 @@ module forth -# VM memory size -size_memory = 640*1024 +# VM mem size +size_mem = 640*1024 # Buffer sizes -size_SysVar = 16 # size_RS = 1024 # Return stack size size_PS = 1024 # Parameter stack size -size_TIB = 4096 # Terminal input buffer size +size_TIB = 1096 # Terminal input buffer size -# VM registers -type Reg - RSP::Int64 # Return stack pointer - PSP::Int64 # Parameter/data stack pointer - IP::Int64 # Instruction pointer - W::Int64 # Working register - X::Int64 # Extra register -end - -# The following array constitutes the memory of the VM. It has the following geography: +# The mem array constitutes the memory of the VM. It has the following geography: # -# memory = +-----------------------+ -# | Built-in Variables | -# +-----------------------+ -# | Return Stack | -# +-----------------------+ -# | Parameter Stack | -# +-----------------------+ -# | Terminal Input Buffer | -# +-----------------------+ -# | Dictionary | -# +-----------------------+ +# mem = +-----------------------+ +# | Built-in Variables | +# +-----------------------+ +# | Return Stack | +# +-----------------------+ +# | Parameter Stack | +# +-----------------------+ +# | Terminal Input Buffer | +# +-----------------------+ +# | Dictionary | +# +-----------------------+ # # Note that all words (user-defined, primitive, variables, etc) are included in -# the dictionary. Additionally, all +# the dictionary. # # Simple linear addressing is used with one exception: references to primitive code # blocks, which are represented as anonymous functions, appear the negative index # into the primitives array which contains only these functions. -memory = Array{Int64,1}(size_memory) +mem = Array{Int64,1}(size_mem) primitives = Array{Function,1}() # Built-in variables @@ -50,232 +40,441 @@ PSP0 = nextVarAddr; nextVarAddr += 1 HERE = nextVarAddr; nextVarAddr += 1 LATEST = nextVarAddr; nextVarAddr += 1 -memory[RSP0] = size_BIVar # bottom of RS -memory[PSP0] = memory[RSP0] + size_RS # bottom of PS -TIB = memory[PSP0] + size_PS # address of terminal input buffer -memory[HERE] = TIB + size_TIB # location of bottom of dictionary -memory[LATEST] = 0 # no previous definition +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 + +DICT = mem[HERE] # Save bottom of dictionary as constant + +# VM registers +type Reg + RSP::Int64 # Return stack pointer + PSP::Int64 # Parameter/data stack pointer + IP::Int64 # Instruction pointer + W::Int64 # Working register + X::Int64 # Extra register +end +reg = Reg(mem[RSP0], mem[PSP0], 0, 0, 0) + +# Stack manipulation + +type StackUnderflow <: Exception end +getRSDepth() = reg.RSP - mem[RSP0] +getPSDepth() = reg.PSP - mem[PSP0] -# Stack manipulation functions +function ensurePSDepth(depth::Int64) + if getPSDepth() begin + pushPS($(varAddr)) + return NEXT + end))) +end - push!(primitives, eval(:((reg::Reg) -> begin - pushPS(reg, $(varAddr)) +function defNewVar(name::AbstractString, initial::Int64; flags::Int64=0) + createHeader(name, flags) + + varAddr = mem[HERE] + 1 + push!(primitives, eval(:(() -> begin + pushPS($(varAddr)) return NEXT end))) - memory[here] = -length(primitives) - here += 1 + mem[mem[HERE]] = -length(primitives); mem[HERE] += 1 + + mem[mem[HERE]] = initial; mem[HERE] += 1 return varAddr end -defConst(name::AbstractString, val::Int64) = defSysVar(name, val) +function defConst(name::AbstractString, val::Int64; flags::Int64=0) + defPrim(name, eval(:(() -> begin + pushPS($(val)) + return NEXT + end))) + + return val +end # Threading Primitives -NEXT = defPrim("NEXT", (reg) -> begin - reg.W = memory[reg.IP] +NEXT = defPrim("NEXT", () -> begin + reg.W = mem[reg.IP] reg.IP += 1 - X = memory[reg.W] + X = mem[reg.W] return X end) -DOCOL = defPrim("DOCOL", (reg) -> begin - pushRS(reg, reg.IP) +DOCOL = defPrim("DOCOL", () -> begin + pushRS(reg.IP) reg.IP = reg.W + 1 return NEXT end) -EXIT = defPrim("EXIT", (reg) -> begin - reg.IP = popRS(reg) +EXIT = defPrim("EXIT", () -> begin + reg.IP = popRS() return NEXT end) # Basic forth primitives -DROP = defPrim("DROP", (reg) -> begin - popPS(reg) +DROP = defPrim("DROP", () -> begin + popPS() return NEXT end) -SWAP = defPrim("SWAP", (reg) -> begin - memory[reg.PSP], memory[reg.PSP-1] = memory[reg.PSP-1], memory[reg.PSP] +SWAP = defPrim("SWAP", () -> begin + a = popPS() + b = popPS() + pushPS(a) + pushPS(b) return NEXT end) -DUP = defPrim("DUP", (reg) -> begin - pushPS(reg, memory[reg.PSP]) +DUP = defPrim("DUP", () -> begin + pushPS(mem[reg.PSP]) return NEXT end) -LIT = defPrim("LIT", (reg) -> begin - pushPS(reg, memory[reg.IP]) +OVER = defPrim("OVER", () -> begin + ensurePSDepth(2) + pushPS(mem[reg.PSP-1]) + return NEXT +end) + +ROT = defPrim("ROT", () -> begin + a = popPS() + b = popPS() + c = popPS() + pushPS(a) + pushPS(c) + pushPS(b) + return NEXT +end) + +NROT = defPrim("-ROT", () -> begin + a = popPS() + b = popPS() + c = popPS() + pushPS(b) + pushPS(a) + pushPS(c) + return NEXT +end) + +TWODROP = defPrim("2DROP", () -> begin + popPS() + popPS() + return NEXT +end) + +TWODUP = defPrim("2DUP", () -> begin + ensurePSDepth(2) + a = mem[reg.PSP-1] + b = mem[reg.PSP] + pushPS(a) + pushPS(b) + return NEXT +end) + +TWOSWAP = defPrim("2SWAP", () -> begin + a = popPS() + b = popPS() + c = popPS() + d = popPS() + pushPS(b) + pushPS(a) + pushPS(c) + pushPS(d) + return NEXT +end) + +QDUP = defPrim("?DUP", () -> begin + ensurePSDepth(1) + val = mem[reg.PSP] + if val != 0 + pushPS(val) + end + return NEXT +end) + +LIT = defPrim("LIT", () -> begin + pushPS(mem[reg.IP]) reg.IP += 1 return NEXT end) # Memory primitives -STORE = defPrim("!", (reg) -> begin - addr = popPS(reg) - dat = popPS(reg) - memory[addr] = dat +STORE = defPrim("!", () -> begin + addr = popPS() + dat = popPS() + mem[addr] = dat return NEXT end) -FETCH = defPrim("@", (reg) -> begin - addr = popPS(reg) - pushPS(reg, memory[addr]) +FETCH = defPrim("@", () -> begin + addr = popPS() + pushPS(mem[addr]) return NEXT end) -ADDSTORE = defPrim("+!", (reg) -> begin - addr = popPS(reg) - toAdd = popPS(reg) - memory[addr] += toAdd +ADDSTORE = defPrim("+!", () -> begin + addr = popPS() + toAdd = popPS() + mem[addr] += toAdd return NEXT end) -SUBSTORE = defPrim("-!", (reg) -> begin - addr = popPS(reg) - toSub = popPS(reg) - memory[addr] -= toSub +SUBSTORE = defPrim("-!", () -> begin + addr = popPS() + toSub = popPS() + mem[addr] -= toSub return NEXT end) # Built-in variables +defExistingVar("HERE", HERE) +defExistingVar("LATEST", LATEST) +defExistingVar("PSP0", PSP0) +defExistingVar("RSP0", RSP0) +STATE = defNewVar("STATE", 0) +BASE = defNewVar("BASE", 10) # Constants defConst("VERSION", 1) defConst("DOCOL", DOCOL) +defConst("DICT", DICT) +F_IMMED = defConst("F_IMMED", 100) +F_HIDEN = defConst("F_HIDDEN", 1000) # Return Stack -TOR = defPrim(">R", (reg) -> begin - pushRS(reg, popPS(reg)) +TOR = defPrim(">R", () -> begin + pushRS(popPS()) return NEXT end) -FROMR = defPrim("R>", (reg) -> begin - pushPS(reg, popRS(reg)) +FROMR = defPrim("R>", () -> begin + pushPS(popRS()) return NEXT end) -RSPFETCH = defPrim("RSP@", (reg) -> begin - pushPS(reg, RSP) +RSPFETCH = defPrim("RSP@", () -> begin + pushPS(reg.RSP) return NEXT end) -RSPSTORE = defPrim("RSP!", (reg) -> begin - RSP = popPS(reg) +RSPSTORE = defPrim("RSP!", () -> begin + RSP = popPS() return NEXT end) -RDROP = defPrim("RDROP", (reg) -> begin - popRS(reg) +RDROP = defPrim("RDROP", () -> begin + popRS() return NEXT end) # Parameter Stack -PSPFETCH = defPrim("PSP@", (reg) -> begin - pushPS(reg, PSP) +PSPFETCH = defPrim("PSP@", () -> begin + pushPS(reg.PSP) return NEXT end) -PSPSTORE = defPrim("PSP!", (reg) -> begin - PSP = popPS(reg) +PSPSTORE = defPrim("PSP!", () -> begin + PSP = popPS() return NEXT end) # I/O -defConst("TIB", tib) -defVar("#TIB", :numtib) -defVar(">IN", :toin) - -KEY = defPrim("KEY", (reg) -> begin - if toin >= numtib +defConst("TIB", TIB) +NUMTIB = defNewVar("#TIB", 0) +TOIN = defNewVar(">IN", 0) +KEY = defPrim("KEY", () -> begin + if mem[TOIN] >= mem[NUMTIB] + mem[TOIN] = 0 + line = readline() + mem[NUMTIB] = length(line) + mem[TIB:(TIB+mem[NUMTIB]-1)] = [Int64(c) for c in collect(line)] end + pushPS(mem[TIB + mem[TOIN]]) + mem[TOIN] += 1 + return NEXT end) -EMIT = defPrim("EMIT", (reg) -> begin - +EMIT = defPrim("EMIT", () -> begin + print(Char(popPS())) return NEXT end) -WORD = defPrim("WORD", (reg) -> begin +WORD = defPrim("WORD", () -> begin + + c = -1 - return NEXT -end) + skip_to_end = false + while true + + callPrim(KEY) + c = Char(popPS()) + + if c == '\\' + skip_to_end = true + continue + end + + if skip_to_end + if c == '\n' + skip_to_end = false + end + continue + end -NUMBER = defPrim("NUMBER", (reg) -> begin + if c == ' ' || c == '\t' + continue + end + + break + end + + wordAddr = mem[HERE] + offset = 0 + + while true + mem[wordAddr + offset] = Int64(c) + offset += 1 + + callPrim(KEY) + c = Char(popPS()) + + if c == ' ' || c == '\t' + break + end + end + + wordLen = offset + + pushPS(wordAddr) + pushPS(wordLen) return NEXT end) - +# +#NUMBER = defPrim("NUMBER", (reg) -> begin +# +# return NEXT +#end) +# #### VM loop #### -function runVM(reg::Reg) - jmp = NEXT - while (jmp = callPrim(reg, jmp)) != 0 end +#function runVM(reg::Reg) +# jmp = NEXT +# while (jmp = callPrim(reg, jmp)) != 0 end +#end + +# Debugging tools + +function coredump(startAddr::Int64; count::Int64 = 16, cellsPerLine::Int64 = 8) + chars = Array{Char,1}(cellsPerLine) + + for i in 0:(count-1) + addr = startAddr + i + if i%cellsPerLine == 0 + print("$addr:") + end + + print("\t$(mem[addr]) ") + + if (mem[addr]>=32 && mem[addr]<176) + chars[i%cellsPerLine + 1] = Char(mem[addr]) + else + chars[i%cellsPerLine + 1] = '.' + end + + if i%cellsPerLine == cellsPerLine-1 + println(string("\t", ASCIIString(chars))) + end + end +end + +function printPS() + count = reg.PSP - mem[PSP0] + + if count > 0 + print("<$count>") + for i in (mem[PSP0]+1):reg.PSP + print(" $(mem[i])") + end + println() + else + println("Parameter stack empty") + end end end