• 2 Posts
  • 3 Comments
Joined 1 year ago
cake
Cake day: October 12th, 2023

help-circle

  • xhash101@alien.topOPBtoLispFighting with nested backquotes
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    Thanks a lot!

    If you’ve referenced these symbols by name in the body of the form, you already knew you needed them,

    Yes, but I do not know how many of them. The purpose of this macro is to create a lexical environment to test a network code. Some functions take just one port as a parameter, but the others - two and more. With this macro it would be very easy to call these functions and to link their IO through the port numbers:

    (with-free-ports 0 10
      (fn-1 port-1 port-2 port-3)
      (fn-2 port-1))
    

    And with the first posted version of with-free-ports I achieved it. The problems appeared when I tried to bind start in run-time. From your reply and from the u/lispm comments I understood that this is not a good idea. What I do not understand, is how to use such macros inside another macros/functions, which can supply with-free-ports with start and end parameters ? Also, I cannot figure out, when exactly macro-expansions take place if I use many enclosed macros. Are they expanded all at once when I compile my code?

    Anyway, I am very grateful for your suggestions. This helps a lot to learn CL.


  • xhash101@alien.topOPBtoLispFighting with nested backquotes
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    Thank you for your reply.

    The compiler generally will not execute the LET and then compile the WITH-FREE-PORTS form with the new binding

    Here is an example when the compiler does exactly that:

    (defmacro with-free-ports (start end &body body)
      `(list ,start ,end ,@body))
    
    (let ((start 1)
          (end 3))      
      (with-free-ports start end NIL))
    

    So, why does it work?