syndicate-2017/racket/syndicate/examples/example-lang.rkt

82 lines
1.9 KiB
Racket
Raw Permalink Normal View History

#lang syndicate/core
2015-03-05 19:56:11 +00:00
(require (only-in racket/port read-line-evt))
(require/activate "../drivers/timer.rkt")
2015-03-05 19:56:11 +00:00
(define sub-all-except-meta
(patch-seq (sub ?)
(unsub (inbound ?))
(unsub (outbound ?))))
2015-03-05 19:56:11 +00:00
(define (quasi-spy e s)
(printf "----------------------------------------\n")
(printf "QUASI-SPY:\n")
(match e
[(? patch? p) (pretty-print-patch p)]
[other
(write other)
(newline)])
(printf "========================================\n")
#f)
(actor quasi-spy (void) sub-all-except-meta)
2015-03-05 19:56:11 +00:00
(define (r e s)
(match e
[(message body) (transition s (message (outbound `(print (got ,body)))))]
2015-03-05 19:56:11 +00:00
[_ #f]))
(define (b e n)
(match e
[#f (if (< n 10)
(transition (+ n 1) (message `(hello ,n)))
#f)]
[_ #f]))
2017-02-25 16:16:25 +00:00
(dataspace-actor (actor r (void) sub-all-except-meta)
(actor b 0 '()))
2015-03-05 19:56:11 +00:00
(define (echoer e s)
(match e
[(message (inbound (external-event _ (list (? eof-object?)))))
(quit)]
[(message (inbound (external-event _ (list line))))
2015-03-05 19:56:11 +00:00
(transition s (message `(print (got-line ,line))))]
[_ #f]))
(actor echoer
(void)
(sub (inbound (external-event (read-line-evt (current-input-port) 'any) ?))))
2015-03-05 19:56:11 +00:00
(define (ticker e s)
(match e
[(? patch? p)
(printf "TICKER PATCH RECEIVED:\n")
(pretty-print-patch p)
#f]
[(message (timer-expired 'tick now))
(printf "TICK ~v\n" now)
(if (< s 3)
(transition (+ s 1) (message (set-timer 'tick 1000 'relative)))
(quit))]
2015-03-05 19:56:11 +00:00
[_ #f]))
2021-05-10 19:30:46 +00:00
(actor (lambda (e s) (quit))
#f
(message (set-timer 'tick 1000 'relative)))
(actor ticker
1
(patch-seq (sub (observe (set-timer ? ? ?)))
(sub (timer-expired 'tick ?))))
2015-03-05 19:56:11 +00:00
(define (printer e s)
(match e
[(message (list 'print v))
(log-info "PRINTER: ~a" v)
#f]
[_ #f]))
(actor printer
(void)
(sub `(print ,?)))