Is this macros pair possible?

I would like to make something like

(my-if cond1 act1 ...)
(my-else act2 ...)

that should expand to

(if cond1 (begin act1 ...) (begin act2 ...))

Is it possible?

I assume you know what you are doing.

#lang racket

(require (for-syntax syntax/parse))

(define-for-syntax current-my-if (make-parameter #f))

(define-syntax (my-if stx)
  (syntax-parse stx
    ((_ condition body ...)
     (when (current-my-if)
       (raise-syntax-error #f "Already in my-if" stx))
     (current-my-if (list #'condition
                          #'(begin
                              body ...)))
     #'(void))))
(define-syntax (my-else stx)
  (syntax-parse stx
    ((_ body ...)
     (unless (current-my-if)
       (raise-syntax-error #f "Not in my-if" stx))
     (define this-my-if (current-my-if))
     (current-my-if #f)
     #`(if #,(car this-my-if)
           #,(cadr this-my-if)
           (begin
             body ...)))))

(my-if #f (displayln "it's true"))
(my-else (displayln "no, false"))

(my-if #t (displayln "woo hoo"))
(my-else (displayln "nope"))

Basically the real expansion is deferred to the my-else expansion and there can be any arbitrary number of expressions in between. Also the notion of "in my-if" is more like a guideline than actual rule here. However if you only need side effects, it is mostly OK.

Still, I would strongly advise against entering this rabbit hole :wink:

1 Like

I want something like my-cond/iffy with sweet-exp

But without outer my-cond.