Removed old INCLUDE def.
[forth.jl.git] / src / forth.jl
index 3d5fbd5..2bd68b9 100644 (file)
@@ -48,13 +48,26 @@ function ensurePSDepth(depth::Int64)
     end
 end
 
+function ensurePSCapacity(toAdd::Int64)
+    if reg.PSP + toAdd >= PSP0 + size_PS
+        error("Parameter stack overflow.")
+    end
+end
+
 function ensureRSDepth(depth::Int64)
     if reg.RSP - RSP0 < depth
         error("Return stack underflow.")
     end
 end
 
+function ensureRSCapacity(toAdd::Int64)
+    if reg.RSP + toAdd >= RSP0 + size_RS
+        error("Return stack overflow.")
+    end
+end
+
 function pushRS(val::Int64)
+    ensureRSCapacity(1)
     mem[reg.RSP+=1] = val
 end
 
@@ -67,6 +80,8 @@ function popRS()
 end
 
 function pushPS(val::Int64)
+    ensurePSCapacity(1)
+
     mem[reg.PSP += 1] = val
 end
 
@@ -585,25 +600,69 @@ end)
 
 # I/O
 
-sources = Array{Any,1}()
-currentSource() = sources[length(sources)]
+openFiles = Dict{Int64,IOStream}()
+nextFileID = 1
+SOURCE_ID, SOURCE_ID_CFA = defNewVar("SOURCE-ID", 0)
+
+
+## File access modes
+FAM_RO = 0
+FAM_WO = 1
+FAM_RO_CFA = defConst("R/O", FAM_RO)
+FAM_WO_CFA = defConst("W/O", FAM_WO)
 
-CLOSEFILES_CFA = defPrimWord("CLOSEFILES", () -> begin
-    while currentSource() != STDIN
-        close(pop!(sources))
+function fileOpener(create::Bool)
+    fnameLen = popPS()
+    fnameAddr = popPS()
+    fam = popPS()
+
+    fname = getString(fnameAddr, fnameLen)
+
+    if create && !isfile(fname)
+        pushPS(0)
+        pushPS(-1) # error
+        return NEXT
     end
 
+    if (fam == FAM_RO)
+        mode = "r"
+    else
+        mode = "w"
+    end
+
+    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)
     return NEXT
 end)
 
-EOF_CFA = defPrimWord("\x04", () -> begin
-    if currentSource() != STDIN
-        close(pop!(sources))
-        return NEXT
-    else
-        return 0
+CLOSE_FILES_CFA = defPrimWord("CLOSE-FILES", () -> begin
+    for fh in values(openFiles)
+        close(fh)
     end
+    empty!(openFiles)
+
+    return NEXT
 end)
+                              
 
 EMIT_CFA = defPrimWord("EMIT", () -> begin
     print(Char(popPS()))
@@ -711,15 +770,7 @@ EXPECT_CFA = defPrimWord("EXPECT", () -> begin
     maxLen = popPS()
     addr = popPS()
 
-    if currentSource() == STDIN
-        line = getLineFromSTDIN()
-    else
-        if !eof(currentSource())
-            line = chomp(readline(currentSource()))
-        else
-            line = "\x04" # eof
-        end
-    end
+    line = getLineFromSTDIN()
 
     mem[SPAN] = min(length(line), maxLen)
     putString(line, addr, maxLen)
@@ -1015,6 +1066,45 @@ IMMEDIATE_CFA = defPrimWord("IMMEDIATE", () -> begin
     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")
+
+    func = eval(parse(exprString))
+    dictWrite(defPrim(func))
+
+    return NEXT
+end)
+
 # Outer Interpreter
 
 EXECUTE_CFA = defPrimWord("EXECUTE", () -> begin
@@ -1055,12 +1145,10 @@ INTERPRET_CFA = defWord("INTERPRET",
     EXIT_CFA])
 
 PROMPT_CFA = defPrimWord("PROMPT", () -> begin
-    if currentSource() == STDIN
-        if mem[STATE] == 0
-            print(" ok")
-        end
-        println()
+    if mem[STATE] == 0
+        print(" ok")
     end
+    println()
 
     return NEXT
 end)
@@ -1074,38 +1162,13 @@ QUIT_CFA = defWord("QUIT",
     BRANCH_CFA,-4])
 
 ABORT_CFA = defWord("ABORT",
-    [CLOSEFILES_CFA, PSP0_CFA, PSPSTORE_CFA, QUIT_CFA])
+    [CLOSE_FILES_CFA, PSP0_CFA, PSPSTORE_CFA, QUIT_CFA])
 
 BYE_CFA = defPrimWord("BYE", () -> begin
     println("\nBye!")
     return 0
 end)
 
-# File I/O
-
-INCLUDE_CFA = defPrimWord("INCLUDE", () -> begin
-    pushPS(32)
-    callPrim(mem[WORD_CFA])
-    wordAddr = popPS()+1
-    wordLen = mem[wordAddr-1]
-    word = getString(wordAddr, wordLen)
-
-    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
-    end
-    push!(sources, open(fname, "r"))
-
-    # Clear input buffer
-    mem[NUMTIB] = 0
-
-    return NEXT
-end)
-
-
 #### VM loop ####
 
 initialized = false
@@ -1117,14 +1180,14 @@ elseif isfile(Pkg.dir("forth","src", "lib.4th"))
 end
 
 function run(;initialize=true)
-    # Begin with STDIN as source
-    push!(sources, STDIN)
 
     global initialized, initFileName
     if !initialized && initialize
         if initFileName != nothing
             print("Including definitions from $initFileName...")
-            push!(sources, open(initFileName, "r"))
+
+            # TODO
+
             initialized = true
         else
             println("No library file found. Only primitive words available.")
@@ -1146,10 +1209,6 @@ function run(;initialize=true)
             showerror(STDOUT, ex)
             println()
 
-            while !isempty(sources) && currentSource() != STDIN
-                close(pop!(sources))
-            end
-
             # QUIT
             reg.IP = ABORT_CFA + 1
             jmp = NEXT