Ammended tab handling.
[forth.jl.git] / src / forth.jl
index 37ce5d1..9df99d0 100644 (file)
@@ -13,7 +13,7 @@ mem = Array{Int64,1}(size_mem)
 primitives = Array{Function,1}()
 primNames = Array{ASCIIString,1}()
 
-# Built-in variables
+# Memory geography and built-in variables
 
 nextVarAddr = 1
 H = nextVarAddr; nextVarAddr += 1              # Next free memory address
@@ -84,6 +84,8 @@ function putString(str::ASCIIString, addr::Int64)
     mem[addr:(addr+length(str)-1)] = [Int64(c) for c in str]
 end
 
+stringAsInts(str::ASCIIString) = [Int(c) for c in collect(str)]
+
 # Primitive creation and calling functions
 
 function defPrim(f::Function; name="nameless")
@@ -583,6 +585,14 @@ end)
 sources = Array{Any,1}()
 currentSource() = sources[length(sources)]
 
+CLOSEFILES_CFA = defPrimWord("CLOSEFILES", () -> begin
+    while currentSource() != STDIN
+        close(pop!(sources))
+    end
+
+    return NEXT
+end)
+
 EOF_CFA = defPrimWord("\x04", () -> begin
     if currentSource() != STDIN
         close(pop!(sources))
@@ -638,8 +648,8 @@ function getLineFromSTDIN()
 
         elseif key == '\b'
             if !isempty(line)
+                print("\b\033[K")
                 line = line[1:length(line)-1]
-                print("\b \b")
             end
 
         elseif key == '\e'
@@ -654,6 +664,9 @@ function getLineFromSTDIN()
                 end
             end
 
+        elseif key == '\t'
+            # Currently do nothing
+
         else
             print(key)
             line = string(line, key)
@@ -1010,8 +1023,11 @@ INTERPRET_CFA = defWord("INTERPRET",
     EXIT_CFA])
 
 PROMPT_CFA = defPrimWord("PROMPT", () -> begin
-    if (mem[STATE] == 0 && currentSource() == STDIN)
-        println(" ok")
+    if currentSource() == STDIN
+        if mem[STATE] == 0
+            print(" ok")
+        end
+        println()
     end
 
     return NEXT
@@ -1026,7 +1042,7 @@ QUIT_CFA = defWord("QUIT",
     BRANCH_CFA,-4])
 
 ABORT_CFA = defWord("ABORT",
-    [PSP0_CFA, PSPSTORE_CFA, QUIT_CFA])
+    [CLOSEFILES_CFA, PSP0_CFA, PSPSTORE_CFA, QUIT_CFA])
 
 BYE_CFA = defPrimWord("BYE", () -> begin
     println("\nBye!")