Skip to content

Commit 76d1dc4

Browse files
committed
changed integral to use syntax-parse
1 parent cd082fe commit 76d1dc4

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

reactive/reactive-thing.rkt

+13-8
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
; (while (and (<= 50 (milliseconds)) (<= (milliseconds) 100)) ...
3535
; or this:
3636
; (while (and (<= 50 (milliseconds)) (>= 100 (milliseconds))) ...
37-
; plus the analogous versions for the lower bound test, so 4 possible combinations in all. (Is there a better way to do this??)
37+
; plus the analogous versions for the lower bound test, so 4 possible combinations in all.
38+
; NOTE: this should be rewritten in better style -- see e.g. the integral macro, which uses syntax-parse
3839
((while (and (<= lower (milliseconds)) (<= (milliseconds) upper)) e ...)
3940
(add-while-with-time-bounds lower upper e ...))
4041
((while (and (<= lower (milliseconds)) (>= upper (milliseconds))) e ...)
@@ -90,18 +91,22 @@
9091
(with-syntax ([id (datum->syntax stx (gensym))])
9192
#'(send this max-min-helper fn (lambda () (send this wally-evaluate expr)) 'id (interesting-time?)))]))
9293
; integral macro
94+
; The form is (integral expr) with additional optional keyword arguments as follows:
95+
; #:var v -- variable of integration; default is (milliseconds) -- note that an expression is allowed here
96+
; #:numeric or #:symbolic -- which kind of integration to use. Default is to try symbolic, and if that doesn't work, use numeric
97+
; #:dt d -- time step (only allowed with #:numeric)
98+
; Example: (integral (sin x) #:var x #:numeric #:dt 0.1)
9399
(require (for-syntax "symbolic-integration.rkt")) ; function to do simple symbolic integration
100+
(require (for-syntax syntax/parse))
94101
(define-syntax (integral stx)
95-
(syntax-case stx ()
96-
[(_ expr var) ; case with explicit variable of integration
97-
(with-syntax ([intgl (datum->syntax stx (symbolic-integral (syntax->datum #'expr) (syntax->datum #'var)))]
98-
[id (datum->syntax stx (gensym))])
99-
#'(send this integral-helper (lambda () (send this wally-evaluate intgl)) 'id (interesting-time?)))]
100-
[(_ expr) ; default variable of integration is (milliseconds)
101-
(with-syntax ([intgl (datum->syntax stx (symbolic-integral (syntax->datum #'expr) '(milliseconds)))]
102+
(syntax-parse stx
103+
[(integral e:expr (~or (~optional (~seq #:var v:expr)) (~optional #:numeric) (~optional #:symbolic) (~optional (~seq #:dt d:expr))) ...)
104+
(with-syntax ([intgl (datum->syntax stx (symbolic-integral (syntax->datum #'e)
105+
(if (attribute v) (syntax->datum #'v) '(milliseconds))))]
102106
[id (datum->syntax stx (gensym))])
103107
#'(send this integral-helper (lambda () (send this wally-evaluate intgl)) 'id (interesting-time?)))]))
104108

109+
105110
(define reactive-thing%
106111
(class abstract-reactive-thing%
107112
(super-new)

tests/integral-tests.rkt

+6-6
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@
129129
(super-new)
130130
(define-public-symbolic* s x1 x2 y1 y2 z1 z2 real?)
131131
(always (equal? s (* 2 (milliseconds))))
132-
(always (equal? x1 (integral 2 (milliseconds))))
133-
(always (equal? x2 (integral (milliseconds) (milliseconds))))
134-
(always (equal? y1 (integral 2 (seconds))))
135-
(always (equal? y2 (integral (seconds) (seconds))))
136-
(always (equal? z1 (integral 2 s)))
137-
(always (equal? z2 (integral s s)))))
132+
(always (equal? x1 (integral 2 #:var (milliseconds))))
133+
(always (equal? x2 (integral (milliseconds) #:var (milliseconds))))
134+
(always (equal? y1 (integral 2 #:var (seconds))))
135+
(always (equal? y2 (integral (seconds) #:var (seconds))))
136+
(always (equal? z1 (integral 2 #:var s)))
137+
(always (equal? z2 (integral s #:var s)))))
138138
(define r (new tester%))
139139
(send r start)
140140
(send-syncd r advance-time-syncd 100)

0 commit comments

Comments
 (0)