diff --git a/src/ludus/base.cljc b/src/ludus/base.cljc index dbe7021..7601496 100644 --- a/src/ludus/base.cljc +++ b/src/ludus/base.cljc @@ -85,18 +85,23 @@ (println (stringify-args args)) :ok)}) +(def refs (atom {})) ;; atom not volatile!, maybe we'll be multithreaded someday + (def deref- {:name "deref" ::data/type ::data/clj :body (fn [ref] (if (::data/ref ref) - (deref (::data/value ref)) + (get @refs (::data/name ref)) (throw (ex-info "Cannot deref something that is not a ref" {}))))}) (def set!- {:name "set!" ::data/type ::data/clj :body (fn [ref value] (if (::data/ref ref) - (reset! (::data/value ref) value) + (do + (swap! refs assoc (::data/name ref) value) + (reset! (::data/value ref) value) + value) (throw (ex-info "Cannot set! something that is not a ref" {}))))}) (def show {:name "show" diff --git a/src/ludus/interpreter.cljc b/src/ludus/interpreter.cljc index bac3602..ef572fd 100644 --- a/src/ludus/interpreter.cljc +++ b/src/ludus/interpreter.cljc @@ -673,6 +673,7 @@ (let [value (interpret-ast expr ctx) box (atom value) ref {::data/ref true ::data/value box ::data/name name}] + (swap! base/refs assoc name value) (vswap! ctx update-ctx {name ref}) ref))) diff --git a/src/ludus/node.cljc b/src/ludus/node.cljc index 82f06a0..f20837f 100644 --- a/src/ludus/node.cljc +++ b/src/ludus/node.cljc @@ -39,11 +39,12 @@ user_result (i/interpret-safe source user_parsed {} testing?) result_str (show/show user_result) test_results @i/test-results + state @base/refs post_scanned (s/scan pre/postlude "postlude") post_tokens (:tokens post_scanned) post_parsed (p/apply-parser g/script post_tokens) post_result (i/interpret-safe source post_parsed {} false) - ludus_result (assoc post_result :result result_str :test test_results) + ludus_result (assoc post_result :result result_str :test test_results :state state) clj_result (ld->clj ludus_result) ] (cond