[parenscript-devel] Refactoring: getting rid of the specialized equality op printer

Daniel Gackle danielgackle at gmail.com
Mon Feb 25 00:52:10 UTC 2013


I pushed a refactoring patch (4a55356 - reproduced below) that
needs some review.

The idea is that the equality operators don't need to be printed
differently from the other operators. They were, in order to allow for
a function #'parenthsize-equality to ensure that expressions like
(= (= a b) c) and (= a (= b c)) are printed with parenthesized operands.
However, as I read the code, there is no possibility that the default
operator printer would ever not parenthesize those operands. In
other words, the mechanism is superfluous. A further explanation
is given in the commit message below.

All the tests pass and I observe no change in the compiled output of
our application, but this is still the kind of change that someone
should look over.

Daniel


commit 4a55356d6d09715fbd10a01c1b9019e013e42663
Author: Daniel Gackle <danielgackle at gmail.com>
Date:   Sun Feb 24 17:09:41 2013 -0700

    Refactoring: got rid of PARENTHESIZE-EQUALITY.

    There is no need for PARENTHESIZE-EQUALITY as distinct from
    PRINT-OP-ARGUMENT, because the latter always parenthesizes equality
    expressions when they are an operand in some other equality
    expression. (In such a case, OP and ARG-OP have the same precedence,
    and neither is associative.)

diff --git a/src/printer.lisp b/src/printer.lisp
index 348f55a..f07a5b2 100644
--- a/src/printer.lisp
+++ b/src/printer.lisp
@@ -145,10 +145,8 @@ vice-versa.")
 (defmethod ps-print ((number number))
   (format *psw-stream* (if (integerp number) "~D" "~F") number))

-(defvar %equality-ops '(ps-js:== ps-js:!= ps-js:=== ps-js:!==))
-
 (let ((precedence-table (make-hash-table :test 'eq)))
-  (loop for level in `((ps-js:getprop ps-js:aref ps-js:funcall)
+  (loop for level in '((ps-js:getprop ps-js:aref ps-js:funcall)
                        (ps-js:new)
                        (ps-js:lambda) ;; you won't find this in JS books
                        (ps-js:++ ps-js:-- ps-js:post++ ps-js:post--)
@@ -157,7 +155,7 @@ vice-versa.")
                        (ps-js:- ps-js:+)
                        (ps-js:<< ps-js:>> ps-js:>>>)
                        (ps-js:< ps-js:> ps-js:<= ps-js:>= ps-js:instanceof
ps-js:in)
-                       ,%equality-ops
+                       (ps-js:== ps-js:!= ps-js:=== ps-js:!==)
                        (ps-js:&)
                        (ps-js:^)
                        (ps-js:\|)
@@ -218,21 +216,12 @@ vice-versa.")
 (defprinter ps-js:post-- (x)
   (ps-print x)"--")

-(defprinter (ps-js:+ ps-js:- ps-js:* ps-js:/ ps-js:% ps-js:&& ps-js:\|\|
ps-js:& ps-js:\| ps-js:-= ps-js:+= ps-js:*= ps-js:/= ps-js:%= ps-js:^
ps-js:<< ps-js:>> ps-js:&= ps-js:^= ps-js:\|= ps-js:= ps-js:in ps-js:>
ps-js:>= ps-js:< ps-js:<=)
+(defprinter (ps-js:+ ps-js:- ps-js:* ps-js:/ ps-js:% ps-js:&& ps-js:\|\|
ps-js:& ps-js:\| ps-js:-= ps-js:+= ps-js:*= ps-js:/= ps-js:%= ps-js:^
ps-js:<< ps-js:>> ps-js:&= ps-js:^= ps-js:\|= ps-js:= ps-js:in ps-js:>
ps-js:>= ps-js:< ps-js:<= ps-js:== ps-js:!= ps-js:=== ps-js:!==)
     (&rest args)
   (loop for (arg . remaining) on args do
        (print-op-argument op arg)
        (when remaining (format *psw-stream* " ~(~A~) " op))))

-(defprinter (ps-js:== ps-js:!= ps-js:=== ps-js:!==) (x y)
-  (flet ((parenthesize-equality (form)
-           (if (and (consp form) (member (car form) %equality-ops))
-               (parenthesize-print form)
-               (print-op-argument op form))))
-    (parenthesize-equality x)
-    (format *psw-stream* " ~A " op)
-    (parenthesize-equality y)))
-
 (defprinter ps-js:aref (array &rest indices)
   (print-op-argument 'ps-js:aref array)
   (dolist (idx indices)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.common-lisp.net/pipermail/parenscript-devel/attachments/20130224/ffb6989a/attachment.html>


More information about the parenscript-devel mailing list