[racket] racket's syntax-case and read-syntax usage

From: Никита Зуев (nikitazu at gmail.com)
Date: Thu Aug 26 07:27:15 EDT 2010

Hello, I’m new to racket language and playing with it’s syntax reading
and generating features. What I want to do is a simple translator that
reads scheme s-expressions and translates them into ocaml. I’m using
`read-syntax’ to get a list of syntax-objects and then recursively
translate them into s-expressions that are looking `ocaml-like’:

;; simplified version transform
(define (transform stx)
  (syntax-case stx
    (require module lambda) ;; and more special forms

    ((require id) #’(open id))

    ((module id expr) #`(module id = #,(transform #’expr)))

    ((lambda () expr) #`(fun () -> #,(transform #’expr)))

    ;; …and so on

The problem is: when I use it in REPL everything works great:

> (syntax->datum (transform #’(require AModule)))
(open AModule)

> (syntax->datum (transform #'(define a (+ 1 2 3))))
(let a = (1 + 2 + 3))

But when I use same procedure on a syntax-object that was read by
`read-syntax', I'm getting exception:
source.ss::23: require: bad syntax in: (require AModule)

I looked at read syntax-object and it looked same as the one I'm
making in REPL, so I have no idea where I'm wrong. :(

Nikita B. Zuev


Sorry for sending two times, first time I forgot that email used in
subscribing should be used to post.

Posted on the users mailing list.