Online Book Reader

Home Category

Beautiful Code [285]

By Root 5090 0
Core forms are handled by exp-core (described shortly); any recursion back to the expression expander is performed explicitly by the core transformer. A constant is rewritten into the constant value, stripped of its syntax wrapper.

The expander uses syntax-case and syntax (in its abbreviated form—i.e., #'template) to parse and refer to the input or portions thereof. Because the expander is also charged with implementing syntax-case, this may seem like a paradox. In actuality, it is handled by bootstrapping one version of the expander using a previous version. The expander would be much more tedious to write if syntax-case and syntax were not used.

The exp-macro procedure applies the transformation procedure (the value part of the macro binding) to the entire macro form, which may either be a single macro keyword or a structured expression with the macro keyword at its head. The exp-macro procedure first adds a fresh mark to the wrap of the input form, then applies the same mark to the wrap of the output form. The first mark serves as an "anti-mark" that cancels out the second mark, so the net effect is that the mark adheres only to the portions of the output that were introduced by the transformer, thus uniquely identifying the portions of the code introduced at this transcription step:

(define exp-macro

(lambda (p x)

(let ([m (make-mark)])

(add-mark m (p (add-mark m x))))))

The exp-core procedure simply applies the given core transformer (the value part of the core binding) to the input form:

(define exp-core

(lambda (p x r mr)

(p x r mr)))

The exp-exprs procedure used to process application subforms simply maps the expander over the forms:

(define exp-exprs

(lambda (x* r mr)

(map (lambda (x) (exp x r mr)) x*)))

25.2.10. Core Transformers

Transformers for several representative core forms (quote, if, lambda, let, and letrec-syntax) are described here. Adding transformers for other core forms, such as letrec or let-syntax, is straightforward.

The exp-quote procedure produces an s-expression representing a quote form, with the data value stripped of its syntax wrap:

(define exp-quote

(lambda (x r mr)

(syntax-case x ()

[(_ d) `(quote ,(strip #'d))])))

The exp-if procedure produces an s-expression representation of an if form, with the sub-forms recursively expanded:

(define exp-if

(lambda (x r mr)

(syntax-case x ()

[(_ e1 e2 e3)

`(if ,(exp #'e1 r mr)

,(exp #'e2 r mr)

,(exp #'e3 r mr))])))

The exp-lambda procedure handles lambda expressions that have only a single formal parameter and only a single body expression. Extending it to handle multiple parameters is straightforward. It is less straightforward to handle arbitrary lambda bodies, including internal definitions, but support for internal definitions is beyond the scope of this chapter.

When the s-expression representation of a lambda expression is produced, a generated variable name is created for the formal parameter. A substitution mapping the identifier to a fresh label is added to the wrap on the body, and the environment is extended with an association from the label to a lexical binding whose value is the generated variable,during the recursive processing of the body:

(define exp-lambda

(lambda (x r mr)

(syntax-case x ()

[(_ (var) body)

(let ([label (make-label)] [new-var (gen-var #'var)])

`(lambda (,new-var)

,(exp (add-subst #'var label #'body)

(extend-env label

(make-binding 'lexical new-var)

r)

mr)))])))

The meta environment is not extended because the meta environment should not include lexical variable bindings.

The exp-let procedure that transforms single-binding let forms is similar to the transformer for lambda, but a bit more involved:

(define exp-let

(lambda (x r mr)

(syntax-case x ()

[(_ ([var expr]) body)

(let ([label (make-label)] [new-var (gen-var #'var)])

`(let ([,new-var ,(exp #'expr r mr)])

,(exp (add-subst #'var label #'body)

(extend-env label

(make-binding 'lexical new-var)

r)

mr)))])))

Return Main Page Previous Page Next Page

®Online Book Reader