Skip to content

Commit

Permalink
[#126] support reader conditionals
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Oct 26, 2019
1 parent e11442e commit 2216e5f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
:java-source-paths ["src-java"]
:source-paths ["src"]
:dependencies [[org.clojure/clojure "1.9.0"]
[borkdude/edamame "0.0.7"]
[borkdude/edamame "0.0.8-alpha.2"]
[org.clojure/tools.reader "1.3.2"]]
:aot [sci.impl.java]
:profiles {:clojure-1.9.0 {:dependencies [[org.clojure/clojure "1.9.0"]]}
Expand Down
4 changes: 3 additions & 1 deletion src/sci/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
- `:preset`: a pretermined set of options. Currently only
`:termination-safe` is supported, which will set `:realize-max` to
`100` and disallows the symbols `loop`, `recur` and `trampoline`."
`100` and disallows the symbols `loop`, `recur` and `trampoline`.
- `:features`: when provided a non-empty set of keywords, sci will process reader conditionals using these features (e.g. #{:bb})."
([s] (eval-string s nil))
([s opts]
(i/eval-string s opts)))
Expand Down
3 changes: 2 additions & 1 deletion src/sci/impl/interpreter.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,8 @@
([s] (eval-string s nil))
([s opts]
(let [init-ctx (opts->ctx opts)
edn-vals (p/parse-string-all s)
features (:features opts)
edn-vals (p/parse-string-all s features)
ret (eval-edn-vals init-ctx edn-vals)]
ret)))

Expand Down
9 changes: 7 additions & 2 deletions src/sci/impl/parser.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,13 @@
(defn parse-string [s]
(edamame/parse-string s opts))

(defn parse-string-all [s]
(edamame/parse-string-all s opts))
(defn parse-string-all
([s]
(edamame/parse-string-all s opts))
([s features]
(edamame/parse-string-all s (assoc opts
:read-cond :allow
:features features))))

;;;; Scratch

Expand Down
4 changes: 4 additions & 0 deletions test/sci/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@
(is (= [1 2] (eval* "(declare foo bar) (defn f [] [foo bar]) (def foo 1) (def bar 2) (f)")))
(is (= 1 (eval* "(def x 1) (declare x) x"))))

(deftest reader-conditionals
(is (= 6 (tu/eval* "(+ 1 2 #?(:bb 3 :clj 100))" {:features #{:bb}})))
(is (= 103 (tu/eval* "(+ 1 2 #?(:bb 3 :clj 100))" {:features #{:clj}}))))

;;;; Scratch

(comment
Expand Down

0 comments on commit 2216e5f

Please sign in to comment.