Add some builtin functions and some examples.

This commit is contained in:
Calvin Rose
2018-03-13 16:40:56 -04:00
parent 2b1dd79f55
commit 9cb7c92ca7
7 changed files with 83 additions and 54 deletions
+32 -12
View File
@@ -4,7 +4,7 @@
"A var that points to the current environment."
_env)
(def defn macro
(def defn :macro
"Define a function"
(fn [name & more]
(def fstart (fn recur [i]
@@ -18,19 +18,19 @@
(def formargs (array-concat ['def name] (array-slice more 0 start) [fnbody]))
(apply tuple formargs)))
(def defmacro macro
(def defmacro :macro
"Define a macro."
(do
(def defn* (get (get _env 'defn) 'value))
(fn [name & more]
(def args (array-concat [] name 'macro more))
(def args (array-concat [] name :macro more))
(apply defn* args))))
(defmacro defn-
"Define a private function that will not be exported."
[name & more]
(apply tuple (array-concat
['defn name 'private] more)))
['defn name :private] more)))
(defmacro comment
"Ignores the body of the comment."
@@ -143,8 +143,28 @@ If no match is found, returns nil"
(reduce f (f start (next)) s)
start))
(defn filter [pred s]
(def s (seq s))
(def {:more more :next next} s)
(var alive true)
(var temp nil)
(var isnew true)
(defn nextgood []
(if alive
(if (more)
(do
(def n (next))
(if (pred n) n (nextgood)))
(varset! alive false))))
(defn nnext [] (def ret temp) (varset! temp (nextgood)) ret)
(defn nmore [] (when isnew (varset! isnew false) (nnext)) alive)
{:more nmore :next nnext})
(defn even? [x] (== 0 (% x 2)))
(defn odd? [x] (== 1 (% x 2)))
(defn nil? [x] (= x nil))
(defn zero? [x] (== x 0))
(defn one? [x] (== x 1))
(defn inc [x] (+ x 1))
(defn dec [x] (- x 1))
@@ -206,12 +226,6 @@ If no match is found, returns nil"
(tuple-prepend body 'do)
(tuple 'varset! sym (tuple '+ sym inc)))))
(defn make-env [parent]
(def parent (if parent parent _env))
(def newenv (setproto @{} parent))
newenv)
(put _env '_env nil)
(defmacro ->
[x & forms]
(defn fop [last nextform]
@@ -230,10 +244,16 @@ If no match is found, returns nil"
(def [h t] (if (= :tuple (type n))
[(get n 0) (array-slice n 1)]
[n []]))
(def parts (array-concat [h] t last))
(def parts (array-concat [h] t [last]))
(apply tuple parts))
(reduce fop x forms))
(defn make-env [parent]
(def parent (if parent parent _env))
(def newenv (setproto @{} parent))
newenv)
(put _env '_env nil)
(def run-context
"Run a context. This evaluates expressions of dst in an environment,
and is encapsulates the parsing, compilation, and evaluation of dst.
@@ -324,7 +344,7 @@ onvalue."
:prefix prefix
} (apply table args))
(defn one [[k v]]
(when (not (get v 'private))
(when (not (get v :private))
(put *env* (symbol (if prefix prefix "") k) v)))
(doseq (map one (pairs env))))
+1 -1
View File
@@ -817,7 +817,7 @@ recur:
Dst entry = dst_table_get(env, headval);
for (;;) {
if (dst_checktype(entry, DST_NIL)) break;
if (dst_checktype(dst_get(entry, dst_csymbolv("macro")), DST_NIL)) break;
if (dst_checktype(dst_get(entry, dst_csymbolv(":macro")), DST_NIL)) break;
fn = dst_get(entry, dst_csymbolv("value"));
if (!dst_checktype(fn, DST_FUNCTION)) break;
if (macrorecur++ > DST_RECURSION_GUARD) {