1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-28 11:09:54 +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
# Maximum path is 3 -> 10 -> 3 -> 9 for a total of 25
(def triangle @[
@[3]
@[7 10]
@[4 3 7]
@[8 9 1 3]
(def triangle '[
[3]
[7 10]
[4 3 7]
[8 9 1 3]
])
(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-push-string buf indent))))
(defn pp-dict-nested [y proto?]
(defn pp-dict-nested [y]
(buffer-push-string indent " ")
(def ps (sort (pairs y)))
(for [i 0 (length ps)]
@ -684,10 +684,6 @@ to call on any table. Does not print table prototype information."
(recur k)
(buffer-push-string buf " ")
(recur v))
(when proto?
(buffer-push-string buf indent)
(buffer-push-string buf "{proto} ")
(recur proto?))
(buffer-popn indent 2)
(buffer-push-string buf indent))
@ -701,9 +697,8 @@ to call on any table. Does not print table prototype information."
(recur v)))
(defn pp-dict [y]
(def proto? (and (table? y) (getproto y)))
(def complex? (or proto? (> (length y) 4)))
((if complex? pp-dict-nested pp-dict-simple) y proto?))
(def complex? (> (length y) 4))
((if complex? pp-dict-nested pp-dict-simple) y))
(def printers {
:array (fn [y] (do-ds y "@[" "]" true pp-seq))
@ -882,7 +877,7 @@ onvalue."
(if good
(if (= (fiber-status f) :error)
(onerr "runtime" res f)
(onvalue res))))
(if going (onvalue res)))))
# Run loop
(def oldenv *env*)
@ -915,6 +910,20 @@ onvalue."
(when tail (file-write stdout " (tailcall)"))
(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 cache @{})
(def loading @{})
@ -923,7 +932,9 @@ onvalue."
(error (string "circular dependency: module " path " is loading")))
(def check (get cache path))
(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))
(do
(def newenv (make-env))
@ -947,8 +958,7 @@ onvalue."
(def v (get newenv k))
(when (not (get v :private))
(put env (symbol prefix k) v))
(:= k (next newenv k)))
env)
(:= k (next newenv k))))
(defmacro import [path & args]
(apply tuple import* '_env path args))

View File

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