2018-01-02 20:13:52 +03:00
|
|
|
(defdynamic caar [pair] (car (car pair)))
|
|
|
|
(defdynamic cadr [pair] (car (cdr pair)))
|
|
|
|
(defdynamic cdar [pair] (cdr (car pair)))
|
|
|
|
(defdynamic cddr [pair] (cdr (cdr pair)))
|
|
|
|
(defdynamic caaar [pair] (car (car (car pair))))
|
|
|
|
(defdynamic caadr [pair] (car (car (cdr pair))))
|
|
|
|
(defdynamic cadar [pair] (car (cdr (car pair))))
|
|
|
|
(defdynamic cdaar [pair] (cdr (car (car pair))))
|
|
|
|
(defdynamic caddr [pair] (car (cdr (cdr pair))))
|
|
|
|
(defdynamic cdadr [pair] (cdr (car (cdr pair))))
|
|
|
|
(defdynamic cddar [pair] (cdr (cdr (car pair))))
|
|
|
|
(defdynamic cdddr [pair] (cdr (cdr (cdr pair))))
|
|
|
|
(defdynamic caaaar [pair] (car (car (car (car pair)))))
|
|
|
|
(defdynamic caaadr [pair] (car (car (car (cdr pair)))))
|
|
|
|
(defdynamic caadar [pair] (car (car (cdr (car pair)))))
|
|
|
|
(defdynamic caaddr [pair] (car (car (cdr (cdr pair)))))
|
|
|
|
(defdynamic cadaar [pair] (car (cdr (car (car pair)))))
|
|
|
|
(defdynamic cadadr [pair] (car (cdr (car (cdr pair)))))
|
|
|
|
(defdynamic caddar [pair] (car (cdr (cdr (car pair)))))
|
|
|
|
(defdynamic cadddr [pair] (car (cdr (cdr (cdr pair)))))
|
|
|
|
(defdynamic cdaaar [pair] (cdr (car (car (car pair)))))
|
|
|
|
(defdynamic cdaadr [pair] (cdr (car (car (cdr pair)))))
|
|
|
|
(defdynamic cdadar [pair] (cdr (car (cdr (car pair)))))
|
|
|
|
(defdynamic cdaddr [pair] (cdr (car (cdr (cdr pair)))))
|
|
|
|
(defdynamic cddaar [pair] (cdr (cdr (car (car pair)))))
|
|
|
|
(defdynamic cddadr [pair] (cdr (cdr (car (cdr pair)))))
|
|
|
|
(defdynamic cdddar [pair] (cdr (cdr (cdr (car pair)))))
|
|
|
|
(defdynamic cddddr [pair] (cdr (cdr (cdr (cdr pair)))))
|
|
|
|
|
2017-06-26 12:15:03 +03:00
|
|
|
(defdynamic cond-internal [xs]
|
|
|
|
(if (= (count xs) 0)
|
|
|
|
(list)
|
|
|
|
(if (= (count xs) 2)
|
2017-12-15 19:43:06 +03:00
|
|
|
(macro-error "cond has even number of branches; add an else branch")
|
2017-06-26 12:15:03 +03:00
|
|
|
(if (= (count xs) 1)
|
|
|
|
(car xs)
|
|
|
|
(list
|
2017-10-17 10:02:12 +03:00
|
|
|
'if
|
2017-06-26 12:15:03 +03:00
|
|
|
(car xs)
|
2018-01-02 20:13:52 +03:00
|
|
|
(cadr xs)
|
|
|
|
(cond-internal (cddr xs)))))))
|
2017-06-26 12:15:03 +03:00
|
|
|
|
|
|
|
(defmacro cond [:rest xs]
|
|
|
|
(cond-internal xs))
|
|
|
|
|
2018-01-17 19:08:02 +03:00
|
|
|
(defmacro for [settings :rest body] ;; settings = variable, from, to, <step>
|
2018-01-17 18:09:34 +03:00
|
|
|
(if (> (count body) 1)
|
|
|
|
(macro-error "Warning: the body of the 'for' loop can only contain one expression")
|
|
|
|
(list
|
|
|
|
'let
|
|
|
|
(array (car settings) (cadr settings))
|
|
|
|
(list
|
|
|
|
'while
|
|
|
|
(list 'Int.< (car settings) (caddr settings))
|
|
|
|
(list 'do
|
2018-01-17 19:08:02 +03:00
|
|
|
(if (= (count body) 0)
|
|
|
|
()
|
|
|
|
(if (list? body)
|
|
|
|
(car body)
|
|
|
|
body))
|
2018-01-17 18:09:34 +03:00
|
|
|
(list
|
|
|
|
'set! (list 'ref (car settings))
|
|
|
|
(list 'Int.+
|
|
|
|
(car settings)
|
|
|
|
(if (= 4 (count settings)) ;; optional arg for step
|
|
|
|
(cadddr settings)
|
|
|
|
1))))))))
|
2017-06-29 18:05:34 +03:00
|
|
|
|
|
|
|
(defmacro refstr [x]
|
2017-10-17 10:02:12 +03:00
|
|
|
(list 'ref
|
|
|
|
(list 'str x)))
|
2017-10-13 14:48:40 +03:00
|
|
|
|
2017-10-18 13:25:42 +03:00
|
|
|
(defmacro foreach [f xs]
|
|
|
|
(list 'for ['i 0 (list 'count (list 'ref xs))]
|
|
|
|
(list f (list 'nth (list 'ref xs) 'i))))
|
2017-10-18 23:44:15 +03:00
|
|
|
|
|
|
|
(defdynamic thread-first-internal [xs]
|
|
|
|
(if (= (count xs) 2)
|
|
|
|
(if (list? (last xs))
|
2018-01-02 20:13:52 +03:00
|
|
|
(cons (caadr xs)
|
2017-10-18 23:44:15 +03:00
|
|
|
(cons (car xs)
|
2018-01-02 20:13:52 +03:00
|
|
|
(cdadr xs)))
|
|
|
|
(list (cadr xs) (car xs)))
|
2017-10-18 23:44:15 +03:00
|
|
|
(if (list? (last xs))
|
|
|
|
(append
|
|
|
|
(list
|
|
|
|
(car (last xs))
|
2017-12-01 12:36:14 +03:00
|
|
|
(thread-first-internal (all-but-last xs)))
|
2017-10-18 23:44:15 +03:00
|
|
|
(cdr (last xs)))
|
2017-12-01 12:36:14 +03:00
|
|
|
(list (last xs) (thread-first-internal (all-but-last xs))))))
|
2017-10-18 23:44:15 +03:00
|
|
|
|
|
|
|
(defdynamic thread-last-internal [xs]
|
|
|
|
(if (= (count xs) 2)
|
|
|
|
(if (list? (last xs))
|
|
|
|
(cons-last (car xs) (last xs))
|
2018-01-02 20:13:52 +03:00
|
|
|
(list (cadr xs) (car xs)))
|
2017-10-18 23:44:15 +03:00
|
|
|
(if (list? (last xs))
|
2017-12-01 12:36:14 +03:00
|
|
|
(cons-last (thread-last-internal (all-but-last xs)) (last xs))
|
|
|
|
(list (last xs) (thread-last-internal (all-but-last xs))))))
|
2017-10-18 23:44:15 +03:00
|
|
|
|
|
|
|
(defmacro => [:rest forms]
|
|
|
|
(thread-first-internal forms))
|
|
|
|
|
|
|
|
(defmacro ==> [:rest forms]
|
|
|
|
(thread-last-internal forms))
|
2017-11-29 17:28:21 +03:00
|
|
|
|
|
|
|
(defmacro swap! [x y]
|
|
|
|
(list 'let (array 'tmp y) (list 'do (list 'set! &y x) (list 'set! &x 'tmp))))
|
2017-11-30 15:43:58 +03:00
|
|
|
|
2018-01-02 20:13:52 +03:00
|
|
|
(defmacro update! [x f]
|
|
|
|
(list 'set! &x (list f x)))
|
|
|
|
|
2017-12-07 16:59:55 +03:00
|
|
|
(defmacro mac-only [:rest forms]
|
2017-11-30 15:43:58 +03:00
|
|
|
(if (= "darwin" (os))
|
2017-12-07 16:59:55 +03:00
|
|
|
(cons (quote do) forms)
|
2017-11-30 15:43:58 +03:00
|
|
|
()))
|
2017-12-04 11:29:15 +03:00
|
|
|
|
2017-12-07 16:59:55 +03:00
|
|
|
(defmacro linux-only [:rest forms]
|
2017-12-04 11:29:15 +03:00
|
|
|
(if (= "linux" (os))
|
2017-12-07 16:59:55 +03:00
|
|
|
(cons (quote do) forms)
|
2017-12-04 11:29:15 +03:00
|
|
|
()))
|
|
|
|
|
2017-12-07 16:59:55 +03:00
|
|
|
(defmacro windows-only [:rest forms]
|
2017-12-04 11:29:15 +03:00
|
|
|
(if (= "windows" (os))
|
2017-12-07 16:59:55 +03:00
|
|
|
(cons (quote do) forms)
|
2017-12-04 11:29:15 +03:00
|
|
|
()))
|
2017-12-12 17:08:33 +03:00
|
|
|
|
|
|
|
(defdynamic use-all-fn [names]
|
|
|
|
(if (= (count names) 0)
|
|
|
|
(macro-error "Trying to call use-all without arguments")
|
|
|
|
(if (= (count names) 1)
|
|
|
|
(list (list 'use (car names)))
|
|
|
|
(cons (list 'use (car names)) (use-all-fn (cdr names))))));(use-all (cdr names))))))
|
|
|
|
|
|
|
|
(defmacro use-all [:rest names]
|
|
|
|
(cons 'do (use-all-fn names)))
|
2017-12-12 19:39:15 +03:00
|
|
|
|
|
|
|
(defmacro when [condition form]
|
|
|
|
(list 'if condition form (list)))
|
2017-12-12 20:45:29 +03:00
|
|
|
|
|
|
|
(defmacro unless [condition form]
|
|
|
|
(list 'if condition (list) form))
|
2017-12-14 18:28:06 +03:00
|
|
|
|
2017-12-15 11:28:14 +03:00
|
|
|
(defmacro let-do [bindings :rest forms]
|
2017-12-14 18:28:06 +03:00
|
|
|
(list 'let bindings
|
|
|
|
(cons 'do forms)))
|
|
|
|
|
2017-12-15 11:24:16 +03:00
|
|
|
(defmacro defn-do [name arguments :rest body]
|
2017-12-14 18:28:06 +03:00
|
|
|
(list 'defn name arguments (cons 'do body)))
|
|
|
|
|
|
|
|
(defmacro comment [:rest forms]
|
|
|
|
())
|
|
|
|
|
2017-12-15 11:24:16 +03:00
|
|
|
(defmacro forever-do [:rest forms]
|
2017-12-14 18:28:06 +03:00
|
|
|
(list 'while true (cons 'do forms)))
|
|
|
|
|
|
|
|
(defdynamic case-internal [name xs]
|
|
|
|
(if (= (count xs) 0)
|
|
|
|
(list)
|
|
|
|
(if (= (count xs) 2)
|
2017-12-15 11:24:16 +03:00
|
|
|
(macro-error "case has even number of branches; add an else branch")
|
2017-12-14 18:28:06 +03:00
|
|
|
(if (= (count xs) 1)
|
|
|
|
(car xs)
|
|
|
|
(list 'if
|
|
|
|
(list '= name (car xs))
|
2018-01-02 20:13:52 +03:00
|
|
|
(cadr xs)
|
|
|
|
(case-internal name (cddr xs)))))))
|
2017-12-14 18:28:06 +03:00
|
|
|
|
|
|
|
(defmacro case [name :rest forms]
|
|
|
|
(case-internal name forms))
|
2017-12-29 13:49:51 +03:00
|
|
|
|
|
|
|
(defdynamic build-vararg [fn forms]
|
|
|
|
(if (= (count forms) 0)
|
|
|
|
(macro-error "vararg macro needs at least one argument")
|
|
|
|
(if (= (count forms) 1)
|
|
|
|
(car forms)
|
|
|
|
(list fn (car forms) (build-vararg fn (cdr forms))))))
|
|
|
|
|
|
|
|
(defmacro and* [:rest forms]
|
|
|
|
(build-vararg 'and forms))
|
|
|
|
|
|
|
|
(defmacro or* [:rest forms]
|
|
|
|
(build-vararg 'or forms))
|
|
|
|
|
|
|
|
(defdynamic build-str* [forms]
|
|
|
|
(if (= (count forms) 0)
|
|
|
|
(list "")
|
|
|
|
(if (= (count forms) 1)
|
|
|
|
(list 'str (car forms))
|
|
|
|
(list 'String.append (list 'str (car forms)) (build-str* (cdr forms))))))
|
|
|
|
|
|
|
|
(defmacro str* [:rest forms]
|
|
|
|
(build-str* forms))
|
|
|
|
|
|
|
|
(defmacro println* [:rest forms]
|
|
|
|
(list 'IO.println (list 'ref (build-str* forms))))
|