aboutsummaryrefslogtreecommitdiff
path: root/legacy/bigloo/engine.scm
blob: bd8a02726ad4edb2ecc251f0b6b736cac28ddf96 (about) (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
;*=====================================================================*/
;*    serrano/prgm/project/skribe/src/bigloo/engine.scm                */
;*    -------------------------------------------------------------    */
;*    Author      :  Manuel Serrano                                    */
;*    Creation    :  Tue Sep  9 08:01:30 2003                          */
;*    Last change :  Fri May 21 16:12:32 2004 (serrano)                */
;*    Copyright   :  2003-04 Manuel Serrano                            */
;*    -------------------------------------------------------------    */
;*    Skribe engines                                                   */
;*=====================================================================*/

;*---------------------------------------------------------------------*/
;*    The module                                                       */
;*---------------------------------------------------------------------*/
(module skribe_engine
   
   (option  (set! dsssl-symbol->keyword 
		  (lambda (s)
		     (string->keyword
		      (string-append ":" (symbol->string s))))))
   
   (include "debug.sch")
   
   (import  skribe_types
	    skribe_eval
	    skribe_param
	    skribe_output)
   
   (export  (make-engine::%engine ::symbol #!key v fmt in fi cu st if)
	    (copy-engine::%engine ::symbol ::%engine #!key v in fi cu st)
	    (find-engine ::symbol #!key version)

	    (default-engine::obj)
	    (default-engine-set! ::%engine)
	    (push-default-engine ::%engine)
	    (pop-default-engine)

	    (processor-get-engine ::obj ::obj ::%engine)
	    
	    (engine-format? ::bstring . e)

	    (engine-custom::obj ::%engine ::symbol)
	    (engine-custom-set! ::%engine ::symbol ::obj)

	    (engine-add-writer! ::%engine ::obj ::procedure ::obj
				::obj ::obj ::obj ::obj ::obj ::obj)))
   
;*---------------------------------------------------------------------*/
;*    *engines* ...                                                    */
;*---------------------------------------------------------------------*/
(define *engines* '())

;*---------------------------------------------------------------------*/
;*    *default-engine* ...                                             */
;*---------------------------------------------------------------------*/
(define *default-engine* #f)
(define *default-engines* '())

;*---------------------------------------------------------------------*/
;*    default-engine-set! ...                                          */
;*---------------------------------------------------------------------*/
(define (default-engine-set! e)
   (if (not (engine? e))
       (skribe-type-error 'default-engine-set! "engine" e (find-runtime-type e))
       (begin
	  (set! *default-engine* e)
	  (set! *default-engines* (cons *default-engine* *default-engines*))
	  e)))

;*---------------------------------------------------------------------*/
;*    default-engine ...                                               */
;*---------------------------------------------------------------------*/
(define (default-engine)
   *default-engine*)

;*---------------------------------------------------------------------*/
;*    push-default-engine ...                                          */
;*---------------------------------------------------------------------*/
(define (push-default-engine e)
   (set! *default-engines* (cons e *default-engines*))
   (default-engine-set! e))

;*---------------------------------------------------------------------*/
;*    pop-default-engine ...                                           */
;*---------------------------------------------------------------------*/
(define (pop-default-engine)
   (if (null? *default-engines*)
       (skribe-error 'pop-default-engine "Empty engine stack" '())
       (begin
	  (set! *default-engines* (cdr *default-engines*))
	  (if (pair? *default-engines*)
	      (default-engine-set! (car *default-engines*))
	      (set! *default-engine* #f)))))

;*---------------------------------------------------------------------*/
;*    processor-get-engine ...                                         */
;*---------------------------------------------------------------------*/
(define (processor-get-engine combinator newe olde)
   (cond
      ((procedure? combinator)
       (combinator newe olde))
      ((engine? newe)
       newe)
      (else
       olde)))

;*---------------------------------------------------------------------*/
;*    engine-format? ...                                               */
;*---------------------------------------------------------------------*/
(define (engine-format? fmt . e)
   (let ((e (cond
	       ((pair? e) (car e))
	       ((%engine? *skribe-engine*) *skribe-engine*)
	       (else (find-engine *skribe-engine*)))))
      (if (not (%engine? e))
	  (skribe-error 'engine-format? "No engine" e)
	  (string=? fmt (%engine-format e)))))

;*---------------------------------------------------------------------*/
;*    make-engine ...                                                  */
;*---------------------------------------------------------------------*/
(define (make-engine ident
		     #!key
		     (version #unspecified)
		     (format "raw")
		     (filter #f)
		     (delegate #f)
		     (symbol-table '())
		     (custom '())
		     (info '()))
   (let ((e (instantiate::%engine
	       (ident ident)
	       (version version)
	       (format format)
	       (filter filter)
	       (delegate delegate)
	       (symbol-table symbol-table)
	       (customs custom)
	       (info info))))
      ;; store the engine in the global table
      (set! *engines* (cons e *engines*))
      ;; return it
      e))

;*---------------------------------------------------------------------*/
;*    copy-engine ...                                                  */
;*---------------------------------------------------------------------*/
(define (copy-engine ident
		     e
		     #!key
		     (version #unspecified)
		     (filter #f)
		     (delegate #f)
		     (symbol-table #f)
		     (custom #f))
   (let ((e (duplicate::%engine e
	       (ident ident)
	       (version version)
	       (filter (or filter (%engine-filter e)))
	       (delegate (or delegate (%engine-delegate e)))
	       (symbol-table (or symbol-table (%engine-symbol-table e)))
	       (customs (or custom (%engine-customs e))))))
      (set! *engines* (cons e *engines*))
      e))

;*---------------------------------------------------------------------*/
;*    find-loaded-engine ...                                           */
;*---------------------------------------------------------------------*/
(define (find-loaded-engine id version)
   (let loop ((es *engines*))
      (cond
	 ((null? es)
	  #f)
	 ((eq? (%engine-ident (car es)) id)
	  (cond
	     ((eq? version #unspecified)
	      (car es))
	     ((eq? version (%engine-version (car es)))
	      (car es))
	     (else
	      (loop (cdr es)))))
	 (else
	  (loop (cdr es))))))

;*---------------------------------------------------------------------*/
;*    find-engine ...                                                  */
;*---------------------------------------------------------------------*/
(define (find-engine id #!key (version #unspecified))
   (with-debug 5 'find-engine
      (debug-item "id=" id " version=" version)
      (or (find-loaded-engine id version)
	  (let ((c (assq id *skribe-auto-load-alist*)))
	     (debug-item "c=" c)
	     (if (and (pair? c) (string? (cdr c)))
		 (begin
		    (skribe-load (cdr c) :engine 'base)
		    (find-loaded-engine id version))
		 #f)))))

;*---------------------------------------------------------------------*/
;*    engine-custom ...                                                */
;*---------------------------------------------------------------------*/
(define (engine-custom e id)
   (with-access::%engine e (customs)
      (let ((c (assq id customs)))
	 (if (pair? c)
	     (cadr c)
	     #unspecified))))

;*---------------------------------------------------------------------*/
;*    engine-custom-set! ...                                           */
;*---------------------------------------------------------------------*/
(define (engine-custom-set! e id val)
   (with-access::%engine e (customs)
      (let ((c (assq id customs)))
	 (if (pair? c)
	     (set-car! (cdr c) val)
	     (set! customs (cons (list id val) customs))))))

;*---------------------------------------------------------------------*/
;*    engine-add-writer! ...                                           */
;*---------------------------------------------------------------------*/
(define (engine-add-writer! e id pred upred opt before action after class va)
   ;; check the arity of a procedure
   (define (check-procedure name proc arity)
      (cond
	 ((not (procedure? proc))
	  (skribe-error id "Illegal procedure" proc))
	 ((not (correct-arity? proc arity))
	  (skribe-error id
			(string-append "Illegal `" name "'procedure")
			proc))))
   (define (check-output name proc)
      (and proc (or (string? proc) (check-procedure name proc 2))))
   ;; check the engine
   (if (not (engine? e))
       (skribe-error id "Illegal engine" e))
   ;; check the options
   (if (not (or (eq? opt 'all) (list? opt)))
       (skribe-error id "Illegal options" opt))
   ;; check the correctness of the predicate and the validator
   (check-procedure "predicate" pred 2)
   (when va (check-procedure "validate" va 2))
   ;; check the correctness of the three actions
   (check-output "before" before)
   (check-output "action" action)
   (check-output "after" after)
   ;; create a new writer...
   (let ((n (instantiate::%writer
	       (ident (if (symbol? id) id 'all))
	       (class class)
	       (pred pred)
	       (upred upred)
	       (options opt)
	       (before before)
	       (action action)
	       (after after)
	       (validate va))))
      ;; ...and bind it
      (with-access::%engine e (writers)
	 (set! writers (cons n writers))
	 n)))