| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
(require 'calc-ext) |
|---|
| 33 |
(require 'calc-macs) |
|---|
| 34 |
|
|---|
| 35 |
(defvar math-rewrite-default-iters 100) |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
(defvar calc-rewr-sel) |
|---|
| 40 |
|
|---|
| 41 |
(defun calc-rewrite-selection (rules-str &optional many prefix) |
|---|
| 42 |
(interactive "sRewrite rule(s): \np") |
|---|
| 43 |
(calc-slow-wrapper |
|---|
| 44 |
(calc-preserve-point) |
|---|
| 45 |
(let* ((num (max 1 (calc-locate-cursor-element (point)))) |
|---|
| 46 |
(reselect t) |
|---|
| 47 |
(pop-rules nil) |
|---|
| 48 |
rules |
|---|
| 49 |
(entry (calc-top num 'entry)) |
|---|
| 50 |
(expr (car entry)) |
|---|
| 51 |
(calc-rewr-sel (calc-auto-selection entry)) |
|---|
| 52 |
(math-rewrite-selections t) |
|---|
| 53 |
(math-rewrite-default-iters 1)) |
|---|
| 54 |
(if (or (null rules-str) (equal rules-str "") (equal rules-str "$")) |
|---|
| 55 |
(if (= num 1) |
|---|
| 56 |
(error "Can't use same stack entry for formula and rules") |
|---|
| 57 |
(setq rules (calc-top-n 1 t) |
|---|
| 58 |
pop-rules t)) |
|---|
| 59 |
(setq rules (if (stringp rules-str) |
|---|
| 60 |
(math-read-exprs rules-str) rules-str)) |
|---|
| 61 |
(if (eq (car-safe rules) 'error) |
|---|
| 62 |
(error "Bad format in expression: %s" (nth 1 rules))) |
|---|
| 63 |
(if (= (length rules) 1) |
|---|
| 64 |
(setq rules (car rules)) |
|---|
| 65 |
(setq rules (cons 'vec rules))) |
|---|
| 66 |
(or (memq (car-safe rules) '(vec var calcFunc-assign |
|---|
| 67 |
calcFunc-condition)) |
|---|
| 68 |
(let ((rhs (math-read-expr |
|---|
| 69 |
(read-string (concat "Rewrite from: " rules-str |
|---|
| 70 |
" to: "))))) |
|---|
| 71 |
(if (eq (car-safe rhs) 'error) |
|---|
| 72 |
(error "Bad format in expression: %s" (nth 1 rhs))) |
|---|
| 73 |
(setq rules (list 'calcFunc-assign rules rhs)))) |
|---|
| 74 |
(or (eq (car-safe rules) 'var) |
|---|
| 75 |
(calc-record rules "rule"))) |
|---|
| 76 |
(if (eq many 0) |
|---|
| 77 |
(setq many '(var inf var-inf)) |
|---|
| 78 |
(if many (setq many (prefix-numeric-value many)))) |
|---|
| 79 |
(if calc-rewr-sel |
|---|
| 80 |
(setq expr (calc-replace-sub-formula (car entry) |
|---|
| 81 |
calc-rewr-sel |
|---|
| 82 |
(list 'calcFunc-select calc-rewr-sel))) |
|---|
| 83 |
(setq expr (car entry) |
|---|
| 84 |
reselect nil |
|---|
| 85 |
math-rewrite-selections nil)) |
|---|
| 86 |
(setq expr (calc-encase-atoms |
|---|
| 87 |
(calc-normalize |
|---|
| 88 |
(math-rewrite |
|---|
| 89 |
(calc-normalize expr) |
|---|
| 90 |
rules many))) |
|---|
| 91 |
calc-rewr-sel nil |
|---|
| 92 |
expr (calc-locate-select-marker expr)) |
|---|
| 93 |
(or (consp calc-rewr-sel) (setq calc-rewr-sel nil)) |
|---|
| 94 |
(if pop-rules (calc-pop-stack 1)) |
|---|
| 95 |
(calc-pop-push-record-list 1 (or prefix "rwrt") (list expr) |
|---|
| 96 |
(- num (if pop-rules 1 0)) |
|---|
| 97 |
(list (and reselect calc-rewr-sel)))) |
|---|
| 98 |
(calc-handle-whys))) |
|---|
| 99 |
|
|---|
| 100 |
(defun calc-locate-select-marker (expr) |
|---|
| 101 |
(if (Math-primp expr) |
|---|
| 102 |
expr |
|---|
| 103 |
(if (and (eq (car expr) 'calcFunc-select) |
|---|
| 104 |
(= (length expr) 2)) |
|---|
| 105 |
(progn |
|---|
| 106 |
(setq calc-rewr-sel (if calc-rewr-sel t (nth 1 expr))) |
|---|
| 107 |
(nth 1 expr)) |
|---|
| 108 |
(cons (car expr) |
|---|
| 109 |
(mapcar 'calc-locate-select-marker (cdr expr)))))) |
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
(defun calc-rewrite (rules-str many) |
|---|
| 114 |
(interactive "sRewrite rule(s): \nP") |
|---|
| 115 |
(calc-slow-wrapper |
|---|
| 116 |
(let (n rules expr) |
|---|
| 117 |
(if (or (null rules-str) (equal rules-str "") (equal rules-str "$")) |
|---|
| 118 |
(setq expr (calc-top-n 2) |
|---|
| 119 |
rules (calc-top-n 1 t) |
|---|
| 120 |
n 2) |
|---|
| 121 |
(setq rules (if (stringp rules-str) |
|---|
| 122 |
(math-read-exprs rules-str) rules-str)) |
|---|
| 123 |
(if (eq (car-safe rules) 'error) |
|---|
| 124 |
(error "Bad format in expression: %s" (nth 1 rules))) |
|---|
| 125 |
(if (= (length rules) 1) |
|---|
| 126 |
(setq rules (car rules)) |
|---|
| 127 |
(setq rules (cons 'vec rules))) |
|---|
| 128 |
(or (memq (car-safe rules) '(vec var calcFunc-assign |
|---|
| 129 |
calcFunc-condition)) |
|---|
| 130 |
(let ((rhs (math-read-expr |
|---|
| 131 |
(read-string (concat "Rewrite from: " rules-str |
|---|
| 132 |
" to: "))))) |
|---|
| 133 |
(if (eq (car-safe rhs) 'error) |
|---|
| 134 |
(error "Bad format in expression: %s" (nth 1 rhs))) |
|---|
| 135 |
(setq rules (list 'calcFunc-assign rules rhs)))) |
|---|
| 136 |
(or (eq (car-safe rules) 'var) |
|---|
| 137 |
(calc-record rules "rule")) |
|---|
| 138 |
(setq expr (calc-top-n 1) |
|---|
| 139 |
n 1)) |
|---|
| 140 |
(if (eq many 0) |
|---|
| 141 |
(setq many '(var inf var-inf)) |
|---|
| 142 |
(if many (setq many (prefix-numeric-value many)))) |
|---|
| 143 |
(setq expr (calc-normalize (math-rewrite expr rules many))) |
|---|
| 144 |
(let (calc-rewr-sel) |
|---|
| 145 |
(setq expr (calc-locate-select-marker expr))) |
|---|
| 146 |
(calc-pop-push-record-list n "rwrt" (list expr))) |
|---|
| 147 |
(calc-handle-whys))) |
|---|
| 148 |
|
|---|
| 149 |
(defun calc-match (pat &optional interactive) |
|---|
| 150 |
(interactive "sPattern: \np") |
|---|
| 151 |
(calc-slow-wrapper |
|---|
| 152 |
(let (n expr) |
|---|
| 153 |
(if (or (null pat) (equal pat "") (equal pat "$")) |
|---|
| 154 |
(setq expr (calc-top-n 2) |
|---|
| 155 |
pat (calc-top-n 1) |
|---|
| 156 |
n 2) |
|---|
| 157 |
(setq pat (if (stringp pat) (math-read-expr pat) pat)) |
|---|
| 158 |
(if (eq (car-safe pat) 'error) |
|---|
| 159 |
(error "Bad format in expression: %s" (nth 1 pat))) |
|---|
| 160 |
(if (not (eq (car-safe pat) 'var)) |
|---|
| 161 |
(calc-record pat "pat")) |
|---|
| 162 |
(setq expr (calc-top-n 1) |
|---|
| 163 |
n 1)) |
|---|
| 164 |
(or (math-vectorp expr) (error "Argument must be a vector")) |
|---|
| 165 |
(if (calc-is-inverse) |
|---|
| 166 |
(calc-enter-result n "mtcn" (math-match-patterns pat expr t)) |
|---|
| 167 |
(calc-enter-result n "mtch" (math-match-patterns pat expr nil)))))) |
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 |
(defvar math-mt-many) |
|---|
| 171 |
|
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
(defvar math-rewrite-whole-expr) |
|---|
| 175 |
|
|---|
| 176 |
(defun math-rewrite (math-rewrite-whole-expr rules &optional math-mt-many) |
|---|
| 177 |
(let* ((crules (math-compile-rewrites rules)) |
|---|
| 178 |
(heads (math-rewrite-heads math-rewrite-whole-expr)) |
|---|
| 179 |
(trace-buffer (get-buffer "*Trace*")) |
|---|
| 180 |
(calc-display-just 'center) |
|---|
| 181 |
(calc-display-origin 39) |
|---|
| 182 |
(calc-line-breaking 78) |
|---|
| 183 |
(calc-line-numbering nil) |
|---|
| 184 |
(calc-show-selections t) |
|---|
| 185 |
(calc-why nil) |
|---|
| 186 |
(math-mt-func (function |
|---|
| 187 |
(lambda (x) |
|---|
| 188 |
(let ((result (math-apply-rewrites x (cdr crules) |
|---|
| 189 |
heads crules))) |
|---|
| 190 |
(if result |
|---|
| 191 |
(progn |
|---|
| 192 |
(if trace-buffer |
|---|
| 193 |
(let ((fmt (math-format-stack-value |
|---|
| 194 |
(list result nil nil)))) |
|---|
| 195 |
(save-excursion |
|---|
| 196 |
(set-buffer trace-buffer) |
|---|
| 197 |
(insert "\nrewrite to\n" fmt "\n")))) |
|---|
| 198 |
(setq heads (math-rewrite-heads result heads t)))) |
|---|
| 199 |
result))))) |
|---|
| 200 |
(if trace-buffer |
|---|
| 201 |
(let ((fmt (math-format-stack-value (list math-rewrite-whole-expr nil nil)))) |
|---|
| 202 |
(save-excursion |
|---|
| 203 |
(set-buffer trace-buffer) |
|---|
| 204 |
(setq truncate-lines t) |
|---|
| 205 |
(goto-char (point-max)) |
|---|
| 206 |
(insert "\n\nBegin rewriting\n" fmt "\n")))) |
|---|
| 207 |
(or math-mt-many (setq math-mt-many (or (nth 1 (car crules)) |
|---|
| 208 |
math-rewrite-default-iters))) |
|---|
| 209 |
(if (equal math-mt-many '(var inf var-inf)) (setq math-mt-many 1000000)) |
|---|
| 210 |
(if (equal math-mt-many '(neg (var inf var-inf))) (setq math-mt-many -1000000)) |
|---|
| 211 |
(math-rewrite-phase (nth 3 (car crules))) |
|---|
| 212 |
(if trace-buffer |
|---|
| 213 |
(let ((fmt (math-format-stack-value (list math-rewrite-whole-expr nil nil)))) |
|---|
| 214 |
(save-excursion |
|---|
| 215 |
(set-buffer trace-buffer) |
|---|
| 216 |
(insert "\nDone rewriting" |
|---|
| 217 |
(if (= math-mt-many 0) " (reached iteration limit)" "") |
|---|
| 218 |
":\n" fmt "\n")))) |
|---|
| 219 |
math-rewrite-whole-expr)) |
|---|
| 220 |
|
|---|
| 221 |
(defun math-rewrite-phase (sched) |
|---|
| 222 |
(while (and sched (/= math-mt-many 0)) |
|---|
| 223 |
(if (listp (car sched)) |
|---|
| 224 |
(while (let ((save-expr math-rewrite-whole-expr)) |
|---|
| 225 |
(math-rewrite-phase (car sched)) |
|---|
| 226 |
(not (equal math-rewrite-whole-expr save-expr)))) |
|---|
| 227 |
(if (symbolp (car sched)) |
|---|
| 228 |
(progn |
|---|
| 229 |
(setq math-rewrite-whole-expr |
|---|
| 230 |
(math-normalize (list (car sched) math-rewrite-whole-expr))) |
|---|
| 231 |
(if trace-buffer |
|---|
| 232 |
(let ((fmt (math-format-stack-value |
|---|
| 233 |
(list math-rewrite-whole-expr nil nil)))) |
|---|
| 234 |
(save-excursion |
|---|
| 235 |
(set-buffer trace-buffer) |
|---|
| 236 |
(insert "\ncall " |
|---|
| 237 |
(substring (symbol-name (car sched)) 9) |
|---|
| 238 |
":\n" fmt "\n"))))) |
|---|
| 239 |
(let ((math-rewrite-phase (car sched))) |
|---|
| 240 |
(if trace-buffer |
|---|
| 241 |
(save-excursion |
|---|
| 242 |
(set-buffer trace-buffer) |
|---|
| 243 |
(insert (format "\n(Phase %d)\n" math-rewrite-phase)))) |
|---|
| 244 |
(while (let ((save-expr math-rewrite-whole-expr)) |
|---|
| 245 |
(setq math-rewrite-whole-expr (math-normalize |
|---|
| 246 |
(math-map-tree-rec math-rewrite-whole-expr))) |
|---|
| 247 |
(not (equal math-rewrite-whole-expr save-expr))))))) |
|---|
| 248 |
(setq sched (cdr sched)))) |
|---|
| 249 |
|
|---|
| 250 |
(defun calcFunc-rewrite (expr rules &optional many) |
|---|
| 251 |
(or (null many) (integerp many) |
|---|
| 252 |
(equal many '(var inf var-inf)) (equal many '(neg (var inf var-inf))) |
|---|
| 253 |
(math-reject-arg many 'fixnump)) |
|---|
| 254 |
(condition-case err |
|---|
| 255 |
(math-rewrite expr rules (or many 1)) |
|---|
| 256 |
(error (math-reject-arg rules (nth 1 err))))) |
|---|
| 257 |
|
|---|
| 258 |
(defun calcFunc-match (pat vec) |
|---|
| 259 |
(or (math-vectorp vec) (math-reject-arg vec 'vectorp)) |
|---|
| 260 |
(condition-case err |
|---|
| 261 |
(math-match-patterns pat vec nil) |
|---|
| 262 |
(error (math-reject-arg pat (nth 1 err))))) |
|---|
| 263 |
|
|---|
| 264 |
(defun calcFunc-matchnot (pat vec) |
|---|
| 265 |
(or (math-vectorp vec) (math-reject-arg vec 'vectorp)) |
|---|
| 266 |
(condition-case err |
|---|
| 267 |
(math-match-patterns pat vec t) |
|---|
| 268 |
(error (math-reject-arg pat (nth 1 err))))) |
|---|
| 269 |
|
|---|
| 270 |
(defun math-match-patterns (pat vec &optional not-flag) |
|---|
| 271 |
(let ((newvec nil) |
|---|
| 272 |
(crules (math-compile-patterns pat))) |
|---|
| 273 |
(while (setq vec (cdr vec)) |
|---|
| 274 |
(if (eq (not (math-apply-rewrites (car vec) crules)) |
|---|
| 275 |
not-flag) |
|---|
| 276 |
(setq newvec (cons (car vec) newvec)))) |
|---|
| 277 |
(cons 'vec (nreverse newvec)))) |
|---|
| 278 |
|
|---|
| 279 |
(defun calcFunc-matches (expr pat) |
|---|
| 280 |
(condition-case err |
|---|
| 281 |
(if (math-apply-rewrites expr (math-compile-patterns pat)) |
|---|
| 282 |
1 |
|---|
| 283 |
0) |
|---|
| 284 |
(error (math-reject-arg pat (nth 1 err))))) |
|---|
| 285 |
|
|---|
| 286 |
(defun calcFunc-vmatches (expr pat) |
|---|
| 287 |
(condition-case err |
|---|
| 288 |
(or (math-apply-rewrites expr (math-compile-patterns pat)) |
|---|
| 289 |
0) |
|---|
| 290 |
(error (math-reject-arg pat (nth 1 err))))) |
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 |
|
|---|
| 297 |
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 |
|
|---|
| 309 |
|
|---|
| 310 |
|
|---|
| 311 |
|
|---|
| 312 |
|
|---|
| 313 |
|
|---|
| 314 |
|
|---|
| 315 |
|
|---|
| 316 |
|
|---|
| 317 |
|
|---|
| 318 |
|
|---|
| 319 |
|
|---|
| 320 |
|
|---|
| 321 |
|
|---|
| 322 |
|
|---|
| 323 |
|
|---|
| 324 |
|
|---|
| 325 |
|
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
|
|---|
| 341 |
|
|---|
| 342 |
|
|---|
| 343 |
|
|---|
| 344 |
|
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 |
|
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
|
|---|
| 363 |
|
|---|
| 364 |
|
|---|
| 365 |
|
|---|
| 366 |
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
|
|---|
| 372 |
|
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 376 |
|
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 |
|
|---|
| 382 |
|
|---|
| 383 |
|
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
|
|---|
| 394 |
|
|---|
| 395 |
|
|---|
| 396 |
|
|---|
| 397 |
|
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 |
|
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
|
|---|
| 404 |
|
|---|
| 405 |
|
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
|
|---|
| 411 |
|
|---|
| 412 |
|
|---|
| 413 |
|
|---|
| 414 |
|
|---|
| 415 |
|
|---|
| 416 |
|
|---|
| 417 |
|
|---|
| 418 |
|
|---|
| 419 |
|
|---|
| 420 |
|
|---|
| 421 |
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 |
|
|---|
| 425 |
|
|---|
| 426 |
|
|---|
| 427 |
|
|---|
| 428 |
|
|---|
| 429 |
|
|---|
| 430 |
|
|---|
| 431 |
|
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
|
|---|
| 435 |
|
|---|
| 436 |
|
|---|
| 437 |
|
|---|
| 438 |
|
|---|
| 439 |
|
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 |
|
|---|
| 443 |
|
|---|
| 444 |
|
|---|
| 445 |
|
|---|
| 446 |
|
|---|
| 447 |
|
|---|
| 448 |
|
|---|
| 449 |
|
|---|
| 450 |
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 |
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 |
|
|---|
| 457 |
|
|---|
| 458 |
|
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
|
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
|
|---|
| 466 |
|
|---|
| 467 |
|
|---|
| 468 |
|
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
|
|---|
| 472 |
|
|---|
| 473 |
|
|---|
| 474 |
|
|---|
| 475 |
|
|---|
| 476 |
(defun math-compile-patterns (pats) |
|---|
| 477 |
(if (and (eq (car-safe pats) 'var) |
|---|
| 478 |
(calc-var-value (nth 2 pats))) |
|---|
| 479 |
(let ((prop (get (nth 2 pats) 'math-pattern-cache))) |
|---|
| 480 |
(or prop |
|---|
| 481 |
(put (nth 2 pats) 'math-pattern-cache (setq prop (list nil)))) |
|---|
| 482 |
(or (eq (car prop) (symbol-value (nth 2 pats))) |
|---|
| 483 |
(progn |
|---|
| 484 |
(setcdr prop (math-compile-patterns |
|---|
| 485 |
(symbol-value (nth 2 pats)))) |
|---|
| 486 |
(setcar prop (symbol-value (nth 2 pats))))) |
|---|
| 487 |
(cdr prop)) |
|---|
| 488 |
(let ((math-rewrite-whole t)) |
|---|
| 489 |
(cdr (math-compile-rewrites (cons |
|---|
| 490 |
'vec |
|---|
| 491 |
(mapcar (function (lambda (x) |
|---|
| 492 |
(list 'vec x t))) |
|---|
| 493 |
(if (eq (car-safe pats) 'vec) |
|---|
| 494 |
(cdr pats) |
|---|
| 495 |
(list pats))))))))) |
|---|
| 496 |
|
|---|
| 497 |
(defvar math-rewrite-whole nil) |
|---|
| 498 |
(defvar math-make-import-list nil) |
|---|
| 499 |
|
|---|
| 500 |
|
|---|
| 501 |
|
|---|
| 502 |
|
|---|
| 503 |
(defvar math-import-list nil) |
|---|
| 504 |
|
|---|
| 505 |
|
|---|
| 506 |
|
|---|
| 507 |
|
|---|
| 508 |
|
|---|
| 509 |
|
|---|
| 510 |
(defvar math-regs) |
|---|
| 511 |
(defvar math-num-regs) |
|---|
| 512 |
(defvar math-prog-last) |
|---|
| 513 |
(defvar math-bound-vars) |
|---|
| 514 |
(defvar math-conds) |
|---|
| 515 |
(defvar math-copy-neg) |
|---|
| 516 |
(defvar math-rhs) |
|---|
| 517 |
(defvar math-pattern) |
|---|
| 518 |
(defvar math-remembering) |
|---|
| 519 |
(defvar math-aliased-vars) |
|---|
| 520 |
|
|---|
| 521 |
(defun math-compile-rewrites (rules &optional name) |
|---|
| 522 |
(if (eq (car-safe rules) 'var) |
|---|
| 523 |
(let ((prop (get (nth 2 rules) 'math-rewrite-cache)) |
|---|
| 524 |
(math-import-list nil) |
|---|
| 525 |
(math-make-import-list t) |
|---|
| 526 |
p) |
|---|
| 527 |
(or (calc-var-value (nth 2 rules)) |
|---|
| 528 |
(error "Rules variable %s has no stored value" (nth 1 rules))) |
|---|
| 529 |
(or prop |
|---|
| 530 |
(put (nth 2 rules) 'math-rewrite-cache |
|---|
| 531 |
(setq prop (list (list (cons (nth 2 rules) nil)))))) |
|---|
| 532 |
(setq p (car prop)) |
|---|
| 533 |
(while (and p (eq (symbol-value (car (car p))) (cdr (car p)))) |
|---|
| 534 |
(setq p (cdr p))) |
|---|
| 535 |
(or (null p) |
|---|
| 536 |
(progn |
|---|
| 537 |
(message "Compiling rule set %s..." (nth 1 rules)) |
|---|
| 538 |
(setcdr prop (math-compile-rewrites |
|---|
| 539 |
(symbol-value (nth 2 rules)) |
|---|
| 540 |
(nth 2 rules))) |
|---|
| 541 |
(message "Compiling rule set %s...done" (nth 1 rules)) |
|---|
| 542 |
(setcar prop (cons (cons (nth 2 rules) |
|---|
| 543 |
(symbol-value (nth 2 rules))) |
|---|
| 544 |
math-import-list)))) |
|---|
| 545 |
(cdr prop)) |
|---|
| 546 |
(if (or (not (eq (car-safe rules) 'vec)) |
|---|
| 547 |
(and (memq (length rules) '(3 4)) |
|---|
| 548 |
(let ((p rules)) |
|---|
| 549 |
(while (and (setq p (cdr p)) |
|---|
| 550 |
(memq (car-safe (car p)) |
|---|
| 551 |
'(vec |
|---|
| 552 |
calcFunc-assign |
|---|
| 553 |
calcFunc-condition |
|---|
| 554 |
calcFunc-import |
|---|
| 555 |
calcFunc-phase |
|---|
| 556 |
calcFunc-schedule |
|---|
| 557 |
calcFunc-iterations)))) |
|---|
| 558 |
p))) |
|---|
| 559 |
(setq rules (list rules)) |
|---|
| 560 |
(setq rules (cdr rules))) |
|---|
| 561 |
(if (assq 'calcFunc-import rules) |
|---|
| 562 |
(let ((pp (setq rules (copy-sequence rules))) |
|---|
| 563 |
p part) |
|---|
| 564 |
(while (setq p (car (cdr pp))) |
|---|
| 565 |
(if (eq (car-safe p) 'calcFunc-import) |
|---|
| 566 |
(progn |
|---|
| 567 |
(setcdr pp (cdr (cdr pp))) |
|---|
| 568 |
(or (and (eq (car-safe (nth 1 p)) 'var) |
|---|
| 569 |
(setq part (calc-var-value (nth 2 (nth 1 p)))) |
|---|
| 570 |
(memq (car-safe part) '(vec |
|---|
| 571 |
calcFunc-assign |
|---|
| 572 |
calcFunc-condition))) |
|---|
| 573 |
(error "Argument of import() must be a rules variable")) |
|---|
| 574 |
(if math-make-import-list |
|---|
| 575 |
(setq math-import-list |
|---|
| 576 |
(cons (cons (nth 2 (nth 1 p)) |
|---|
| 577 |
(symbol-value (nth 2 (nth 1 p)))) |
|---|
| 578 |
math-import-list))) |
|---|
| 579 |
(while (setq p (cdr (cdr p))) |
|---|
| 580 |
(or (cdr p) |
|---|
| 581 |
(error "import() must have odd number of arguments")) |
|---|
| 582 |
(setq part (math-rwcomp-substitute part |
|---|
| 583 |
(car p) (nth 1 p)))) |
|---|
| 584 |
(if (eq (car-safe part) 'vec) |
|---|
| 585 |
(setq part (cdr part)) |
|---|
| 586 |
(setq part (list part))) |
|---|
| 587 |
(setcdr pp (append part (cdr pp)))) |
|---|
| 588 |
(setq pp (cdr pp)))))) |
|---|
| 589 |
(let ((rule-set nil) |
|---|
| 590 |
(all-heads nil) |
|---|
| 591 |
(nil-rules nil) |
|---|
| 592 |
(rule-count 0) |
|---|
| 593 |
(math-schedule nil) |
|---|
| 594 |
(math-iterations nil) |
|---|
| 595 |
(math-phases nil) |
|---|
| 596 |
(math-all-phases nil) |
|---|
| 597 |
(math-remembering nil) |
|---|
| 598 |
math-pattern math-rhs math-conds) |
|---|
| 599 |
(while rules |
|---|
| 600 |
(cond |
|---|
| 601 |
((and (eq (car-safe (car rules)) 'calcFunc-iterations) |
|---|
| 602 |
(= (length (car rules)) 2)) |
|---|
| 603 |
(or (integerp (nth 1 (car rules))) |
|---|
| 604 |
(equal (nth 1 (car rules)) '(var inf var-inf)) |
|---|
| 605 |
(equal (nth 1 (car rules)) '(neg (var inf var-inf))) |
|---|
| 606 |
(error "Invalid argument for iterations(n)")) |
|---|
| 607 |
(or math-iterations |
|---|
| 608 |
(setq math-iterations (nth 1 (car rules))))) |
|---|
| 609 |
((eq (car-safe (car rules)) 'calcFunc-schedule) |
|---|
| 610 |
(or math-schedule |
|---|
| 611 |
(setq math-schedule (math-parse-schedule (cdr (car rules)))))) |
|---|
| 612 |
((eq (car-safe (car rules)) 'calcFunc-phase) |
|---|
| 613 |
(setq math-phases (cdr (car rules))) |
|---|
| 614 |
(if (equal math-phases '((var all var-all))) |
|---|
| 615 |
(setq math-phases nil)) |
|---|
| 616 |
(let ((p math-phases)) |
|---|
| 617 |
(while p |
|---|
| 618 |
(or (integerp (car p)) |
|---|
| 619 |
(error "Phase numbers must be small integers")) |
|---|
| 620 |
(or (memq (car p) math-all-phases) |
|---|
| 621 |
(setq math-all-phases (cons (car p) math-all-phases))) |
|---|
| 622 |
(setq p (cdr p))))) |
|---|
| 623 |
((or (and (eq (car-safe (car rules)) 'vec) |
|---|
| 624 |
(cdr (cdr (car rules))) |
|---|
| 625 |
(not (nthcdr 4 (car rules))) |
|---|
| 626 |
(setq math-conds (nth 3 (car rules)) |
|---|
| 627 |
math-rhs (nth 2 (car rules)) |
|---|
| 628 |
math-pattern (nth 1 (car rules)))) |
|---|
| 629 |
(progn |
|---|
| 630 |
(setq math-conds nil |
|---|
| 631 |
math-pattern (car rules)) |
|---|
| 632 |
(while (and (eq (car-safe math-pattern) 'calcFunc-condition) |
|---|
| 633 |
(= (length math-pattern) 3)) |
|---|
| 634 |
(let ((cond (nth 2 math-pattern))) |
|---|
| 635 |
(setq math-conds (if math-conds |
|---|
| 636 |
(list 'calcFunc-land math-conds cond) |
|---|
| 637 |
cond) |
|---|
| 638 |
math-pattern (nth 1 math-pattern)))) |
|---|
| 639 |
(and (eq (car-safe math-pattern) 'calcFunc-assign) |
|---|
| 640 |
(= (length math-pattern) 3) |
|---|
| 641 |
(setq math-rhs (nth 2 math-pattern) |
|---|
| 642 |
math-pattern (nth 1 math-pattern))))) |
|---|
| 643 |
(let* ((math-prog (list nil)) |
|---|
| 644 |
(math-prog-last math-prog) |
|---|
| 645 |
(math-num-regs 1) |
|---|
| 646 |
(math-regs (list (list nil 0 nil nil))) |
|---|
| 647 |
(math-bound-vars nil) |
|---|
| 648 |
(math-aliased-vars nil) |
|---|
| 649 |
(math-copy-neg nil)) |
|---|
| 650 |
(setq math-conds (and math-conds (math-flatten-lands math-conds))) |
|---|
| 651 |
(math-rwcomp-pattern math-pattern 0) |
|---|
| 652 |
(while math-conds |
|---|
| 653 |
(let ((expr (car math-conds))) |
|---|
| 654 |
(setq math-conds (cdr math-conds)) |
|---|
| 655 |
(math-rwcomp-cond-instr expr))) |
|---|
| 656 |
(math-rwcomp-instr 'done |
|---|
| 657 |
(if (eq math-rhs t) |
|---|
| 658 |
(cons 'vec |
|---|
| 659 |
(delq |
|---|
| 660 |
nil |
|---|
| 661 |
(nreverse |
|---|
| 662 |
(mapcar |
|---|
| 663 |
(function |
|---|
| 664 |
(lambda (v) |
|---|
| 665 |
(and (car v) |
|---|
| 666 |
(list |
|---|
| 667 |
'calcFunc-assign |
|---|
| 668 |
(math-build-var-name |
|---|
| 669 |
(car v)) |
|---|
| 670 |
(math-rwcomp-register-expr |
|---|
| 671 |
(nth 1 v)))))) |
|---|
| 672 |
math-regs)))) |
|---|
| 673 |
(math-rwcomp-match-vars math-rhs)) |
|---|
| 674 |
math-remembering) |
|---|
| 675 |
(setq math-prog (cdr math-prog)) |
|---|
| 676 |
(let* ((heads (math-rewrite-heads math-pattern)) |
|---|
| 677 |
(rule (list (vconcat |
|---|
| 678 |
(nreverse |
|---|
| 679 |
(mapcar (function (lambda (x) (nth 3 x))) |
|---|
| 680 |
math-regs))) |
|---|
| 681 |
math-prog |
|---|
| 682 |
heads |
|---|
| 683 |
math-phases)) |
|---|
| 684 |
(head (and (not (Math-primp math-pattern)) |
|---|
| 685 |
(not (and (eq (car (car math-prog)) 'try) |
|---|
| 686 |
(nth 5 (car math-prog)))) |
|---|
| 687 |
(not (memq (car (car math-prog)) '(func-opt |
|---|
| 688 |
apply |
|---|
| 689 |
select |
|---|
| 690 |
alt))) |
|---|
| 691 |
(if (memq (car (car math-prog)) '(func |
|---|
| 692 |
func-def)) |
|---|
| 693 |
(nth 2 (car math-prog)) |
|---|
| 694 |
(if (eq (car math-pattern) 'calcFunc-quote) |
|---|
| 695 |
(car-safe (nth 1 math-pattern)) |
|---|
| 696 |
(car math-pattern)))))) |
|---|
| 697 |
(let (found) |
|---|
| 698 |
(while heads |
|---|
| 699 |
(if (setq found (assq (car heads) all-heads)) |
|---|
| 700 |
(setcdr found (1+ (cdr found))) |
|---|
| 701 |
(setq all-heads (cons (cons (car heads) 1) all-heads))) |
|---|
| 702 |
(setq heads (cdr heads)))) |
|---|
| 703 |
(if (eq head '-) (setq head '+)) |
|---|
| 704 |
(if (memq head '(calcFunc-cons calcFunc-rcons)) (setq head 'vec)) |
|---|
| 705 |
(if head |
|---|
| 706 |
(progn |
|---|
| 707 |
(nconc (or (assq head rule-set) |
|---|
| 708 |
(car (setq rule-set (cons (cons head |
|---|
| 709 |
(copy-sequence |
|---|
| 710 |
nil-rules)) |
|---|
| 711 |
rule-set)))) |
|---|
| 712 |
(list rule)) |
|---|
| 713 |
(if (eq head '*) |
|---|
| 714 |
&nbs |
|---|