summaryrefslogtreecommitdiff
path: root/Sacla/share.lisp
blob: 9d4a4d7e0d1f7483418c6ecf459e37699b2a9fb1 (plain)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
;; Copyright (C) 2002-2004, Yuji Minejima <ggb01164@nifty.ne.jp>
;; ALL RIGHTS RESERVED.
;;
;; $Id: share.lisp,v 1.10 2004/09/02 06:59:43 yuji Exp $
;; 
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions
;; are met:
;; 
;;  * Redistributions of source code must retain the above copyright
;;    notice, this list of conditions and the following disclaimer.
;;  * Redistributions in binary form must reproduce the above copyright
;;    notice, this list of conditions and the following disclaimer in
;;    the documentation and/or other materials provided with the
;;    distribution.
;; 
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
;; LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;; DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
;; THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

(in-package #:common-lisp)


(deftype proper-list () '(satisfies proper-list-p))
(deftype proper-sequence () '(satisfies proper-sequence-p))
(deftype string-designator () '(or character symbol string))
(deftype package-designator () '(or string-designator package))
(deftype function-designator () '(or symbol function))
(deftype extended-function-designator ()
  '(or function (satisfies function-name-p)))
(deftype character-designator-simbol ()
  '(satisfies character-designator-symbol-p))

(defun character-designator-symbol-p (object)
  (and (symbolp object) (= (length (symbol-name object)) 1)))

(defun function-name-p (object)
  (or (symbolp object)
      (and (consp object)
	   (eq (car object) 'setf)
	   (symbolp (cadr object))
	   (null (cddr object)))))

(defun proper-sequence-p (object)
  (or (vectorp object) (proper-list-p object)))

(defun error-circular-list (list)
  (error 'type-error :datum list :expected-type 'proper-list))

(defun error-index-too-large (sequence index)
  (error 'type-error
	 :datum index
	 :expected-type `(integer 0 ,(1- (length sequence)))))

(defmacro apply-key (key element)
  `(if ,key
       (funcall ,key ,element)
     ,element))


(defmacro do-sublist ((var list start end from-end result) &body body)
  (let ((rev (gensym))
	(i   (gensym))
	(x   (gensym)))
    `(symbol-macrolet ((,var (car ,x)))
       (if ,from-end
	   (let ((,rev nil))
	     (do ((x (nthcdr ,start ,list) (cdr x))
		  (i ,start (1+ i)))
		 ((>= i ,end))
	       (setq ,rev (cons x ,rev)))
	     (do* ((,rev ,rev (cdr ,rev))
		   (,x (car ,rev) (car ,rev)))
		 ((null ,rev) ,result)
	       ,@body))
	 (do ((,x (nthcdr ,start ,list) (cdr ,x))
	      (,i ,start (1+ ,i)))
	     ((>= ,i ,end) ,result)
	   ,@body)))))

(defmacro do-subvector ((var vector start end from-end result) &body body)
  (let ((i     (gensym))
	(step  (gensym))
	(limit (gensym)))
    `(symbol-macrolet ((,var (aref ,vector ,i)))
       (let ((,step (if ,from-end -1 1))
	     (,limit (if ,from-end (1- ,start) ,end)))
	 (do ((,i (if ,from-end (1- ,end) ,start) (+ ,i ,step)))
	     ((= ,i ,limit) ,result)
	  ,@body)))))

(defmacro do-subsequence ((var sequence-form start-form &optional end-form
			       from-end-form result-form) &body body)
  (let ((sequence (gensym))
	(start    (gensym))
	(end      (gensym)))
    `(let* ((,sequence ,sequence-form)
	    (,start ,start-form)
	    (,end (or ,end-form (length ,sequence))))
       (check-subsequence ,sequence ,start ,end)
       (etypecase ,sequence
	 (list
	  (do-sublist (,var ,sequence ,start ,end ,from-end-form ,result-form)
             ,@body))
	 (vector
	  (do-subvector (,var ,sequence ,start ,end ,from-end-form ,result-form)
	     ,@body))))))

(defun declarationp (expr)
  (and (consp expr) (eq (car expr) 'declare)))

(defun declarations-and-forms (body)
  (block nil
    (let ((decls nil)
	  (forms body))
      (tagbody
       top
	 (when (not (declarationp (car forms)))
	   (return (values (reverse decls) forms)))
	 (push (car forms) decls) (psetq forms (cdr forms))
	 (go top)))))


(defun required-argument ()
  (error "required argument not specified."))


(defun %symbol (designator)
  (if (symbolp designator)
      designator
      (error 'type-error :datum designator :expected-type 'symbol)))
(defun %keyword (designator)
  (intern (string designator) "KEYWORD"))
(defun %list (designator)
  (if (listp designator)
      designator
      (list designator)))
(defun symbol-list (designator) (mapcar #'%symbol (%list designator)))
(defun string-list (designator) (mapcar #'string (%list designator)))



(defun store-value-report (stream place)
  (format stream "Supply a new value for ~S." place))
(defun store-value-interactive ()
  (format *query-io* "~&Type a form to be evaluated:~%")
  (list (eval (read *query-io*))))



(defun mapappend (function &rest lists)
  (apply #'append (apply #'mapcar function lists)))

;; (define-condition simple-program-error (simple-condition program-error) ())

;; (define-modify-macro appendf (&rest args)
;;    append "Append onto list")


;; (defvar *message-prefix* "")
;; ;; for debug
;; (defvar *error-function* #'error)
;; (defun error (datum &rest arguments)
;;   (if (stringp datum)
;;       (let ((format-control (concatenate 'string *message-prefix* datum)))
;;         (apply *error-function* format-control arguments))
;;       (apply *error-function* datum arguments)))