summaryrefslogtreecommitdiffstats
path: root/stdlib
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2025-06-20 20:54:43 -0700
committerKaz Kylheku <kaz@kylheku.com>2025-06-20 20:54:43 -0700
commitc11c2304890a46073349d938b1d17a1e26b093d9 (patch)
tree08b4a99847293e9519f0032d15c01fe601199d11 /stdlib
parent6cd91580c3d709c21db40b919a9f1cce8ccd0675 (diff)
downloadtxr-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')
-rw-r--r--stdlib/optimize.tl18
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