Improved error reporting.
[forth.jl.git] / src / forth.jl
index 947aafb..3882a53 100644 (file)
@@ -1,6 +1,6 @@
 module forth
 
-import Base.REPLCompletions
+import Base.REPLCompletions, Base.invokelatest
 
 # VM mem size
 size_mem = 1000000 # 1 mega-int
@@ -123,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]
@@ -677,6 +677,10 @@ READ_LINE_CFA = defPrimWord("READ-LINE", () -> begin
     maxSize = popPS()
     addr = popPS()
 
+    if !(fid in keys(openFiles))
+        error(string("Invalid FID ", fid, "."))
+    end
+
     fh = openFiles[fid]
     line = readline(fh, chomp=false)
 
@@ -1336,6 +1340,7 @@ function run(fileName=nothing; initialize=true)
             jmp = callPrim(jmp)
 
         catch ex
+            println(string("Error in primitive '", getPrimName(jmp), "' at address ", jmp))
             showerror(STDOUT, ex)
             println()