2017-06-26 12:15:03 +03:00
|
|
|
(defdynamic cond-internal [xs]
|
|
|
|
(if (= (count xs) 0)
|
|
|
|
(list)
|
|
|
|
(if (= (count xs) 2)
|
|
|
|
(list)
|
|
|
|
(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)
|
|
|
|
(car (cdr xs))
|
|
|
|
(cond-internal (cdr (cdr xs))))))))
|
|
|
|
|
|
|
|
(defmacro cond [:rest xs]
|
|
|
|
(cond-internal xs))
|
|
|
|
|
|
|
|
(defmacro for [settings body] ;; settings = variable, from, to, <step>
|
|
|
|
(list
|
2017-10-17 10:02:12 +03:00
|
|
|
'let
|
2017-06-26 12:15:03 +03:00
|
|
|
(array (car settings) (car (cdr settings)))
|
|
|
|
(list
|
2017-10-17 10:02:12 +03:00
|
|
|
'while
|
|
|
|
(list 'Int.< (car settings) (car (cdr (cdr settings))))
|
|
|
|
(list 'do
|
2017-06-26 12:15:03 +03:00
|
|
|
body
|
|
|
|
(list
|
2017-10-17 10:02:12 +03:00
|
|
|
'set! (list 'ref (car settings))
|
|
|
|
(list 'Int.+
|
2017-06-26 12:15:03 +03:00
|
|
|
(car settings)
|
|
|
|
(if (= 4 (count settings)) ;; optional arg for step
|
|
|
|
(car (cdr (cdr (cdr 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))
|
|
|
|
(cons (car (car (cdr xs)))
|
|
|
|
(cons (car xs)
|
|
|
|
(cdr (car (cdr xs)))))
|
|
|
|
(list (car (cdr xs)) (car xs)))
|
|
|
|
(if (list? (last xs))
|
|
|
|
(append
|
|
|
|
(list
|
|
|
|
(car (last xs))
|
|
|
|
(thread-first-internal (init xs)))
|
|
|
|
(cdr (last xs)))
|
|
|
|
(list (last xs) (thread-first-internal (init xs))))))
|
|
|
|
|
|
|
|
(defdynamic thread-last-internal [xs]
|
|
|
|
(if (= (count xs) 2)
|
|
|
|
(if (list? (last xs))
|
|
|
|
(cons-last (car xs) (last xs))
|
|
|
|
(list (car (cdr xs)) (car xs)))
|
|
|
|
(if (list? (last xs))
|
|
|
|
(cons-last (thread-last-internal (init xs)) (last xs))
|
|
|
|
(list (last xs) (thread-last-internal (init xs))))))
|
|
|
|
|
|
|
|
(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))))
|