close Warning: Can't synchronize with repository "(default)" (The repository directory has changed, you should resynchronize the repository with: trac-admin $ENV repository resync '(default)'). Look in the Trac log for more information.

Changeset 4423


Ignore:
Timestamp:
2016-06-08T11:04:20-07:00 (8 years ago)
Author:
Marek Rychlik
Message:

* empty log message *

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/f4grobner/infix-printer.lisp

    r4422 r4423  
    2828(in-package "INFIX-PRINTER")
    2929
    30 (proclaim '(optimize (speed 0) (space 0) (safety 3) (debug 3)))
     30;;(proclaim '(optimize (speed 0) (space 0) (safety 3) (debug 3)))
     31
     32(defun find-alt-op (op)
     33  "Find ALT-OP, used for arguments above 1. Example:
     34We should have
     35      (+ X (- Y) Z) --> X - Y + Z
     36so when an argument of an expression with operator '+ has an operator '-, we
     37should use '- as a separator, before we render Y. Thus ALT-OP for '+ is '-"
     38  (ecase op
     39    (+ '-)
     40    (- '-)
     41    (* '/)
     42    (/ '/)))
     43
     44(defun inverse-op (op)
     45  (ecase op
     46    (- '+)
     47    (/ '*)))
    3148
    3249(defun infix-print-separated-list (lst sep stream op print-level
    3350                                   &optional
    3451                                     (alt-op nil)
    35                                      (alt-sep alt-op)
    3652                                   &aux
    3753                                     (beg t)
     
    4460indicates whether this is the start of a sequence of arguments with
    4561the main operator OP.  The argument ALT-OP, if not NIL, replaces
    46 operator OP for list elements beyond the first one. Similarly, ALT-SEP
    47 replaces SEP for list elements beyond the first one."
     62operator OP for list elements beyond the first one."
    4863
    4964  (cond
     
    5772   (t
    5873    (dolist (arg lst)
    59 
    60       ;;(format t "Processing arg: ~S~%" arg)
    6174      (setf true-sep sep)
    62 
    6375      (incf count)
    6476         
     77      ;; Treat negative number X as '(- (- X))
     78      (when (and (realp arg) (minusp arg))
     79        (setf arg (list '- (- arg))))
     80
    6581      ;; The following code handles unary minus
    6682      ;; Thus:
    6783      ;; (+ x (- y z)) --> X + Y - Z
    6884      ;; (+ x (- y)) --> X - Y
     85      ;; (- x (- y)) --> X + Y
    6986      (when (and alt-op
    7087                 (> count 1)
     
    7289                 (endp (cddr arg))
    7390                 (eq alt-op (car arg)))
    74         (psetf arg     (cadr arg)
    75                alt-sep nil)
    76         ;;(format t "Arg changed: ~S, true-sep: ~S~%" arg true-sep)
    77         )
    78 
    79 
     91        (psetf arg (cadr arg)
     92               true-sep (if (eq op alt-op) (inverse-op op) alt-op)))
     93       
     94      ;; Unless at the beginning, print the separator
    8095      (cond
    8196        (beg
     
    137152       ;; Arithmetic operators
    138153       ((+ - * /)
    139         (infix-print-separated-list
    140          (cdr expr)                     
    141          (car expr)                     
    142          stream
    143          (car expr)                     
    144          (1+ print-level)               
    145          (cond ((eq (car expr) '+) '-)
    146                ((eq (car expr) '-) '-)
    147                ((eq (car expr) '*) '/)
    148                ((eq (car expr) '/) '/)
    149                (t nil))))
     154       
     155        (let ((op (car expr))
     156              (args (cdr expr)))
     157          (infix-print-separated-list
     158           args op stream op
     159           (1+ print-level)
     160           (find-alt-op op))
     161          )
     162        )
    150163
    151164       ;; Exponentials
Note: See TracChangeset for help on using the changeset viewer.