mu/shell/data.limg

197 lines
5.0 KiB
Plaintext
Raw Normal View History

2021-04-24 04:36:48 +00:00
(
(globals . (
2021-06-21 04:29:33 +00:00
(mac . [define mac (litmac litfn () ((name . params) . body)
`(define ,name (litmac litfn () ,params ,@body)))])
(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
2021-06-21 04:29:33 +00:00
(+ 1 (len (cdr l)))])
(nth . [def (nth n xs)
if (< n 1)
2021-06-07 05:14:24 +00:00
(car xs)
2021-06-21 04:29:33 +00:00
(nth (- n 1) (cdr xs))])
(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)))])
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-06 23:41:37 +00:00
(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
()
2021-06-21 04:29:33 +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) (car (cdr xs)))
(pair (cdr (cdr xs))))])
(with . [mac (with vars_vals . body)
`((fn ,(map1 car (pair vars_vals))
,@body)
,@(map1 (compose car cdr) (pair vars_vals)))])
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)
()
(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))])
(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-21 04:29:33 +00:00
(++ x)])
(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-21 04:29:33 +00:00
(++ y)])
(hline . [def (hline screen y color)
(hline1 screen y 0 (width screen) color)])
(vline . [def (vline screen x color)
(vline1 screen x 0 (height screen) color)])
(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)))
(pixel screen x y color)
let e2 (* err 2)
when (>= e2 dy)
(+= x sx)
when (<= e2 dx)
(+= y sy)
(+= err
(+ (if (>= e2 dy)
dy
0)
(if (<= e2 dx)
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)])
(fill_rect . [def (fill_rect screen x1 y1 x2 y2 color)
for y y1 (< y y2) (++ y)
(hline1 screen y x1 x2 color)])
(circle . [def (circle screen cx cy r clr)
2021-06-21 05:33:05 +00:00
with (x (- 0 r)
y 0
err (- 2 (* 2 r))
continue 1)
while continue
(pixel screen (- cx x) (+ cy y) clr)
(pixel screen (- cx y) (- cy x) clr)
(pixel screen (+ cx x) (- cy y) clr)
(pixel screen (+ cx y) (+ cy x) clr)
(set r err)
when (<= r y)
(++ y)
(+= err
(+ 1 (* 2 y)))
when (or (> r x) (> err y))
(++ x)
(+= err
(+ 1 (* 2 x)))
(set continue (< x 0))])
2021-06-21 04:29:33 +00:00
(ring . [def (ring screen cx cy r0 w clr)
for r r0 (< r (+ r0 w)) (++ r)
(circle screen cx cy r clr)])
(Greys . [define Greys
2021-06-07 05:38:56 +00:00
(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)])
(palette . [def (palette p i)
(nth (% i (len p)) p)])
(pat . [def (pat screen)
2021-06-12 01:20:30 +00:00
(let w (width screen)
(let h (height screen)
(for y 0 (< y h) (++ y)
(for x 0 (< x w) (++ x)
(pixel screen x y
2021-06-21 04:29:33 +00:00
(* x y))))))])
(main . [def (main screen keyboard)
(pat screen)])
2021-04-24 04:36:48 +00:00
))
2021-06-21 05:41:36 +00:00
(sandbox . [(pat screen)])
2021-04-24 04:36:48 +00:00
)