X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=forth.jl.git;a=blobdiff_plain;f=src%2Fforth.jl;h=4c81dc35b59f305078a7a2a08f4e95927b640b8b;hp=e8c5dbdce50552d3a70348d08061f772232a3e99;hb=2de79279a0f7157a41c0f32674ba96f56cb1b02f;hpb=e86ac614c819b40a4fe25dd0b2e4f2ac51658969 diff --git a/src/forth.jl b/src/forth.jl index e8c5dbd..4c81dc3 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -1,6 +1,6 @@ module forth -import Base.REPLCompletions +import Base.REPLCompletions, Base.invokelatest # VM mem size size_mem = 1000000 # 1 mega-int @@ -9,6 +9,7 @@ size_mem = 1000000 # 1 mega-int size_RS = 1000 # Return stack size size_PS = 1000 # Parameter stack size size_TIB = 1000 # Terminal input buffer size +size_FIB = 1000 # File input buffer size # Memory arrays mem = Array{Int64,1}(size_mem) @@ -25,7 +26,8 @@ CURRENT = nextVarAddr; nextVarAddr += 1 # Current compilation dict 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 +FIB = TIB + size_TIB # address of terminal input buffer +mem[H] = FIB + size_FIB # location of bottom of dictionary mem[FORTH_LATEST] = 0 # zero FORTH dict latest (no previous def) mem[CURRENT] = FORTH_LATEST-1 # Compile words to system dict initially @@ -95,7 +97,11 @@ end # Handy functions for adding/retrieving strings to/from memory. -getString(addr::Int64, len::Int64) = AbstractString([Char(c) for c in mem[addr:(addr+len-1)]]) +getString(addr::Int64, len::Int64) = String([Char(c) for c in mem[addr:(addr+len-1)]]) + +function putString(str::AbstractString, addr::Int64) + mem[addr:(addr+length(str)-1)] = [Int64(c) for c in str] +end function putString(str::AbstractString, addr::Int64, maxLen::Int64) len = min(length(str), maxLen) @@ -117,7 +123,7 @@ function callPrim(addr::Int64) if addr >=0 || -addr>length(primitives) error("Attempted to execute non-existent primitive at address $addr.") else - primitives[-addr]() + invokelatest(primitives[-addr]) end end getPrimName(addr::Int64) = primNames[-addr] @@ -495,7 +501,7 @@ end) XOR_CFA = defPrimWord("XOR", () -> begin b = popPS() a = popPS() - pushPS(a $ b) + pushPS(xor(a, b)) return NEXT end) @@ -602,7 +608,6 @@ end) openFiles = Dict{Int64,IOStream}() nextFileID = 1 -SOURCE_ID, SOURCE_ID_CFA = defNewVar("SOURCE-ID", 0) ## File access modes @@ -611,14 +616,14 @@ FAM_WO = 1 FAM_RO_CFA = defConst("R/O", FAM_RO) FAM_WO_CFA = defConst("W/O", FAM_WO) -OPEN_FILE_CFA = defPrimWord("OPEN-FILE", () -> begin +function fileOpener(create::Bool) + fam = popPS() fnameLen = popPS() fnameAddr = popPS() - fam = popPS() fname = getString(fnameAddr, fnameLen) - if (!isfile(fname)) + if create && !isfile(fname) pushPS(0) pushPS(-1) # error return NEXT @@ -630,19 +635,83 @@ OPEN_FILE_CFA = defPrimWord("OPEN-FILE", () -> begin mode = "w" end + global nextFileID openFiles[nextFileID] = open(fname, mode) pushPS(nextFileID) pushPS(0) nextFileID += 1 +end +OPEN_FILE_CFA = defPrimWord("OPEN-FILE", () -> begin + fileOpener(false) return NEXT end); CREATE_FILE_CFA = defPrimWord("CREATE-FILE", () -> begin + fileOpener(true) + return NEXT +end); + +CLOSE_FILE_CFA = defPrimWord("CLOSE-FILE", () -> begin + fid = popPS() + close(openFiles[fid]) + delete!(openFiles, fid) + + pushPS(0) # Result code 0 + return NEXT +end) + +CLOSE_FILES_CFA = defPrimWord("CLOSE-FILES", () -> begin + for fh in values(openFiles) + close(fh) + end + empty!(openFiles) + + pushPS(0) # Result code 0 + return NEXT +end) + +READ_LINE_CFA = defPrimWord("READ-LINE", () -> begin + fid = popPS() + maxSize = popPS() + addr = popPS() + + fh = openFiles[fid] + line = readline(fh, chomp=false) + + eofFlag = endswith(line, '\n') ? 0 : -1 + line = chomp(line) + + putString(line, addr, maxSize) + + pushPS(length(line)) + pushPS(eofFlag) + pushPS(0) + + return NEXT +end) + +READ_FILE_CFA = defPrimWord("READ-FILE", () -> begin + fid = popPS() + size = popPS() + addr = popPS() + + fh = openFiles[fid] + + string = join(map(x -> Char(x), read(fh, size)), "") + + eofFlag = length(string) == size ? 0 : -1 ; + + putString(string, addr, length(string)) + + pushPS(length(string)) + pushPS(eofFlag) + return NEXT end) + EMIT_CFA = defPrimWord("EMIT", () -> begin print(Char(popPS())) return NEXT @@ -656,7 +725,7 @@ end function getKey() raw_mode!(true) - byte = readbytes(STDIN, 1)[1] + byte = read(STDIN, 1)[1] raw_mode!(false) if byte == 0x0d @@ -698,7 +767,7 @@ function getLineFromSTDIN() if key == '\n' print(" ") - return AbstractString(line) + return String(line) elseif key == '\x04' if isempty(line) @@ -912,19 +981,54 @@ end) TIB_CFA = defConst("TIB", TIB) NUMTIB, NUMTIB_CFA = defNewVar("#TIB", 0) + +FIB_CFA = defConst("FIB", FIB) +NUMFIB, NUMFIB_CFA = defNewVar("#FIB", 0) + +IB_CFA = defPrimWord("IB", () -> begin + pushPS(mem[SOURCE_ID_VAR] == 0 ? TIB : FIB) + return NEXT +end) + +NUMIB_CFA = defPrimWord("#IB", () -> begin + pushPS(mem[SOURCE_ID_VAR] == 0 ? NUMTIB : NUMFIB) + return NEXT +end) + TOIN, TOIN_CFA = defNewVar(">IN", 0) +SOURCE_ID_VAR, SOURCE_ID_VAR_CFA = defNewVar("SOURCE-ID-VAR", 0) + QUERY_CFA = defWord("QUERY", [TIB_CFA, LIT_CFA, 160, EXPECT_CFA, SPAN_CFA, FETCH_CFA, NUMTIB_CFA, STORE_CFA, LIT_CFA, 0, TOIN_CFA, STORE_CFA, EXIT_CFA]) +EOF_FLAG, EOF_FLAG_CFA = defNewVar("EOF-FLAG", 0) + +# ( fid -- ) +# EOF-FLAG set to true if EOF is reached +QUERY_FILE_CFA = defWord("QUERY-FILE", + [FIB_CFA, LIT_CFA, 160, ROT_CFA, READ_LINE_CFA, + DROP_CFA, EOF_FLAG_CFA, STORE_CFA, + NUMFIB_CFA, STORE_CFA, + LIT_CFA, 0, TOIN_CFA, STORE_CFA, + EXIT_CFA]) + WORD_CFA = defPrimWord("WORD", () -> begin delim = popPS() + if mem[SOURCE_ID_VAR] == 0 + bufferAddr = TIB + sizeAddr = NUMTIB + else + bufferAddr = FIB + sizeAddr = NUMFIB + end + # Chew up initial occurrences of delim - while (mem[TOIN] begin # Start reading in word count = 0 - while (mem[TOIN] begin mem[countAddr] = count pushPS(countAddr) + #println("Processing word: '$(getString(countAddr+1,mem[countAddr]))' (state $(mem[STATE]))") + return NEXT end) @@ -1021,7 +1127,7 @@ end, flags=F_IMMED) HIDDEN_CFA = defPrimWord("HIDDEN", () -> begin lenAndFlagsAddr = mem[mem[CURRENT]+1] + 1 - mem[lenAndFlagsAddr] = mem[lenAndFlagsAddr] $ F_HIDDEN + mem[lenAndFlagsAddr] = xor(mem[lenAndFlagsAddr], F_HIDDEN) return NEXT end) @@ -1041,46 +1147,22 @@ SEMICOLON_CFA = defWord(";", IMMEDIATE_CFA = defPrimWord("IMMEDIATE", () -> begin lenAndFlagsAddr = mem[mem[CURRENT]+1] + 1 - mem[lenAndFlagsAddr] = mem[lenAndFlagsAddr] $ F_IMMED + mem[lenAndFlagsAddr] = xor(mem[lenAndFlagsAddr], F_IMMED) return NEXT end, flags=F_IMMED) -CODE_CFA = defPrimWord("CODE", () -> begin - pushPS(32) - callPrim(mem[WORD_CFA]) - callPrim(mem[HEADER_CFA]) - - exprString = "() -> begin\n" - while true - if mem[TOIN] >= mem[NUMTIB] - exprString = string(exprString, "\n") - if currentSource() == STDIN - println() - end - - pushPS(TIB) - pushPS(160) - callPrim(mem[EXPECT_CFA]) - mem[NUMTIB] = mem[SPAN] - mem[TOIN] = 0 - end - - pushPS(32) - callPrim(mem[WORD_CFA]) - cAddr = popPS() - thisWord = getString(cAddr+1, mem[cAddr]) - - if uppercase(thisWord) == "END-CODE" - break - end - - exprString = string(exprString, " ", thisWord) - end - exprString = string(exprString, "\nreturn NEXT\nend") - +# ( addr n -- primAddr ) +CREATE_PRIM_CFA = defPrimWord("CREATE-PRIM", () -> begin + len = popPS() + addr = popPS() + + exprString = string("() -> begin\n", + getString(addr, len), "\n", + "return NEXT\n", + "end") func = eval(parse(exprString)) - dictWrite(defPrim(func)) + pushPS(defPrim(func)) return NEXT end) @@ -1095,7 +1177,7 @@ INTERPRET_CFA = defWord("INTERPRET", [LIT_CFA, 32, WORD_CFA, # Read next space-delimited word DUP_CFA, FETCH_CFA, ZE_CFA, ZBRANCH_CFA, 3, - DROP_CFA, EXIT_CFA, # Exit if TIB is exhausted + DROP_CFA, EXIT_CFA, # Exit if input buffer is exhausted STATE_CFA, FETCH_CFA, ZBRANCH_CFA, 24, # Compiling @@ -1133,90 +1215,130 @@ PROMPT_CFA = defPrimWord("PROMPT", () -> begin end) QUIT_CFA = defWord("QUIT", - [LIT_CFA, 0, STATE_CFA, STORE_CFA, - LIT_CFA, 0, NUMTIB_CFA, STORE_CFA, - RSP0_CFA, RSPSTORE_CFA, - QUERY_CFA, - INTERPRET_CFA, PROMPT_CFA, - BRANCH_CFA,-4]) + [LIT_CFA, 0, STATE_CFA, STORE_CFA, # Set mode to interpret + LIT_CFA, 0, SOURCE_ID_VAR_CFA, STORE_CFA, # Set terminal as input stream + LIT_CFA, 0, NUMTIB_CFA, STORE_CFA, # Clear the input buffer + RSP0_CFA, RSPSTORE_CFA, # Clear the return stack + QUERY_CFA, # Read line of input + INTERPRET_CFA, PROMPT_CFA, # Interpret line + BRANCH_CFA,-4]) # Loop + +INCLUDED_CFA = defWord("INCLUDED", + [LIT_CFA, 0, STATE_CFA, STORE_CFA, # Set mode to interpret + FAM_RO_CFA, OPEN_FILE_CFA, DROP_CFA, # Open the file + SOURCE_ID_VAR_CFA, FETCH_CFA, SWAP_CFA, # Store current source on stack + SOURCE_ID_VAR_CFA, STORE_CFA, # Mark this as the current source + SOURCE_ID_VAR_CFA, FETCH_CFA, QUERY_FILE_CFA, # Read line from file + EOF_FLAG_CFA, FETCH_CFA, + NUMFIB_CFA, FETCH_CFA, ZE_CFA, AND_CFA, # Test for EOF and empty line + INVERT_CFA, ZBRANCH_CFA, 4, # Break out if EOF + INTERPRET_CFA, # Interpret line + BRANCH_CFA, -14, # Loop + SOURCE_ID_VAR_CFA, FETCH_CFA, + CLOSE_FILE_CFA, DROP_CFA, # Close file + SOURCE_ID_VAR_CFA, STORE_CFA, # Restore input source + LIT_CFA, 0, NUMIB_CFA, STORE_CFA, # Zero #IB + LIT_CFA, 0, TOIN_CFA, STORE_CFA, # Zero >IN + EXIT_CFA]) + +INCLUDE_CFA = defWord("INCLUDE", [LIT_CFA, 32, WORD_CFA, + DUP_CFA, INCR_CFA, + SWAP_CFA, FETCH_CFA, + INCLUDED_CFA, EXIT_CFA]); ABORT_CFA = defWord("ABORT", - [CLOSEFILES_CFA, PSP0_CFA, PSPSTORE_CFA, QUIT_CFA]) + [CLOSE_FILES_CFA, DROP_CFA, PSP0_CFA, PSPSTORE_CFA, QUIT_CFA]) BYE_CFA = defPrimWord("BYE", () -> begin - println("\nBye!") + if mem[SOURCE_ID_VAR] == 0 + println("\nBye!") + end return 0 end) -# File I/O +EOF_CFA = defPrimWord("\x04", () -> begin + return 0 +end) -INCLUDE_CFA = defPrimWord("INCLUDE", () -> begin - pushPS(32) - callPrim(mem[WORD_CFA]) - wordAddr = popPS()+1 - wordLen = mem[wordAddr-1] - word = getString(wordAddr, wordLen) +### Library loading ### - fname = word - if !isfile(fname) - fname = Pkg.dir("forth","src",word) - if !isfile(fname) - error("No file named $word found in current directory or package source directory.") - end +oldCWD = "" +SETLIBCWD_CFA = defPrimWord("SETLIBCWD", () -> begin + global oldCWD = pwd() + if !isfile("lib.4th") # Exception for debugging. + cd(Pkg.dir("forth","src")) end - push!(sources, open(fname, "r")) - - # Clear input buffer - mem[NUMTIB] = 0 + return NEXT +end) +RESTORECWD_CFA = defPrimWord("RESTORECWD", () -> begin + cd(oldCWD) return NEXT end) +INCLUDED_LIB_CFA = defWord("INCLUDED-LIB", + [SETLIBCWD_CFA, INCLUDED_CFA, RESTORECWD_CFA, EXIT_CFA]) + +INCLUDE_LIB_CFA = defWord("INCLUDE-LIB", [LIT_CFA, 32, WORD_CFA, + DUP_CFA, INCR_CFA, + SWAP_CFA, FETCH_CFA, + INCLUDED_LIB_CFA, EXIT_CFA]); + +SKIP_WELCOME, SKIP_WELCOME_CFA = defNewVar("SKIP-WELCOME", 0) #### VM loop #### initialized = false -initFileName = nothing -if isfile("lib.4th") - initFileName = "lib.4th" -elseif isfile(Pkg.dir("forth","src", "lib.4th")) - initFileName = Pkg.dir("forth","src","lib.4th") -end +libFileName = "lib.4th" -function run(;initialize=true) - # Begin with STDIN as source - push!(sources, STDIN) +function run(fileName=nothing; initialize=true) - global initialized, initFileName + # Start with IP pointing to first instruction of outer interpreter + pushRS(QUIT_CFA+1) + + # Include optional file + if fileName != nothing + putString(fileName, mem[H]) + pushPS(mem[H]) + mem[H] += length(fileName) + pushPS(length(fileName)) + pushRS(INCLUDED_CFA+1) + + mem[SKIP_WELCOME] = -1 + end + + # Load library files + global initialized, libFileName if !initialized && initialize - if initFileName != nothing - print("Including definitions from $initFileName...") - push!(sources, open(initFileName, "r")) + if libFileName != nothing + #print("Including definitions from $libFileName...") + + putString(libFileName, mem[H]) + pushPS(mem[H]) + pushPS(length(libFileName)) + pushRS(INCLUDED_LIB_CFA+1) + initialized = true else println("No library file found. Only primitive words available.") end end - # Start with IP pointing to first instruction of outer interpreter - reg.IP = QUIT_CFA + 1 # Primitive processing loop. # Everyting else is simply a consequence of this loop! - jmp = NEXT + jmp = mem[EXIT_CFA] while jmp != 0 try - #println("Entering prim $(getPrimName(jmp))") + #print("Entering prim $(getPrimName(jmp)), PS: ") + #printPS() + jmp = callPrim(jmp) catch ex showerror(STDOUT, ex) println() - while !isempty(sources) && currentSource() != STDIN - close(pop!(sources)) - end - # QUIT reg.IP = ABORT_CFA + 1 jmp = NEXT @@ -1265,7 +1387,7 @@ function dump(startAddr::Int64; count::Int64 = 100, cellsPerLine::Int64 = 10) i += 1 end - println("\t", AbstractString(chars)) + println("\t", String(chars)) end end