Hi,

I'm trying to write a simple javascript DSL, and got stuck in the macros
:). (I'm coming from lisp macros)  Take for example this one:

(define-syntax js
  (ir-macro-transformer
    (lambda (expr inject compare)
      (let ((body (cdr expr)) (next (cadr expr)))
      (printf "next=~a~n" next)
        (cond
          [(string? next) (string-append "\"" next "\"")]
          [(number? next) (number->string next)]
          [(null? next) ""]
          [(list? next) `(string-append (js ,(car next)) "("  ")")]
          )))))


It is supposed to handle numbers and function calls, without building the
parameter list in the function calls.

CSI> (js 1)
next=1
"1"
CSI> (js (1 2 3))
next=(1 2 3)
next=1
"1()"


However, when trying to build the parameter list I get and error which I
don't understand:

(define-syntax js
  (ir-macro-transformer
    (lambda (expr inject compare)
      (let ((body (cdr expr)) (next (cadr expr)))
      (printf "next=~a~n" next)
        (cond
          [(string? next) (string-append "\"" next "\"")]
          [(number? next) (number->string next)]
          [(null? next) ""]
          [(list? next) `(string-append (js ,(car next)) "(" (apply
string-append (map js ,(cdr next))) ")")]
          )))))

CSI> (js (1 2 3))
next=(1 2 3)
next=1
Error: unbound variable: js
[ inspect ]

Restarts:
  0: [ABORT] Return to SLIME's top level

Backtrace:
  0: <eval>   [ more... ] (map248 js245 (2 3))
  1: <eval>   [ more... ] (apply247 string-append246 (map248 js245 (2 3)))
  2: <eval>   [ more... ] (string-append246 (js245 1) "(" (apply247
string-append246 (map248 js245 (2 3))) ")")
  3: <syntax>             (2 3)
  4: <syntax>             (map248 js245 (2 3))
  5: <syntax>             (apply247 string-append246 (map248 js245 (2 3)))
  6: <eval>   [ more... ] (number->string next)
  7: <eval>   [ more... ] (number? next)
  8: <eval>   [ more... ] (printf "next=~a~n" next)
  9: <eval>   [ more... ] (cadr expr)
 10: <eval>   [ more... ] (cdr expr)
 11: <syntax>             (js245 1)
 12: <syntax>             (string-append246 (js245 1) "(" (apply247
string-append246 (map248 js245 (2 3))) ")")
 13: <eval>   [ more... ] (cdr next)
 14: <eval>   [ more... ] (##sys#list (##core#quote map) (##core#quote js)
(cdr next))
 15: <eval>   [ more... ] (##sys#list (##core#quote apply) (##core#quote
string-append) (##sys#list (##core#quote map) (##core#quote js) (cdr next)))

So, my questions are:
1/ Why is "js" not bound in the second example? I also tried to use
letrec-syntax but with same result.
2/ Are there other ways to achieve what I want?
3/ I also tried to use syntax-rules, but from what I understood this is not
possible, because you can't execute arbitrary expressions (in this case a
cond) at macroexpansion time. Am I right here?

Thanks,
Răzvan
_______________________________________________
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to