system.

First define some error classes that you can signal when embed-value and embed- code are called in the interpreter.

(define-condition embedded-lisp-in-interpreter (error)

((form :initarg :form :reader form)))

(define-condition value-in-interpreter (embedded-lisp-in-interpreter) ()

(:report

(lambda (c s)

(format s 'Can't embed values when interpreting. Value: ~s' (form c)))))

(define-condition code-in-interpreter (embedded-lisp-in-interpreter) ()

(:report

(lambda (c s)

(format s 'Can't embed code when interpreting. Code: ~s' (form c)))))

Now you can implement embed-value and embed-code to signal those errors and provide a restart that'll evaluate the form with EVAL.

(defmethod embed-value ((pp html-pretty-printer) value)

(restart-case (error 'value-in-interpreter :form value)

(evaluate ()

:report (lambda (s) (format s 'EVAL ~s in null lexical environment.' value))

(raw-string pp (escape (princ-to-string (eval value)) *escapes*) t))))

(defmethod embed-code ((pp html-pretty-printer) code)

(restart-case (error 'code-in-interpreter :form code)

(evaluate ()

:report (lambda (s) (format s 'EVAL ~s in null lexical environment.' code))

(eval code))))

Now you can do something like this:

HTML> (defvar *x* 10)

*X*

HTML> (emit-html '(:p *x*))

and you'll get dropped into the debugger with this message:

Can't embed values when interpreting. Value: *X*

[Condition of type VALUE-IN-INTERPRETER]

Restarts:

0: [EVALUATE] EVAL *X* in null lexical environment.

1: [ABORT] Abort handling SLIME request.

2: [ABORT] Abort entirely from this process.

If you invoke the evaluate restart, embed-value will EVAL *x*, get the value 10, and generate this HTML:

<p>10</p>

Then, as a convenience, you can provide restart functions—functions that invoke the evaluate restart—in certain situations. The evaluate restart function unconditionally invokes the restart, while eval-dynamic-variables and eval-code invoke it only if the form in the condition is a dynamic variable or potential code.

(defun evaluate (&optional condition)

(declare (ignore condition))

(invoke-restart 'evaluate))

(defun eval-dynamic-variables (&optional condition)

(when (and (symbolp (form condition)) (boundp (form condition)))

(evaluate)))

(defun eval-code (&optional condition)

(when (consp (form condition))

(evaluate)))

Now you can use HANDLER-BIND to set up a handler to automatically invoke the evaluate restart for you.

HTML> (handler-bind ((value-in-interpreter #'evaluate)) (emit-html '(:p *x*)))

<p>10</p>

T

Finally, you can define a macro to provide a nicer syntax for binding handlers for the two kinds of errors.

(defmacro with-dynamic-evaluation ((&key values code) &body body)

`(handler-bind (

,@(if values `((value-in-interpreter #'evaluate)))

,@(if code `((code-in-interpreter #'evaluate))))

,@body))

With this macro defined, you can write this:

HTML> (with-dynamic-evaluation (:values t) (emit-html '(:p *x*)))

<p>10</p>

T

The Basic Evaluation Rule

Now to connect the FOO language to the processor interface, all you need is a function that takes an object and processes it, invoking the appropriate processor functions to generate HTML. For instance, when given a simple form like this:

(:p 'Foo')

this function might execute this sequence of calls on the processor:

(freshline processor)

(raw-string processor '<p' nil)

(raw-string processor '>' nil)

(raw-string processor 'Foo' nil)

(raw-string processor '</p>' nil)

(freshline processor)

For now you can define a simple function that just checks whether a form is, in fact, a legal FOO form and, if it is, hands it off to the function process-sexp-html for processing. In the next chapter, you'll add some bells and whistles to this function to allow it to handle macros and special operators. But for now it looks like this:

(defun process (processor form)

(if (sexp-html-p form)

(process-sexp-html processor form)

(error 'Malformed FOO form: ~s' form)))

The function sexp-html-p determines whether the given object is a legal FOO expression, either a self-evaluating form or a properly formatted cons.

(defun sexp-html-p (form)

(or (self-evaluating-p form) (cons-form-p form)))

Self-evaluating forms are easily handled: just convert to a string with PRINC-TO-

Вы читаете Practical Common Lisp
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату