Runtime evaluation/In an environment: Difference between revisions

Content added Content deleted
(Omit from Lily)
m (→‎{{header|TXR}}: lang tag: txr -> txrlisp)
Line 841: Line 841:
In TXR's embedded Lisp dialect, we can implement the same solution as Lisp or Scheme: transform the code fragment by wrapping a <code>let</code> around it which binds a variable, and then evaluating the whole thing:
In TXR's embedded Lisp dialect, we can implement the same solution as Lisp or Scheme: transform the code fragment by wrapping a <code>let</code> around it which binds a variable, and then evaluating the whole thing:


<lang txr>(defun eval-subtract-for-two-values-of-x (code-fragment x1 x0)
<lang txrlisp>(defun eval-subtract-for-two-values-of-x (code-fragment x1 x0)
(- (eval ^(let ((x ,x1)) ,code-fragment))
(- (eval ^(let ((x ,x1)) ,code-fragment))
(eval ^(let ((x ,x0)) ,code-fragment))))
(eval ^(let ((x ,x0)) ,code-fragment))))
Line 849: Line 849:
Cutting edge TXR code provides access to the environment manipulation functions, making this possible:
Cutting edge TXR code provides access to the environment manipulation functions, making this possible:


<lang txr>(defun eval-subtract-for-two-values-of-x (code-fragment x1 x0)
<lang txrlisp>(defun eval-subtract-for-two-values-of-x (code-fragment x1 x0)
(let ((e1 (make-env (list (cons 'x x1)))) ;; create two environments stuffed with binding for x
(let ((e1 (make-env (list (cons 'x x1)))) ;; create two environments stuffed with binding for x
(e0 (make-env (list (cons 'x x0)))))
(e0 (make-env (list (cons 'x x0)))))
Line 859: Line 859:
Alternatively, empty environments can be made and extended with bindings:
Alternatively, empty environments can be made and extended with bindings:


<lang txr>(defun eval-subtract-for-two-values-of-x (code-fragment x1 x0)
<lang txrlisp>(defun eval-subtract-for-two-values-of-x (code-fragment x1 x0)
(let ((e1 (make-env))
(let ((e1 (make-env))
(e0 (make-env)))
(e0 (make-env)))
Line 879: Line 879:
However, our two <code>let</code> constructs carefully save and restore the dynamic environment (and therefore any prior value of <code>x</code>), even in the face of exceptions, and
However, our two <code>let</code> constructs carefully save and restore the dynamic environment (and therefore any prior value of <code>x</code>), even in the face of exceptions, and


<lang txr>(defvar x)
<lang txrlisp>(defvar x)


(defun eval-subtract-for-two-values-of-x (code-fragment x1 x0)
(defun eval-subtract-for-two-values-of-x (code-fragment x1 x0)