10 (#%+-aux (#%+ x (#%car rest)) (#%cdr rest))
16 (#%+-aux (#%+ x (#%car rest)) (#%cdr rest))
22 (#%--aux (#%- x (#%car rest)) (#%cdr rest))
28 (#%--aux (#%- x (#%car rest)) (#%cdr rest))
34 (#%*-aux (#%* x (#%car rest)) (#%cdr rest))
40 (#%*-aux (#%* x (#%car rest)) (#%cdr rest))
61 (or (#%< x y) (#%= x y))))
69 (or (#%> x y) (#%= x y))))
112 (#%length-aux lst 0)))
117 (#%length-aux (cdr lst) (#%+ n 1))
123 (#%cons (#%car lst1) (append (#%cdr lst1) lst2))
128 (reverse-aux lst '())))
133 (reverse-aux (#%cdr lst) (#%cons (#%car lst) rev))
140 (list-ref (#%cdr lst) (#%- i 1)))))
146 (list-set! (#%cdr lst) (#%- i 1) x))))
158 (if (#%< x 0) (#%neg x) x)))
166 (#%list->string chars)))
170 (#%string->list str)))
174 (#%list->string chars)))
176 (define string-length ;; TODO are all these string operations efficient ? they all convert to lists. use true vectors when we have them ?
178 (length (#%string->list str))))
180 (define string-append
182 (#%list->string (append (#%string->list str1) (#%string->list str2)))))
185 (lambda (str start end)
188 (#%substring-aux1 (#%string->list str) start)
191 (define #%substring-aux1
193 (if (>= n 1) ;; TODO had an off-by-one
194 (#%substring-aux1 (#%cdr lst) (#%- n 1))
197 (define #%substring-aux2
200 (#%cons (#%car lst) (#%substring-aux2 (#%cdr lst) (#%- n 1)))
206 (#%cons (f (#%car lst))
215 (for-each f (#%cdr lst)))
220 (let ((k (#%get-cont)))
223 (#%return-to-cont k r))))))
228 (define start-first-process
230 (set! root-k (#%get-cont))
231 (set! readyq (#%cons #f #f))
232 (#%set-cdr! readyq readyq)
237 (let* ((k (#%get-cont))
238 (next (#%cons k (#%cdr readyq))))
239 (#%set-cdr! readyq next)
240 (#%graft-to-cont root-k thunk))))
244 (let ((next (#%cdr readyq)))
245 (if (#%eq? next readyq)
248 (#%set-cdr! readyq (#%cdr next))
249 (#%return-to-cont (#%car next) #f))))))
253 (let ((k (#%get-cont)))
254 (#%set-car! readyq k)
255 (set! readyq (#%cdr readyq))
256 (let ((next-k (#%car readyq)))
257 (#%set-car! readyq #f)
258 (#%return-to-cont next-k #f)))))
265 (lambda (freq-div duration)
266 (#%beep freq-div duration)))
286 (or (#%getchar-wait 0 3)
291 (#%getchar-wait duration 3)))
295 (#%sleep-aux (#%+ (#%clock) duration))))
299 (if (#%< (#%clock) wake-up)
300 (#%sleep-aux wake-up)
309 (lambda (id duty period)
310 (#%led id duty period)))
314 (if (#%eq? state 'red)
321 (for-each putchar (#%string->list x))
332 (display (number->string x))
337 (#%write-list (#%cdr x)))
339 (display "#<symbol>")
340 (display "#<object>")))))))
348 (#%putchar #\space 3)
350 (#%write-list (#%cdr lst)))
354 (#%putchar #\) 3))))))
356 (define number->string
360 (#%cons #\- (#%number->string-aux (#%neg n) '()))
361 (#%number->string-aux n '())))))
363 (define #%number->string-aux
365 (let ((rest (#%cons (#%+ #\0 (#%remainder n 10)) lst)))
368 (#%number->string-aux (#%quotient n 10) rest)))))
373 (#%putchar #\newline 3)))
389 (#%car (#%car (#%car p)))))
392 (#%car (#%car (#%cdr p)))))
395 (#%car (#%cdr (#%car p)))))
398 (#%car (#%cdr (#%cdr p)))))
401 (#%cdr (#%car (#%car p)))))
404 (#%cdr (#%car (#%cdr p)))))
407 (#%cdr (#%cdr (#%car p)))))
410 (#%cdr (#%cdr (#%cdr p)))))
413 (lambda (x y) ;; TODO rewrite once we have cond, also add vectors, actually, we do have cond, but I don't really trust it
416 (if (and (#%pair? x) (#%pair? y))
417 (and (equal? (#%car x) (#%car y))
418 (equal? (#%cdr x) (#%cdr y)))
419 (if (and (#%u8vector? x) (#%u8vector? y))
420 (u8vector-equal? x y)
421 #f))))) ;; TODO could this have a problem ?
423 (define u8vector-equal?
425 (let ((lx (#%u8vector-length x)))
426 (if (#%= lx (#%u8vector-length y))
427 (u8vector-equal?-loop x y lx)
429 (define u8vector-equal?-loop
433 (and (#%= (#%u8vector-ref x l) (#%u8vector-ref y l))
434 (u8vector-equal?-loop x y (#%- l 1)))))) ;; TODO test this
437 (lambda (t l) ;; TODO rewrite once we have cond
440 (if (equal? t (caar l))
442 (assoc t (#%cdr l))))))
444 ;; TODO ordinary vectors are never more that 6 elements long in the stack, so implementing them as lists is acceptable
446 (define vector-ref list-ref)
447 (define vector-set! list-set!)
449 (define bitwise-ior (lambda (x y) (#%ior x y)))
450 (define bitwise-xor (lambda (x y) (#%xor x y)))
451 ;; TODO add bitwise-and ? bitwise-not ?
453 (define current-time (lambda () (#%clock)))
454 (define time->seconds (lambda (t) (#%quotient t 100))) ;; TODO no floats, is that a problem ?
456 (define else #t) ; for cond, among others
461 (define list->u8vector ;; TODO not used except for server
463 (let* ((n (length x))
464 (v (#%make-u8vector n)))
465 (list->u8vector-loop v 0 x)
467 (define list->u8vector-loop
469 (#%u8vector-set! v n (#%car x))
470 (if (#%not (#%null? (#%cdr x)))
471 (list->u8vector-loop v (#%+ n 1) (#%cdr x)))))
472 (define u8vector-length (lambda (x) (#%u8vector-length x)))
473 (define u8vector-ref (lambda (x y) (#%u8vector-ref x y)))
474 (define u8vector-set! (lambda (x y z) (#%u8vector-set! x y z)))
475 (define make-u8vector
477 (make-u8vector-loop (#%make-u8vector n) n x)))
478 (define make-u8vector-loop
480 (#%u8vector-set! v n x)
481 (if (#%> n 0) (make-u8vector-loop v (#%- n 1) x))))