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))))
33 (#%+-aux (#%+ x (#%car rest)) (#%cdr rest))
49 (#%--aux (#%- x (#%car rest)) (#%cdr rest))
61 (#%*-aux (#%mul x (#%car rest)) (#%cdr rest))
66 (let* ((x-neg? (< x 0))
68 (x (if x-neg? (neg x) x))
69 (y (if y-neg? (neg y) y)))
70 (let ((prod (#%mul-non-neg x y)))
71 (cond ((and x-neg? y-neg?)
80 (define quotient ;; TODO similar to #%mul, abstract ?
82 (let* ((x-neg? (< x 0))
84 (x (if x-neg? (neg x) x))
85 (y (if y-neg? (neg y) y)))
86 (let ((quot (#%quotient x y)))
87 (cond ((and x-neg? y-neg?)
108 (or (< x y) (= x y))))
116 (or (> x y) (= x y))))
159 (#%length-aux lst 0)))
164 (#%length-aux (cdr lst) (#%+ n 1))
170 (#%cons (#%car lst1) (append (#%cdr lst1) lst2))
175 (reverse-aux lst '())))
180 (reverse-aux (#%cdr lst) (#%cons (#%car lst) rev))
187 (list-ref (#%cdr lst) (#%- i 1)))))
193 (list-set! (#%cdr lst) (#%- i 1) x))))
205 (if (#%< x 0) (neg x) x)))
211 (define #%box (lambda (a) (#%cons a '())))
213 (define #%unbox (lambda (a) (#%car a)))
215 (define #%box-set! (lambda (a b) (#%set-car! a b)))
227 (#%list->string chars)))
231 (#%string->list str)))
235 (#%list->string chars)))
237 (define string-length
239 (length (#%string->list str))))
241 (define string-append
243 (#%list->string (append (#%string->list str1) (#%string->list str2)))))
246 (lambda (str start end)
249 (#%substring-aux1 (#%string->list str) start)
252 (define #%substring-aux1
255 (#%substring-aux1 (#%cdr lst) (#%- n 1))
258 (define #%substring-aux2
261 (#%cons (#%car lst) (#%substring-aux2 (#%cdr lst) (#%- n 1)))
271 (#%cons (f (#%car lst))
280 (for-each f (#%cdr lst)))
285 (let ((k (#%get-cont)))
288 (#%return-to-cont k r))))))
293 (define start-first-process
295 (set! root-k (#%get-cont))
296 (set! readyq (#%cons #f #f))
297 (#%set-cdr! readyq readyq)
302 (let* ((k (#%get-cont))
303 (next (#%cons k (#%cdr readyq))))
304 (#%set-cdr! readyq next)
305 (#%graft-to-cont root-k thunk))))
309 (let ((next (#%cdr readyq)))
310 (if (#%eq? next readyq)
313 (#%set-cdr! readyq (#%cdr next))
314 (#%return-to-cont (#%car next) #f))))))
318 (let ((k (#%get-cont)))
319 (#%set-car! readyq k)
320 (set! readyq (#%cdr readyq))
321 (let ((next-k (#%car readyq)))
322 (#%set-car! readyq #f)
323 (#%return-to-cont next-k #f)))))
330 (lambda (freq-div duration)
331 (#%beep freq-div duration)))
351 (or (#%getchar-wait 0 3)
356 (#%getchar-wait duration 3)))
360 (#%sleep-aux (#%+ (#%clock) duration))))
364 (if (#%< (#%clock) wake-up)
365 (#%sleep-aux wake-up)
374 (lambda (id duty period)
375 (#%led id duty period)))
379 (if (#%eq? state 'red)
386 (for-each putchar (#%string->list x))
392 (begin (#%putchar #\" 3)
396 (display (number->string x)))
398 (begin (#%putchar #\( 3)
400 (#%write-list (#%cdr x))))
402 (display "#<symbol>"))
404 (display (if x "#t" "#f")))
406 (display "#<object>")))))
407 ;; TODO have vectors and co ?
414 (begin (#%putchar #\space 3)
416 (#%write-list (#%cdr lst))))
418 (begin (display " . ")
420 (#%putchar #\) 3))))))
422 (define number->string
426 (#%cons #\- (#%number->string-aux (neg n) '()))
427 (#%number->string-aux n '())))))
429 (define #%number->string-aux
431 (let ((rest (#%cons (#%+ #\0 (remainder n 10)) lst)))
434 (#%number->string-aux (quotient n 10) rest)))))
439 (#%putchar #\newline 3)))
455 (#%car (#%car (#%car p)))))
458 (#%car (#%car (#%cdr p)))))
461 (#%car (#%cdr (#%car p)))))
464 (#%car (#%cdr (#%cdr p)))))
467 (#%cdr (#%car (#%car p)))))
470 (#%cdr (#%car (#%cdr p)))))
473 (#%cdr (#%cdr (#%car p)))))
476 (#%cdr (#%cdr (#%cdr p)))))
482 ((and (#%pair? x) (#%pair? y))
483 (and (equal? (#%car x) (#%car y))
484 (equal? (#%cdr x) (#%cdr y))))
485 ((and (#%u8vector? x) (#%u8vector? y))
486 (u8vector-equal? x y))
490 (define u8vector-equal?
492 (let ((lx (#%u8vector-length x)))
493 (if (#%= lx (#%u8vector-length y))
494 (u8vector-equal?-loop x y (- lx 1))
496 (define u8vector-equal?-loop
500 (and (#%= (#%u8vector-ref x l) (#%u8vector-ref y l))
501 (u8vector-equal?-loop x y (#%- l 1))))))
510 (assoc t (#%cdr l))))))
519 (memq t (#%cdr l))))))
522 (define vector-ref list-ref)
523 (define vector-set! list-set!)
525 (define bitwise-ior (lambda (x y) (#%ior x y)))
526 (define bitwise-xor (lambda (x y) (#%xor x y)))
527 ;; TODO add bitwise-and ? bitwise-not ?
529 (define current-time (lambda () (#%clock)))
530 (define time->seconds (lambda (t) (quotient t 100)))
535 (define list->u8vector
537 (let* ((n (length x))
538 (v (#%make-u8vector n)))
539 (list->u8vector-loop v 0 x)
541 (define list->u8vector-loop
543 (#%u8vector-set! v n (#%car x))
544 (if (#%not (#%null? (#%cdr x)))
545 (list->u8vector-loop v (#%+ n 1) (#%cdr x)))))
546 (define u8vector-length (lambda (x) (#%u8vector-length x)))
547 (define u8vector-ref (lambda (x y) (#%u8vector-ref x y)))
548 (define u8vector-set! (lambda (x y z) (#%u8vector-set! x y z)))
549 (define make-u8vector
551 (make-u8vector-loop (#%make-u8vector n) (- n 1) x)))
552 (define make-u8vector-loop
555 (begin (u8vector-set! v n x)
556 (make-u8vector-loop v (- n 1) x))
558 (define u8vector-copy!
559 (lambda (source source-start target target-start n)
561 (begin (u8vector-set! target target-start
562 (u8vector-ref source source-start))
563 (u8vector-copy! source (+ source-start 1)
564 target (+ target-start 1)
567 (define network-init (lambda () (#%network-init)))
568 (define network-cleanup (lambda () (#%network-cleanup)))
569 (define receive-packet-to-u8vector
571 (#%receive-packet-to-u8vector x)))
572 (define send-packet-from-u8vector
574 (#%send-packet-from-u8vector x y)))