Interpret do expressions
This commit is contained in:
parent
0a34e22a98
commit
5a95f58ec1
|
@ -123,9 +123,8 @@
|
|||
)))
|
||||
|
||||
(defn- call-fn [fn tuple ctx]
|
||||
(let [passed (interpret-ast tuple ctx)]
|
||||
(case (::data/type fn)
|
||||
::data/clj (apply (:body fn) (next passed))
|
||||
::data/clj (apply (:body fn) (next tuple))
|
||||
|
||||
::data/fn
|
||||
(let [clauses (:clauses fn)]
|
||||
|
@ -135,7 +134,7 @@
|
|||
(let [pattern (:pattern clause)
|
||||
body (:body clause)
|
||||
new-ctx (atom {::parent ctx})
|
||||
match? (match pattern passed new-ctx)
|
||||
match? (match pattern tuple new-ctx)
|
||||
success (:success match?)
|
||||
clause-ctx (:ctx match?)]
|
||||
(if success
|
||||
|
@ -149,15 +148,15 @@
|
|||
;; TODO: clean this up
|
||||
;; TODO: error with a passed tuple longer than 1
|
||||
(if (= clojure.lang.Keyword (type fn))
|
||||
(if (= 2 (count passed))
|
||||
(let [target (second passed) kw fn]
|
||||
(if (= 2 (count tuple))
|
||||
(let [target (second tuple) kw fn]
|
||||
(if (::data/struct target)
|
||||
(if (contains? target kw)
|
||||
(kw target)
|
||||
(throw (ex-info (str "Struct error: no member at " kw) {})))
|
||||
(kw target)))
|
||||
(throw (ex-info "Called keywords take a single argument" {})))
|
||||
(throw (ex-info "I don't know how to call that" {:fn fn}))))))
|
||||
(throw (ex-info "I don't know how to call that" {:fn fn})))))
|
||||
|
||||
;; TODO: add placeholder partial application
|
||||
(defn- interpret-synthetic-term [prev-value curr ctx]
|
||||
|
@ -168,7 +167,7 @@
|
|||
(get prev-value (:value curr))
|
||||
(throw (ex-info (str "Struct error: no member " (:value curr)) {})))
|
||||
(get prev-value (:value curr)))
|
||||
(call-fn prev-value curr ctx))))
|
||||
(call-fn prev-value (interpret-ast curr ctx) ctx))))
|
||||
|
||||
(defn- interpret-synthetic [ast ctx]
|
||||
(let [terms (:terms ast)
|
||||
|
@ -197,6 +196,12 @@
|
|||
(swap! ctx update-ctx {name fn})
|
||||
fn))))))
|
||||
|
||||
(defn- interpret-do [ast ctx]
|
||||
(let [exprs (:exprs ast)
|
||||
origin (interpret-ast (first exprs) ctx)
|
||||
fns (rest exprs)]
|
||||
(reduce #(call-fn (interpret-ast %2 ctx) [::data/tuple %1] ctx) origin fns)))
|
||||
|
||||
(defn- map-values [f]
|
||||
(map (fn [kv]
|
||||
(let [[k v] kv]
|
||||
|
@ -219,6 +224,8 @@
|
|||
|
||||
::ast/fn (interpret-fn ast ctx)
|
||||
|
||||
::ast/pipeline (interpret-do ast ctx)
|
||||
|
||||
::ast/block
|
||||
(let [exprs (:exprs ast)
|
||||
inner (pop exprs)
|
||||
|
@ -267,7 +274,7 @@
|
|||
|
||||
(def source "
|
||||
|
||||
panic! (\"whoops\")
|
||||
|
||||
|
||||
")
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user