chicken-users
[Top][All Lists]
Advanced

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

Re: [Chicken-users] struggling with macros


From: Kristian Lein-Mathisen
Subject: Re: [Chicken-users] struggling with macros
Date: Sun, 11 Nov 2012 18:02:39 +0100


Hi Răzvan,

Just as a side-note: It may be a good idea to play around with your implementation as a normal function, and them wrap that in a macro once it's up on its feet. That way you can isolate problems with your implementation and problems with the macros.

This approach is taken by the bind egg (see bind.scm and bind-translator.scm). It could for example be setup like this:

----- js-transformer.scm
;; define a function "js-transformer" that does this:
;;(js-transformer 123) => "123"
;;(js-transformer "123") => "\"123\""
;;(js-transformer '(1 2 3)) => "1(2, 3)" ;; note the quote!

----- js.scm
;; import js-transformer into syntax env
(begin-for-syntax (include "js-transformer.scm")) ;; or (import-for-syntax "js-transformer.scm") if you have a module (I think)

;; define a macro that uses js-transformer at expansion-time
(define-syntax js
  (ir-macro-transformer
    (lambda (expr inject compare)
            (js-transform (cdr expr)))))


K.


On Sun, Nov 11, 2012 at 1:23 PM, Răzvan Rotaru <address@hidden> wrote:
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
address@hidden
https://lists.nongnu.org/mailman/listinfo/chicken-users



reply via email to

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