[racket] compiling interactions?

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Fri Jan 14 16:44:31 EST 2011

> It looks like you've constructed your namespace with
> "module->namespace".  The provides the bindings from inside the
> module, not the ones it provides.  I think you want to use
> namespace-require or something similar.


Ah, thank you!  That works.  Here's what I have now, just in case it
helps anyone else:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#lang racket/base

(define language-namespace-cache (make-hash))
;; lookup-language-namespace: module-path -> namespace
;; Returns a namespace associated with the lang.
(define (lookup-language-namespace lang)
  (hash-ref language-namespace-cache lang
            (lambda ()
              (let ([ns (make-base-empty-namespace)])
                (parameterize ([current-namespace ns])
                  (namespace-require lang))
                (hash-set! language-namespace-cache lang ns)
                ns))))

;; get-interaction-bytecode: (or sexp syntax) -> bytes
(define (get-interaction-bytecode x #:language-module
                                  (language-module 'racket/base))
  (let ([module-namespace
         (lookup-language-namespace language-module)])
    (parameterize ([current-namespace module-namespace])
      (serialize-compiled-code
       (compile (namespace-syntax-introduce
                 (datum->syntax #f (cons '#%top-interaction x))))))))


;; serialize-compiled-code: compiled-code -> bytes
(define (serialize-compiled-code a-compiled-code)
  (let ([op (open-output-bytes)])
    (write a-compiled-code op)
    (get-output-bytes op)))


Posted on the users mailing list.