Re:Writing Complex Macros in Rust: Reverse Polish Notation
by jnordwick on Thursday February 01, 2018 @04:27PM
(#16286065)
Does the evaluate at compile time to a constant or does it produce that series of let bindings?
[ Parent
| on HN ]
Re:Writing Complex Macros in Rust: Reverse Polish Notation
by kazinator on Thursday February 01, 2018 @08:33PM
(#16287627)
Nope; it makes the let bindings. The job of reducing to a constant is better left to the processing of the let rather than duplicated in the macro.
If you have a compiler which can reduce (let ((x 3)) ((y 4)) (+ x y)) to 5, why replicate that in a macro.
If you do not have one (as is the case here), then it's better to work on getting one than compensating for it in macros.
Yet, with the following small change to compile-expr, whereby we avoid generating temps for constant expressions, we can at least get some nicer-looking output:
(defun compile-expr (rct expr)
(cond
((member expr rct.temps) expr)
((constantp expr) expr)
(t (let ((temp (allocate-temp rct)))
(push ^(,temp ,expr) rct.lets)
temp))))
1> (rpn-compile '(3 4 + 5 6 + *))
(let* ((#:g0266 (+ 4 3))
(#:g0267 (+ 6 5))
(#:g0268 (* #:g0267 #:g0266)))
#:g0268)