(require reactive.scm)

(define (t)
  (let ((x 0))
    (do ((i 0 (+ i 1)))
	((= i 500))
      (let ((a 1))
	(reactive-set! x (* 2 a))
	(set! a 2)
	(if (not (= x 4))
	    (format *stderr* "x: ~D ~D~%" x a)))
      (let ((a 1))
	(reactive-set! a (* 2 x))
	(set! x 2)
	(if (not (= a 4))
	    (format *stderr* "a: ~D ~D~%" a x)))
      (let ((a 3))
	(set! a 2))
      (if (not (= x 2))
	  (format *stderr* "x: ~D~%" x))
      (let ((a 1))
	(do ((k 0 (+ k 1)))
	    ((= k 1))
	  (let ((b 2))
	    (do ((j 0 (+ j 1)))
		((= j 10))
	      (let ((c 3))
		(reactive-set! x (+ a b c))
		(set! c 2)
		(if (not (= x 5))
		    (format *stderr* "set: ~S ~S ~S ~S~%" x a b c))))
	    (set! b 3)
	    (if (not (= x 6))
		(format *stderr* "set: ~S ~S ~S~%" x a b))))
	(set! a 4)
	(if (not (= x 9))
	    (format *stderr* "set: ~S ~S~%" x a)))
      (reactive-let ((y (* x 2))
		     (z (+ (* x 3) 1)))
	(set! x 1)
	(if (or (not (= y 2))
		(not (= z 4)))
	    (format *stderr* "let: ~D ~D ~D~%" x y z)))
      (reactive-let* ((y (* x 2))
		      (z (+ (* x 3) y)))
	(set! x 1)
	(if (or (not (= y 2))
		(not (= z 5)))
	    (format *stderr* "let*: ~D ~D ~D~%" x y z)))
      
      (if (zero? (modulo i 9)) (gc)))))

(t)

(when (> (*s7* 'profile) 0)
  (show-profile 200))
(exit)


;; 3097 if initial heap=64k (3270 if 128k), 3036 if 32k
