在Racket脚本中调用`racket`

Ben*_*man 6 testing automated-tests dynamic-compilation racket

一般问题:

我可以racket从正在运行的Racket脚本中调用当前的可执行文件吗?

基本上,我想(system "racket ...")(find-executable-path "racket")不返回我正在使用的Racket可执行文件的路径的情况下替换.

语境:

我真正想要的是尝试编译一些表达式并断言它们会引发编译错误.这是用于单元测试.

Joh*_*nts 5

我不相信你需要在这里走出可执行文件.试试这个:

#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))))))
Run Code Online (Sandbox Code Playgroud)

当我说Racket在提供编译器以规范方式运行程序的能力方面非常干净时,我认为我是正确的.