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)) ;; TODO had an error and looped
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
199 (if (>= n 1) ;; TODO had an off-by-one
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)))
278 ;; (#%dac level))) ;; gone
290 (or (#%getchar-wait 0 3)
295 (#%getchar-wait duration 3)))
299 (#%sleep-aux (#%+ (#%clock) duration))))
303 (if (#%< (#%clock) wake-up)
304 (#%sleep-aux wake-up)
313 (lambda (id duty period)
314 (#%led id duty period)))
318 (if (#%eq? state 'red)
325 (for-each putchar (#%string->list x))
336 (display (number->string x))
341 (#%write-list (#%cdr x)))
343 (display "#<symbol>")
344 (display "#<object>")))))))
352 (#%putchar #\space 3)
354 (#%write-list (#%cdr lst)))
358 (#%putchar #\) 3))))))
360 (define number->string
364 (#%cons #\- (#%number->string-aux (#%neg n) '()))
365 (#%number->string-aux n '())))))
367 (define #%number->string-aux
369 (let ((rest (#%cons (#%+ #\0 (#%remainder n 10)) lst)))
372 (#%number->string-aux (#%quotient n 10) rest)))))
377 (#%putchar #\newline 3)))
388 (define cddr ;; TODO implement all of them up to 4 chars ?
393 (car (car (cdr p)))))
396 (cdr (car (cdr p)))))
399 (lambda (x y) ;; TODO rewrite once we have cond, also add vectors
402 (if (and (pair? x) (pair? y))
403 (and (equal? (car x) (car y))
404 (equal? (cdr x) (cdr y)))
405 #f)))) ;; TODO could this have a problem ?
408 (lambda (t l) ;; TODO rewrite once we have cond
411 (if (equal? t (caar l))
413 (assoc t (cdr l))))))
415 ;; TODO ordinary vectors are never more that 6 elements long in the stack, so implementing them as lists is acceptable
417 (define vector-ref list-ref)
418 (define vector-set! list-set!)
420 (define bitwise-ior (lambda (x y) (#%ior x y)))
421 (define bitwise-xor (lambda (x y) (#%xor x y)))
422 ;; TODO add bitwise-and ? bitwise-not ?
424 (define current-time (lambda () (clock)))
425 (define time->seconds (lambda (t) (quotient t 100))) ;; TODO no floats, is that a problem ?
427 (define else #t) ; for cond, among others
432 (define list->u8vector ;; TODO not used except for server
434 (let* ((n (length x))
435 (v (#%make-u8vector n)))
436 (list->u8vector-loop v 0 x)
438 (define list->u8vector-loop
440 (u8vector-set! v n (car x))
441 (if (not (null? (cdr x))) (list->u8vector-loop v (+ n 1) (cdr x)))))
442 (define u8vector-length (lambda (x) (#%u8vector-length x)))
443 (define u8vector-ref (lambda (x y) (#%u8vector-ref x y)))
444 (define u8vector-set! (lambda (x y z) (#%u8vector-set! x y z)))
445 (define make-u8vector
447 (make-u8vector-loop (#%make-u8vector n) n x)))
448 (define make-u8vector-loop
450 (u8vector-set! v n x)
451 (if (> n 0) (make-u8vector-loop v (- n 1) x))))
455 ;; TODO make sure constant vectors end up in rom
456 ;; (define u8vector ;; TODO use chris okasaki's random access lists for mutable vectors, and in-rom vectors (strings) for the rest, these functions are for the in-rom vectors
457 ;; (lambda (first . rest) ;; TODO can't we have all in the same arg ?
458 ;; (list->u8vector (cons first rest))))
459 ;; ;; TODO maybe still use the parser hack for the in-rom vectors, since they are known at compile time (but some might have variables inside instead of only numbers, would not work then)
461 ;; (define u8vector-ref
463 ;; (#%car (#%substring-aux1 (#%string->list u8) i))))
464 ;; ;; TODO yuck, this is O(n), do better, since we have contiguous memory for in-rom vectors, but not that important since these rom vectors are all small
466 (define print display) ;; TODO watch out for differences between the 2