1
0
mirror of https://github.com/janet-lang/janet synced 2025-04-30 06:33:20 +00:00

Fix repl default chunks.

This commit is contained in:
Calvin Rose 2018-11-30 14:17:10 -05:00
parent 464edf729b
commit 25c50f5026
2 changed files with 48 additions and 34 deletions

View File

@ -1115,14 +1115,12 @@ value, one key will be ignored."
and is encapsulates the parsing, compilation, and evaluation of janet. and is encapsulates the parsing, compilation, and evaluation of janet.
env is the environment to evaluate the code in, chunks is a function env is the environment to evaluate the code in, chunks is a function
that returns strings or buffers of source code (from a repl, file, that returns strings or buffers of source code (from a repl, file,
network connection, etc. onvalue and onerr are callbacks that are network connection, etc. onstatus is a callback that is
invoked when a result is returned and when an error is produced, invoked when a result is returned or any other signal is raised.
respectively.
This function can be used to implement a repl very easily, simply This function can be used to implement a repl very easily, simply
pass a function that reads line from stdin to chunks, and print to pass a function that reads line from stdin to chunks, status-pp to onstatus"
onvalue." [env chunks onstatus where &]
[env chunks onvalue onerr where &]
# Are we done yet? # Are we done yet?
(var going true) (var going true)
@ -1142,21 +1140,17 @@ value, one key will be ignored."
(do (do
(:= good false) (:= good false)
(def {:error err :line errl :column errc :fiber errf} res) (def {:error err :line errl :column errc :fiber errf} res)
(onerr (onstatus
where :compile
"compile"
(if (< 0 errl) (if (< 0 errl)
(string err "\n in a form at line " errl ", column " errc) (string err "\n in a form at line " errl ", column " errc)
err) err)
errf)))) errf
where))))
:a)) :a))
(def res (resume f nil)) (def res (resume f nil))
(when good (when good
(def sig (fiber.status f)) (if going (onstatus (fiber.status f) res f where))))
(if going
(if (= sig :dead)
(onvalue res)
(onerr where "runtime" res f)))))
(def oldenv *env*) (def oldenv *env*)
(:= *env* env) (:= *env* env)
@ -1175,19 +1169,29 @@ value, one key will be ignored."
:full (eval1 (parser.produce p)) :full (eval1 (parser.produce p))
:error (do :error (do
(def (line col) (parser.where p)) (def (line col) (parser.where p))
(onerr where "parse" (onstatus :parse
(string (parser.error p) (string (parser.error p)
" on line " line " on line " line
", column " col)))))) ", column " col)
nil
where)))))
(:= *env* oldenv) (:= *env* oldenv)
env) env)
(defn default-error-handler (defn status-pp
[source t x f &] "Pretty print a signal and asscoaited state. Can be used as the
onsignal argument to run-context."
[sig x f source]
(def title
(case sig
:parse "parse error"
:compile "compile error"
:error "error"
(string "status " sig)))
(file.write stderr (file.write stderr
(string t " error in " source ": ") (string title " in " source ": ")
(if (bytes? x) x (string.pretty x)) (if (bytes? x) x (string.pretty x))
"\n") "\n")
(when f (when f
@ -1234,7 +1238,12 @@ value, one key will be ignored."
(buffer.push-string buf str) (buffer.push-string buf str)
(buffer.push-string buf "\n"))) (buffer.push-string buf "\n")))
(var returnval nil) (var returnval nil)
(run-context *env* chunks (fn [x] (:= returnval x)) default-error-handler "eval") (run-context *env* chunks
(fn [sig x f source]
(if (= sig :dead)
(:= returnval x)
(status-pp sig x f source)))
"eval")
returnval) returnval)
(do (do
@ -1322,10 +1331,11 @@ value, one key will be ignored."
(do (do
# Normal janet module # Normal janet module
(defn chunks [buf _] (file.read f 1024 buf)) (defn chunks [buf _] (file.read f 1024 buf))
(run-context newenv chunks identity (run-context newenv chunks
(if exit-on-error (fn [sig x f source]
(fn [a b c d &] (default-error-handler a b c d) (os.exit 1)) (when (not= sig :dead)
default-error-handler) (status-pp sig x f source)
(if exit-on-error (os.exit 1))))
path) path)
(file.close f)) (file.close f))
(do (do
@ -1367,15 +1377,19 @@ value, one key will be ignored."
(defn repl (defn repl
"Run a repl. The first parameter is an optional function to call to "Run a repl. The first parameter is an optional function to call to
get a chunk of source code. Should return nil for end of file." get a chunk of source code that should return nil for end of file.
[chunks onvalue onerr &] The second parameter is a function that is called when a signal is
caught."
[chunks onsignal &]
(def newenv (make-env)) (def newenv (make-env))
(default chunks (fn [&] (file.read stdin :line))) (default chunks (fn [buf _] (file.read stdin :line buf)))
(default onvalue (fn [x] (default onsignal (fn [sig x f source]
(case sig
:dead (do
(put newenv '_ @{:value x}) (put newenv '_ @{:value x})
(print (string.pretty x 20)))) (print (string.pretty x 20)))
(default onerr default-error-handler) (status-pp sig x f source))))
(run-context newenv chunks onvalue onerr "repl")) (run-context newenv chunks onsignal "repl"))
(defn all-symbols (defn all-symbols
"Get all symbols available in the current environment." "Get all symbols available in the current environment."

View File

@ -11,7 +11,7 @@
# Flag handlers # Flag handlers
(def handlers :private (def handlers :private
{"h" (fn [&] {"h" (fn [&]
(print "usage: " (get process.args 0) " [options] scripts...") (print "usage: " process.args@0 " [options] scripts...")
(print (print
`Options are: `Options are:
-h Show this help -h Show this help