Pinning down insidious bug in interpreter.
[forth.jl.git] / src / forth.jl
index 90cf1cf..766bd4e 100644 (file)
@@ -121,7 +121,13 @@ function defPrim(f::Function; name="nameless")
     return -length(primitives)
 end
 
-callPrim(addr::Int64) = primitives[-addr]()
+function callPrim(addr::Int64)
+    if addr >=0 || -addr>length(primitives)
+        error("Attempted to execute non-existent primitive at address $addr.")
+    else
+        primitives[-addr]()
+    end
+end
 getPrimName(addr::Int64) = primNames[-addr]
 
 # Word creation functions
@@ -721,8 +727,9 @@ end)
 # Outer interpreter
 
 TRACE = defPrimWord("TRACE", () -> begin
-    print("RS: "); printRS()
+    println("reg.W: $(reg.W) reg.IP: $(reg.IP)")
     print("PS: "); printPS()
+    print("RS: "); printRS()
     print("[paused]")
     readline()
 
@@ -812,6 +819,11 @@ PARSE = defPrimWord("PARSE", () -> begin
     return NEXT
 end)
 
+BYE = defPrimWord("BYE", () -> begin
+    println("Bye!")
+    return 0
+end)
+
 STATE, STATE_CFA = defNewVar("STATE", 0)
 
 INTERPRET = defWord("INTERPRET",
@@ -820,29 +832,30 @@ INTERPRET = defWord("INTERPRET",
     DUP, FETCH, ZE, ZBRANCH, 3,
         DROP, EXIT, # Exit if TIB is exhausted
 
-    STATE_CFA, FETCH, ZBRANCH, 28,
+    STATE_CFA, FETCH, ZBRANCH, 31,
         # Compiling
-        DUP, FIND, ZBRANCH, 17,
+        DUP, FIND, QDUP, ZBRANCH, 19,
 
             # Found word. 
+            SWAP, DROP,
             DUP, TOCFA, SWAP, INCR, FETCH, LIT, F_IMMED, AND, ZBRANCH, 4,
                 # Immediate: Execute!
-                EXECUTE, BRANCH, -30,
+                EXECUTE, BRANCH, -33,
 
                 # Not immediate: Compile!
-                COMMA, BRANCH, -33,
+                COMMA, BRANCH, -36,
 
             # No word found, parse number
-            BTICK, LIT, COMMA, NUMBER, COMMA, BRANCH, -40,
+            NUMBER, BTICK, LIT, COMMA, COMMA, BRANCH, -43,
         
        # Interpreting
         DUP, FIND, QDUP, ZBRANCH, 7,
 
             # Found word. Execute!
-            SWAP, DROP, TOCFA, EXECUTE, BRANCH, -51,
+            SWAP, DROP, TOCFA, EXECUTE, BRANCH, -54,
 
             # No word found, parse number and leave on stack
-            NUMBER, BRANCH, -54,
+            NUMBER, BRANCH, -57,
     EXIT]
 )
 
@@ -860,11 +873,6 @@ QUIT = defWord("QUIT",
     INTERPRET, PROMPT,
     BRANCH,-4])
 
-BYE = defPrimWord("BYE", () -> begin
-    println("Bye!")
-    return 0
-end)
-
 INCLUDE = defPrimWord("INCLUDE", () -> begin
     pushPS(32)
     callPrim(mem[WORD])
@@ -934,9 +942,6 @@ IMMEDIATE = defPrimWord("IMMEDIATE", () -> begin
     return NEXT
 end, flags=F_IMMED)
 
-TICK = defWord("'",
-    [LIT, 32, WORD, FIND, TOCFA, EXIT])
-
 
 #### VM loop ####
 
@@ -948,7 +953,7 @@ elseif isfile(Pkg.dir("forth/src/lib.4th"))
     initFileName = Pkg.dir("forth/src/lib.4th")
 end
 
-function run(;initialize=false)
+function run(;initialize=true)
     # Begin with STDIN as source
     push!(sources, STDIN)
 
@@ -982,6 +987,11 @@ function run(;initialize=false)
                 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]