Debian Codegraph @

chaos/eval/#eval-ast2.lisp#

raw
1 ;;;-*- Mode:LISP; Package:CHAOS; Base:10; Syntax:Common-lisp -*-
2 ;;;
3 ;;; Copyright (c) 2000-2015, Toshimi Sawada. All rights reserved.
4 ;;;
5 ;;; Redistribution and use in source and binary forms, with or without
6 ;;; modification, are permitted provided that the following conditions
7 ;;; are met:
8 ;;;
9 ;;; * Redistributions of source code must retain the above copyright
10 ;;; notice, this list of conditions and the following disclaimer.
11 ;;;
12 ;;; * Redistributions in binary form must reproduce the above
13 ;;; copyright notice, this list of conditions and the following
14 ;;; disclaimer in the documentation and/or other materials
15 ;;; provided with the distribution.
16 ;;;
17 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
18 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ;;;
29 (in-package :chaos)
30 #|==============================================================================
31 System: CHAOS
32 Module: eval
33 File: eval-ast2.lisp
34 ==============================================================================|#
35 #-:chaos-debug
36 (declaim (optimize (speed 3) (safety 0) #-GCL (debug 0)))
37 #+:chaos-debug
38 (declaim (optimize (speed 1) (safety 3) #-GCL (debug 3)))
39
40 ;;; ** DESCRIPTION =============================================================
41 ;;; Evaluators of ASTs of Chaos's script sub-language.
42 ;;;
43
44 (defparameter *chaos-version* "2.0")
45
46 (declaim (special *interactive*))
47
48 (defun top-noshow ()
49 (or (and (null *chaos-input-source*)
50 (<= *chaos-input-level* 0))
51 *chaos-quiet*))
52
53 ;;; ****
54 ;;; EVAL
55 ;;; ****
56 (defun eval-form (ast)
57 (let ((form (%eval-form ast)))
58 (cond ((eq form '$$term)
59 (eval-$$term))
60 (t (eval-ast form))
61 )))
62
63 (defun eval-$$term ()
64 ;; some module must be opened.
65 (unless *open-module*
66 (with-output-chaos-error ('no-context)
67 (format t "no module is open.")))
68 ;; $$term must be bound.
69 (unless $$term
70 (with-output-chaos-error ('no-term)
71 (format t "$$term is not bound.")))
72 ;;
73 (let ((in-form (with-output-to-string (stream)
74 (with-in-module (*current-module*)
75 (term-print $$term stream)))))
76 (with-input-from-string (*standard-input* in-form)
77 (process-cafeobj-input))
78 ))
79
80 ;;; *********
81 ;;; LISP-EVAL
82 ;;; *********
83 (defun eval-lisp-form (ast)
84 (let ((form (%lisp-eval-form ast)))
85 (setq form
86 (cond ((consp form)
87 (if (symbolp (car form))
88 (if (fboundp (car form))
89 form
90 (with-output-chaos-error ('invalid-lisp-form)
91 (format t "no such Lisp function or macro \"~a\"."
92 (car form))))
93 form))
94 (t (if (symbolp form)
95 (if (boundp form)
96 form
97 (with-output-chaos-error ('invalid-lisp-form)
98 (format t "unbound Lisp symbol \"~a\"." form)))
99 form))))
100 (eval form)))
101
102 ;;; ************
103 ;;; DYNA-COMMENT
104 ;;; ************
105 (defun print-dyna-comment (ast)
106 (fresh-all)
107 (flush-all)
108 (let ((flg nil))
109 (dolist (form (%dyna-comment-form ast))
110 (when flg (princ " "))
111 (if (consp form)
112 (print-simple-princ-open form)
113 (princ form))
114 (setf flg t))
115 (flush-all)
116 t))
117
118 ;;; ******
119 ;;; REDUCE
120 ;;; ******
121 (defun perform-reduction (ast)
122 (let ((preterm (%reduce-term ast))
123 (modexp (%reduce-module ast))
124 (mode (%reduce-mode ast))
125 (result-as-text (%reduce-return-text ast)))
126 (unless mode
127 (setq mode :red))
128 (perform-reduction* preterm modexp mode result-as-text)))
129
130 (defun perform-reduction* (preterm &optional modexp mode (result-as-text nil))
131 (let ((result nil) ; normalized term
132 (mod (if modexp ; context of rewriting
133 (eval-modexp modexp)
134 (get-context-module)))
135 term ; target term
136 stat-form ; statistics in string
137 term-form) ; normalized term in string form
138 ;; prepare rewriting context
139 (when (modexp-is-error mod)
140 (with-output-chaos-error ('no-such-module)
141 (princ "Incorrect module expression, no such module ")
142 (print-chaos-object modexp)))
143 ;; set rewrting context
144 (context-push-and-move (get-context-module t) mod)
145 (when *auto-context-change*
146 (change-context (get-context-module t) mod))
147 ;; parse target term
148 (setq term (prepare-term preterm mod))
149 ;; print out prelude message
150 (unless *chaos-quiet*
151 (with-in-module (mod)
152 (format t "~&-- ~a in " (if (eq mode :exec)
153 "execute"
154 (if (eq mode :exec+)
155 "execute!"
156 (if (eq mode :red)
157 "reduce"
158 "bhavioral reduce"))))
159 (print-simple-mod-name mod)
160 (princ " : ")
161 (let ((*print-indent* (+ 4 *print-indent*)))
162 (print-check 0 20)
163 (term-print-with-sort term))
164 (flush-all)))
165 ;; do the rewriting
166 (let ((*chaos-quiet* t))
167 (compile-module mod))
168 (setq result (reducer term mod mode))
169 ;; print out the resultant term
170 (with-in-module (mod)
171 (if result-as-text
172 (setq term-form (term-print-with-sort-string result))
173 (progn
174 (format t "~%")
175 (term-print-with-sort result))))
176 (when *show-stats*
177 (setf stat-form (generate-statistics-form))
178 (format t "~%~a" stat-form)
179 (flush-all))
180 ;; reset the context
181 (context-pop-and-recover)
182 ;; done all
183 (if result-as-text
184 (values term-form stat-form)
185 nil)))
186
187 (defun perform-meta-reduction (pre-term &optional modexp mode)
188 (let ((*rewrite-exec-mode* (or (eq mode :exec)
189 (eq mode :exec+)))
190 (*rewrite-semantic-reduce* nil)
191 sort)
192 (let ((mod (if modexp
193 (eval-modexp modexp)
194 (get-context-module))))
195 (if (modexp-is-error mod)
196 (with-output-chaos-error ('no-such-module)
197 (princ "incorrect module expression, no such module ")
198 (print-chaos-object modexp))
199 (progn
200 (context-push-and-move (get-context-module t) mod)
201 (setq sort *cosmos*)
202 (when *auto-context-change*
203 (change-context (get-context-module t) mod)) ;;; what?
204 (with-in-module (mod)
205 (!setup-reduction *current-module*)
206 (setq $$mod *current-module*)
207 (setq *rewrite-semantic-reduce*
208 (and (eq mode :red)
209 (module-has-behavioural-axioms mod)))
210 (let* ((*parse-variables* nil)
211 (term (simple-parse *current-module* pre-term sort))
212 (res nil))
213 (when (or (null (term-sort term))
214 (sort<= (term-sort term) *syntax-err-sort* *chaos-sort-order*))
215 (return-from perform-meta-reduction nil))
216 (setq res (car (canonicalize-variables (list term) mod)))
217 (catch 'rewrite-abort
218 (let ((*do-empty-match* nil)) ; t
219 (if (and *rewrite-exec-mode*
220 *cexec-normalize*)
221 (rewrite-exec res *current-module* mode)
222 (rewrite res *current-module* mode))))
223 (context-pop-and-recover)
224 res)))))))
225
226 ;;; **************
227 ;;; TEST REDUCTION
228 ;;; **************
229 (defun perform-test-reduction (ast)
230 (let ((preterm (%test-reduce-term ast))
231 (modexp (%test-reduce-module ast))
232 (mode (%test-reduce-mode ast))
233 (expect (%test-reduce-expect ast)))
234 (perform-reduction* preterm modexp (if (eq mode :red) nil t))
235 (with-in-module ($$mod)
236 (let ((ans (simple-parse-ground "a test reduction" *current-module* expect)))
237 (if (and (not (symbolp $$norm))
238 (term-equational-equal $$norm ans))
239 (progn
240 (unless *chaos-quiet*
241 (with-output-simple-msg ()
242 (princ "** success test reduction.")))
243 *bool-true*)
244 (progn
245 (unless *chaos-quiet*
246 (with-output-simple-msg ()
247 (format t "** test reduction failed~%-- expected:~a~%" expect)))
248 *bool-false*))))))
249
250 ;;; *****
251 ;;; PARSE
252 ;;; *****
253 (defun do-parse-term (ast)
254 (let* ((preterm (%parse-term ast))
255 (modexp (%parse-module ast))
256 (result-as-text (%parse-return-text ast)))
257 (when result-as-text
258 (with-output-simple-msg ()
259 (princ "`return result as string' option is not implemented yet.")))
260 (do-parse-term* preterm modexp)))
261
262 (defun do-parse-term* (preterm &optional modexp)
263 (let ((mod (if modexp
264 (eval-modexp modexp)
265 (get-context-module)))
266 (target-term nil))
267 (when (modexp-is-error mod)
268 (with-output-chaos-error ('no-such-module)
269 (princ "incorrect module expression, no such module: ")
270 (print-chaos-object modexp)))
271 ;;
272 (context-push-and-move (get-context-module t) mod)
273 (with-in-module (mod)
274 (prepare-for-parsing *current-module*)
275 (let ((*parse-variables* nil))
276 (setq target-term (car (canonicalize-variables (list (simple-parse *current-module* preterm *cosmos*)) mod)))
277 ;; ******** MEL
278 (when *mel-sort*
279 (!setup-reduction mod)
280 (setq target-term (apply-sort-memb target-term mod)))
281 (reset-target-term target-term *current-module* mod)
282 ;; ********
283 (format t "~%")
284 (term-print-with-sort target-term *standard-output*)
285 (flush-all)))
286 (context-pop-and-recover)
287 (values target-term mod)))
288
289 ;;; *TODO*
290 (defun red-loop (mod &optional prompt)
291 (declare (ignore mod prompt))
292 (with-output-simple-msg ()
293 (princ "sorry, red-loop is not implemented yet.."))
294 (return-from red-loop nil))
295
296 (defun check-bad-rules (mod)
297 (declare (ignore mod))
298 nil)
299
300 ;;; *****************
301 ;;; TRACE/TRACE-WHOLE
302 ;;; *****************
303 (defun eval-trace (ast)
304 (let ((flag (%trace-flag ast)))
305 (if (eq flag :on)
306 (trace-on)
307 (trace-off))))
308
309 (defun eval-trace-whole (ast)
310 (let ((flag (%trace-whole-flag ast)))
311 (if (eq flag :on)
312 (trace-whole-on)
313 (trace-whole-off))))
314
315 (defun rewrite-debug-on () ())
316 (defun rewrite-debug-off () ())
317
318 (defun trace-on ()
319 (setq $$trace-rewrite t)
320 (rewrite-debug-on))
321
322 (defun trace-off ()
323 (setq $$trace-rewrite nil)
324 (rewrite-debug-off))
325
326 (defun trace-whole-on ()
327 (setq $$trace-rewrite-whole t)
328 (rewrite-debug-on))
329
330 (defun trace-whole-off ()
331 (setq $$trace-rewrite-whole nil)
332 (rewrite-debug-off))
333
334 ;;; *************
335 ;;; REWRITE LIMIT
336 ;;; *************
337 (defun eval-rewrite-count-limit (ast)
338 (let ((count (%rewrite-count-limit ast)))
339 (if (eq count 'off)
340 (set-rewrite-count-limit 'off)
341 (multiple-value-bind (num len)
342 (parse-integer count :junk-allowed t)
343 (if (= len (length count))
344 (set-rewrite-count-limit num)
345 (with-output-chaos-error ('invalid-value)
346 (format t "invalid rewrite count limit ~a" count)))))))
347
348 (defun set-rewrite-count-limit (num)
349 (if (integerp num)
350 (if (zerop num)
351 (progn (setq *rewrite-count-limit* most-positive-fixnum)
352 (rewrite-debug-off))
353 (if (> num 0)
354 (progn (setq *rewrite-count-limit* num)
355 (rewrite-debug-on))
356 (with-output-chaos-error ('invalid-value)
357 (format t "invalid rewrite count limit value ~d" num)
358 (print-next) (princ "must be a positive integer.")
359 )))
360 (progn (setq *rewrite-count-limit* most-positive-fixnum)
361 (rewrite-debug-off))))
362
363 (defun get-int-or-null (value)
364 (if (or (null value)
365 (equal value '(".")))
366 nil
367 (multiple-value-bind (num len)
368 (parse-integer (car value) :junk-allowed t)
369 (if (= len (length (car value)))
370 num
371 (with-output-chaos-error ('invalid-value)
372 (format t "value must be a positive integer."))))))
373
374 (defun set-rewrite-count-limit2 (value)
375 (let ((num (get-int-or-null value)))
376 (if num
377 (set-rewrite-count-limit num)
378 (set-rewrite-count-limit 'off))))
379
380 (defun set-cond-trial-limit (value)
381 (setq *condition-trial-limit*
382 (get-int-or-null value)))
383
384 (defun set-term-id-size-limit (value)
385 (let ((num (get-int-or-null value)))
386 (if num
387 (setq *term-id-limit* num)
388 (setq *term-id-limit* 51)))) ; the default value
389
390 ;;; ********************
391 ;;; REWRITE STOP PATTERN
392 ;;; ********************
393 (defun eval-stop-at (ast)
394 (let ((pat (%stop-at-pattern ast)))
395 (set-rewrite-stop-pattern2 pat)))
396
397 (defun set-rewrite-stop-pattern (pat)
398 (if (memq pat '(null nil none))
399 (progn (setq *rewrite-stop-pattern* nil)
400 (rewrite-debug-off))
401 (progn (setq *rewrite-stop-pattern* pat)
402 (rewrite-debug-on))))
403
404 (defun set-rewrite-stop-pattern2 (pat)
405 (if (or (null pat)
406 (member pat '(("none") ("off") ("nil") ("null"))))
407 (set-rewrite-stop-pattern 'none)
408 (let ((mod (get-context-module)))
409 (let* ((*parse-variables* (module-variables mod))
410 (term (simple-parse mod
411 pat *cosmos*)))
412 (when (or (null (term-sort term))
413 (sort<= (term-sort term) *syntax-err-sort*
414 *chaos-sort-order*))
415 (return-from set-rewrite-stop-pattern2 nil))
416 (setq term (car (canonicalize-variables (list term) mod)))
417 (set-rewrite-stop-pattern term)))))
418
419 ;;; *******
420 ;;; STEPPER
421 ;;; *******
422 (defun eval-step (ast)
423 (let ((flag (%step-flag ast)))
424 (if (eq flag :on)
425 (step-on)
426 (step-off))))
427
428 (defun step-on ()
429 (setq *rewrite-stepping* t)
430 (setq *steps-to-be-done* 1)
431 (rewrite-debug-on))
432
433 (defun step-off ()
434 (setq *rewrite-stepping* nil)
435 (rewrite-debug-off))
436
437 ;;; *****
438 ;;; INPUT
439 ;;; *****
440 (defun eval-input-file (ast)
441 (chaos-input-file :file (%input-file-name ast)
442 :proc (%input-proc ast)
443 :load-path (%input-load-path ast)
444 :suffix (%input-suffixes ast)
445 :args (%input-args ast)))
446
447 ;;; ***************
448 ;;; DESCRIBE-MODULE
449 ;;; ***************
450
451 (defun eval-describe-module (ast)
452 (let ((modexp (%describe-module-modexp ast))
453 mod)
454 (setf mod (eval-modexp modexp))
455 (when (modexp-is-error mod)
456 (with-output-chaos-error ('no-such-module)
457 (format t "incorrect module expression, or unknown module: ")
458 (print-modexp modexp)))
459 (describe-module mod)))
460
461
462 ;;; ***********
463 ;;; OPEN-MODULE
464 ;;; ***********
465 (defun eval-open-module (ast)
466 (let ((modexp (%open-module-modexp ast))
467 mod)
468 (setf mod (if (null modexp)
469 (get-context-module)
470 (eval-modexp modexp)))
471 (when (modexp-is-error mod)
472 (with-output-chaos-error ('no-such-module)
473 (princ "incorrect module expression or unknown module ")
474 (print-modexp modexp)))
475 (unless mod
476 (with-output-chaos-error ('no-context)
477 (princ "no module to be opened!")))
478 (!open-module mod)))
479
480 (defparameter *module-open-form*
481 (%module-decl* "%"
482 :object
483 :user
484 nil))
485
486 (defun !open-module (mod)
487 (let ((*auto-context-change* nil))
488 (when *open-module*
489 (with-output-chaos-warning ()
490 (princ "another module is already opened: ")
491 (print-mod-name *open-module*) (print-next)
492 (princ "closing this module...") (print-next)
493 (eval-close-module nil)))
494 (setq *open-module* mod)
495 (setq *last-before-open* (get-context-module t))
496 (clear-term-memo-table *term-memo-table*)
497 (let ((*chaos-quiet* nil)
498 (*copy-variables* t)
499 open-mod)
500 (setf (%module-decl-kind *module-open-form*) (module-kind mod))
501 (setq open-mod (eval-ast *module-open-form*))
502 (ignoring-chaos-error
503 (import-module open-mod :using (compile-module mod))
504 ;; prepare for immediage use
505 (compile-module open-mod))
506 (change-context *last-before-open* open-mod)
507 open-mod)))
508
509 ;;; ************
510 ;;; CLOSE-MODULE
511 ;;; ************
512 (defun eval-close-module (&rest ast)
513 (declare (ignore ast))
514 (if *open-module*
515 (let ((omod (eval-modexp "%")))
516 (initialize-module omod)
517 (when (eq omod (get-context-module t))
518 (change-context (get-context-module t) *last-before-open*))
519 (setq *open-module* nil)
520 (setq *last-before-open* nil))
521 (with-output-chaos-warning ()
522 (princ "no module is open."))))
523
524 ;;; ***********
525 ;;; SAVE SYSTEM
526 ;;; ***********
527
528 (defun eval-save (ast)
529 (let ((file (%save-file ast)))
530 (save-system :file file)))
531
532 (defvar *seen-saved* nil)
533 (defparameter .fill-space. " ")
534
535 (defun save-system (&key (file "/tmp/chaos-system")
536 (compile nil))
537 (setq *seen-saved* nil)
538 (with-open-file (stream file :direction :output
539 :if-exists :new-version
540 :if-does-not-exist :create)
541 (flet ((exporting-modules (mod)
542 (let ((e-mods (module-exporting-modules mod))
543 (res nil))
544 (dolist (ee e-mods)
545 (when (module-type (car ee))
546 (push ee res)))
547 res)))
548 (let ((top-modules nil))
549 (dolist (entry *modules-so-far-table*)
550 (let ((mod (cdr entry)))
551 (unless (or (exporting-modules mod)
552 (module-is-hard-wired mod)
553 (module-is-system-module mod)
554 (eq (module-type mod) nil))
555 (push mod top-modules))))
556 (princ *chaos-binary-magic* stream)
557 (terpri stream)
558 (princ "#|" stream)
559 (let ((*print-line-limit* 80))
560 (terpri stream)
561 (print-centering "-- Chaos snap shot file --" .fill-space. stream)
562 (terpri stream)
563 (print-centering (format nil "chaos version: ~a" *chaos-version*)
564 .fill-space. stream)
565 (terpri stream)
566 (print-centering (format nil "file: ~a" file) .fill-space. stream)
567 (terpri stream)
568 (print-centering (format nil "date: ~a" (get-time-string))
569 .fill-space. stream)
570 (terpri stream)
571 (print-centering
572 "* NOTE : DO NOT MODIFY THIS FILE ULESS YOU REALLY KNOW WHAT YOU ARE DOING!."
573 .fill-space.
574 stream))
575 (terpri stream)
576 (princ "|#" stream)
577 (format stream "~&(in-package \"CHAOS\")")
578 (format stream "~&(eval-when (:execute :load-toplevel)")
579 (format stream "~&;;; system standard prelude.")
580 (dolist (elt *system-standard-prelude*)
581 (format stream "~&(eval-ast-if-need-no-error '~s)" elt))
582 (format stream "~&;;; user defined modules.")
583 (dolist (module top-modules)
584 (dump-user-module stream module))
585 (format stream "~&;;; views.")
586 (dolist (entry *modexp-view-table*)
587 (let ((v (cdr entry)))
588 (unless (or (memq v *seen-saved*)
589 ;; *TO DO*
590 ;; (view-is-system-view y)
591 )
592 (format stream "~&(eval-ast-if-need-no-error '~s)"
593 (%view-decl* (view-name v)
594 (view-decl-form v))))))
595 ;; the end
596 (format stream "~%)")
597 (when compile
598 (compile-file file))))))
599
600 (defun dump-user-module (stream module)
601 (let ((subs nil))
602 (dag-dfs (module-dag module)
603 #'(lambda (node)
604 (let ((mod (car (dag-node-datum node))))
605 (push mod subs))))
606 (dolist (sub (nreverse subs))
607 (when (and (module-p sub) (module-is-user-module sub))
608 (let ((name (module-name sub)))
609 (unless (or (modexp-is-parameter-theory name)
610 (equal name "%")
611 (memq sub *seen-saved*))
612 (push sub *seen-saved*)
613 (let ((decl (object-decl-form sub))
614 (*print-pretty* t))
615 (when decl
616 (format stream "~&(eval-ast-if-need-no-error '~s)"
617 decl))
618 nil ))))) ; do nothing now.
619 ))
620
621 ;;;
622 (defun eval-ast-if-need (ast)
623 (when (chaos-ast? ast) ; for safety
624 (case (ast-type ast)
625 (%module-decl
626 (let* ((name (%module-decl-name ast))
627 (canon (normalize-modexp name)))
628 (let ((mod (find-module-in-env canon)))
629 (cond (mod
630 (when (or (module-is-inconsistent mod)
631 (not (equal ast (module-decl-form mod))))
632 (eval-ast ast)))
633 (t (eval-ast ast))))))
634 (t (eval-ast ast)))))
635
636 (defun eval-ast-if-need-no-error (ast)
637 (ignoring-chaos-error (eval-ast-if-need ast)))
638
639 ;;; **************
640 ;;; RESTORE SYSTEM
641 ;;; **************
642 (defun eval-restore (ast)
643 (let ((file (%restore-file ast)))
644 (restore-system file)))
645
646 (defun restore-system (file)
647 (catch *top-level-tag*
648 (with-chaos-top-error ()
649 (with-chaos-error ()
650 (let ((*chaos-quiet* (not *chaos-verbose*)))
651 (if (probe-file file)
652 (progn (load file)
653 (format t "~%done restoring."))
654 (with-output-chaos-error ('no-file)
655 (format t "No such file ~a" file))))))))
656
657 ;;; ************
658 ;;; RESET SYSTEM
659 ;;; ************
660
661 ;;; recover definitions of soft-wired modules and standard prelude.
662 ;;;
663 (defun eval-reset-system (&rest ignore)
664 (declare (ignore ignore))
665 (let ((msg? (and (at-top-level) (not *chaos-quiet*))))
666 (when msg?
667 (with-output-simple-msg ()
668 (format t "~%** start resetting system")
669 (format t "~& re-installing system built-in modules...")))
670 (install-chaos-soft-wired-modules)
671 (init-tram-bi-modules)
672 (init-builtin-universal)
673 (when msg?
674 (with-output-simple-msg ()
675 (format t "~& re-installing prelude...")))
676 (dolist (elt *system-standard-prelude*)
677 (eval-ast-if-need elt))
678 (when msg?
679 (with-output-simple-msg ()
680 (format t "~&** done restting system.")
681 (force-output)))))
682
683 ;;; **********
684 ;;; FULL-RESET
685 ;;; **********
686 (defun eval-full-reset (&rest ignore)
687 (declare (ignore ignore))
688 (let ((msg? (and (at-top-level) (not *chaos-quiet*))))
689 (when msg?
690 (with-output-simple-msg ()
691 (format t "~%** start full resetting system")
692 (format t "~% clearing system data bases:")))
693 (when msg?
694 (format t "~% start clean up modules ."))
695 (dolist (m *modules-so-far-table*)
696 (clean-up-module (cdr m))
697 (when msg?
698 (print-in-progress ".")))
699 (when msg?
700 (format t "~% start clean up views ."))
701 (dolist (v *modexp-view-table*)
702 (clean-up-view (cdr v))
703 (when msg?
704 (print-in-progress ".")))
705 ;;
706 (clear-global-db)
707 (clear-trs-db)
708 (clear-term-memo-table *term-memo-table*)
709 ;;
710 (when msg?
711 (princ " done")
712 (terpri))
713 (setq *chaos-features* nil)
714 (setq *open-module* nil)
715 (setq *last-before-open* nil)
716 (setq *current-sort-order* nil)
717 (setq *current-opinfo-table* nil)
718 (setq *old-context* nil)
719 (setq *bootstrapping-bool* nil)
720 (setq .int-module. nil)
721 ;;
722 (when msg?
723 (with-output-simple-msg ()
724 (format t "~& rebooting system kernel...")))
725 (boot-chaos)
726 (when msg?
727 (with-output-simple-msg ()
728 (format t "~& re-installing hard-wired modules...")))
729 (install-chaos-hard-wired-modules)
730 (when msg?
731 (with-output-simple-msg ()
732 (format t "~& re-installing builtin modules...")))
733 (install-chaos-soft-wired-modules)
734 (init-tram-bi-modules)
735 (init-builtin-universal)
736 (when msg?
737 (with-output-simple-msg ()
738 (format t "~& re-installing prelude ...")))
739 (dolist (elt *system-standard-prelude*)
740 (eval-ast-if-need elt))
741 (when msg?
742 (with-output-simple-msg ()
743 (format t "~&** done system full reset.")
744 (force-output)))))
745
746 ;;; ************
747 ;;; LOAD PRELUDE
748 ;;; ************
749
750 (defun eval-load-prelude (ast)
751 (load-prelude (%load-prelude-file ast)
752 (or (%load-prelude-processor ast)
753 'process-cafeobj-input)))
754
755 (defun load-prelude (file processor &optional (no-error nil))
756 (load-prelude* file processor t (not no-error)))
757 (defun load-prelude+ (file processor &optional (no-error nil))
758 (load-prelude* file processor nil (not no-error)))
759
760 (defun load-prelude* (file processor &optional override (errorp t))
761 (catch *top-level-tag*
762 (with-chaos-top-error ()
763 (with-chaos-error ()
764 (let ((*dribble-ast* t)
765 (*ast-log* nil)
766 (f nil))
767 (setq f (chaos-input-file :file file :proc processor
768 :load-path (list *system-prelude-dir*)
769 :errorp errorp))
770 (if (and *system-standard-prelude* (not override))
771 (setq *system-standard-prelude*
772 (nconc *system-standard-prelude*
773 (nreverse *ast-log*)))
774 (setq *system-standard-prelude* (nreverse *ast-log*)))
775 f)))))
776
777 ;;; *******
778 ;;; PROVIDE
779 ;;; *******
780 (defun eval-provide-command (ast)
781 (pushnew (%provide-feature ast) *chaos-features*
782 :test #'equal))
783
784 ;;; *******
785 ;;; REQUIRE
786 ;;; *******
787 (defun eval-require-command (ast)
788 (let ((feature (%require-feature ast))
789 (file (%require-file ast))
790 (proc (%require-proc ast)))
791 (unless (member feature *chaos-features* :test #'equal)
792 (unless proc
793 (with-output-panic-message ()
794 (format t "require: reading proc is not specified!")
795 (chaos-to-top)))
796 (if file
797 (funcall proc file)
798 (funcall proc (format nil "~{~A~^/~}"
799 (cl-ppcre:split "::" feature)))))))
800
801 ;;; *******
802 ;;; PROTECT
803 ;;; *******
804 (defun eval-protect (ast)
805 (let* ((mod (%protect-module ast))
806 (mode (%protect-mode ast))
807 (modval (eval-mod-ext (if (atom mod)
808 (list mod)
809 mod))))
810 (if (and modval (not (modexp-is-error modval)))
811 (if (eq mode :unset)
812 (setf (module-protected-mode modval) nil)
813 (setf (module-protected-mode modval) t)))))
814
815 ;;; *******
816 ;;; DRIBBLE
817 ;;; *******
818 (defun eval-dribble (ast)
819 (let ((file (%dribble-file ast)))
820 (if file
821 (let ((stream (open file :direction :output
822 :if-exists :supersede
823 :if-does-not-exist :create)))
824 (unless stream
825 (with-output-chaos-error ('no-such-file)
826 (format t "could not open file ~a." file)
827 ))
828 (with-output-msg ()
829 (format t "starts dribbling to ~a (~a)." (namestring file)
830 (get-time-string)))
831 (setq *dribble-ast* t)
832 (setq *dribble-stream* stream))
833 (when *dribble-stream*
834 (with-output-msg ()
835 (princ "finishing dribble."))
836 (close *dribble-stream*)
837 (setq *dribble-stream* nil)
838 (setq *dribble-ast* nil)))))
839
840 ;;; **********
841 ;;; SAVE-CHAOS
842 ;;; **********
843 (defun eval-save-chaos (ast)
844 (if (%save-chaos-top ast)
845 (save-chaos (%save-chaos-top ast)
846 (%save-chaos-file ast))
847 (save-chaos 'cafeobj-top-level
848 (%save-chaos-file ast))))
849
850 ;;; **
851 ;;; LS
852 ;;; **
853 (defun eval-ls (ast)
854 (let ((dir (%ls-dir ast)))
855 (if (equal dir '("."))
856 (chaos-ls "../")
857 (if dir
858 (chaos-ls dir)
859 (chaos-ls ".")))
860 (force-output)))
861
862 ;;; ***
863 ;;; PWD
864 ;;; ***
865 (defun eval-pwd (ast)
866 (declare (ignore ast))
867 (princ (chaos-pwd))
868 (force-output))
869
870 ;;; *****
871 ;;; SHELL
872 ;;; *****
873 (defun eval-shell (ast)
874 (let ((command (%shell-command ast)))
875 (run-shell command)))
876
877 (defun run-shell (command)
878 #+(OR GCL LUCID EXCL CLISP :SBCL)
879 (when command
880 (setq command (reduce #'(lambda (x y) (concatenate 'string x " " y))
881 command))
882 #+GCL (system command)
883 #+EXCL (excl:shell command)
884 #+CLISP (ext::shell command)
885 )
886 #+CMU
887 (when command
888 (let ((com (car command))
889 (args (cdr command)))
890 (ext:run-program com args :output t)))
891 #+:openmcl
892 (when command
893 (let ((com (car command))
894 (args (cdr command)))
895 (ccl:run-program com args :output t)))
896 #+:SBCL
897 (when command
898 (sb-ext:process-exit-code
899 (apply 'sb-ext:run-program
900 #+win32 "sh" #-win32 "/bin/sh"
901 (list "-c" command)
902 :input nil :output *terminal-io*
903 #+win32 '(:search t) #-win32 nil))
904 ))
905
906 ;;; **
907 ;;; CD
908 ;;; **
909 (defun eval-cd (ast)
910 (chaos-cd (%cd-directory ast))
911 (princ (chaos-pwd))
912 (force-output))
913
914 ;;; *****
915 ;;; PUSHD
916 ;;; *****
917 (defun eval-pushd (ast)
918 (chaos-pushd (%pushd-directory ast) t)
919 (chaos-print-directory-stack))
920
921 ;;; ****
922 ;;; POPD
923 ;;; ****
924 (defun eval-popd (ast)
925 (if (%popd-num ast)
926 (chaos-popd (%popd-num ast))
927 (chaos-popd)))
928
929 ;;; ****
930 ;;; DIRS
931 ;;; ****
932 (defun eval-dirs (ast)
933 (declare (ignore ast))
934 (chaos-print-directory-stack))
935
936 ;;; ****
937 ;;; SHOW
938 ;;; ****
939 (defun show-describe (tag dat)
940 (let ((it (car dat))
941 (describe (eq tag 'describe)))
942 (case-equal it
943 (("mod" "module") (print-mod (cdr dat) describe))
944 (("view") (show-view (cdr dat) describe))
945 ("sorts" (show-sorts (cdr dat) describe))
946 ("sort" (show-sort (cdr dat) describe))
947 (("psort" "principal-sort")
948 (print-principal-sort (cdr dat)))
949 (("ops" "operators") (show-ops (cdr dat) describe))
950 (("op" "operator") (show-op (cdr dat) describe))
951 ("context" (show-context (cdr dat)))
952 ("select" (eval-mod-ext (if (equal tag 'select)
953 dat
954 (cdr dat))
955 t))
956 (("sign" "sig" "signature")(show-sign (cdr dat) describe))
957 ("memo" (show-term-memo-table))
958 ("vars" (print-vars (cdr dat)))
959 (("axioms" "eqs" "eqns" "rls") (print-axs (cdr dat) describe))
960 ("subs" (print-subs (cdr dat)))
961 ;; ("name" (print-name (cdr dat)))
962 ;; ("abbrev" (print-abbrev-name (cdr dat)))
963 ("tree"
964 (if (and $$term (not (eq 'void $$term)))
965 (print-term-tree $$term *chaos-verbose*)
966 (with-output-chaos-warning ()
967 (princ "no current term to display."))))
968 ("term"
969 (let* ((target (if (not (or (equal (second dat) "tree")
970 (equal (second dat) "graph")))
971 (second dat)
972 nil))
973 (tree? (if target
974 (third dat)
975 (second dat))))
976 (show-term target tree?)))
977 ("subterm"
978 (if $$subterm
979 (show-term $$subterm (cadr dat))
980 (with-output-chaos-warning ()
981 (format t "no subterm is selected."))))
982 ("selection" (show-apply-selection))
983 (("let" "binding") (show-bindings))
984 ("sub" (multiple-value-bind (no len)
985 (if (cadr dat)
986 (parse-integer (cadr dat) :junk-allowed t)
987 (values 1 0))
988 (unless (and (integerp no)
989 (= (length (cadr dat)) len))
990 (with-output-chaos-error ()
991 (format t "Invalid submodule number ~a" (cadr dat))
992 (chaos-to-top)))
993 (show-sub (cddr dat) (1- no) describe)))
994 ("params" (print-params (cdr dat)))
995 ("param" (multiple-value-bind (no len)
996 (if (cadr dat)
997 (parse-integer (cadr dat) :junk-allowed t)
998 (values 1 0))
999 (if (and (integerp no) (= len (length (cadr dat))))
1000 (show-param (cddr dat) (1- no) describe)
1001 (show-param (cddr dat) (cadr dat) describe))))
1002 ("time" (let ((val (timer)))
1003 (format t "~8,3f cpu ~8,3f real~%" (car val) (cadr val))))
1004 ("rules" (print-rules (cdr dat)))
1005 ("all" (case-equal (cadr dat)
1006 ("rules"
1007 (let ((*module-all-rules-every* t))
1008 (print-rules (cddr dat))))
1009 (("axioms" "eqns" "eqs" "rls")
1010 (let ((*module-all-rules-every* t))
1011 (print-axs (cddr dat) describe)))
1012 ("ops" (show-ops (cddr dat) describe t))
1013 ("sorts" (show-sorts (cddr dat) describe t))
1014 (("sign" "sig" "signature") (show-sign (cddr dat) describe t))
1015 ;; obsolate.
1016 ;; (("switch" "switches" "modes") (show-modes t))
1017 (otherwise (with-output-chaos-error ()
1018 (format t "no such `show' option ~a" (cadr dat))))))
1019 ("modules" (print-modules (cdr dat)))
1020 ("views" (print-views (cdr dat)))
1021 ("rule" (apply-print-rule (cadr dat)))
1022 ("pending" (print-pending))
1023 (("modes" "switches") (show-modes t))
1024 (("switch" "sw" "mode") (show-modes (cdr dat)))
1025 ("limit" (cafein-show-rewrite-limit))
1026 ("stop" (cafein-show-stop-pattern))
1027 (("features" "feature") (print-simple-princ-open *chaos-features*))
1028 ;; PigNose Commands
1029 #+:BigPink
1030 ("clause" (pignose-show-clause (cadr dat) describe))
1031 #+:BigPink
1032 ;; ("option" (pignose-show-option (cadr dat) describe))
1033 ("option" (pignose-show-option (cadr dat)))
1034 ;; =(*)=> support
1035 (("exec" "search" "sch")
1036 (let ((option (cadr dat))
1037 (num (caddr dat)))
1038 (case-equal option
1039 ("graph" (show-rwl-sch-graph num))
1040 (otherwise
1041 (with-output-chaos-error ()
1042 (format t "no such `show exec' option ~a"
1043 (cadr dat)))))))
1044 ("path" (let ((opt (cadr dat)))
1045 (if (member opt '("labels" "label") :test #'equal)
1046 (show-rwl-sch-path (caddr dat) (cadddr dat) :label)
1047 (show-rwl-sch-path opt (caddr dat) nil)))
1048 ("state" (let ((num (cadr dat))
1049 (depth (caddr dat)))
1050 (show-rwl-sch-path num depth nil t)))
1051 ;;
1052 (("undocumented" "undoc")
1053 (show-undocumented))
1054 ;; CITP
1055 ("unproved"
1056 (print-unproved-goals *proof-tree*))
1057 ("goal" (let ((name (cadr dat)))
1058 (print-named-goal *proof-tree* name)))
1059 ("proof" (let ((target (second dat)))
1060 (when (or (null target) (equal target "."))
1061 (setq target "root"))
1062 (print-proof-tree target describe)))
1063
1064 ("autoload"
1065 (format t "** Autoload settings:")
1066 (format t "~%Module~14TPathname")
1067 (format t "~%========================================")
1068 (dolist (al *autoload-alist*)
1069 (format t "~%~a~14T~a" (car al) (cdr al))))
1070 ("libpath" (pr-search-path))
1071 ;;
1072 ;; helpers
1073 ;;
1074 ("?"
1075 (princ "** general module inspection commands.")
1076 (terpri)
1077 (princ " show|describe [sorts|ops|vars|axioms|params|subs|sign]")
1078 (terpri)
1079 (princ " show|describe [<Modexp>]") (terpri)
1080 (princ " show|describe sort <Sort>") (terpri)
1081 (princ " show|describe op <Operator>") (terpri)
1082 (princ " show|describe [all] {sorts|ops|sign|axioms}")
1083 ;; (princ " show select <Modexp>") (terpri)
1084 (terpri)
1085 (princ "** print view.")
1086 (terpri)
1087 (princ " show view <view-name>")
1088 (terpri)
1089 (princ "** print submodules/prameters")
1090 (terpri)
1091 (princ " show sub <number> [<Modexp>]")
1092 (terpri)
1093 (princ " show param <argname> [<Modexp>]")
1094 ;;
1095 (terpri)
1096 (princ "** supporting commands for proof.")
1097 (terpri)
1098 (princ " show [all] rules [<Modexp>] .") (terpri)
1099 ;; (princ " show abbrev [<Modexp>] .") (terpri)
1100 (princ " show tree") (terpri)
1101 (princ " show term [let-variable] [tree | graph]") (terpri)
1102 (princ " show subterm [tree]") (terpri)
1103 (princ " show let") (terpri)
1104 (princ " show selection") (terpri)
1105 (princ " show [all] rule <RuleSpec> .") (terpri)
1106 (princ " show pending") (terpri)
1107 (princ " show context") (terpri)
1108 ;; (princ " show apply context") (terpri)
1109 (princ "** misc.")
1110 (terpri)
1111 (princ " show modules") (terpri)
1112 (princ " show views") (terpri)
1113 ;; (princ " show [all] modes") (terpri)
1114 (princ " show switch [<Switch>]") (terpri)
1115 (princ " show time") (terpri)
1116 (princ " show limit") (terpri)
1117 (princ " show stop") (terpri)
1118 (princ " show features") (terpri)
1119 (princ " show memo")(terpri)
1120 ;;
1121 ;;
1122 (princ "** PigNose resolve base proof system commands.")
1123 (terpri)
1124 (princ " show clause <ClauseId>")
1125 (terpri)
1126 )
1127 (otherwise (print-mod dat describe)))
1128 ))
1129
1130 (defun eval-show (ast)
1131 (show-describe 'show (%show-args ast)))
1132
1133 ;;; ********
1134 ;;; DESCRIBE
1135 ;;; ********
1136 (defun eval-describe (ast)
1137 (show-describe 'describe (%describe-args ast)))
1138
1139 ;;; ******
1140 ;;; SELECT
1141 ;;; ******
1142 (defun eval-select (ast)
1143 (let ((modexp (%select-modexp ast)))
1144 (eval-mod-ext modexp t)))
1145
1146 ;;; ***
1147 ;;; SET
1148 ;;; ***
1149 (defun eval-set (ast)
1150 (let ((which (%set-switch ast))
1151 (value (%set-value ast)))
1152 (set-chaos-switch which value)))
1153
1154 ;;; **********
1155 ;;; REGULARIZE
1156 ;;; **********
1157
1158 (defun eval-regularize (ast)
1159 (let ((mod (eval-mod-ext (%regularize-modexp ast))))
1160 (unless *chaos-quiet*
1161 (with-output-simple-msg ()
1162 (format t ">> started process of regularizing signature ..."))
1163 (terpri)
1164 (force-output))
1165 (regularize-signature mod)))
1166
1167 ;;; *****
1168 ;;; CHECK
1169 ;;; *****
1170 (defun eval-check (ast)
1171 (let ((args (%check-args ast)))
1172 (case (%check-what ast)
1173
1174 ;; regularity
1175 (:regularity
1176 (let ((module (eval-mod-ext args)))
1177 (unless *chaos-quiet*
1178 (with-output-simple-msg ()
1179 (format t ">> start regularity check ...")
1180 (terpri)
1181 (force-output)))
1182 (check-regularity module)))
1183
1184 ;; operator strictness
1185 (:strictness
1186 (let ((mod (get-context-module)))
1187 (!setup-reduction mod)
1188 (with-in-module (mod)
1189 (if args
1190 (let ((parsedop (parse-op-name args)))
1191 (let ((ops (find-qual-operators parsedop mod)))
1192 (if ops
1193 (dolist (op ops)
1194 (check-operator-strictness op mod t))
1195 (with-output-chaos-error ('no-such-operator)
1196 (princ "no such operator")
1197 (print-chaos-object parsedop)))))
1198 (check-operator-strictness-whole mod t)))))
1199
1200 ;; TRS compatibility
1201 (:compatibility
1202 (let ((module (eval-mod-ext args)))
1203 (unless *chaos-quiet*
1204 (with-output-simple-msg ()
1205 (format t ">> started compatibility check:"))
1206 (terpri)
1207 (force-output))
1208 (let ((res (check-compatibility module))
1209 (*print-indent* (+ 2 *print-indent*)))
1210 (if res
1211 (with-in-module (module)
1212 (with-output-simple-msg ()
1213 (format t ">> module (corresponding TRS) is NOT compatible:")
1214 (dolist (r-ms res)
1215 (format t "~%- rewrite rule")
1216 (let ((*print-indent* (+ 2 *print-indent*)))
1217 (print-next)
1218 (print-chaos-object (car r-ms)))
1219 (format t "~& violates the compatibility,")
1220 (format t "~& and following operator(s) can possibly be affected:")
1221 (let ((*print-indent* (+ 2 *print-indent*)))
1222 (dolist (m (cdr r-ms))
1223 (print-next)
1224 (print-chaos-object m))))))
1225 (with-output-simple-msg ()
1226 (format t ">> module is compatible."))))))
1227 ;;;
1228 (:coherency
1229 (let ((mod (get-context-module)))
1230 (!setup-reduction mod)
1231 (with-in-module (mod)
1232 (if args
1233 (let ((parsedop (parse-op-name args)))
1234 (let ((ops (find-qual-operators parsedop mod)))
1235 (if ops
1236 (dolist (op ops)
1237 (check-operator-coherency op mod t))
1238 (with-output-chaos-error ('no-such-operator)
1239 (princ "no such operator")
1240 (print-chaos-object parsedop)))))
1241 (check-operator-coherency-whole mod)))))
1242 ;;
1243 ;; SENSIBILITY of the signature
1244 ;;
1245 (:sensible
1246 (let ((module (eval-mod-ext args)))
1247 (unless *chaos-quiet*
1248 (with-output-simple-msg ()
1249 (format t ">> Start sensible check ...")
1250 (terpri)
1251 (force-output)))
1252 (check-sensible module t)))
1253 (:rew-coherence
1254 (let ((module (eval-mod-ext (cdr args))))
1255 (let ((r-arg (car args)))
1256 (unless (or (equal "coherency" r-arg)
1257 (equal "coh" r-arg))
1258 (with-output-chaos-error ('invalid-arg)
1259 (format t "check rewriting: Invalid argument ~s" r-arg)))
1260 (check-rwl-coherency module))))
1261
1262 ;; PigNose extention
1263 #+:BigPink
1264 (:invariance
1265 (pn-check-invariance args))
1266 #+:BigPink
1267 (:safety
1268 (pn-check-safety args))
1269 #+:BigPink
1270 (:refinement
1271 (pn-check-refinement args))
1272 ;; unknown
1273 (t (with-output-chaos-error ('invalid-arg)
1274 (format t "unknown option to check: ~a" (%check-what ast)))))))
1275
1276 ;;; *************
1277 ;;; TRAM COMPILER
1278 ;;; *************
1279 (defun eval-tram (ast)
1280 (let ((command (%tram-command ast))
1281 (modexp (%tram-modexp ast))
1282 (pre-term (%tram-term ast))
1283 (debug (%tram-debug ast)))
1284 ;; reset interface
1285 (when (eq command :reset)
1286 (kill-tram-process)
1287 (return-from eval-tram t))
1288 ;; first we check the context
1289 (let ((mod (if modexp
1290 (eval-modexp modexp)
1291 (get-context-module))))
1292 (when (modexp-is-error mod)
1293 (with-output-chaos-error ('no-such-module)
1294 (princ "incorrect module expression, no such module ")
1295 (print-chaos-object modexp)))
1296 ;; process specified command
1297 (case command
1298 ((:compile :compile-all)
1299 (multiple-value-bind (res err)
1300 (tram-compile-chaos-module (if (eq command :compile)
1301 nil
1302 t)
1303 mod debug)
1304 (when err
1305 (with-output-simple-msg ()
1306 (princ "[Error] error occured while module compilation ...")))
1307 (fresh-line)
1308 (princ (car res))
1309 (when (cadr res)
1310 (print-next)
1311 (princ (cadr res)))
1312 (force-output)))
1313 ((:reduce :execute)
1314 (multiple-value-bind (result err-flg)
1315 (tram-reduce mod pre-term (if (eq command :reduce)
1316 nil
1317 t))
1318 (if err-flg
1319 (with-output-simple-msg ()
1320 (princ "[Error] error occured while reduction in compiled code...")
1321 (terpri)
1322 (princ (car result))
1323 (when (cadr result)
1324 (terpri)
1325 (princ (cadr result)))
1326 (force-output))
1327 (progn
1328 (context-push-and-move (get-context-module t) mod)
1329 (let ((*print-indent* (+ 4 *print-indent*)))
1330 (with-in-module (mod)
1331 (setq $$term (car result))
1332 (update-lowest-parse $$term)
1333 (when *mel-sort*
1334 (setf $$term
1335 (apply-sort-memb $$term mod)))
1336 (fresh-line)
1337 (term-print $$term)
1338 (print-check 0 3)
1339 (princ ":")
1340 (print-sort-name (term-sort $$term) mod)
1341 (when (cadr result)
1342 (terpri)
1343 (princ (cadr result)))
1344 (force-output)
1345 (reset-target-term $$term (get-context-module t) mod)))
1346 (context-pop-and-recover)))))
1347 ;;
1348 (otherwise (with-output-panic-message ()
1349 (format t "internal error, unknown tram command ~a"
1350 command)
1351 (chaos-error 'panic)))))))
1352
1353 ;;; ********
1354 ;;; AUTOLOAD
1355 ;;; ********
1356 (defun eval-autoload (ast)
1357 (let ((modname (%autoload-mod-name ast))
1358 (file (%autoload-file ast)))
1359 (let ((entry (assoc modname *autoload-alist* :test #'equal)))
1360 (if entry
1361 (setf (cdr entry) file)
1362 (push (cons modname file) *autoload-alist*)))))
1363
1364 ;;; ************
1365 ;;; NO AUTOLOAD
1366 ;;; ************
1367 (defun eval-no-autoload (ast)
1368 (let ((modname (%no-autoload-mod-name ast)))
1369 (unless (assoc modname *autoload-alist* :test #'equal)
1370 (with-output-chaos-warning ()
1371 (format t "Module ~s is not specified as 'autoload'." modname)))
1372 (setq *autoload-alist*
1373 (remove-if #'(lambda (x) (equal modname x)) *autoload-alist*
1374 :key #'car))))
1375
1376 ;;; *********************
1377 ;;; MISC SUPOORT ROUTINES
1378 ;;; *********************
1379
1380 ;;; NOT USED.
1381 (defvar *real-time* 0)
1382 (defvar *run-time* 0)
1383
1384 (defun timer ()
1385 (let ((real *real-time*) (sys *run-time*))
1386 (setq *real-time* (get-internal-real-time))
1387 (setq *run-time* (get-internal-run-time))
1388 (list (float (/ (- *run-time* sys) internal-time-units-per-second))
1389 (float (/ (- *real-time* real) internal-time-units-per-second)))))
1390
1391 #||
1392 (defmacro call-that (x)
1393 `(progn (setq ,x (term-copy-and-returns-list-variables $$norm)) 'done))
1394
1395 (defun termcopy (x) (term-copy-and-returns-list-variables x))
1396
1397 (defun show ()
1398 (when (and $$term (not (eq 'void $$term)))
1399 (term-print $$term))
1400 (values)
1401 )
1402 ||#
1403
1404 ;;; =====
1405 ;;; CBRED
1406 ;;; =====
1407 (defun eval-cbred (ast)
1408 (let ((modexp (%cbred-module ast))
1409 (pre-lhs (%cbred-lhs ast))
1410 (pre-rhs (%cbred-rhs ast))
1411 (*consider-object* t)
1412 (*rewrite-semantic-reduce* nil)
1413 sort
1414 time1
1415 time2
1416 (time-for-parse nil)
1417 (time-for-reduction nil)
1418 (number-matches 0))
1419 (let ((mod (if modexp
1420 (eval-modexp modexp)
1421 (get-context-module t))))
1422 (unless (eq mod (get-context-module t))
1423 (clear-term-memo-table *term-memo-table*))
1424 (when (or (null mod) (modexp-is-error mod))
1425 (if (null mod)
1426 (with-output-chaos-error ('no-context)
1427 (princ "no context mdoule."))
1428 (with-output-chaos-error ('no-such-module)
1429 (princ "no such module: ")
1430 (print-chaos-object modexp))))
1431 (context-push-and-move (get-context-module t) mod)
1432 (when *auto-context-change*
1433 (change-context (get-context-module t) mod))
1434 (with-in-module (mod)
1435 (!setup-reduction mod)
1436 (setq $$mod *current-module*)
1437 (setq sort *cosmos*)
1438 (when *show-stats* (setq time1 (get-internal-run-time)))
1439 (let* ((*parse-variables* nil)
1440 (lhs (simple-parse *current-module* pre-lhs sort))
1441 (rhs (simple-parse *current-module* pre-rhs sort)))
1442 (when (or (null (term-sort lhs))
1443 (null (term-sort rhs))
1444 (sort<= (term-sort lhs) *syntax-err-sort* *chaos-sort-order*)
1445 (sort<= (term-sort rhs) *syntax-err-sort* *chaos-sort-order*))
1446 (context-pop-and-recover)
1447 (return-from eval-cbred nil))
1448 (let ((canon (canonicalize-variables (list lhs rhs) mod)))
1449 (setq lhs (first canon))
1450 (setq rhs (second canon)))
1451 (when *show-stats*
1452 (setq time2 (get-internal-run-time))
1453 (setq time-for-parse
1454 (format nil "~,3f sec"
1455 (elapsed-time-in-seconds time1 time2))))
1456 ;;
1457 (unless *chaos-quiet*
1458 (fresh-all)
1459 (flush-all)
1460 (princ "-- cbred in ")
1461 (print-simple-mod-name *current-module*)
1462 (print-check 0 3)
1463 (princ " : ")
1464 (let ((*print-indent* (+ 4 *print-indent*)))
1465 (term-print lhs)
1466 (print-check 0 4)
1467 (princ " == ")
1468 (term-print rhs))
1469 (flush-all))
1470 (setq $$matches 0)
1471 (setq time1 (get-internal-run-time))
1472
1473 (let ((*perform-on-demand-reduction* t)
1474 (*rule-count* 0))
1475 (multiple-value-bind (ok? goals)
1476 (do-cbred *current-module* lhs rhs)
1477 ;;
1478 (setq time2 (get-internal-run-time))
1479 (setq time-for-reduction
1480 (format nil "~,3f sec"
1481 (elapsed-time-in-seconds time1 time2)))
1482 (setq number-matches $$matches)
1483 (setq $$matches 0)
1484 (with-output-simple-msg ()
1485 (princ "** Result: ")
1486 (if ok?
1487 (princ "true")
1488 (progn
1489 (princ "falied.")
1490 (let ((*print-indent* *print-indent*))
1491 (print-next)
1492 (princ "remaining goals:")
1493 (dolist (goal goals)
1494 (print-next)
1495 (term-print (car goal))
1496 (print-check 0 4)
1497 (princ " == ")
1498 ;; (print-check)
1499 (term-print (cdr goal)))))))
1500 ;;
1501 (when *show-stats*
1502 (format t "~%(~a for parse, ~s rewrites(~a), ~d matches"
1503 time-for-parse
1504 *rule-count*
1505 time-for-reduction
1506 number-matches))
1507 (if (zerop *term-memo-hash-hit*)
1508 (format t ")~%")
1509 (format t ", ~d memo hits)~%"
1510 *term-memo-hash-hit*))
1511 (flush-all)))))
1512 (context-pop-and-recover))))
1513
1514 ;;; INSEPCT
1515 (defun eval-inspect (ast)
1516 (let ((modexp (%inspect-modexp ast))
1517 mod)
1518 (setf mod (if (null modexp)
1519 (get-context-module)
1520 (eval-modexp modexp)))
1521 (when (modexp-is-error mod)
1522 (with-output-chaos-error ('no-such-module)
1523 (princ "incorrect module expression or unknown module: ")
1524 (print-modexp modexp)))
1525 ;;
1526 (unless mod
1527 (with-output-chaos-error ('no-context)
1528 (princ "no module to be inspeted!")))
1529 ;;
1530 (!inspect-module mod) ))
1531
1532 ;;;
1533 ;;; WHAT-IS
1534 ;;;
1535 (defun eval-what-is (ast)
1536 (let ((name (%what-is-name ast))
1537 (modexp (%what-is-module ast))
1538 (mod nil))
1539 (setf mod (if (null modexp)
1540 (get-context-module)
1541 (eval-modexp modexp)))
1542 (when (modexp-is-error mod)
1543 (with-output-chaos-error ('no-such-module))
1544 (princ "incorrect module expression or unknown module: ")
1545 (print-modexp modexp))
1546 ;;!!!
1547 name
1548 ;;
1549 ;; (!what-is name mod)
1550 ))
1551
1552 ;;; EVAL-LOOK-UP
1553 ;;;
1554 (defun eval-look-up (ast)
1555 (let ((name (%look-up-name ast))
1556 (modexp (%look-up-module ast))
1557 (mod nil))
1558 (setf mod (if (null modexp)
1559 (get-context-module)
1560 (eval-modexp modexp)))
1561 (when (modexp-is-error mod)
1562 (with-output-chaos-error ('no-such-module)
1563 (princ "incorrect module expression or unknown module: ")
1564 (print-modexp modexp)))
1565 ;;
1566 (!look-up name mod)))
1567
1568 ;;;
1569 ;;; DELIMITER
1570 ;;;
1571 (defun eval-delimiter (ast)
1572 (let ((op (%delimiter-operation ast))
1573 (chars (%delimiter-char-list ast)))
1574 (case op
1575 ((:set :add) (!force-single-reader chars))
1576 ((:delete) (!unset-single-reader chars))
1577 (otherwise (with-output-chaos-error ('internal)
1578 (format t "Internal error, invalid delimiter operation ~s" op))))))
1579
1580 ;;; *******************
1581 ;;; Chaos Top
1582 ;;; *******************
1583 (defun eval-chaos-top (ast)
1584 (let ((parameters (%chaos-parameters ast)))
1585 (declare (ignore parameters)) ; for now
1586 (chaos-top)))
1587
1588 ;;;
1589 (eval-when (:execute :load-toplevel :compile-toplevel)
1590 (defmacro with-no-chaos-counter-parts ((name) &body body)
1591 ` (block body
1592 (when (and *dribble-ast* *dribble-stream*)
1593 (format *dribble-stream* "~&;; ** ~s has no chaos couterparts.~%" ,name))
1594 (when *eval-ast*
1595 ,@body))))
1596
1597 ;;; *********
1598 ;;; CONTROL-D
1599 ;;; *********
1600 ;;; specialy handled for interacting with CafeOBJ server.
1601
1602 (defun cafeobj-eval-control-d (&rest ignore)
1603 (declare (ignore ignore))
1604 (when *eval-ast*
1605 (princ eof-char)
1606 (force-output)))
1607
1608 ;;; ***********
1609 ;;; LISP Escape
1610 ;;; ***********
1611
1612 (defun cafeobj-eval-lisp-proc (ast)
1613 (let ((res (eval-ast ast)))
1614 (setq $ res)
1615 (unless *cafeobj-input-quiet*
1616 (let ((*print-case* :upcase))
1617 (format t "~&-> ~s" res)))))
1618
1619 (defun cafeobj-eval-lisp-q-proc (ast)
1620 (let ((*cafeobj-input-quiet* t))
1621 (declare (special *cafeobj-input-quiet*))
1622 (cafeobj-eval-lisp-proc ast)))
1623
1624 ;;; *****
1625 ;;; INPUT
1626 ;;; *****
1627 ;;; reading in file.
1628
1629 (defvar .in-in. nil)
1630 (declaim (special .in-in.))
1631
1632 (defun cafeobj-eval-input-proc (file)
1633 (!input-file file))
1634
1635 (defun !input-file (file)
1636 (when *eval-ast*
1637 (unless (or (at-top-level) *cafeobj-input-quiet*)
1638 (format t "~&-- reading in file : ~a~%" (namestring file))))
1639 (setq .in-in. t)
1640 (with-chaos-top-error ()
1641 (with-chaos-error ()
1642 (eval-ast
1643 (%input* file *chaos-libpath*
1644 'process-cafeobj-input '(".bin" ".cafe" ".mod") nil))
1645 ))
1646 ;; (cafeobj-input file)
1647 (when *eval-ast*
1648 (unless (or (at-top-level) *cafeobj-input-quiet*)
1649 (format t "~%-- done reading in file: ~a~%" (namestring file)))))
1650
1651 ;;; ***********
1652 ;;; SAVE-SYSTEM
1653 ;;; ***********
1654 ;;; save current status CafeOBJ interpreter as a executable file.
1655
1656 (defun save-cafeobj (&optional (file "./xbin/cafeobj.exe"))
1657 (setq -cafeobj-load-time- (get-time-string))
1658 (set-cafeobj-standard-library-path)
1659 (eval-ast (%save-chaos* 'cafeobj-top-level file)))
1660
1661 (defun save-cafeobj-no-top (&optional (file "./xbin/cafeobj.exe"))
1662 (setq -cafeobj-load-time- (get-time-string))
1663 (set-cafeobj-standard-library-path)
1664 (eval-ast (%save-chaos* nil file)))
1665
1666 (defun cafeobj-eval-save-system (inp)
1667 (let ((file (cadr inp)))
1668 (if (equal file ".")
1669 (save-cafeobj-no-top)
1670 (save-cafeobj-no-top file))))
1671
1672 ;;; **********
1673 ;;; CLEAR-MEMO
1674 ;;; **********
1675 (defun cafeobj-eval-clear-memo-proc (&rest ignore)
1676 (declare (ignore ignore))
1677 (clear-term-memo-table *term-memo-table*))
1678
1679 ;;; *******
1680 ;;; PRELUDE
1681 ;;; *******
1682 ;;; reading in prelude file
1683
1684 ;;; defined in cafeobjvar.lisp
1685 ;;; (defvar *cafeobj-standard-prelude-path* nil)
1686
1687 (defun cafeobj-eval-prelude-proc (ast)
1688 (setq *cafeobj-standard-prelude-path*
1689 (with-chaos-error ()
1690 (eval-ast ast))))
1691
1692 (defun cafeobj-eval-prelude-proc+ (inp)
1693 (let ((f (cafeobj-probe-file (cadr inp))))
1694 (unless f
1695 (with-output-chaos-error ()
1696 (format t "no such file ~a" (cadr inp))))
1697 ;;
1698 (setq *cafeobj-standard-prelude-path* f)
1699 ;; now not uses AST
1700 (load-prelude+ f 'process-cafeobj-input)))
1701
1702 ;;; ********
1703 ;;; Comments
1704 ;;; ********
1705
1706 (defun cafeobj-eval-comment-proc (inp)
1707 (declare (ignore inp))
1708 nil)
1709
1710 ;;; WHAT?
1711 (defun cafeobj-eval-what-proc (inp)
1712 (cafeobj-what-is inp))
1713
1714 ;;; DELIMITER
1715 (defun eval-delimiter-proc (pre-args)
1716 (declare (type list pre-args)
1717 (values t))
1718 (let ((args nil)
1719 (op nil)
1720 (ast nil))
1721 (case-equal (the simple-string (second pre-args))
1722 ("=" (setq op :set))
1723 ("+" (setq op :add))
1724 ("-" (setq op :delete))
1725 (t (with-output-chaos-error ('internal)
1726 (format t "delimiter op given ivalid op ~a" (second pre-args)))))
1727 (setq pre-args (fourth pre-args))
1728 (dolist (a pre-args)
1729 (push a args))
1730 (setq ast (%delimiter* op (nreverse args)))
1731 (eval-ast ast)))
1732
1733 (defun eval-show-delimiter (&rest ignore)
1734 (declare (ignore ignore))
1735 (lex-show-delimiters *standard-output*))
1736
1737
1738 ;;;
1739 ;;; EVAL-GENDOC
1740 ;;;
1741 (defun eval-gendoc (ast)
1742 (let ((file (%gendoc-file ast)))
1743 (export-refman file)))
1744
1745 ;;; EOF