Created
May 14, 2020 04:23
-
-
Save 97jaz/ec714dfe9b66b674bff53b629b28c9d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(require racket/match | |
racket/port | |
syntax/parse/define | |
(for-syntax racket/base)) | |
(define (read-message inp) | |
(match (read-byte inp) | |
[#x00 (define payload-size (read-byte inp)) | |
(define payload (read-bytes payload-size inp)) | |
(list payload)] | |
[#x01 'failure])) | |
(define-syntax <- (λ (stx) (raise-syntax-error #f "Illegal use of <-" stx))) | |
(define-syntax-parser evt-do #:literals (<-) | |
[(_ x) #'(if (evt? x) | |
x | |
(handle-evt always-evt (λ (_) x)))] | |
[(_ [x <- e] rest ...) | |
#'(replace-evt e (λ (x) (evt-do rest ...)))]) | |
(define (read-byte-evt in) | |
(handle-evt in read-byte)) | |
(define (read-message-evt in) | |
(evt-do [b <- (read-byte-evt in)] | |
(match b | |
[#x00 (evt-do [size <- (read-byte-evt in)] | |
[payload <- (read-bytes-evt size in)] | |
(list payload))] | |
[#x01 'failure]))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment