testingautomated-testsracketdynamic-compilation

Invoke `racket` in a Racket script


General question:

Can I invoke the current racket executable from within a running Racket script?

Basically, I'd like a replacement for (system "racket ...") in the case that (find-executable-path "racket") does not return a path to the Racket executable I'm currently using.

Context:

What I really want is to try compiling a few expressions and assert that they raise compilation errors. This is for unit testing.


Solution

  • I don't believe you need to step outside of the executable here. Try this:

    #lang racket
    
    (require syntax/modread)
    
    ;; define a namespace anchor to attach a namespace to:
    (define-namespace-anchor anchor)
    ;; define a namespace for expansion:
    (define target-namespace (namespace-anchor->namespace anchor))
    
    (define program-to-compile
      "#lang racket
    (+ 3 4)")
    
    ;; go ahead and expand
    (with-module-reading-parameterization
     (λ()
       (parameterize ([current-namespace target-namespace])
       (expand
        (read-syntax
         "bogus-filename"
         (open-input-string program-to-compile))))))
    

    I think I'm correct when I say that Racket is singularly clean in its ability to provide the compiler to running programs in a disciplined way.