1
0
mirror of https://github.com/janet-lang/janet synced 2024-11-24 17:27:18 +00:00

Merge branch 'master' of github.com:bakpakin/dst

This commit is contained in:
Calvin Rose 2018-05-05 14:42:28 -04:00
commit 06b80e56eb
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

@ -691,7 +691,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)]
@ -700,10 +700,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))
@ -717,9 +713,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))
@ -898,7 +893,7 @@ onvalue."
(if good
(if (= (fiber-status f) :error)
(onerr "runtime" res f)
(onvalue res))))
(if going (onvalue res)))))
# Run loop
(def oldenv *env*)
@ -931,6 +926,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 @{})
@ -939,7 +948,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))
@ -963,8 +974,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"))