Better handling of calling a non-function
This commit is contained in:
parent
297a7f2111
commit
dd9b89b5e1
1 changed files with 31 additions and 25 deletions
|
|
@ -160,6 +160,10 @@
|
||||||
|
|
||||||
(define-lookup)
|
(define-lookup)
|
||||||
|
|
||||||
|
(defmacro one-of [x elements]
|
||||||
|
`(let [x# ~x]
|
||||||
|
(case x# (~@elements) x# nil)))
|
||||||
|
|
||||||
(defn resolve-symbol [expr]
|
(defn resolve-symbol [expr]
|
||||||
(let [n (name expr)]
|
(let [n (name expr)]
|
||||||
(if (str/starts-with? n "'")
|
(if (str/starts-with? n "'")
|
||||||
|
|
@ -180,31 +184,33 @@
|
||||||
(into (empty expr) (map i expr))
|
(into (empty expr) (map i expr))
|
||||||
(seq? expr)
|
(seq? expr)
|
||||||
(if-let [f (first expr)]
|
(if-let [f (first expr)]
|
||||||
(if-let [v (var-lookup f)]
|
(let [f (or (one-of f [if when and or -> ->>])
|
||||||
(apply-fn v i (rest expr))
|
(interpret f in))]
|
||||||
(case f
|
(if-let [v (var-lookup f)]
|
||||||
(if when)
|
(apply-fn v i (rest expr))
|
||||||
(let [[_if cond then else] expr]
|
(case f
|
||||||
(if (interpret cond in)
|
(if when)
|
||||||
(interpret then in)
|
(let [[_if cond then else] expr]
|
||||||
(interpret else in)))
|
(if (interpret cond in)
|
||||||
->
|
(interpret then in)
|
||||||
(interpret (expand-> (rest expr)) in)
|
(interpret else in)))
|
||||||
->>
|
->
|
||||||
(interpret (expand->> (rest expr)) in)
|
(interpret (expand-> (rest expr)) in)
|
||||||
and
|
->>
|
||||||
(eval-and in (rest expr))
|
(interpret (expand->> (rest expr)) in)
|
||||||
or
|
and
|
||||||
(eval-or in (rest expr))
|
(eval-and in (rest expr))
|
||||||
;; fallback
|
or
|
||||||
;; read fn passed as higher order fn, still needs input
|
(eval-or in (rest expr))
|
||||||
(cond (-> f meta ::fn)
|
;; fallback
|
||||||
(apply-fn (f in) i (rest expr))
|
;; read fn passed as higher order fn, still needs input
|
||||||
(symbol? f)
|
(cond (-> f meta ::fn)
|
||||||
(apply-fn (resolve-symbol f) i (rest expr))
|
(apply-fn (f in) i (rest expr))
|
||||||
(ifn? f)
|
(symbol? f)
|
||||||
(apply-fn f i (rest expr))
|
(apply-fn (resolve-symbol f) i (rest expr))
|
||||||
:else nil)))
|
(ifn? f)
|
||||||
|
(apply-fn f i (rest expr))
|
||||||
|
:else (throw (Exception. (format "Cannot call %s as a function." (pr-str f))))))))
|
||||||
expr)
|
expr)
|
||||||
;; read fn passed as higher order fn, still needs input
|
;; read fn passed as higher order fn, still needs input
|
||||||
(-> expr meta ::fn)
|
(-> expr meta ::fn)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue