More minor repl improvements

This commit is contained in:
Scott Richmond 2022-05-27 20:02:32 -04:00
parent bad0591408
commit 18bcf6d8c6

View File

@ -15,6 +15,10 @@
(def prompt "=> ")
(defn- exit []
(println "\nGoodbye!")
(System/exit 0))
(def base-ctx (merge prelude/prelude
{::repl true
"repl"
@ -25,10 +29,19 @@
:flush
{:name "flush"
::data/type ::data/clj
:body (fn []
:body (fn
([]
(let [session @current-session]
(swap! session #(assoc % :ctx (volatile! base-ctx)))
:ok))}
:ok))
([name]
(if-let [session (get @sessions name)]
(do
(swap! session #(assoc % :ctx (volatile! base-ctx)))
:ok)
(do
(println "No session named" name)
:error))))}
:new
{:name "new"
@ -48,7 +61,13 @@
:ok)
(do
(println "No session named" name)
:error)))}}}))
:error)))}
:quit
{:name "quit"
::data/type ::data/clj
:body (fn [] (exit))}
}}))
(defn- new-session [name]
(let [session (atom {:name name
@ -57,27 +76,29 @@
(swap! sessions #(assoc % name session))
session))
(defn- exit []
(println "\nGoodbye!")
(System/exit 0))
(defn repl-loop []
(let [session-atom @current-session
session @session-atom
orig-ctx (:ctx session)]
(print (str (:name session) prompt))
(flush)
(let [raw-input (read-line)
input (if raw-input raw-input (exit))
(let [raw-input (read-line)]
(cond
(= nil raw-input) (exit)
(= "" raw-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))]
(if (= result ::interpreter/error)
(repl-loop)
(recur)
(do
(println (show/show result))
(when (not (= @ctx @orig-ctx))
(swap! session-atom #(assoc % :ctx ctx)))
(repl-loop))))))
(recur))))))))
(defn launch []
(println "Welcome to Ludus (v. 0.1.0-alpha)")