165-env-filter-gc.scm (1352B)
1 ; Filtered ENV nodes preserve lexical shadowing and mutable bindings across 2 ; ordinary evaluation, loop updates, closure capture, and collection. 3 4 (define global-value 100) 5 6 (define (exercise x) 7 ; Repeated global references from a lexical environment take the negative 8 ; filter path; repeated x references take the exact lexical path. 9 (if (= (+ x global-value) 105) 0 (sys-exit 1)) 10 (if (= (+ x global-value) 105) 0 (sys-exit 2)) 11 (set! x (+ x 1)) 12 (if (= x 6) 0 (sys-exit 3)) 13 14 (collect-garbage) 15 (if (= (+ x global-value) 106) 0 (sys-exit 4)) 16 17 (let ((global-value 9)) 18 (if (= global-value 9) 0 (sys-exit 5)) 19 (set! global-value 11) 20 (if (= global-value 11) 0 (sys-exit 6))) 21 (if (= global-value 100) 0 (sys-exit 7)) 22 x) 23 24 (if (= (exercise 5) 6) 0 (sys-exit 8)) 25 26 ; do mutates existing ENV nodes in parallel after evaluating every step. 27 (define loop-result 28 (do ((i 0 (+ i 1)) 29 (sum 0 (+ sum i))) 30 ((= i 5) sum))) 31 (if (= loop-result 10) 0 (sys-exit 9)) 32 33 ; Captured nodes retain identity and mutation visibility. 34 (define read-captured #f) 35 (define write-captured #f) 36 (let ((value 41)) 37 (set! read-captured (lambda () value)) 38 (set! write-captured (lambda (x) (set! value x)))) 39 (if (= (read-captured) 41) 0 (sys-exit 10)) 40 (write-captured 77) 41 (collect-garbage) 42 (if (= (read-captured) 77) 0 (sys-exit 11)) 43 44 (sys-exit 0)