More minor repl improvements
This commit is contained in:
parent
bad0591408
commit
18bcf6d8c6
|
@ -15,6 +15,10 @@
|
||||||
|
|
||||||
(def prompt "=> ")
|
(def prompt "=> ")
|
||||||
|
|
||||||
|
(defn- exit []
|
||||||
|
(println "\nGoodbye!")
|
||||||
|
(System/exit 0))
|
||||||
|
|
||||||
(def base-ctx (merge prelude/prelude
|
(def base-ctx (merge prelude/prelude
|
||||||
{::repl true
|
{::repl true
|
||||||
"repl"
|
"repl"
|
||||||
|
@ -25,10 +29,19 @@
|
||||||
:flush
|
:flush
|
||||||
{:name "flush"
|
{:name "flush"
|
||||||
::data/type ::data/clj
|
::data/type ::data/clj
|
||||||
:body (fn []
|
:body (fn
|
||||||
|
([]
|
||||||
(let [session @current-session]
|
(let [session @current-session]
|
||||||
(swap! session #(assoc % :ctx (volatile! base-ctx)))
|
(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
|
:new
|
||||||
{:name "new"
|
{:name "new"
|
||||||
|
@ -48,7 +61,13 @@
|
||||||
:ok)
|
:ok)
|
||||||
(do
|
(do
|
||||||
(println "No session named" name)
|
(println "No session named" name)
|
||||||
:error)))}}}))
|
:error)))}
|
||||||
|
|
||||||
|
:quit
|
||||||
|
{:name "quit"
|
||||||
|
::data/type ::data/clj
|
||||||
|
:body (fn [] (exit))}
|
||||||
|
}}))
|
||||||
|
|
||||||
(defn- new-session [name]
|
(defn- new-session [name]
|
||||||
(let [session (atom {:name name
|
(let [session (atom {:name name
|
||||||
|
@ -57,27 +76,29 @@
|
||||||
(swap! sessions #(assoc % name session))
|
(swap! sessions #(assoc % name session))
|
||||||
session))
|
session))
|
||||||
|
|
||||||
(defn- exit []
|
|
||||||
(println "\nGoodbye!")
|
|
||||||
(System/exit 0))
|
|
||||||
|
|
||||||
(defn repl-loop []
|
(defn repl-loop []
|
||||||
(let [session-atom @current-session
|
(let [session-atom @current-session
|
||||||
session @session-atom
|
session @session-atom
|
||||||
orig-ctx (:ctx session)]
|
orig-ctx (:ctx session)]
|
||||||
(print (str (:name session) prompt))
|
(print (str (:name session) prompt))
|
||||||
(flush)
|
(flush)
|
||||||
(let [raw-input (read-line)
|
(let [raw-input (read-line)]
|
||||||
input (if raw-input raw-input (exit))
|
(cond
|
||||||
|
(= nil raw-input) (exit)
|
||||||
|
|
||||||
|
(= "" raw-input) (recur)
|
||||||
|
|
||||||
|
:else
|
||||||
|
(let [input (if raw-input raw-input (exit))
|
||||||
parsed (-> input (scanner/scan) (parser/parse))
|
parsed (-> input (scanner/scan) (parser/parse))
|
||||||
{result :result ctx :ctx} (interpreter/interpret-repl parsed (:ctx session))]
|
{result :result ctx :ctx} (interpreter/interpret-repl parsed (:ctx session))]
|
||||||
(if (= result ::interpreter/error)
|
(if (= result ::interpreter/error)
|
||||||
(repl-loop)
|
(recur)
|
||||||
(do
|
(do
|
||||||
(println (show/show result))
|
(println (show/show result))
|
||||||
(when (not (= @ctx @orig-ctx))
|
(when (not (= @ctx @orig-ctx))
|
||||||
(swap! session-atom #(assoc % :ctx ctx)))
|
(swap! session-atom #(assoc % :ctx ctx)))
|
||||||
(repl-loop))))))
|
(recur))))))))
|
||||||
|
|
||||||
(defn launch []
|
(defn launch []
|
||||||
(println "Welcome to Ludus (v. 0.1.0-alpha)")
|
(println "Welcome to Ludus (v. 0.1.0-alpha)")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user