mu/shell/data.limg

200 lines
5.0 KiB
Plaintext
Raw Normal View History

2021-04-24 04:36:48 +00:00
(
(globals . (
(mac . [define mac
(litmac litfn () ((m . params) . body)
`(define ,m
(litmac litfn () ,params ,@body)))])
2021-06-21 04:29:33 +00:00
(def . [mac (def (name . params) . body)
`(define ,name (fn ,params ,@body))])
(do . [mac (do . body)
`((fn () ,@body))])
2021-06-06 23:39:07 +00:00
(let . [(mac (let var val . body)
`((fn (,var) ,@body) ,val))])
2021-06-21 04:29:33 +00:00
(when . [mac (when cond . body)
`(if ,cond (do ,@body) ())])
(iflet . [mac (iflet var expr then else)
2021-06-06 23:41:37 +00:00
`(let ,var ,expr
2021-06-21 04:29:33 +00:00
(if ,var ,then ,else))])
(aif . [mac (aif expr then else)
`(iflet it ,expr ,then ,else)])
(list . [def (list . args)
2021-06-06 23:40:35 +00:00
# we should probably make a copy here
2021-06-21 04:29:33 +00:00
args])
(ret . [mac (ret var val . body)
`(let ,var ,val ,@body ,var)])
2021-06-21 04:29:33 +00:00
(len . [def (len l)
if (no l)
0
(1 + (len (cdr l)))])
2021-06-21 04:29:33 +00:00
(nth . [def (nth n xs)
if (n < 1)
2021-06-07 05:14:24 +00:00
(car xs)
(nth n-1 (cdr xs))])
2021-06-21 04:29:33 +00:00
(map1 . [def (map1 f xs)
if (no xs)
()
(cons (f (car xs))
2021-06-21 04:29:33 +00:00
(map1 f (cdr xs)))])
(compose . [def (compose f g)
2021-06-06 23:41:37 +00:00
(fn args
2021-06-21 05:24:03 +00:00
(f (apply g args)))])
(caar . [define caar (compose car car)])
(cadr . [define cadr (compose car cdr)])
(cddr . [define cddr (compose cdr cdr)])
(cdar . [define cdar (compose cdr car)])
2021-06-21 04:29:33 +00:00
(some . [def (some f xs)
if (no xs)
2021-06-06 23:41:37 +00:00
()
2021-06-21 04:29:33 +00:00
if (f (car xs))
2021-06-06 23:41:37 +00:00
xs
2021-06-21 04:29:33 +00:00
(some f (cdr xs))])
2021-06-21 05:45:13 +00:00
(any . [define any some])
2021-06-21 04:29:33 +00:00
(all . [def (all f xs)
if (no xs)
2021-06-06 23:41:37 +00:00
1
2021-06-21 04:29:33 +00:00
if (f (car xs))
2021-06-06 23:41:37 +00:00
(all f (cdr xs))
2021-06-21 04:29:33 +00:00
()])
(find . [def (find x xs)
if (no xs)
2021-06-06 23:41:37 +00:00
()
if (x = (car xs))
2021-06-06 23:41:37 +00:00
1
2021-06-21 04:29:33 +00:00
(find x (cdr xs))])
2021-06-21 05:24:03 +00:00
(pair . [def (pair xs)
if (no xs)
()
if (no (cdr xs))
(list (list (car xs)))
(cons (list (car xs) (cadr xs))
(pair (cddr xs)))])
2021-06-21 06:09:50 +00:00
(with . [mac (with bindings . body)
`((fn ,(map1 car (pair bindings))
2021-06-21 05:24:03 +00:00
,@body)
2021-06-21 06:09:50 +00:00
,@(map1 cadr (pair bindings)))])
2021-06-21 04:29:33 +00:00
(afn . [mac (afn params . body)
2021-06-06 23:41:37 +00:00
`(let self ()
2021-06-21 04:29:33 +00:00
(set self (fn ,params ,@body)))])
(seq . [def (seq n)
2021-06-07 02:03:41 +00:00
((afn (i)
(if (i > n)
2021-06-07 02:03:41 +00:00
()
(cons i (self i+1))))
2021-06-21 04:29:33 +00:00
1)])
(each . [mac (each x xs . body)
`(walk ,xs (fn (,x) ,@body))])
(walk . [def (walk xs f)
when xs
2021-06-07 02:03:41 +00:00
(f (car xs))
2021-06-21 04:29:33 +00:00
(walk (cdr xs) f)])
(rem . [def (rem f xs)
if (no xs)
2021-06-06 23:41:37 +00:00
()
2021-06-21 04:29:33 +00:00
let rest (rem f (cdr xs))
if (f (car xs))
2021-06-06 23:41:37 +00:00
rest
2021-06-21 04:29:33 +00:00
(cons (car xs) rest)])
(keep . [def (keep f xs)
if (no xs)
2021-06-06 23:41:37 +00:00
()
2021-06-21 04:29:33 +00:00
let rest (keep f (cdr xs))
if (f (car xs))
2021-06-06 23:41:37 +00:00
(cons (car xs) rest)
2021-06-21 04:29:33 +00:00
rest])
(++ . [mac (++ var) `(set ,var (,var + 1))])
(+= . [mac (var += inc)
`(set ,var (,var + ,inc))])
2021-06-21 04:29:33 +00:00
(for . [mac (for var init test update . body)
2021-06-06 00:49:03 +00:00
`(let ,var ,init
(while ,test
,@body
2021-06-21 04:29:33 +00:00
,update))])
(hline1 . [def (hline1 screen y x xmax color)
while (x < xmax)
2021-05-07 17:14:51 +00:00
(pixel screen x y color)
2021-06-23 07:59:14 +00:00
++x])
2021-06-21 04:29:33 +00:00
(vline1 . [def (vline1 screen x y ymax color)
while (y < ymax)
2021-05-07 17:14:51 +00:00
(pixel screen x y color)
2021-06-23 07:59:14 +00:00
++y])
(hline . [def (hline scr y color)
(hline1 scr y 0 (width scr) color)])
(vline . [def (vline scr x color)
(vline1 scr x 0 (height scr) color)])
2021-06-21 04:29:33 +00:00
(line . [def (line screen x0 y0 x1 y1 color)
2021-06-21 05:33:05 +00:00
with (x x0
y y0
dx (abs x1-x0)
dy (0 - (abs y1-y0))
sx (sgn x1-x0)
sy (sgn y1-y0))
let err dx+dy
while (not (and (x = x1)
(y = y1)))
2021-06-21 05:33:05 +00:00
(pixel screen x y color)
let e2 err*2
when (e2 >= dy)
x += sx
when (e2 <= dx)
y += sy
err +=
(+ (if (e2 >= dy)
2021-06-21 05:33:05 +00:00
dy
0)
(if (e2 <= dx)
2021-06-21 05:33:05 +00:00
dx
0))])
2021-06-21 04:29:33 +00:00
(read_line . [def (read_line keyboard)
ret str (stream)
let c (key keyboard)
while (not (or (c = 0) (c = 10)))
(write str c)
(set c (key keyboard))])
2021-06-21 04:29:33 +00:00
(wait . [def (wait keyboard)
while (= 0 (key keyboard))
()])
(sq . [def (sq n) (n * n)])
(cube . [def (cube n) (n * n * n)])
2021-06-21 04:29:33 +00:00
(fill_rect . [def (fill_rect screen x1 y1 x2 y2 color)
2021-06-23 07:59:14 +00:00
for y y1 (y < y2) ++y
2021-06-21 04:29:33 +00:00
(hline1 screen y x1 x2 color)])
(circle . [def (circle scr cx cy r clr)
with (x (0 - r)
2021-06-21 05:33:05 +00:00
y 0
2021-06-23 08:01:42 +00:00
err (2 - r*2)
2021-06-21 05:33:05 +00:00
continue 1)
while continue
(pixel scr cx-x cy+y clr)
(pixel scr cx-y cy-x clr)
(pixel scr cx+x cy-y clr)
(pixel scr cx+y cy+x clr)
2021-06-21 05:33:05 +00:00
(set r err)
when (r <= y)
2021-06-23 07:59:14 +00:00
++y
2021-06-23 08:01:42 +00:00
err += (y*2 + 1)
when (or (r > x) (err > y))
2021-06-23 07:59:14 +00:00
++x
2021-06-23 08:01:42 +00:00
err += (x*2 + 1)
set continue (x < 0)])
2021-06-21 04:29:33 +00:00
(ring . [def (ring screen cx cy r0 w clr)
2021-06-23 07:59:14 +00:00
for r r0 (r < (r0 + w)) ++r
2021-06-21 04:29:33 +00:00
(circle screen cx cy r clr)])
(Greys . [define Greys
(map1 (fn(n) (n + 15))
2021-06-21 04:29:33 +00:00
(seq 16))])
(Pinks . [define Pinks '(84 85 59 60 61
13 36 37 5 108)])
2021-06-21 04:29:33 +00:00
(palette . [def (palette p i)
(nth (i % (len p)) p)])
2021-06-21 04:29:33 +00:00
(pat . [def (pat screen)
with (w (width screen)
h (height screen))
2021-06-23 07:59:14 +00:00
for y 0 (y < h) ++y
for x 0 (x < w) ++x
(pixel screen x y x*y)])
2021-06-21 04:29:33 +00:00
(main . [def (main screen keyboard)
(pat screen)])
2021-04-24 04:36:48 +00:00
))
(sandbox . [circle screen 35 35 14 3])
2021-04-24 04:36:48 +00:00
)