Changeset 4423 for branches/f4grobner
- Timestamp:
- 2016-06-08T11:04:20-07:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/f4grobner/infix-printer.lisp
r4422 r4423 28 28 (in-package "INFIX-PRINTER") 29 29 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: 34 We should have 35 (+ X (- Y) Z) --> X - Y + Z 36 so when an argument of an expression with operator '+ has an operator '-, we 37 should 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 (/ '*))) 31 48 32 49 (defun infix-print-separated-list (lst sep stream op print-level 33 50 &optional 34 51 (alt-op nil) 35 (alt-sep alt-op)36 52 &aux 37 53 (beg t) … … 44 60 indicates whether this is the start of a sequence of arguments with 45 61 the 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." 62 operator OP for list elements beyond the first one." 48 63 49 64 (cond … … 57 72 (t 58 73 (dolist (arg lst) 59 60 ;;(format t "Processing arg: ~S~%" arg)61 74 (setf true-sep sep) 62 63 75 (incf count) 64 76 77 ;; Treat negative number X as '(- (- X)) 78 (when (and (realp arg) (minusp arg)) 79 (setf arg (list '- (- arg)))) 80 65 81 ;; The following code handles unary minus 66 82 ;; Thus: 67 83 ;; (+ x (- y z)) --> X + Y - Z 68 84 ;; (+ x (- y)) --> X - Y 85 ;; (- x (- y)) --> X + Y 69 86 (when (and alt-op 70 87 (> count 1) … … 72 89 (endp (cddr arg)) 73 90 (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 80 95 (cond 81 96 (beg … … 137 152 ;; Arithmetic operators 138 153 ((+ - * /) 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 ) 150 163 151 164 ;; Exponentials
Note:
See TracChangeset
for help on using the changeset viewer.