Add actors to repl

This commit is contained in:
Scott Richmond 2022-06-01 10:44:34 -04:00
parent 1e28556baa
commit 46581e103d
2 changed files with 40 additions and 29 deletions

View File

@ -70,12 +70,11 @@
(future
(reset! vm-state :running)
(loop []
(if (= @vm-state :running)
(do
(when (= @vm-state :running)
(run! run-process (values @processes))
(recur))
;; (println "Ludus VM shutting down")
)))))
(recur)
;; (println "Ludus VM shutting down")
)))))
(defn- stop-vm []
(reset! vm-state :stopped)
@ -649,24 +648,31 @@
(pp/pprint (ex-data e)))))
(defn interpret-repl
([parsed]
(let [base-ctx (volatile! (merge {} prelude/prelude))]
(try
(let [result (interpret-ast (::parser/ast parsed) base-ctx)]
{:result result :ctx base-ctx})
(catch clojure.lang.ExceptionInfo e
(println "Ludus panicked!")
(println (ex-message e))
{:result ::error :ctx base-ctx}))))
([parsed ctx]
(let [orig-ctx @ctx]
(try
(let [result (interpret-ast (::parser/ast parsed) ctx)]
{:result result :ctx ctx})
(catch clojure.lang.ExceptionInfo e
(println "Ludus panicked!")
(println (ex-message e))
{:result ::error :ctx (volatile! orig-ctx)})))))
(let [orig-ctx @ctx
process (new-process)
pid (:pid @process)]
(try
(start-vm)
(with-bindings {#'self pid}
(let [result (interpret-ast (::parser/ast parsed) ctx)]
{:result result :ctx ctx :pid pid}))
(catch clojure.lang.ExceptionInfo e
(println "Ludus panicked!")
(println (ex-message e))
{:result :error :ctx (volatile! orig-ctx) :pid pid}))))
([parsed ctx pid]
(let [orig-ctx @ctx]
(try
(start-vm)
(with-bindings {#'self pid}
(let [result (interpret-ast (::parser/ast parsed) ctx)]
{:result result :ctx ctx :pid pid}))
(catch clojure.lang.ExceptionInfo e
(println "Ludus panicked!")
(println (ex-message e))
{:result :error :ctx (volatile! orig-ctx) :pid pid}
)))))
(comment

View File

@ -79,25 +79,30 @@
(defn repl-loop []
(let [session-atom @current-session
session @session-atom
orig-ctx (:ctx session)]
orig-ctx (:ctx session)
pid (:pid session)]
(print (str (:name session) prompt))
(flush)
(let [raw-input (read-line)]
(let [input (read-line)]
(cond
(= nil raw-input) (exit)
(= nil input) (exit)
(= "" raw-input) (recur)
(= "" input) (recur)
:else
(let [input (if raw-input raw-input (exit))
parsed (-> input (scanner/scan) (parser/parse))
{result :result ctx :ctx} (interpreter/interpret-repl parsed (:ctx session))]
(let [parsed (-> input (scanner/scan) (parser/parse))
{result :result ctx :ctx pid- :pid}
(if pid
(interpreter/interpret-repl parsed orig-ctx pid)
(interpreter/interpret-repl parsed orig-ctx))]
(if (= result ::interpreter/error)
(recur)
(do
(println (show/show result))
(when (not (= @ctx @orig-ctx))
(swap! session-atom #(assoc % :ctx ctx)))
(when (not (= pid pid-))
(swap! session-atom #(assoc % :pid pid-)))
(recur))))))))
(defn launch []