diff options
author | Kaz Kylheku <kaz@kylheku.com> | 2025-06-20 20:54:43 -0700 |
---|---|---|
committer | Kaz Kylheku <kaz@kylheku.com> | 2025-06-20 20:54:43 -0700 |
commit | c11c2304890a46073349d938b1d17a1e26b093d9 (patch) | |
tree | 08b4a99847293e9519f0032d15c01fe601199d11 /stdlib/optimize.tl | |
parent | 6cd91580c3d709c21db40b919a9f1cce8ccd0675 (diff) | |
download | txr-c11c2304890a46073349d938b1d17a1e26b093d9.tar.gz txr-c11c2304890a46073349d938b1d17a1e26b093d9.tar.bz2 txr-c11c2304890a46073349d938b1d17a1e26b093d9.zip |
compiler: rewrite the rewrite function.
* stdlib/optimize.tl (rewrite): Rewrite. The function is
begging to be rewritten. I mean, just look at its name!
This is not just for shits and giggles. The rewrite makes it
tail-recursive, so it makes a test case for TCO. Instead of
using a list-builder object, it does the traditional thing:
builds the output in reverse and then calls nreverse. The
generated code is lovely.
(rewrite-case): Pass a nil argument for the new accumulator
parameter of rewrite.
Diffstat (limited to 'stdlib/optimize.tl')
-rw-r--r-- | stdlib/optimize.tl | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/stdlib/optimize.tl b/stdlib/optimize.tl index 906abdb8..d72be2fc 100644 --- a/stdlib/optimize.tl +++ b/stdlib/optimize.tl @@ -135,7 +135,7 @@ ^(rewrite (lambda (,sym) (match-case ,sym ,*cases)) - ,list)) + ,list nil)) (defmeth basic-blocks link-graph (bb : first-time) (unless first-time @@ -868,13 +868,15 @@ (each ((cl clist)) cl.(apply-treg-compacting-map map)))))) -(defun rewrite (fun list) - (build - (while* list - (let ((nlist [fun list])) - (if (eq list nlist) - (if list (add (pop list))) - (set list nlist)))))) +(defun rewrite (fun list out) + (let ((nlist [fun list])) + (if (eq list nlist) + (if list + (push (pop list) out)) + (set list nlist)) + (if list + (rewrite fun list out) + (nreverse out)))) (defun dedup-labels (insns) (rewrite-case tail insns |