From 8e147f798978b986ad9f5f3436d3551ae95979a6 Mon Sep 17 00:00:00 2001 From: Jesse McDonald Date: Thu, 15 Apr 2010 00:11:32 -0500 Subject: [PATCH] Don't create extra lambdas when both results of (if ...) are trivial. Also map () directly to %nil, and allow (%if ...) to provide a value to a set! form. --- compiler.ss | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/compiler.ss b/compiler.ss index da47a05..2fafc97 100755 --- a/compiler.ss +++ b/compiler.ss @@ -23,7 +23,7 @@ (cdr form))] [(%bind %if %tail-call %apply %lambda %set! %cons %car %cdr) form] [else (simplify-funcall form)]) - form)) + (if (eq? form '()) '%nil form))) (define (simplify-set! form) (let ([value-form (simplify-form (third form))]) @@ -33,7 +33,7 @@ (if (pair? after) (cons subform after) (if (or (not (pair? subform)) - (memq (first subform) '(%apply %car %cdr %cons %bind))) + (memq (first subform) '(%apply %car %cdr %cons %bind %if))) (list (simplify-set! `(set! ,(second form) ,subform))) (list subform)))) '() @@ -125,11 +125,14 @@ (define-values (cond-expr true-expr false-expr) (apply values (cdr form))) (simplify-form - `(let ([,cond-val ,cond-expr] - [,true-fn (lambda () ,true-expr)] - [,false-fn (lambda () ,false-expr)]) - (let ([,next-fn (%if ,cond-val ,true-fn ,false-fn)]) - (%apply ,next-fn %nil))))) + (if (or (pair? true-expr) (pair? false-expr)) + `(let ([,cond-val ,cond-expr] + [,true-fn (lambda () ,true-expr)] + [,false-fn (lambda () ,false-expr)]) + (let ([,next-fn (%if ,cond-val ,true-fn ,false-fn)]) + (%apply ,next-fn %nil))) + `(let ([,cond-val ,cond-expr]) + (%if ,cond-val ,(simplify-form true-expr) ,(simplify-form false-expr)))))) ; (lambda (required... [optional default-expr]... . rest) bodyexpr...) ; => (lambda argv