1
0
mirror of https://github.com/janet-lang/janet synced 2025-02-26 12:50:02 +00:00

Add -e option to dst for executing inline scripts from the shell.

This commit is contained in:
bakpakin 2018-05-05 14:05:56 -04:00
parent 6b5c5ab0ad
commit d9e5019a71
3 changed files with 44 additions and 25 deletions

View File

@ -14,11 +14,11 @@
# Test it # Test it
# Maximum path is 3 -> 10 -> 3 -> 9 for a total of 25 # Maximum path is 3 -> 10 -> 3 -> 9 for a total of 25
(def triangle @[ (def triangle '[
@[3] [3]
@[7 10] [7 10]
@[4 3 7] [4 3 7]
@[8 9 1 3] [8 9 1 3]
]) ])
(print (maxpath triangle)) (print (maxpath triangle))

View File

@ -675,7 +675,7 @@ to call on any table. Does not print table prototype information."
(buffer-popn indent 2) (buffer-popn indent 2)
(buffer-push-string buf indent)))) (buffer-push-string buf indent))))
(defn pp-dict-nested [y proto?] (defn pp-dict-nested [y]
(buffer-push-string indent " ") (buffer-push-string indent " ")
(def ps (sort (pairs y))) (def ps (sort (pairs y)))
(for [i 0 (length ps)] (for [i 0 (length ps)]
@ -684,10 +684,6 @@ to call on any table. Does not print table prototype information."
(recur k) (recur k)
(buffer-push-string buf " ") (buffer-push-string buf " ")
(recur v)) (recur v))
(when proto?
(buffer-push-string buf indent)
(buffer-push-string buf "{proto} ")
(recur proto?))
(buffer-popn indent 2) (buffer-popn indent 2)
(buffer-push-string buf indent)) (buffer-push-string buf indent))
@ -701,9 +697,8 @@ to call on any table. Does not print table prototype information."
(recur v))) (recur v)))
(defn pp-dict [y] (defn pp-dict [y]
(def proto? (and (table? y) (getproto y))) (def complex? (> (length y) 4))
(def complex? (or proto? (> (length y) 4))) ((if complex? pp-dict-nested pp-dict-simple) y))
((if complex? pp-dict-nested pp-dict-simple) y proto?))
(def printers { (def printers {
:array (fn [y] (do-ds y "@[" "]" true pp-seq)) :array (fn [y] (do-ds y "@[" "]" true pp-seq))
@ -882,7 +877,7 @@ onvalue."
(if good (if good
(if (= (fiber-status f) :error) (if (= (fiber-status f) :error)
(onerr "runtime" res f) (onerr "runtime" res f)
(onvalue res)))) (if going (onvalue res)))))
# Run loop # Run loop
(def oldenv *env*) (def oldenv *env*)
@ -915,6 +910,20 @@ onvalue."
(when tail (file-write stdout " (tailcall)")) (when tail (file-write stdout " (tailcall)"))
(file-write stdout "\n")))) (file-write stdout "\n"))))
(defn eval
"Evaluates a string in the current environment. If more control over the
environment is needed, use run-context."
[str]
(var state (string str))
(defn chunks [buf]
(def ret state)
(:= state nil)
(if ret
(buffer-push-string buf ret)))
(var returnval nil)
(run-context *env* chunks (fn [x] (:= returnval x)) default-error-handler)
returnval)
(def require (do (def require (do
(def cache @{}) (def cache @{})
(def loading @{}) (def loading @{})
@ -923,7 +932,9 @@ onvalue."
(error (string "circular dependency: module " path " is loading"))) (error (string "circular dependency: module " path " is loading")))
(def check (get cache path)) (def check (get cache path))
(if check check (do (if check check (do
(if (= ".so" (string-slice path -3 -1)) (if (or
(= ".so" (string-slice path -3 -1))
(= ".dll" (string-slice path -4 -1)))
((native path)) ((native path))
(do (do
(def newenv (make-env)) (def newenv (make-env))
@ -947,8 +958,7 @@ onvalue."
(def v (get newenv k)) (def v (get newenv k))
(when (not (get v :private)) (when (not (get v :private))
(put env (symbol prefix k) v)) (put env (symbol prefix k) v))
(:= k (next newenv k))) (:= k (next newenv k))))
env)
(defmacro import [path & args] (defmacro import [path & args]
(apply tuple import* '_env path args)) (apply tuple import* '_env path args))

View File

@ -10,24 +10,33 @@
(print "Options are:") (print "Options are:")
(print " -h Show this help") (print " -h Show this help")
(print " -v Print the version string") (print " -v Print the version string")
(print " -e Execute a string of dst")
(print " -r Enter the repl after running all scripts") (print " -r Enter the repl after running all scripts")
(os-exit 0)) (os-exit 0)
"v" (fn [] (print VERSION) (os-exit 0)) 1)
"r" (fn [] (:= *should-repl* true)) "v" (fn [] (print VERSION) (os-exit 0) 1)
"r" (fn [] (:= *should-repl* true) 1)
"e" (fn [i]
(:= *no-file* false)
(eval (get args (+ i 1)))
2)
}) })
(defn- dohandler [n] (defn- dohandler [n i]
(def h (get handlers n)) (def h (get handlers n))
(if h (h) (print "unknown flag -" n))) (if h (h i) (print "unknown flag -" n)))
# Process arguments # Process arguments
(for [i 1 (length args)] (var i 1)
(def lenargs (length args))
(while (< i lenargs)
(def arg (get args i)) (def arg (get args i))
(if (= "-" (string-slice arg 0 1)) (if (= "-" (string-slice arg 0 1))
(dohandler (string-slice arg 1 2)) (+= i (dohandler (string-slice arg 1 2) i))
(do (do
(:= *no-file* false) (:= *no-file* false)
(import arg)))) (import arg)
(++ i))))
(when (or *should-repl* *no-file*) (when (or *should-repl* *no-file*)
(print (string "Dst " VERSION " Copyright (C) 2017-2018 Calvin Rose")) (print (string "Dst " VERSION " Copyright (C) 2017-2018 Calvin Rose"))