LIPS-scheme / lips

Scheme based powerful lisp interpreter in JavaScript
https://lips.js.org
Other
427 stars 35 forks source link

syntax-parameterize expression, use body expression that is not hygiene #356

Closed jcubic closed 7 months ago

jcubic commented 7 months ago

Base example:

(define-syntax-parameter it (syntax-rules () ((_) (syntax-error "Use outside aif"))))

(define-syntax awhen
  (syntax-rules ()
    ((_ test body ...)
     (let ((tmp test))
       (syntax-parameterize
        ((it (syntax-rules ()
               ((_) tmp))))
        (if tmp
            (begin
              body ...)))))))

Don't rename the symbols from inside syntax-parameterize:

(let ((alist '((foo . "lorem") (bar . "ipsum") (baz . "dolor")))
      (begin (lambda () 10)))
  (print (awhen (assoc 'bar alist) #void)))
;; ==> 10
jcubic commented 7 months ago

The problem is only with begin, becase syntax-parameterize expand into:

(begin (#:if #:tmp (#:begin #void)))

The expression is wrapped with body that is not hygiene.

jcubic commented 7 months ago

The problem was in few places where JavaSript based macro used begin symbol and evaluated the expression.