0

I want to create an interpreted function definition, not a compiled one.

SBCL manual says :

Variable: *evaluator-mode* [sb-ext] : Toggle between different evaluator implementations. If set to :compile, an implementation of eval that calls the compiler will be used. If set to :interpret, an interpreter will be used.

So, I try to create a BAR function (which does not exist) :

(let ((sb-ext::*evaluator-mode* :interpret))
  (defun bar (x) (+ x 1)))

But then, I check, and BAR is already compiled :

CL-USER> (compiled-function-p #'bar)
T

So, how do you create an interpreted version of BAR ?

2 Answers 2

2

The let form in your question only sets the evaluator mode at runtime. By then, the function has already been compiled.

You need to set it at load time and also make sure to load the file instead of compiling then loading it.

Try this:

In your-file.lisp:

;; at load time, set evaluator mode to interpret (before bar definition is met)
(eval-when (:load-toplevel :execute)
  (setf sb-ext::*evaluator-mode* :interpret))

;;define your interpreted function
(defun bar (x)
  (+ x 1))

;; set evaluator back to compile mode (optional)
(eval-when (:load-toplevel :execute)
  (setf sb-ext::*evaluator-mode* :compile))

;;check if bar is a compiled function
(print (compiled-function-p #'bar)) ;;prints NIL

Then load it with (load "your-file.lisp") (this doesn't compile the file first).

Sign up to request clarification or add additional context in comments.

Comments

0

I think that *evaluator-mode* is pretty inherently a global variable. For instance, if you do this:

> (setf sb-ext:*evaluator-mode* ':interpret)
:interpret
> (setf (symbol-function 'bar)
        (lambda (x) x))
#<interpreted-function nil {10026E7E2B}>
> (compiled-function-p #'bar)
nil

you get an interpreted function. But if you do this:

> (setf sb-ext:*evaluator-mode* ':compile)
:compile
> (setf (symbol-function 'bar)
        (let ((sb-ext:*evaluator-mode* ':interpret))
          (lambda (x) x)))
#<function (lambda (x)) {52C3687B}>
> (compiled-function-p #'bar)
t

You don't. My take on this, which may be wrong, is that the value which is in effect at the start of each top-level form is what counts: once the system has decided that it's going to use the compiling-evaluator for a form then it can't change its mind.

And note that there is a complicated definition of 'top-level form', and in particular that when processing a file then in a form like

(let (...)
  (x ...))

then (x ...) is not a top-level form.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.