1
0
mirror of https://github.com/janet-lang/janet synced 2024-07-01 17:43:15 +00:00
janet/src/compiler/boot.dst

778 lines
20 KiB
Plaintext
Raw Normal View History

# Bootstrap the dst environment
# Copyright 2018 (C) Calvin Rose
2018-03-24 05:44:17 +00:00
(var *env*
"The current environment."
2018-03-18 18:01:58 +00:00
_env)
(def defn :macro
"Define a function"
2018-03-24 05:44:17 +00:00
(fn [name & more]
(def fstart (fn recur [i]
(def ith (ast-unwrap1 (get more i)))
(def t (type ith))
(def tuple? (= t :tuple))
(def array? (= t :array))
(if (if tuple? tuple? array?) i (recur (+ i 1)))))
(def start (fstart 0))
(def fnbody (tuple-prepend (tuple-prepend (tuple-slice more start) name) 'fn))
(def formargs (array-concat @['def name] (array-slice more 0 start) @[fnbody]))
(apply1 tuple formargs)))
(def defmacro :macro
"Define a macro."
(do
2018-03-18 18:01:58 +00:00
(def defn* (get (get _env 'defn) :value))
(fn [name & more]
(apply1 defn* (array-concat
@[name :macro] more)))))
(defmacro defmacro-
"Define a private macro that will not be exported."
[name & more]
(apply1 tuple (array-concat
@['defmacro name :private] more)))
2018-03-12 04:57:13 +00:00
(defmacro defn-
"Define a private function that will not be exported."
[name & more]
(apply1 tuple (array-concat
@['defn name :private] more)))
2018-03-12 04:57:13 +00:00
# Basic predicates
2018-03-15 01:46:56 +00:00
(defn even? [x] (== 0 (% x 2)))
(defn odd? [x] (== 1 (% x 2)))
(defn zero? [x] (== x 0))
(defn pos? [x] (> x 0))
(defn neg? [x] (< x 0))
2018-03-15 01:46:56 +00:00
(defn one? [x] (== x 1))
(defn table? [x] (= (type x) :table ))
(defn struct? [x] (= (type x) :struct))
(defn array? [x] (= (type x) :array))
(defn tuple? [x] (= (type x) :tuple))
(defn boolean? [x] (= (type x) :boolean))
(defn true? [x] (= (type x) true))
(defn false? [x] (= (type x) false))
(defn nil? [x] (= x nil))
(def atomic? (do
(def non-atomic-types {
:array true
:tuple true
:table true
:struct true
})
(fn [x] (not (get non-atomic-types (type x))))))
2018-03-15 01:46:56 +00:00
# C style macros and functions for imperative sugar
(defn inc [x] (+ x 1))
(defn dec [x] (- x 1))
(defmacro ++ [x] (tuple ':= x (tuple + x 1)))
(defmacro -- [x] (tuple ':= x (tuple - x 1)))
(defmacro += [x n] (tuple ':= x (tuple + x n)))
(defmacro -= [x n] (tuple ':= x (tuple - x n)))
(defmacro *= [x n] (tuple ':= x (tuple * x n)))
(defmacro /= [x n] (tuple ':= x (tuple / x n)))
(defmacro %= [x n] (tuple ':= x (tuple % x n)))
(defmacro &= [x n] (tuple ':= x (tuple & x n)))
(defmacro |= [x n] (tuple ':= x (tuple | x n)))
(defmacro ^= [x n] (tuple ':= x (tuple ^ x n)))
(defmacro >>= [x n] (tuple ':= x (tuple >> x n)))
(defmacro <<= [x n] (tuple ':= x (tuple << x n)))
(defmacro >>>= [x n] (tuple ':= x (tuple >>> x n)))
2018-03-12 06:06:51 +00:00
(defmacro comment
"Ignores the body of the comment."
[])
(defmacro when
2018-03-12 06:06:51 +00:00
"Evaluates the body when the condition is true. Otherwise returns nil."
2018-03-24 05:44:17 +00:00
[cond & body]
(tuple 'if cond (tuple-prepend body 'do)))
(defmacro cond
2018-03-12 06:06:51 +00:00
"Evaluates conditions sequentially until the first true condition
is found, and then executes the corresponding body. If there are an
odd number of forms, the last expression is executed if no forms
are matched. If there are no matches, return nil."
[& pairs]
(defn aux [i]
(def restlen (- (length pairs) i))
(if (= restlen 0) nil
(if (= restlen 1) (get pairs i)
(tuple 'if (get pairs i)
(get pairs (+ i 1))
(aux (+ i 2))))))
(aux 0))
2018-03-16 17:40:10 +00:00
(defn doc*
[env sym]
(def x (get env sym))
(if (not x)
(print "symbol " x " not found.")
(do
(def d (get x 'doc))
(print "\n" (if d d "no documentation found.") "\n"))))
2018-03-16 17:40:10 +00:00
(defmacro doc
"Shows documentation for the given symbol."
[sym]
2018-03-16 19:58:11 +00:00
(tuple doc* '_env (tuple 'quote sym)))
2018-03-16 17:40:10 +00:00
(def apply
(fn [f & args]
(def last (- (length args) 1))
(apply1 f (array-concat (array-slice args 0 -2) (get args last)))))
(defmacro select
2018-03-12 06:06:51 +00:00
"Select the body that equals the dispatch value. When pairs
has an odd number of arguments, the last is the default expression.
If no match is found, returns nil"
[dispatch & pairs]
(def sym (gensym))
(defn aux [i]
(def restlen (- (length pairs) i))
(if (= restlen 0) nil
(if (= restlen 1) (get pairs i)
(tuple 'if (tuple = sym (get pairs i))
(get pairs (+ i 1))
(aux (+ i 2))))))
(tuple 'do
(tuple 'def sym dispatch)
(aux 0)))
2018-03-24 05:44:17 +00:00
(defmacro and [& forms]
2018-03-15 01:46:56 +00:00
(def len (length forms))
(if (= len 0) true ((fn aux [i]
(cond
(>= (inc i) len) (get forms i)
(tuple 'if (get forms i) (aux (inc i)) false))) 0)))
2018-03-24 05:44:17 +00:00
(defmacro or [& forms]
2018-03-15 01:46:56 +00:00
(def len (length forms))
(if (= len 0) false ((fn aux [i]
(cond
(>= (inc i) len) (get forms i)
(tuple 'if (get forms i) true (aux (inc i))))) 0)))
2018-03-24 05:44:17 +00:00
(defn identity
2018-03-12 06:06:51 +00:00
"A function that returns its first argument."
2018-03-12 04:57:13 +00:00
[x] x)
(def iter (do
(defn array-iter [x]
(def len (length x))
(var i 0)
{
:more (fn [] (< i len))
:next (fn []
(def ret (get x i))
2018-03-16 22:15:34 +00:00
(:= i (+ i 1))
2018-03-24 05:44:17 +00:00
ret)
})
(def iters {
:array array-iter
:tuple array-iter
2018-03-16 17:40:10 +00:00
:struct identity})
(fn [x]
(def makei (get iters (type x)))
(if makei (makei x) (error "expected sequence")))))
2018-03-16 17:40:10 +00:00
(defn range2 [bottom top]
(var i bottom)
{
:more (fn [] (< i top))
:next (fn []
(def ret i)
2018-03-16 22:15:34 +00:00
(:= i (+ i 1))
2018-03-24 05:44:17 +00:00
ret)
})
2018-03-16 17:40:10 +00:00
(defn range [top] (range2 0 top))
2018-03-24 05:44:17 +00:00
(defn doiter [itr]
(def {:more more :next next} (iter itr))
(while (more) (next)))
2018-03-24 05:44:17 +00:00
(defn foreach [itr f]
(def {:more more :next next} (iter itr))
2018-03-14 03:39:49 +00:00
(while (more) (f (next))))
2018-03-24 05:44:17 +00:00
(defn iter2array [itr]
(def {:more more :next next} (iter itr))
(def a @[])
2018-03-15 01:46:56 +00:00
(while (more) (array-push a (next)))
a)
(defn map [f itr]
(def {:more more :next next} (iter itr))
{:more more :next (fn [] (f (next)))})
(defn reduce [f start itr]
(def itr (iter itr))
(def {:more more :next next} itr)
(if (more)
(reduce f (f start (next)) itr)
start))
2018-03-11 20:30:38 +00:00
(defn filter [pred itr]
(def itr (iter itr))
(def {:more more :next next} itr)
(var alive true)
(var temp nil)
(var isnew true)
(defn nextgood []
(if alive
(if (more)
(do
(def n (next))
(if (pred n) n (nextgood)))
2018-03-16 22:15:34 +00:00
(:= alive false))))
(defn nnext [] (def ret temp) (:= temp (nextgood)) ret)
(defn nmore [] (when isnew (:= isnew false) (nnext)) alive)
{:more nmore :next nnext})
2018-03-11 20:30:38 +00:00
(defmacro let [bindings & body]
(def head (ast-unwrap1 bindings))
(when (odd? (length head)) (error "expected even number of bindings to let"))
(def len (length head))
(var i 0)
(var accum @['do])
(while (< i len)
2018-03-24 05:44:17 +00:00
(array-push accum (tuple 'def
2018-03-11 20:30:38 +00:00
(get head i)
(get head (+ 1 i))))
2018-03-16 22:15:34 +00:00
(:= i (+ i 2)))
2018-03-11 20:30:38 +00:00
(array-push accum (tuple-prepend body 'do))
(apply1 tuple accum))
2018-02-03 22:22:04 +00:00
(defn pairs [x]
(var lastkey (next x nil))
{
:more (fn [] lastkey)
:next (fn []
(def ret (tuple lastkey (get x lastkey)))
2018-03-16 22:15:34 +00:00
(:= lastkey (next x lastkey))
ret)
})
(defn keys [x]
(var lastkey (next x nil))
{
:more (fn [] lastkey)
:next (fn []
(def ret lastkey)
2018-03-16 22:15:34 +00:00
(:= lastkey (next x lastkey))
ret)
})
(defn values [x]
(var lastkey (next x nil))
{
:more (fn [] lastkey)
:next (fn []
(def ret (get x lastkey))
2018-03-16 22:15:34 +00:00
(:= lastkey (next x lastkey))
ret)
})
(defn partial [f & more]
(if (zero? (length more)) f
(fn [& r] (apply1 f (array-concat @[] more r)))))
(defmacro for [head & body]
(def head (ast-unwrap1 head))
(def sym (get head 0))
(def start (get head 1))
(def end (get head 2))
(def _inc (get head 3))
(def inc (if _inc _inc 1))
(def endsym (gensym))
(tuple 'do
(tuple 'var sym start)
(tuple 'def endsym end)
(tuple 'while (tuple '< sym endsym)
(tuple-prepend body 'do)
2018-03-16 22:15:34 +00:00
(tuple ':= sym (tuple '+ sym inc)))))
2018-03-26 17:36:58 +00:00
(defn every? [pred seq]
(var res true)
(var i 0)
(def len (length seq))
(while (< i len)
(def item (get seq i))
(if (pred item)
(++ i)
(do (:= res false) (:= i len))))
res)
(defn juxt*
[& funs]
(def len (length funs))
(fn [& args]
(def ret @[])
(for [i 0 len]
(array-push ret (apply1 (get funs i) args)))
(apply1 tuple ret)))
(defmacro juxt
[& funs]
(def parts @['tuple])
(def $args (gensym))
(for [i 0 (length funs)]
(array-push parts (tuple apply1 (get funs i) $args)))
(tuple 'fn (tuple '& $args) (apply1 tuple parts)))
2018-03-12 16:47:05 +00:00
(defmacro ->
[x & forms]
(defn fop [last nextform]
(def n (ast-unwrap1 nextform))
(def [h t] (if (= :tuple (type n))
[tuple (get n 0) (array-slice n 1)]
[tuple n @[]]))
(def parts (array-concat @[h last] t))
(apply1 tuple parts))
2018-03-12 16:47:05 +00:00
(reduce fop x forms))
(defmacro ->>
[x & forms]
(defn fop [last nextform]
(def n (ast-unwrap1 nextform))
(def [h t] (if (= :tuple (type n))
[tuple (get n 0) (array-slice n 1)]
[tuple n @[]]))
(def parts (array-concat @[h] t @[last]))
(apply1 tuple parts))
2018-03-12 16:47:05 +00:00
(reduce fop x forms))
(defn reverse-array
"Reverses the order of the elements in a given array or tuple and returns a new array."
[t]
(var n (dec (length t)))
(var reversed @[])
(while (>= n 0)
(array-push reversed (get t n))
(-- n))
reversed)
(defn reverse-tuple
"Reverses the order of the elements given an array or tuple and returns a tuple"
[t]
(apply1 tuple (reverse-array t)))
(defn reverse
"Reverses order of elements in a given array or tuple"
[t]
(select (type t)
:tuple (reverse-tuple t)
:array (reverse-array t)))
(defmacro if-not
"Sorthand for (if (not ... "
[condition exp-1 exp-2]
(tuple 'if (tuple not condition)
exp-1
exp-2))
(defmacro when-not
"Sorthand for (when (not ... "
[condition exp-1]
(tuple 'when (tuple not condition) exp-1))
(defmacro if-let
"Takes the first one or two forms in a vector and if both are true binds
all the forms with let and evaluates the first expression else
evaluates the second"
[bindings then else]
(tuple 'let bindings
(tuple 'if (tuple 'and (tuple 'get bindings 1)
(tuple 'if
(tuple '> (tuple 'length bindings) 2)
(tuple 'get bindings 3) 'true))
then
else)))
(defmacro when-let
"Takes the first one or two forms in vector and if true binds
2018-03-24 05:44:17 +00:00
all the forms with let and evaluates the body"
[bindings & body]
(tuple 'let bindings
(tuple
'when
(tuple 'and (tuple 'get bindings 1)
(tuple 'if
(tuple '> (tuple 'length bindings) 2)
(tuple 'get bindings 3) 'true))
(apply1 tuple (array-concat @['do] body)))))
(defn comp
"Takes multiple functions and returns a function that is the composition
of those functions."
[& functions]
(select (length functions)
0 nil
1 (get functions 0)
2 (let [[f g] functions] (fn [x] (f (g x))))
3 (let [[f g h] functions] (fn [x] (f (g (h x)))))
4 (let [[f g h i] functions] (fn [x] (f (g (h (i x))))))
(let [[f g h i j] functions]
(apply comp (fn [x] (f (g (h (i (j x))))))
(tuple-slice functions 5 -1)))))
(defn zipcoll
2018-03-24 05:44:17 +00:00
"Creates an table or tuple from two arrays/tuples. If a third argument of
:struct is givent resault is struct else is table."
[coll-1 coll-2 the-type]
(var zipping-table @{})
(def {:more more1 :next next1} (iter coll-1))
(def {:more more2 :next next2} (iter coll-2))
(while (and (more1) (more2))
(put zipping-table (next1) (next2)))
2018-03-24 05:44:17 +00:00
(if (struct? the-type)
(table-to-struct zipping-table)
zipping-table))
(defn update
2018-03-24 05:44:17 +00:00
"Accepts a key argument and passes its' associated value to a function.
The key then, is associated to the function's return value"
[coll a-key a-function & args]
(def old-value (get coll a-key))
(put coll a-key (apply a-function old-value args)))
(defn merge
"Merges mutliple tables/structs to one. If a key appears in more than one
collection, then later values replace any previous ones.
The type of the first collection determines the type of the resulting
collection"
[& colls]
(def container @{})
(for [i 0 (length colls)]
(def c (get colls i))
(var key (next c nil))
(while (not= nil key)
(put container key (get c key))
(:= key (next c key))))
(if (table? (get colls 0)) container (table-to-struct container)))
2018-03-14 23:08:00 +00:00
# Start pretty printer
2018-03-26 17:36:58 +00:00
(defn pp [x]
(def buf @"")
(def indent @"\n")
(def seen @{})
(var nextid 0)
# Forward declaration
(var recur nil)
(defn do-ds
[y start end checkcycle dispatch]
(def id (get seen y))
(if (and checkcycle id)
(do
(buffer-push-string buf "<cycle ")
(buffer-push-string buf (string id))
(buffer-push-string buf ">"))
2018-03-14 23:08:00 +00:00
(do
2018-03-26 17:36:58 +00:00
(put seen y (++ nextid))
2018-03-14 23:08:00 +00:00
(buffer-push-string buf start)
2018-03-26 17:36:58 +00:00
(dispatch y)
(buffer-push-string buf end))))
(defn pp-seq [y]
(def len (length y))
(if (< len 5)
(do
2018-03-14 23:08:00 +00:00
(for [i 0 len]
(when (not= i 0) (buffer-push-string buf " "))
2018-03-26 17:36:58 +00:00
(recur (get y i))))
2018-03-14 23:08:00 +00:00
(do
2018-03-26 17:36:58 +00:00
(buffer-push-string indent " ")
(for [i 0 len]
(when (not= i len) (buffer-push-string buf indent))
(recur (get y i)))
(buffer-popn indent 2)
(buffer-push-string buf indent))))
(defn pp-dict-nested [y]
(var k (next y nil))
(buffer-push-string indent " ")
(buffer-push-string buf indent)
(while k
(def v (get y k))
(recur k)
(buffer-push-string buf " ")
(recur v)
(:= k (next y k))
(when k (buffer-push-string buf indent)))
(buffer-popn indent 2)
(buffer-push-string buf indent))
(defn pp-dict-simple [y]
(var k (next y nil))
(while k
(def v (get y k))
(recur k)
(buffer-push-string buf " ")
(recur v)
(:= k (next y k))
(when k (buffer-push-string buf " "))))
(defn pp-dict [y]
((if (> 4 (length y)) pp-dict-simple pp-dict-nested) y))
(def printers {
:array (fn [y] (do-ds y "@[" "]" true pp-seq))
:tuple (fn [y] (do-ds y "(" ")" false pp-seq))
:table (fn [y] (do-ds y "@{" "}" true pp-dict))
:struct (fn [y] (do-ds y "{" "}" false pp-dict))
2018-03-14 23:08:00 +00:00
})
2018-03-26 17:36:58 +00:00
(:= recur (fn [y]
(def p (get printers (type y)))
(if p
(p y)
(buffer-push-string buf (describe y)))))
2018-03-14 23:08:00 +00:00
2018-03-26 17:36:58 +00:00
(recur x)
(buffer-push-string buf "\n")
2018-03-14 23:08:00 +00:00
2018-03-26 17:36:58 +00:00
(file-write stdout buf))
2018-03-14 23:08:00 +00:00
# End pretty printer
2018-03-15 01:46:56 +00:00
(defn unique [s]
(def tab @{})
(foreach s (fn [x] (put tab x true)))
2018-03-15 01:46:56 +00:00
(keys tab))
2018-03-18 18:01:58 +00:00
(defn macroexpand1
"Expand macros in a form, but do not recursively expand macros."
[x]
2018-03-24 05:44:17 +00:00
(defn doarray [a]
2018-03-18 18:01:58 +00:00
(def len (length a))
(def newa @[])
2018-03-18 18:01:58 +00:00
(for [i 0 len]
(array-push newa (macroexpand1 (get a i))))
newa)
(defn dotable [t]
(def newt @{})
(var key (next t nil))
(while (not= nil key)
(put newt (macroexpand1 key) (macroexpand1 (get t key)))
(:= key (next t key)))
newt)
(defn expandlast [t]
(def len (length t))
(def last (get t (- len 1)))
(tuple-append (tuple-slice t 0 -2) (macroexpand1 last)))
(defn expandall [t]
(def args (doarray (tuple-slice t 1)))
(apply tuple (get t 0) args))
(defn expandfn [t]
(def args (doarray (tuple-slice t 2)))
(apply tuple 'fn (get t 1) args))
(def specs {
':= expandlast
'ast-quote identity
'def expandlast
'do expandall
'fn expandfn
'if expandall
'quote identity
'var expandlast
'while expandall
2018-03-24 05:44:17 +00:00
})
2018-03-18 18:01:58 +00:00
(defn dotup [t]
(def h (get t 0))
(def s (get specs h))
(def entry (get *env* h))
(def m (get entry :value))
(def m? (get entry :macro))
(cond
s (s t)
m? (apply1 m (tuple-slice t 1))
(apply1 tuple (doarray t))))
(defn doarray* [a]
(def res (doarray a))
(if (= (apply tuple res) (apply tuple a)) a res))
(defn dotable* [t]
(def res (dotable t))
(if (= (table-to-struct res) (table-to-struct t)) t res))
(def ux (ast-unwrap1 x))
(select (type ux)
:tuple (dotup ux)
:array (doarray* ux)
:struct (table-to-struct (dotable ux))
:table (dotable* ux)
ux))
(defn macroexpand
"Expand macros completely."
[x]
(var previous x)
(var current (macroexpand1 x))
(var counter 0)
(while (not= current previous)
(if (> (++ counter) 200)
(error "macro expansion too nested"))
(:= previous current)
(:= current (macroexpand1 current)))
current)
2018-03-26 00:39:38 +00:00
(defmacro coro
"A wrapper for making fibers. Same as (fiber (fn [] ...body))."
[& body]
(tuple fiber (apply tuple 'fn [] body)))
(defn make-env [parent]
(def parent (if parent parent _env))
(def newenv (setproto @{} parent))
(put newenv '_env @{:value newenv :private true})
newenv)
2018-03-24 05:44:17 +00:00
(def run-context
2018-03-12 06:06:51 +00:00
"Run a context. This evaluates expressions of dst in an environment,
and is encapsulates the parsing, compilation, and evaluation of dst.
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,
network connection, etc. onvalue and onerr are callbacks that are
invoked when a result is returned and when an error is produced,
respectively.
2018-03-24 05:44:17 +00:00
2018-03-12 06:06:51 +00:00
This function can be used to implemement a repl very easily, simply
pass a function that reads line from stdin to chunks, and print to
onvalue."
(do
(defn val-stream [chunks onerr]
(var going true)
# Stream of characters
(def chars (fiber (fn []
(def buf @"")
(var len 1)
(while (< 0 len)
(buffer-clear buf)
2018-03-24 05:44:17 +00:00
(chunks buf)
2018-03-16 22:15:34 +00:00
(:= len (length buf))
(for [i 0 len]
(yield (get buf i))))
0)))
(var temp nil)
(var tempval nil)
# Stream of values
2018-03-26 00:39:38 +00:00
(def f (coro
(def p (parser 1))
(while going
(select (parser-status p)
:full (yield (parser-produce p))
:error (onerr "parse" (parser-error p))
(select (fiber-status chars)
:new (parser-byte p (resume chars))
:pending (parser-byte p (resume chars))
(:= going false))))
(when (not= :root (parser-status p))
(onerr "parse" "unexpected end of source"))
nil))
(defn more [] (if temp true
(do
2018-03-16 22:15:34 +00:00
(:= temp true)
(:= tempval (resume f))
going)))
(defn next [] (if temp
2018-03-16 22:15:34 +00:00
(do (:= temp nil) tempval)
(resume f)))
{:more more :next next})
(fn [env chunks onvalue onerr]
(defn doone [source]
2018-03-18 13:13:21 +00:00
(var good true)
2018-03-26 00:39:38 +00:00
(def f (coro
(def res (compile source env))
(if (= (type res) :function)
(res)
2018-03-18 13:13:21 +00:00
(do
(:= good false)
2018-03-26 00:39:38 +00:00
(onerr "compile" (get res :error))))))
(def res (resume f))
2018-03-18 13:13:21 +00:00
(if good
(if (= (fiber-status f) :error)
(onerr "runtime" res f)
2018-03-18 13:13:21 +00:00
(onvalue res))))
2018-03-26 00:39:38 +00:00
(def oldenv *env*)
(:= *env* env)
(foreach (val-stream chunks onerr) doone)
2018-03-26 00:39:38 +00:00
(:= *env* oldenv)
env)))
(defn default-error-handler
[t x f]
2018-03-22 01:48:19 +00:00
(file-write stdout (string t " error: "))
(pp x)
(when f
(def st (fiber-stack f))
(def len (length st))
(for [i 0 len]
(def {
:function func
:tail tail
:pc pc
:c c
:name name
} (get st i))
(file-write stdout " in")
(when c (file-write stdout " cfunction"))
2018-03-24 05:44:17 +00:00
(when name (file-write stdout (string " " name)))
(when func (file-write stdout (string " " func)))
(when pc (file-write stdout (string " (pc=" pc ")")))
(when tail (file-write stdout " (tailcall)"))
2018-03-22 01:48:19 +00:00
(file-write stdout "\n"))))
(def require (do
(def cache @{})
(def loading @{})
(fn [path]
(when (get loading path)
(error (string "circular dependency: module " path " is loading")))
(def check (get cache path))
(if check check (do
(def newenv (make-env))
(put cache path newenv)
(put loading path true)
(def f (file-open path))
(defn chunks [buf] (file-read f 1024 buf))
(run-context newenv chunks identity default-error-handler)
(file-close f)
(put loading path nil)
newenv)))))
2018-03-16 17:40:10 +00:00
(defn import* [env path & args]
(def newenv (require path))
(def {
:prefix prefix
} (apply1 table args))
2018-03-16 17:40:10 +00:00
(foreach (pairs newenv) (fn [[k v]]
(when (not (get v :private))
2018-03-16 17:40:10 +00:00
(put env (symbol (if prefix prefix "") k) v)))))
(defmacro import [path & args]
2018-03-24 05:44:17 +00:00
(apply tuple import* '_env path args))
2018-03-14 03:39:49 +00:00
(defn repl [getchunk]
(def newenv (make-env))
(defn chunks [buf]
2018-03-14 14:54:29 +00:00
(file-write stdout "> ")
(file-flush stdout)
(file-read stdin :line buf))
(defn onvalue [x]
2018-03-18 18:01:58 +00:00
(put newenv '_ @{:value x})
2018-03-14 23:08:00 +00:00
(pp x))
(run-context newenv (if getchunk getchunk chunks)
onvalue default-error-handler))