[racket] Evaluating a module with an unintended language

From: Jordan Johnson (jmj at fellowhuman.com)
Date: Thu Nov 1 18:57:15 EDT 2012

Looking at some of the recent tutorials on building and using languages in
Racket, I was prompted to wonder what I'd have to do in order to take an
existing module (in its own file) and evaluate it with a language other
than the one it is specified to use, without modifying the module's
original source file.  E.g., take a BSL module containing

;; hi : String -> String
(define (hi person) (string-append "Hi, " person "!"))

and evaluate it in a different language.

This is what I've come up with so far, which seems to do the trick:

(require racket/sandbox)
;; mod-path->evaluator : path module-path -> (any -> any)
(define (mod-path->evaluator p replacement-lang)
  (parameterize ([read-accept-reader #t])
    (let ([mod (read (open-input-file p))])
      (match mod
        [(list 'module name lang body ...)
         (make-module-evaluator
          `(module ,name ,replacement-lang , at body))]))))


The explicitness of destructuring and rebuilding of the module bugs me a
bit, but assuming that replacement-lang* *defines all the forms used in the
module's intended language, is this more or less the way that those of you
who have built plenty of languages in Racket would do it?  Any suggested
improvements?

That said, it's still pretty neat to see how easily this was done once I
found the necessary parts.

jmj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20121101/9308a72b/attachment-0001.html>

Posted on the users mailing list.