Add -i flag to run .jimage files as scripts.

This commit is contained in:
Calvin Rose 2021-09-19 19:45:21 -05:00
parent 6c58347916
commit 033c6f1fdb
3 changed files with 74 additions and 50 deletions

View File

@ -2,6 +2,7 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## Unreleased - ??? ## Unreleased - ???
- Add `-i` flag to janet binary to make it easier to run image files from the command line
- Remove `thread/` module. - Remove `thread/` module.
- Add `(number ...)` pattern to peg for more efficient number parsing using Janet's - Add `(number ...)` pattern to peg for more efficient number parsing using Janet's
scan-number function without immediate string creation. scan-number function without immediate string creation.

View File

@ -3,7 +3,7 @@
janet \- run the Janet language abstract machine janet \- run the Janet language abstract machine
.SH SYNOPSIS .SH SYNOPSIS
.B janet .B janet
[\fB\-hvsrpnqk\fR] [\fB\-hvsrpnqik\fR]
[\fB\-e\fR \fISOURCE\fR] [\fB\-e\fR \fISOURCE\fR]
[\fB\-E\fR \fISOURCE ...ARGUMENTS\fR] [\fB\-E\fR \fISOURCE ...ARGUMENTS\fR]
[\fB\-l\fR \fIMODULE\fR] [\fB\-l\fR \fIMODULE\fR]
@ -213,6 +213,11 @@ Precompiles Janet source code into an image, a binary dump that can be efficient
Source should be a path to the Janet module to compile, and output should be the file path of Source should be a path to the Janet module to compile, and output should be the file path of
resulting image. Output should usually end with the .jimage extension. resulting image. Output should usually end with the .jimage extension.
.TP
.BR \-i
When this flag is passed, a script passed to the interpreter will be treated as a janet image file
rather than a janet source file.
.TP .TP
.BR \-l\ lib .BR \-l\ lib
Import a Janet module before running a script or repl. Multiple files can be loaded Import a Janet module before running a script or repl. Multiple files can be loaded

View File

@ -3508,6 +3508,12 @@
# conditional compilation for reduced os # conditional compilation for reduced os
(def- getenv-alias (if-let [entry (in root-env 'os/getenv)] (entry :value) (fn [&]))) (def- getenv-alias (if-let [entry (in root-env 'os/getenv)] (entry :value) (fn [&])))
(defn- run-main
[env subargs arg]
(if-let [main (get (in env 'main) :value)]
(let [thunk (compile [main ;subargs] env arg)]
(if (function? thunk) (thunk) (error (thunk :error))))))
(defn cli-main (defn cli-main
`Entrance for the Janet CLI tool. Call this function with the command line `Entrance for the Janet CLI tool. Call this function with the command line
arguments as an array or tuple of strings to invoke the CLI interface.` arguments as an array or tuple of strings to invoke the CLI interface.`
@ -3515,17 +3521,18 @@
(setdyn :args args) (setdyn :args args)
(var *should-repl* false) (var should-repl false)
(var *no-file* true) (var no-file true)
(var *quiet* false) (var quiet false)
(var *raw-stdin* false) (var raw-stdin false)
(var *handleopts* true) (var handleopts true)
(var *exit-on-error* true) (var exit-on-error true)
(var *colorize* true) (var colorize true)
(var *debug* false) (var debug-flag false)
(var *compile-only* false) (var compile-only false)
(var *warn-level* nil) (var warn-level nil)
(var *error-level* nil) (var error-level nil)
(var expect-image false)
(if-let [jp (getenv-alias "JANET_PATH")] (setdyn :syspath jp)) (if-let [jp (getenv-alias "JANET_PATH")] (setdyn :syspath jp))
(if-let [jprofile (getenv-alias "JANET_PROFILE")] (setdyn :profilepath jprofile)) (if-let [jprofile (getenv-alias "JANET_PROFILE")] (setdyn :profilepath jprofile))
@ -3555,8 +3562,9 @@
-k : Compile scripts but do not execute (flycheck) -k : Compile scripts but do not execute (flycheck)
-m syspath : Set system path for loading global modules -m syspath : Set system path for loading global modules
-c source output : Compile janet source code into an image -c source output : Compile janet source code into an image
-i : Load the script argument as an image file instead of source code
-n : Disable ANSI color output in the REPL -n : Disable ANSI color output in the REPL
-l lib : Import a module before processing more arguments -l lib : Use a module before processing more arguments
-w level : Set the lint warning level - default is "normal" -w level : Set the lint warning level - default is "normal"
-x level : Set the lint error level - default is "none" -x level : Set the lint error level - default is "none"
-- : Stop handling options -- : Stop handling options
@ -3564,29 +3572,31 @@
(os/exit 0) (os/exit 0)
1) 1)
"v" (fn [&] (print janet/version "-" janet/build) (os/exit 0) 1) "v" (fn [&] (print janet/version "-" janet/build) (os/exit 0) 1)
"s" (fn [&] (set *raw-stdin* true) (set *should-repl* true) 1) "s" (fn [&] (set raw-stdin true) (set should-repl true) 1)
"r" (fn [&] (set *should-repl* true) 1) "r" (fn [&] (set should-repl true) 0)
"p" (fn [&] (set *exit-on-error* false) 1) "p" (fn [&] (set exit-on-error false) 1)
"q" (fn [&] (set *quiet* true) 1) "q" (fn [&] (set quiet true) 1)
"k" (fn [&] (set *compile-only* true) (set *exit-on-error* false) 1) "i" (fn [&] (set expect-image true) 1)
"n" (fn [&] (set *colorize* false) 1) "k" (fn [&] (set compile-only true) (set exit-on-error false) 1)
"n" (fn [&] (set colorize false) 1)
"m" (fn [i &] (setdyn :syspath (in args (+ i 1))) 2) "m" (fn [i &] (setdyn :syspath (in args (+ i 1))) 2)
"c" (fn c-switch [i &] "c" (fn c-switch [i &]
(def e (dofile (in args (+ i 1)))) (def path (in args (+ i 1)))
(def e (dofile path))
(spit (in args (+ i 2)) (make-image e)) (spit (in args (+ i 2)) (make-image e))
(set *no-file* false) (set no-file false)
3) 3)
"-" (fn [&] (set *handleopts* false) 1) "-" (fn [&] (set handleopts false) 1)
"l" (fn l-switch [i &] "l" (fn l-switch [i &]
(import* (in args (+ i 1)) (import* (in args (+ i 1))
:prefix "" :exit *exit-on-error*) :prefix "" :exit exit-on-error)
2) 2)
"e" (fn e-switch [i &] "e" (fn e-switch [i &]
(set *no-file* false) (set no-file false)
(eval-string (in args (+ i 1))) (eval-string (in args (+ i 1)))
2) 2)
"E" (fn E-switch [i &] "E" (fn E-switch [i &]
(set *no-file* false) (set no-file false)
(def subargs (array/slice args (+ i 2))) (def subargs (array/slice args (+ i 2)))
(def src ~|,(parse (in args (+ i 1)))) (def src ~|,(parse (in args (+ i 1))))
(def thunk (compile src)) (def thunk (compile src))
@ -3594,9 +3604,9 @@
((thunk) ;subargs) ((thunk) ;subargs)
(error (get thunk :error))) (error (get thunk :error)))
math/inf) math/inf)
"d" (fn [&] (set *debug* true) 1) "d" (fn [&] (set debug-flag true) 1)
"w" (fn [i &] (set *warn-level* (get-lint-level i)) 2) "w" (fn [i &] (set warn-level (get-lint-level i)) 2)
"x" (fn [i &] (set *error-level* (get-lint-level i)) 2) "x" (fn [i &] (set error-level (get-lint-level i)) 2)
"R" (fn [&] (setdyn :profilepath nil) 1)}) "R" (fn [&] (setdyn :profilepath nil) 1)})
(defn- dohandler [n i &] (defn- dohandler [n i &]
@ -3608,29 +3618,37 @@
(def lenargs (length args)) (def lenargs (length args))
(while (< i lenargs) (while (< i lenargs)
(def arg (in args i)) (def arg (in args i))
(if (and *handleopts* (= "-" (string/slice arg 0 1))) (if (and handleopts (= "-" (string/slice arg 0 1)))
(+= i (dohandler (string/slice arg 1) i)) (+= i (dohandler (string/slice arg 1) i))
(do (do
(set *no-file* false)
(def env (make-env))
(def subargs (array/slice args i)) (def subargs (array/slice args i))
(put env :args subargs) (set no-file false)
(put env :lint-error *error-level*) (if expect-image
(put env :lint-warn *warn-level*)
(if *compile-only*
(flycheck arg :exit *exit-on-error* :env env)
(do (do
(dofile arg :exit *exit-on-error* :env env) (def env (load-image (slurp arg)))
(if-let [main (get (in env 'main) :value)] (put env :args subargs)
(let [thunk (compile [main ;(tuple/slice args i)] env arg)] (put env :lint-error error-level)
(if (function? thunk) (thunk) (error (thunk :error))))))) (put env :lint-warn warn-level)
(if debug-flag (put env :debug true))
(run-main env subargs arg))
(do
(def env (make-env))
(put env :args subargs)
(put env :lint-error error-level)
(put env :lint-warn warn-level)
(if debug-flag (put env :debug true))
(if compile-only
(flycheck arg :exit exit-on-error :env env)
(do
(dofile arg :exit exit-on-error :env env)
(run-main env subargs arg)))))
(set i lenargs)))) (set i lenargs))))
(if (or *should-repl* *no-file*) (if (or should-repl no-file)
(if (if
*compile-only* (flycheck stdin :source "stdin" :exit *exit-on-error*) compile-only (flycheck stdin :source "stdin" :exit exit-on-error)
(do (do
(if-not *quiet* (if-not quiet
(print "Janet " janet/version "-" janet/build " " (os/which) "/" (os/arch) " - '(doc)' for help")) (print "Janet " janet/version "-" janet/build " " (os/which) "/" (os/arch) " - '(doc)' for help"))
(flush) (flush)
(defn getprompt [p] (defn getprompt [p]
@ -3644,15 +3662,15 @@
(when-let [profile.janet (dyn :profilepath)] (when-let [profile.janet (dyn :profilepath)]
(def new-env (dofile profile.janet :exit true)) (def new-env (dofile profile.janet :exit true))
(merge-module env new-env "" false)) (merge-module env new-env "" false))
(if *debug* (put env :debug true)) (if debug-flag (put env :debug true))
(def getter (if *raw-stdin* getstdin getline)) (def getter (if raw-stdin getstdin getline))
(defn getchunk [buf p] (defn getchunk [buf p]
(getter (getprompt p) buf env)) (getter (getprompt p) buf env))
(setdyn :pretty-format (if *colorize* "%.20Q" "%.20q")) (setdyn :pretty-format (if colorize "%.20Q" "%.20q"))
(setdyn :err-color (if *colorize* true)) (setdyn :err-color (if colorize true))
(setdyn :doc-color (if *colorize* true)) (setdyn :doc-color (if colorize true))
(setdyn :lint-error *error-level*) (setdyn :lint-error error-level)
(setdyn :lint-warn *error-level*) (setdyn :lint-warn error-level)
(repl getchunk nil env))))) (repl getchunk nil env)))))
### ###