2018-01-31 22:39:18 +00:00
|
|
|
(def defmacro macro
|
|
|
|
(fn [name & more] (tuple 'def name 'macro (tuple-prepend (tuple-prepend more name) 'fn))))
|
|
|
|
|
|
|
|
(defmacro defn
|
|
|
|
[name & more]
|
|
|
|
(tuple 'def name (tuple-prepend (tuple-prepend more name) 'fn)))
|
|
|
|
|
|
|
|
(defmacro when
|
|
|
|
[cond & body]
|
|
|
|
(tuple 'if cond (tuple-prepend body 'do)))
|
|
|
|
|
2018-02-07 18:19:34 +00:00
|
|
|
(defmacro or [x y] (tuple 'if x true y))
|
|
|
|
(defmacro and [x y] (tuple 'if x y false))
|
|
|
|
|
2018-02-07 05:44:51 +00:00
|
|
|
(def identity (fn [x] x))
|
|
|
|
|
2018-01-31 22:39:18 +00:00
|
|
|
(def seq (do
|
|
|
|
(defn array-seq [x]
|
|
|
|
(def len (length x))
|
|
|
|
(var i 0)
|
|
|
|
{
|
|
|
|
:more (fn [] (< i len))
|
|
|
|
:next (fn []
|
|
|
|
(def ret (get x i))
|
|
|
|
(varset! i (+ i 1))
|
|
|
|
ret)
|
|
|
|
})
|
2018-03-11 19:35:23 +00:00
|
|
|
(defn fiber-seq [x]
|
|
|
|
{
|
|
|
|
:more (fn [] (or
|
|
|
|
(= (fiber-status x) :pending)
|
|
|
|
(= (fiber-status x) :new)))
|
|
|
|
:next (fn []
|
|
|
|
(resume x))
|
|
|
|
})
|
2018-01-31 22:39:18 +00:00
|
|
|
(def seqs {
|
|
|
|
:array array-seq
|
|
|
|
:tuple array-seq
|
2018-03-11 19:35:23 +00:00
|
|
|
:fiber fiber-seq
|
2018-01-31 22:39:18 +00:00
|
|
|
:struct (fn [x] x)})
|
|
|
|
(fn [x]
|
|
|
|
(def makeseq (get seqs (type x)))
|
|
|
|
(if makeseq (makeseq x) (error "expected sequence")))))
|
|
|
|
|
|
|
|
(defn range [top]
|
|
|
|
(var i 0)
|
|
|
|
{
|
|
|
|
:more (fn [] (< i top))
|
|
|
|
:next (fn []
|
|
|
|
(def ret i)
|
|
|
|
(varset! i (+ i 1))
|
|
|
|
ret)
|
|
|
|
})
|
|
|
|
|
|
|
|
(defn doseq [s]
|
|
|
|
(def s (seq s))
|
|
|
|
(def more? (get s :more))
|
|
|
|
(def getnext (get s :next))
|
|
|
|
(while (more?)
|
|
|
|
(getnext)))
|
|
|
|
|
|
|
|
(defn map [f s]
|
|
|
|
(def s (seq s))
|
|
|
|
(def more (get s :more))
|
|
|
|
(def getnext (get s :next))
|
|
|
|
{
|
|
|
|
:more more
|
|
|
|
:next (fn [] (f (getnext)))
|
|
|
|
})
|
|
|
|
|
|
|
|
(defn reduce [f start s]
|
|
|
|
(def s (seq s))
|
|
|
|
(def more? (get s :more))
|
|
|
|
(def getnext (get s :next))
|
|
|
|
(var ret start)
|
|
|
|
(while (more?)
|
|
|
|
(varset! ret (f ret (getnext))))
|
|
|
|
ret)
|
|
|
|
|
|
|
|
(defmacro for [head & body]
|
2018-02-03 22:22:04 +00:00
|
|
|
(def head (ast-unwrap1 head))
|
2018-01-31 22:39:18 +00:00
|
|
|
(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))
|
2018-02-12 16:49:10 +00:00
|
|
|
(def endsym (gensym))
|
2018-01-31 22:39:18 +00:00
|
|
|
(tuple 'do
|
|
|
|
(tuple 'var sym start)
|
2018-02-12 16:49:10 +00:00
|
|
|
(tuple 'def endsym end)
|
|
|
|
(tuple 'while (tuple '< sym endsym)
|
2018-01-31 22:39:18 +00:00
|
|
|
(tuple-prepend body 'do)
|
2018-03-11 19:35:23 +00:00
|
|
|
(tuple 'varset! sym (tuple '+ sym 1)))))
|
2018-02-03 22:22:04 +00:00
|
|
|
|
2018-02-06 15:31:42 +00:00
|
|
|
(defn pairs [x]
|
|
|
|
(var lastkey (next x nil))
|
|
|
|
{
|
|
|
|
:more (fn [] lastkey)
|
|
|
|
:next (fn []
|
|
|
|
(def ret (tuple lastkey (get x lastkey)))
|
|
|
|
(varset! lastkey (next x lastkey))
|
|
|
|
ret)
|
|
|
|
})
|
|
|
|
|
|
|
|
(defn keys [x]
|
|
|
|
(var lastkey (next x nil))
|
|
|
|
{
|
|
|
|
:more (fn [] lastkey)
|
|
|
|
:next (fn []
|
|
|
|
(def ret lastkey)
|
|
|
|
(varset! lastkey (next x lastkey))
|
|
|
|
ret)
|
|
|
|
})
|
|
|
|
|
|
|
|
(defn values [x]
|
|
|
|
(var lastkey (next x nil))
|
|
|
|
{
|
|
|
|
:more (fn [] lastkey)
|
|
|
|
:next (fn []
|
|
|
|
(def ret (get x lastkey))
|
|
|
|
(varset! lastkey (next x lastkey))
|
|
|
|
ret)
|
|
|
|
})
|
2018-02-07 05:44:51 +00:00
|
|
|
|
2018-03-10 18:34:46 +00:00
|
|
|
# Compile time
|
2018-02-07 05:44:51 +00:00
|
|
|
|
|
|
|
(var *read* nil)
|
|
|
|
(var *onvalue* identity)
|
2018-03-11 19:35:23 +00:00
|
|
|
(var *env* (setproto @{} _env))
|
2018-02-07 05:44:51 +00:00
|
|
|
|
|
|
|
(def require-loading @{})
|
|
|
|
|
2018-03-10 18:34:46 +00:00
|
|
|
(defn onerr [t e]
|
|
|
|
(print (string t " error: " e)))
|
|
|
|
|
2018-03-11 19:35:23 +00:00
|
|
|
(defn char-stream [getchunk]
|
|
|
|
(fiber (fn []
|
2018-02-07 05:44:51 +00:00
|
|
|
(def buf @"")
|
|
|
|
(var len 1)
|
|
|
|
(while (< 0 len)
|
|
|
|
(buffer-clear buf)
|
|
|
|
(getchunk buf)
|
|
|
|
(varset! len (length buf))
|
|
|
|
(for [i 0 len]
|
2018-03-11 19:35:23 +00:00
|
|
|
(yield (get buf i))))
|
|
|
|
0)))
|
2018-02-07 05:44:51 +00:00
|
|
|
|
2018-03-11 19:35:23 +00:00
|
|
|
(defn val-stream [chars ondone]
|
|
|
|
(fiber (fn []
|
2018-02-12 16:49:10 +00:00
|
|
|
(def p (parser 1))
|
2018-03-11 19:35:23 +00:00
|
|
|
(var going true)
|
|
|
|
(while going
|
2018-02-07 05:44:51 +00:00
|
|
|
(def s (parser-status p))
|
|
|
|
(if (= s :full)
|
2018-03-11 19:35:23 +00:00
|
|
|
(yield (parser-produce p))
|
2018-02-07 05:44:51 +00:00
|
|
|
(if (= s :error)
|
|
|
|
(onerr "parse" (parser-error p))
|
2018-03-11 19:35:23 +00:00
|
|
|
(do
|
|
|
|
(def stat (fiber-status chars))
|
|
|
|
(if (or (= :new stat) (= :pending stat))
|
|
|
|
(parser-byte p (resume chars))
|
|
|
|
(varset! going false))))))
|
|
|
|
(ondone))))
|
2018-02-07 05:44:51 +00:00
|
|
|
|
|
|
|
(defn require [path]
|
|
|
|
(when (get require-loading path)
|
2018-03-11 19:35:23 +00:00
|
|
|
(error (string "circular dependency: module " path " is already loading")))
|
2018-02-07 05:44:51 +00:00
|
|
|
(def oldread *read*)
|
|
|
|
(def oldonvalue *onvalue*)
|
2018-03-11 19:35:23 +00:00
|
|
|
(def oldenv *env*)
|
2018-02-07 05:44:51 +00:00
|
|
|
(def f (file-open path))
|
2018-03-11 19:35:23 +00:00
|
|
|
(defn getter [buf] (file-read f 1024 buf) buf)
|
|
|
|
(defn resetter []
|
2018-02-07 05:44:51 +00:00
|
|
|
(put require-loading path nil)
|
|
|
|
(varset! *read* oldread)
|
|
|
|
(varset! *onvalue* oldonvalue)
|
2018-03-11 19:35:23 +00:00
|
|
|
(varset! *env* oldenv)
|
|
|
|
(file-close f)
|
|
|
|
nil)
|
|
|
|
(def cs (char-stream getter))
|
|
|
|
(def vs (val-stream cs resetter))
|
2018-02-07 05:44:51 +00:00
|
|
|
(varset! *onvalue* identity)
|
2018-03-11 19:35:23 +00:00
|
|
|
(varset! *read* (fn [] (resume vs)))
|
|
|
|
(varset! *env* (setproto @{} _env))
|
|
|
|
*env*)
|
2018-02-07 05:44:51 +00:00
|
|
|
|
|
|
|
(defn dorepl []
|
|
|
|
(def oldread *read*)
|
2018-03-11 19:35:23 +00:00
|
|
|
(defn getter [buf]
|
2018-02-07 05:44:51 +00:00
|
|
|
(file-write stdout ">> ")
|
|
|
|
(file-read stdin :line buf))
|
2018-03-11 19:35:23 +00:00
|
|
|
(defn resetter []
|
2018-02-07 05:44:51 +00:00
|
|
|
(varset! *read* oldread)
|
2018-03-11 19:35:23 +00:00
|
|
|
nil)
|
|
|
|
(def cs (char-stream getter))
|
|
|
|
(def vs (val-stream cs resetter))
|
2018-02-07 05:44:51 +00:00
|
|
|
(varset! *onvalue* (fn [ret]
|
2018-03-11 19:35:23 +00:00
|
|
|
(put *env* '_ @{'value ret})
|
2018-02-07 05:44:51 +00:00
|
|
|
(describe ret)))
|
2018-03-11 19:35:23 +00:00
|
|
|
(varset! *read* (fn [] (resume vs))))
|
2018-02-07 05:44:51 +00:00
|
|
|
|
2018-02-07 18:19:34 +00:00
|
|
|
(defn dostring [str]
|
|
|
|
(def oldread *read*)
|
2018-03-11 19:35:23 +00:00
|
|
|
(defn getter [buf]
|
2018-02-07 18:19:34 +00:00
|
|
|
(buffer-push-string buf str)
|
2018-03-11 19:35:23 +00:00
|
|
|
(buffer-push-string buf "\n"))
|
|
|
|
(defn resetter []
|
2018-02-07 18:19:34 +00:00
|
|
|
(varset! *read* oldread)
|
2018-03-11 19:35:23 +00:00
|
|
|
nil)
|
|
|
|
(def cs (char-stream getter))
|
|
|
|
(def vs (val-stream cs resetter))
|
2018-02-07 18:19:34 +00:00
|
|
|
(varset! *onvalue* identity)
|
2018-03-11 19:35:23 +00:00
|
|
|
(varset! *read* (fn [] (resume vs))))
|
2018-02-07 18:19:34 +00:00
|
|
|
|
2018-02-07 05:44:51 +00:00
|
|
|
(defn init-loop []
|
|
|
|
(while *read*
|
|
|
|
(def wrapper (fiber (fn []
|
|
|
|
(while *read*
|
|
|
|
(def source (*read*))
|
2018-03-10 18:34:46 +00:00
|
|
|
(def res (compile source *env*))
|
2018-02-07 05:44:51 +00:00
|
|
|
(if (= (type res) :function)
|
|
|
|
(*onvalue* (res))
|
|
|
|
(onerr "compile" (get res :error)))))))
|
2018-03-11 19:35:23 +00:00
|
|
|
(def eb (resume wrapper))
|
|
|
|
(if (= (fiber-status wrapper) :error) (onerr "runtime" eb wrapper))))
|
2018-02-07 05:44:51 +00:00
|
|
|
|
|
|
|
(defn init-repl [] (dorepl) (init-loop))
|