core: Stage 3 — the *in* reader family is Clojure (50-io tier)

*in*, read-line, read, with-in-str, and line-seq land as a new overlay IO
tier (core/50-io.clj). *in* is a dynamic var holding a reader — a plain map
of two closures, :read-line-fn (next line, nil at EOF) and :read-fn (next
form, advancing past exactly that form). The default *in* reads real stdin
with a shared leftover buffer, so read and read-line interleave correctly;
with-in-str rebinds *in* to a string reader over one atom-held buffer —
(read) consumes its form, a following (read-line) returns the rest of that
line, as in Clojure. read has the 0/1/3 arities (EOF throws, or returns
eof-value when eof-error? is false).

The Janet seed grows two seams next to read-string: __stdin-read-line (one
line off stdin, newline stripped) and __parse-next (one form off a string ->
[form rest], nil at end of input) — and loses the line-seq stub.

Two traps hit and documented for future tiers: a map LITERAL with :jolt/type
as a key is read as a tagged form (don't tag overlay value maps), and a
leftover seed stub holding the same name breaks direct-linked self-recursion
— the overlay line-seq's recursive call bound to the stub's root, truncating
after one line. The stub's string-splitting behavior is kept as a documented
extension.

20 new io-spec rows (read-line EOF/interleave, read arities + eval round-trip,
line-seq incl. real-stdin paths). Gate green: conformance 326x3, suite 4577,
full jpm test.
This commit is contained in:
Yogthos 2026-06-10 14:49:07 -04:00
parent 101027a0cd
commit c665b8eb9f
5 changed files with 148 additions and 4 deletions

View file

@ -61,7 +61,8 @@
{:ns "clojure.core.20-coll" :kernel false}
{:ns "clojure.core.25-sorted" :kernel false}
{:ns "clojure.core.30-macros" :kernel false}
{:ns "clojure.core.40-lazy" :kernel false}])
{:ns "clojure.core.40-lazy" :kernel false}
{:ns "clojure.core.50-io" :kernel false}])
(defn- eval-overlay-source [ctx src]
(var s src)

View file

@ -2566,8 +2566,8 @@
(defn core-enumeration-seq [x] (core-seq x))
(defn core-iterator-seq [x] (core-seq x))
# xml-seq now lives in the Clojure collection tier (core/20-coll.clj).
(defn core-line-seq [rdr]
(if (string? rdr) (core-seq (string/split "\n" rdr)) nil))
# line-seq now lives in the Clojure IO tier (core/50-io.clj), over the reader
# protocol of the *in* family.
(defn core-re-matcher [re s] @{:jolt/type :jolt/matcher :re re :s s :pos 0})
# JVM reflection / proxies — not applicable on a Janet host; resolve-only.
@ -3106,7 +3106,6 @@
"test" core-test
"enumeration-seq" core-enumeration-seq
"iterator-seq" core-iterator-seq
"line-seq" core-line-seq
"re-matcher" core-re-matcher
"bean" core-bean
"print-method" core-print-method

View file

@ -1122,6 +1122,20 @@
# Reader / expansion as plain fns: read-string parses one form; macroexpand-1
# expands a (quoted, already-evaluated) call form once via its macro var.
(ns-intern core "read-string" (fn [s] (parse-string s)))
# The *in* reader family's host seams. __stdin-read-line: one line from real
# stdin, newline stripped, nil at EOF. __parse-next: one form off a string ->
# [form rest-of-string], nil when only whitespace remains. *in*, read-line,
# read, with-in-str, and line-seq are Clojure over these (core/50-io.clj).
(ns-intern core "__stdin-read-line"
(fn []
(let [l (file/read stdin :line)]
(if (nil? l) nil
(let [s (string l)]
(if (string/has-suffix? "\n" s) (string/slice s 0 -2) s))))))
(ns-intern core "__parse-next"
(fn [s]
(if (= 0 (length (string/trim s))) nil
(let [r (parse-next s)] (tuple (r 0) (r 1))))))
(def expand-1 (fn [the-form]
(if (and (array? the-form) (> (length the-form) 0)
(struct? (first the-form)) (= :symbol ((first the-form) :jolt/type)))