Skip to content

Commit 58ac7ac

Browse files
committed
Changed eager read-all to lazy read-seq #999
1 parent 5c8304b commit 58ac7ac

File tree

4 files changed

+44
-37
lines changed

4 files changed

+44
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
* Added support for custom data readers (#924)
1111
* Added `*default-data-reader-fn*` (#924)
1212
* Added `basilisp.pprint/print-table` function (#983)
13-
* Added `basilisp.core/read-all` function (#986)
13+
* Added `basilisp.core/read-seq` function (#986) (#999)
1414
* Added various compiler arguments to CLI commands (#989)
1515

1616
### Changed

src/basilisp/contrib/nrepl_server.lpy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@
134134
*e *e]
135135
(try
136136
(let [result (last
137-
(for [form (read-all (io/StringIO code))]
137+
(for [form (read-seq (io/StringIO code))]
138138
(basilisp.lang.compiler/compile-and-exec-form form
139139
ctx
140140
*ns*)))]

src/basilisp/core.lpy

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4437,16 +4437,31 @@
44374437
*default-data-reader-fn*
44384438
nil)
44394439

4440-
(defn- read-iterator
4441-
[opts x]
4442-
(let [read (:read opts basilisp.lang.reader/read)]
4443-
(read x
4444-
*resolver*
4445-
*data-readers*
4446-
(:eof opts)
4447-
(= (:eof opts) :eofthrow)
4448-
(:features opts)
4449-
(not= :preserve (:read-cond opts)))))
4440+
(defn read-seq
4441+
"Create an lazy sequence that will contain each form from the
4442+
``stream``\\. If no stream is specified, uses the value currently
4443+
bound to :lpy:var:`*in*`. It is the callers responsibility to ensure
4444+
that the stream resource is not closed before the sequence has been
4445+
realised.
4446+
4447+
Callers may bind a map of readers to :lpy:var:`*data-readers*` to customize
4448+
the data readers used reading this string
4449+
4450+
The stream must satisfy the interface of :external:py:class:`io.TextIOBase`\\, but
4451+
does not require any pushback capabilities. The default
4452+
``basilisp.lang.reader.StreamReader`` can wrap any object implementing ``TextIOBase``
4453+
and provide pushback capabilities."
4454+
([stream]
4455+
(read-seq {} stream))
4456+
([opts stream]
4457+
(let [read (:read opts basilisp.lang.reader/read)]
4458+
(lazy-seq (read stream
4459+
*resolver*
4460+
*data-readers*
4461+
(:eof opts)
4462+
(= (:eof opts) :eofthrow)
4463+
(:features opts)
4464+
(not= :preserve (:read-cond opts)))))))
44504465

44514466
(defn read-string
44524467
"Read a string of Basilisp code.
@@ -4460,7 +4475,7 @@
44604475
([s]
44614476
(read-string {:eof :eofthrow} s))
44624477
([opts s]
4463-
(first (read-iterator (assoc opts :read basilisp.lang.reader/read-str) s))))
4478+
(first (read-seq (assoc opts :read basilisp.lang.reader/read-str) s))))
44644479

44654480
(defn read
44664481
"Read the next form from the ``stream``\\. If no stream is specified, uses the value
@@ -4479,30 +4494,9 @@
44794494
([stream]
44804495
(read stream true nil))
44814496
([opts stream]
4482-
(first (read-iterator opts stream)))
4497+
(first (read-seq opts stream)))
44834498
([stream eof-error? eof-value]
4484-
(first (read-iterator {:eof (if eof-error? :eofthrow eof-value)} stream))))
4485-
4486-
(defn read-all
4487-
"Eagerly read all forms from the ``stream``\\. If no stream is specified, uses the
4488-
value currently bound to :lpy:var:`*in*`.
4489-
4490-
Callers may bind a map of readers to :lpy:var:`*data-readers*` or a default data
4491-
reader function to :lpy:var::`*default-data-reader-fn*` to customize the data
4492-
readers used reading this string
4493-
4494-
The stream must satisfy the interface of :external:py:class:`io.TextIOBase`\\, but
4495-
does not require any pushback capabilities. The default
4496-
``basilisp.lang.reader.StreamReader`` can wrap any object implementing ``TextIOBase``
4497-
and provide pushback capabilities."
4498-
([stream]
4499-
(read-all nil stream))
4500-
([opts stream]
4501-
(let [eof (python/object)]
4502-
(->> (read-iterator (assoc opts :eof eof) stream)
4503-
seq
4504-
(take-while #(not (identical? % eof)))
4505-
doall))))
4499+
(first (read-seq {:eof (if eof-error? :eofthrow eof-value)} stream))))
45064500

45074501
(defn eval
45084502
"Evaluate a form (not a string) and return its result.
@@ -4535,7 +4529,7 @@
45354529
(python/str))
45364530
ctx (basilisp.lang.compiler.CompilerContext. (or src "<Load Input>"))]
45374531
(last
4538-
(for [form (seq (read-all reader))]
4532+
(for [form (read-seq {} reader)]
45394533
(basilisp.lang.compiler/compile-and-exec-form form
45404534
ctx
45414535
*ns*)))))

tests/basilisp/test_core_fns.lpy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,19 @@
23352335
(let [output (load "corpus/core-load-1" "corpus/core-load-2")]
23362336
(is (= :core-load-2 output))))
23372337

2338+
(testing "load ns form before reading more"
2339+
(let [ns (-> *ns* name symbol)
2340+
file (str load-dir-test "/core_load_before_read.lpy")
2341+
sub-ns 'tests.basilisp.corpus.core-load-before-read]
2342+
(spit file
2343+
(str "(ns " sub-ns
2344+
") (defmacro a [] 1) (defmacro b [] `(a)) (b)"))
2345+
(try
2346+
(= 1 (load "corpus/core-load-before-read"))
2347+
(finally
2348+
(remove-ns sub-ns)
2349+
(in-ns ns)))))
2350+
23382351
(finally
23392352
(when (bio/exists? load-dir-test)
23402353
(shutil/rmtree load-dir-test))))))

0 commit comments

Comments
 (0)