guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A macro containing a mini-macro?


From: rain1
Subject: Re: A macro containing a mini-macro?
Date: Thu, 13 Sep 2018 23:24:28 +0100
User-agent: Roundcube Webmail/1.3.6

On 2018-09-13 23:04, HiPhish wrote:
Hello Schemers,

I have written a small macro for writing test specifications:

    (define-syntax test-cases
      (syntax-rules ()
        ((_ title
           (given (byte byte* ...))
           ...)
         (begin
           (test-begin title)
           (call-with-values (λ () (open-bytevector-output-port))
             (λ (out get-bv)
               (pack given out)
               (let ((received (get-bv))
(expected (u8-list->bytevector '(byte byte* ...))))
                 (test-assert (bytevector=? received expected)))))
           ...
           (test-end title)))))

The idea is that I can specify a series of test cases where each case consists of an object and a sequence of bytes which this object is to be serialized to:

    (test-cases "Single precision floating point numbers"
(+3.1415927410125732 (#xCA #b01000000 #b01001001 #b00001111 #b11011011))
      (-3.1415927410125732 (#xCA #b11000000 #b01001001 #b00001111
#b11011011)))

This works fine, but sometimes there is a sequence of the same bytes and it
would be more readable if I could write something like this:

    ((make-vector 16 0) (#xDC (16 #x00)))

instead of writing out 16 times `#x00`. This would require being able to make
a distinction in the pattern whether `byte` is of the pattern

    byte

or

    (count byte)

and if it's the latter construct a list of `count` `byte`s (via `(make-list count byte)` for example) and splice it in. This distinction needs to be made for each byte specification because I want to mix actual bytes and these "RLE-
encoded" byte specifications.

So I guess what I'm looking for is to have a `syntax-rules` inside a `syntax-
rules` in a way. Can this be done?

You can implement the DSL that transforms bytevector descriptions like (#xDC (16 #x00)) into a bytevector as a procedure, suppose we call it byte-dsl. Then you only need to change u8-list->bytevector with byte-dsl. This lets you do what you wanted without the difficult task of macros inside macros.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]