From: Tim Vaughan Date: Sat, 11 Mar 2017 20:24:45 +0000 (+1300) Subject: Added READ-FILE. X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=537dae593d08b551430039129dccc2dbdd11ad2d;p=forth.jl.git Added READ-FILE. --- diff --git a/src/forth.jl b/src/forth.jl index e391d00..01b39a2 100644 --- a/src/forth.jl +++ b/src/forth.jl @@ -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()))