summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2014-08-17 21:22:40 -0400
committerDavid Thompson <dthompson2@worcester.edu>2014-08-25 19:33:08 -0400
commitbc336c2095a15a2a1739755c3725aedf2d9223ab (patch)
tree59d8c89215d95c918b397a2c4ddd2b2528b0eef2
parent28467b74f122d4c7ba35d00ac42efb7199ff71b1 (diff)
Add signal-generator macro.
* sly/signal.scm (yield): New variable. (signal-generator): New macro.
-rw-r--r--sly/signal.scm26
1 files changed, 25 insertions, 1 deletions
diff --git a/sly/signal.scm b/sly/signal.scm
index a3e79c4..9780b1c 100644
--- a/sly/signal.scm
+++ b/sly/signal.scm
@@ -26,6 +26,7 @@
#:use-module (srfi srfi-9)
#:use-module (srfi srfi-26)
#:use-module (sly agenda)
+ #:use-module (sly coroutine)
#:export (signal?
make-signal
define-signal
@@ -48,7 +49,8 @@
signal-tap
signal-sample
signal-delay
- signal-throttle))
+ signal-throttle
+ signal-generator))
;;;
;;; Signals
@@ -349,3 +351,25 @@ ticks of the current agenda."
(%signal-set! self value)
(set! last-time (agenda-time)))))
(list signal)))
+
+(define-syntax-parameter yield
+ (lambda (form)
+ (syntax-violation 'yield
+ "yield used outside of a signal-generator form"
+ form)))
+
+(define-syntax-rule (signal-generator body ...)
+ (let ((signal (make-signal #f)))
+ (define (handler k value)
+ (signal-set! signal value)
+ (call-with-prompt 'signal-generator k handler))
+ (coroutine
+ (call-with-prompt
+ 'signal-generator
+ (syntax-parameterize
+ ((yield (syntax-rules ()
+ ((_ exp)
+ (abort-to-prompt 'signal-generator exp)))))
+ (lambda () body ...))
+ handler))
+ signal))