Compile the file as if its contents were in the specified package

I wrote a kind of DSL on top of Common Lisp. The domain is rather strange, and my language is very different from Common Lisp. I put the whole interface in a package foo:

(defpackage :foo
  (:use     :common-lisp
            :internal-machinery)
  (:shadow  :in-package
            :*packages*))

Switching between packages is beyond the scope of the language concept, so I turned off this ability by obscuring the in-packageand characters *package*. Now the user (programmer) of my language will not be able to switch packets. Good.

Obviously, I want to use the Common Lisp compiler to compile programs written in this language. The function compile-filelooks fine to me. But there are difficulties.

I want to compile the file as if its contents were inside my package foo. Including (in-package :foo)on top of each program in my prototype language is an undesirable option.

To make things even worse, I have to compile the file inside the function:

(in-package :internal-machinery)

(defun compile-stuff (filename)
  (in-package :foo)                 ; it will have no effect, because
                                    ; this macro must be top level form

  (compile-file filename)           ; other options are omitted

  (in-package :internal-machinery)) ; no way, even if it were top level
                                    ; form, in-package is shadowed

I have no idea if this is possible or not, so any help would be appreciated.

+4
source share
1 answer

What about

(defun compile-stuff (filename)
  (let ((*package* (find-package '#:foo)))
    (compile-file filename)))

PS. As Rainer noted in a comment, if you offer the user REPL, you cannot change the user with (cl:in-package "CL-USER").

+6
source

Source: https://habr.com/ru/post/1547313/


All Articles