3 (define-macro (cond . a)
4 (if (null? a) '(if #f #f)
5 (cond ((eq? (caar a) 'else) `(begin . ,(cdar a)))
6 ((and (not (null? (cdar a))) (eq? (cadar a) '=>))
9 (if ,x (,(caddar a) ,x) (cond . ,(cdr a))))))
10 (else `(if ,(caar a) (begin . ,(cdar a)) (cond . ,(cdr a)))))))
12 (define-macro (case a . cs)
15 (cond . ,(map (lambda (c)
16 (if (eq? (car c) 'else) c
17 `((memq ,x ',(car c)) . ,(cdr c))))
27 (#%+-aux (#%+ x (#%car rest)) (#%cdr rest))
33 (#%+-aux (#%+ x (#%car rest)) (#%cdr rest))
39 (#%--aux (#%- x (#%car rest)) (#%cdr rest))
45 (#%--aux (#%- x (#%car rest)) (#%cdr rest))
51 (#%*-aux (#%* x (#%car rest)) (#%cdr rest))
57 (#%*-aux (#%* x (#%car rest)) (#%cdr rest))
78 (or (#%< x y) (#%= x y))))
86 (or (#%> x y) (#%= x y))))
129 (#%length-aux lst 0)))
134 (#%length-aux (cdr lst) (#%+ n 1))
140 (#%cons (#%car lst1) (append (#%cdr lst1) lst2))
145 (reverse-aux lst '())))
150 (reverse-aux (#%cdr lst) (#%cons (#%car lst) rev))
157 (list-ref (#%cdr lst) (#%- i 1)))))
163 (list-set! (#%cdr lst) (#%- i 1) x))))
175 (if (#%< x 0) (#%neg x) x)))
181 (define #%box (lambda (a) (#%cons a '())))
183 (define #%unbox (lambda (a) (#%car a)))
185 (define #%box-set! (lambda (a b) (#%set-car! a b)))
197 (#%list->string chars)))
201 (#%string->list str)))
205 (#%list->string chars)))
207 (define string-length
209 (length (#%string->list str))))
211 (define string-append
213 (#%list->string (append (#%string->list str1) (#%string->list str2)))))
216 (lambda (str start end)
219 (#%substring-aux1 (#%string->list str) start)
222 (define #%substring-aux1
225 (#%substring-aux1 (#%cdr lst) (#%- n 1))
228 (define #%substring-aux2
231 (#%cons (#%car lst) (#%substring-aux2 (#%cdr lst) (#%- n 1)))
241 (#%cons (f (#%car lst))
250 (for-each f (#%cdr lst)))
255 (let ((k (#%get-cont)))
258 (#%return-to-cont k r))))))
263 (define start-first-process
265 (set! root-k (#%get-cont))
266 (set! readyq (#%cons #f #f))
267 (#%set-cdr! readyq readyq)
272 (let* ((k (#%get-cont))
273 (next (#%cons k (#%cdr readyq))))
274 (#%set-cdr! readyq next)
275 (#%graft-to-cont root-k thunk))))
279 (let ((next (#%cdr readyq)))
280 (if (#%eq? next readyq)
283 (#%set-cdr! readyq (#%cdr next))
284 (#%return-to-cont (#%car next) #f))))))
288 (let ((k (#%get-cont)))
289 (#%set-car! readyq k)
290 (set! readyq (#%cdr readyq))
291 (let ((next-k (#%car readyq)))
292 (#%set-car! readyq #f)
293 (#%return-to-cont next-k #f)))))
300 (lambda (freq-div duration)
301 (#%beep freq-div duration)))
321 (or (#%getchar-wait 0 3)
326 (#%getchar-wait duration 3)))
330 (#%sleep-aux (#%+ (#%clock) duration))))
334 (if (#%< (#%clock) wake-up)
335 (#%sleep-aux wake-up)
344 (lambda (id duty period)
345 (#%led id duty period)))
349 (if (#%eq? state 'red)
356 (for-each putchar (#%string->list x))
362 (begin (#%putchar #\" 3)
366 (display (number->string x)))
368 (begin (#%putchar #\( 3)
370 (#%write-list (#%cdr x))))
372 (display "#<symbol>"))
374 (display (if x "#t" "#f")))
376 (display "#<object>")))))
377 ;; TODO have vectors and co ?
384 (begin (#%putchar #\space 3)
386 (#%write-list (#%cdr lst))))
388 (begin (display " . ")
390 (#%putchar #\) 3))))))
392 (define number->string
396 (#%cons #\- (#%number->string-aux (#%neg n) '()))
397 (#%number->string-aux n '())))))
399 (define #%number->string-aux
401 (let ((rest (#%cons (#%+ #\0 (#%remainder n 10)) lst)))
404 (#%number->string-aux (#%quotient n 10) rest)))))
409 (#%putchar #\newline 3)))
425 (#%car (#%car (#%car p)))))
428 (#%car (#%car (#%cdr p)))))
431 (#%car (#%cdr (#%car p)))))
434 (#%car (#%cdr (#%cdr p)))))
437 (#%cdr (#%car (#%car p)))))
440 (#%cdr (#%car (#%cdr p)))))
443 (#%cdr (#%cdr (#%car p)))))
446 (#%cdr (#%cdr (#%cdr p)))))
452 ((and (#%pair? x) (#%pair? y))
453 (and (equal? (#%car x) (#%car y))
454 (equal? (#%cdr x) (#%cdr y))))
455 ((and (#%u8vector? x) (#%u8vector? y))
456 (u8vector-equal? x y))
460 (define u8vector-equal?
462 (let ((lx (#%u8vector-length x)))
463 (if (#%= lx (#%u8vector-length y))
464 (u8vector-equal?-loop x y (- lx 1))
466 (define u8vector-equal?-loop
470 (and (#%= (#%u8vector-ref x l) (#%u8vector-ref y l))
471 (u8vector-equal?-loop x y (#%- l 1))))))
480 (assoc t (#%cdr l))))))
489 (memq t (#%cdr l))))))
492 (define vector-ref list-ref)
493 (define vector-set! list-set!)
495 (define bitwise-ior (lambda (x y) (#%ior x y)))
496 (define bitwise-xor (lambda (x y) (#%xor x y)))
497 ;; TODO add bitwise-and ? bitwise-not ?
499 (define current-time (lambda () (#%clock)))
500 (define time->seconds (lambda (t) (#%quotient t 100)))
505 (define list->u8vector
507 (let* ((n (length x))
508 (v (#%make-u8vector n 0)))
509 (list->u8vector-loop v 0 x)
511 (define list->u8vector-loop
513 (#%u8vector-set! v n (#%car x))
514 (if (#%not (#%null? (#%cdr x)))
515 (list->u8vector-loop v (#%+ n 1) (#%cdr x)))))
516 (define u8vector-length (lambda (x) (#%u8vector-length x)))
517 (define u8vector-ref (lambda (x y) (#%u8vector-ref x y)))
518 (define u8vector-set! (lambda (x y z) (#%u8vector-set! x y z)))
519 (define make-u8vector
521 (#%make-u8vector n x)))
522 (define u8vector-copy!
523 (lambda (source source-start target target-start n)
524 (#%u8vector-copy! source source-start target target-start n)))
526 (define network-init (lambda () (#%network-init)))
527 (define network-cleanup (lambda () (#%network-cleanup)))
528 (define receive-packet-to-u8vector
530 (#%receive-packet-to-u8vector x)))
531 (define send-packet-from-u8vector
533 (#%send-packet-from-u8vector x y)))