Skip to content

Commit

Permalink
lib/load-file-once: basic support for multiple imports
Browse files Browse the repository at this point in the history
  • Loading branch information
asarhaddon committed Jul 15, 2019
1 parent 70305b6 commit 13e679c
Show file tree
Hide file tree
Showing 17 changed files with 98 additions and 23 deletions.
12 changes: 9 additions & 3 deletions lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ However, here are some guidelines.
is not possible, for example for macros, give them a name starting
with an underscore.

- Support successive imports safely by giving the same definitions
again.

If a module provides tests, you may run against an implementation IMPL
with these commands.
```
make IMPL^stepA
cd tests
python ../runtest.py lib/MODULE.mal ../IMPL/run
```

Users and implementors should use the following syntax in order to
ensure that the same file is only loaded once.

```
(load-file "../lib/load-file-once.mal")
(load-file-once "../lib/foo.mal")
(load-file-once "../lib/bar.mal")
```
18 changes: 18 additions & 0 deletions lib/load-file-once.mal
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
;; Like load-file, but will never load the same path twice.

;; This file is normally loaded with `load-file`, so it needs a
;; different mechanism to neutralize multiple inclusions of
;; itself. Moreover, the file list should never be reset.

(def! load-file-once
(try*
load-file-once
(catch* _
(let* [seen (atom {"../lib/load-file-once.mal" nil})]
(fn* [filename]
(if (not (contains? @seen filename))
(do
(swap! seen assoc filename nil)
(load-file filename))))))))

nil
3 changes: 2 additions & 1 deletion lib/perf.mal
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
;; Mesure performances.

(load-file "../lib/trivial.mal") ; gensym inc
(load-file "../lib/load-file-once.mal")
(load-file-once "../lib/trivial.mal") ; gensym inc

;; Evaluate an expression, but report the time spent
(defmacro! time
Expand Down
3 changes: 2 additions & 1 deletion lib/test_cascade.mal
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
;; Iteration on evaluations interpreted as boolean values.

(load-file "../lib/trivial.mal") ; gensym
(load-file "../lib/load-file-once.mal")
(load-file-once "../lib/trivial.mal") ; gensym

;; `(cond test1 result1 test2 result2 .. testn resultn)`
;; is rewritten (in the step files) as
Expand Down
3 changes: 2 additions & 1 deletion lib/threading.mal
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
;; Composition of partially applied functions.

(load-file "../lib/reducers.mal") ; reduce
(load-file "../lib/load-file-once.mal")
(load-file-once "../lib/reducers.mal") ; reduce

;; Rewrite x (a a1 a2) .. (b b1 b2) as
;; (b (.. (a x a1 a2) ..) b1 b2)
Expand Down
1 change: 1 addition & 0 deletions tests/lib/load-file-once-inc.mal
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(swap! counter (fn* [x] (+ 1 x)))
38 changes: 38 additions & 0 deletions tests/lib/load-file-once.mal
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(def! counter (atom 0))
;=>(atom 0)

;; The counter is increased by each `load-file`.
(load-file "../tests/lib/load-file-once-inc.mal")
;=>1
(load-file "../tests/lib/load-file-once-inc.mal")
;=>2

;; load-file-once is available
(load-file "../lib/load-file-once.mal")
;=>nil

;; First import actually calls `load-file`.
(load-file-once "../tests/lib/load-file-once-inc.mal")
;=>3

;; Later imports do nothing.
(load-file-once "../tests/lib/load-file-once-inc.mal")
;=>nil
@counter
;=>3

;; Loading the module twice does not reset its memory.
(load-file "../lib/load-file-once.mal")
;=>nil
(load-file-once "../tests/lib/load-file-once-inc.mal")
;=>nil
@counter
;=>3

;; even if done with itself
(load-file-once "../lib/load-file-once.mal")
;=>nil
(load-file-once "../tests/lib/load-file-once-inc.mal")
;=>nil
@counter
;=>3
6 changes: 3 additions & 3 deletions tests/lib/memoize.mal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(load-file "../tests/computations.mal")
;=>nil
(load-file "../lib/memoize.mal")
(load-file "../lib/load-file-once.mal")
(load-file-once "../tests/computations.mal")
(load-file-once "../lib/memoize.mal")
;=>nil

(def! N 32)
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/pprint.mal
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(load-file "../lib/pprint.mal")
(load-file "../lib/load-file-once.mal")
(load-file-once "../lib/pprint.mal")
;=>nil

(pprint '(7 8 9 "ten" [11 12 [13 14]] 15 16))
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/protocols.mal
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(load-file "../lib/protocols.mal")
(load-file "../lib/load-file-once.mal")
(load-file-once "../lib/protocols.mal")
;=>nil

;; Testing find-type for normal objects.
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/reducers.mal
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(load-file "../lib/reducers.mal")
(load-file "../lib/load-file-once.mal")
(load-file-once "../lib/reducers.mal")
;=>nil

;; Testing reduce
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/test_cascade.mal
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(load-file "../lib/test_cascade.mal")
(load-file "../lib/load-file-once.mal")
(load-file-once "../lib/test_cascade.mal")
;=>nil

;; Testing or
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/threading.mal
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(load-file "../lib/threading.mal")
(load-file "../lib/load-file-once.mal")
(load-file-once "../lib/threading.mal")
;=>nil

;; Testing -> macro
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/trivial.mal
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
(load-file "../lib/trivial.mal")
(load-file "../lib/load-file-once.mal")
(load-file-once "../lib/trivial.mal")
;=>nil

(inc 12)
Expand Down
7 changes: 4 additions & 3 deletions tests/perf1.mal
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(load-file "../lib/threading.mal") ; ->
(load-file "../lib/perf.mal") ; time
(load-file "../lib/test_cascade.mal") ; or
(load-file "../lib/load-file-once.mal")
(load-file-once "../lib/threading.mal") ; ->
(load-file-once "../lib/perf.mal") ; time
(load-file-once "../lib/test_cascade.mal") ; or

;;(prn "Start: basic macros performance test")

Expand Down
5 changes: 3 additions & 2 deletions tests/perf2.mal
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(load-file "../tests/computations.mal") ; fib sumdown
(load-file "../lib/perf.mal") ; time
(load-file "../lib/load-file-once.mal")
(load-file-once "../tests/computations.mal") ; fib sumdown
(load-file-once "../lib/perf.mal") ; time

;;(prn "Start: basic math/recursion test")

Expand Down
7 changes: 4 additions & 3 deletions tests/perf3.mal
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(load-file "../lib/threading.mal") ; ->
(load-file "../lib/perf.mal") ; run-fn-for
(load-file "../lib/test_cascade.mal") ; or
(load-file "../lib/load-file-once.mal")
(load-file-once "../lib/threading.mal") ; ->
(load-file-once "../lib/perf.mal") ; run-fn-for
(load-file-once "../lib/test_cascade.mal") ; or

;;(prn "Start: basic macros/atom test")

Expand Down

0 comments on commit 13e679c

Please sign in to comment.