mtl^urass wrote:I'm trying to create a macro or something that will run a body with all the keys in a hashtable passed to it bound to their values in a local environment.
Why would you want to do that? The code must necessarily know what variables it is using if it is referring to them by name, so the obvious way would be to separate the body into a function taking those values as arguments, and then call that function with appropriate values from the hashtable.
Still, there are ways to achieve this directly. One option is to use
PROGV, but then you would have to declare all those variables used as
special. This is because names of lexical variables are compiled away.
The use of EVAL is a possibility of course. In CL you can pass a lexical environment, but you can trivially establish it by surrounding the form to be executed with a generated LET form.
Code: Select all
(defun eval-in-context (hashtable form)
(eval (list 'let
(loop for key being the hash-key of hashtable using (hash-value value)
collect (list key value))
form)))
This doesn't really seem like a good idea. For most applications you would either use the approach above or write custom interpreter. EVAL is obviously fully general CL evaluator, and hence relatively slow if you want to evaluate some constrained language.