Added READ-FILE.
authorTim Vaughan <tgvaughan@gmail.com>
Sat, 11 Mar 2017 20:24:45 +0000 (09:24 +1300)
committerTim Vaughan <tgvaughan@gmail.com>
Sat, 11 Mar 2017 20:24:45 +0000 (09:24 +1300)
src/forth.jl

index e391d00..01b39a2 100644 (file)
@@ -683,8 +683,6 @@ READ_LINE_CFA = defPrimWord("READ-LINE", () -> begin
     eofFlag = endswith(line, '\n') ? 0 : -1
     line = chomp(line)
 
-    #println("Reading: $line");
-
     putString(line, addr, maxSize)
 
     pushPS(length(line))
@@ -694,6 +692,25 @@ READ_LINE_CFA = defPrimWord("READ-LINE", () -> begin
     return NEXT
 end)
 
+READ_FILE_CFA = defPrimWord("READ-FILE", () -> begin
+    fid = popPS()
+    size = popPS()
+    addr = popPS()
+
+    fh = openFiles[fid]
+
+    string = join(map(x -> Char(x), read(fh, size)), "")
+
+    eofFlag = length(string) == size ? 0 : -1 ;
+
+    putString(string, addr, length(string))
+
+    pushPS(length(string))
+    pushPS(eofFlag)
+
+    return NEXT
+end)
+
 
 EMIT_CFA = defPrimWord("EMIT", () -> begin
     print(Char(popPS()))