Fixed library finding bug.
authorTim Vaughan <tgvaughan@gmail.com>
Tue, 11 Oct 2016 09:43:39 +0000 (22:43 +1300)
committerTim Vaughan <tgvaughan@gmail.com>
Tue, 11 Oct 2016 09:43:39 +0000 (22:43 +1300)
README.md
src/forth.jl

index 047c720..0a0b287 100644 (file)
--- a/README.md
+++ b/README.md
@@ -41,12 +41,12 @@ the library file.  Once this is complete you can start entering forth commands:
     star * ok
 
 There's an example Mandelbrot Set drawing program included in the examples
-directory.  To run it, use the `INCLUDE` word to open the file and compile its
+directory.  To run it, use the `INCLUDE-LIB` word to open the file and compile its
 definitions.  Although the exact location of the examples directory in your
-filesystem is platform dependent, `INCLUDE` includes the forth.jl src/ directory
+filesystem is platform dependent, `INCLUDE-LIB` includes the forth.jl src/ directory
 in its search path so the following should always work:
 
-    include ../examples/mandelbrot.4th
+    include-lib ../examples/mandelbrot.4th
     Enter 'mandel' to draw the Mandelbrot Set. ok
     mandel
                                                                                 *                   
index a1a4799..9beb814 100644 (file)
@@ -1253,6 +1253,27 @@ INCLUDE_CFA = defWord("INCLUDE", [LIT_CFA, 32, WORD_CFA,
     SWAP_CFA, FETCH_CFA,
     INCLUDED_CFA, EXIT_CFA]);
 
+oldCWD = ""
+SETLIBCWD_CFA = defPrimWord("SETLIBCWD", () -> begin
+    global oldCWD = pwd()
+    if !isfile("lib.4th") # Exception for debugging.
+        cd(Pkg.dir("forth","src"))
+    end
+    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]);
 
 ABORT_CFA = defWord("ABORT",
     [CLOSE_FILES_CFA, DROP_CFA, PSP0_CFA, PSPSTORE_CFA, QUIT_CFA])
@@ -1269,12 +1290,7 @@ end)
 #### 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
+initFileName = "lib.4th"
 
 function run(;initialize=true)
 
@@ -1290,7 +1306,7 @@ function run(;initialize=true)
             putString(initFileName, mem[H])
             pushPS(mem[H])
             pushPS(length(initFileName))
-            pushRS(INCLUDED_CFA+1)
+            pushRS(INCLUDED_LIB_CFA+1)
 
             initialized = true
         else