aboutsummaryrefslogtreecommitdiff
path: root/src/bigloo/eval.scm
blob: b5c6548505e65ff70a4541363b8a03548c3b490e (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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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
;*=====================================================================*/
;*    serrano/prgm/project/skribe/src/bigloo/eval.scm                  */
;*    -------------------------------------------------------------    */
;*    Author      :  Manuel Serrano                                    */
;*    Creation    :  Wed Jul 23 12:48:11 2003                          */
;*    Last change :  Wed May 18 15:52:01 2005 (serrano)                */
;*    Copyright   :  2003-05 Manuel Serrano                            */
;*    -------------------------------------------------------------    */
;*    The Skribe evaluator                                             */
;*=====================================================================*/

;*---------------------------------------------------------------------*/
;*    The module                                                       */
;*---------------------------------------------------------------------*/
(module skribe_eval

   (option  (set! dsssl-symbol->keyword 
		  (lambda (s)
		     (string->keyword
		      (string-append ":" (symbol->string s))))))

   (include "debug.sch")
   
   (import skribe_param
	   skribe_types
	   skribe_resolve
	   skribe_verify
	   skribe_output
	   skribe_read
	   skribe_lib
	   skribe_engine)

   (export (skribe-eval-location)
	   (skribe-error ::obj ::obj ::obj)
	   (skribe-type-error ::obj ::obj ::obj ::bstring)
	   (skribe-warning ::int . obj)
	   (skribe-warning/ast ::int ::%ast . obj)
	   (skribe-message ::bstring . obj)
	   (skribe-load ::bstring #!rest opt #!key engine path)
	   (skribe-load-options)
	   (skribe-include ::bstring . rest)
	   (skribe-open-bib-file ::bstring ::obj)
	   (skribe-eval-port ::input-port ::obj #!key env)
	   (skribe-eval ::obj ::%engine #!key env)
	   (skribe-path::pair-nil)
	   (skribe-path-set! ::obj)
	   (skribe-image-path::pair-nil)
	   (skribe-image-path-set! ::obj)
	   (skribe-bib-path::pair-nil)
	   (skribe-bib-path-set! ::obj)
	   (skribe-source-path::pair-nil)
	   (skribe-source-path-set! ::obj)))

;*---------------------------------------------------------------------*/
;*    skribe-eval-location ...                                         */
;*---------------------------------------------------------------------*/
(define (skribe-eval-location)
   (evmeaning-location))

;*---------------------------------------------------------------------*/
;*    skribe-error ...                                                 */
;*---------------------------------------------------------------------*/
(define (skribe-error proc msg obj)
   (if (ast? obj)
       (skribe-ast-error proc msg obj)
       (error/evloc proc msg obj)))

;*---------------------------------------------------------------------*/
;*    skribe-type-error ...                                            */
;*---------------------------------------------------------------------*/
(define (skribe-type-error proc msg obj etype)
   (let ((ty (if (%markup? obj)
		 (format "~a#~a" (markup-markup obj) (markup-ident obj))
		 (find-runtime-type obj))))
      (skribe-error proc
		    (bigloo-type-error-msg msg etype ty)
		    obj)))

;*---------------------------------------------------------------------*/
;*    skribe-ast-error ...                                             */
;*---------------------------------------------------------------------*/
(define (skribe-ast-error proc msg obj)
   (let ((l (ast-loc obj))
	 (shape (if (%markup? obj)
		    (%markup-markup obj)
		    (find-runtime-type obj))))
      (if (location? l)
	  (error/location proc msg shape (location-file l) (location-pos l))
	  (error/evloc proc msg shape))))

;*---------------------------------------------------------------------*/
;*    error/evloc ...                                                  */
;*---------------------------------------------------------------------*/
(define (error/evloc proc msg obj)
   (let ((l (evmeaning-location)))
      (if (location? l)
	  (error/location proc msg obj (location-file l) (location-pos l))
	  ((begin error) proc msg obj))))

;*---------------------------------------------------------------------*/
;*    skribe-warning ...                                               */
;*---------------------------------------------------------------------*/
(define (skribe-warning level . obj)
   (if (>= *skribe-warning* level)
       (let ((l (evmeaning-location)))
	  (if (location? l)
	      (apply warning/location (location-file l) (location-pos l) obj)
	      (apply warning obj)))))

;*---------------------------------------------------------------------*/
;*    skribe-warning/ast ...                                           */
;*---------------------------------------------------------------------*/
(define (skribe-warning/ast level ast . obj)
   (if (>= *skribe-warning* level)
       (let ((l (%ast-loc ast)))
	  (if (location? l)
	      (apply warning/location (location-file l) (location-pos l) obj)
	      (apply skribe-warning level obj)))))

;*---------------------------------------------------------------------*/
;*    skribe-message ...                                               */
;*---------------------------------------------------------------------*/
(define (skribe-message fmt . obj)
   (if (> *skribe-verbose* 0)
       (apply fprintf (current-error-port) fmt obj)))

;*---------------------------------------------------------------------*/
;*    *skribe-loaded* ...                                              */
;*    -------------------------------------------------------------    */
;*    This hash table stores the list of loaded files in order         */
;*    to avoid one file to be loaded twice.                            */
;*---------------------------------------------------------------------*/
(define *skribe-loaded* (make-hashtable))

;*---------------------------------------------------------------------*/
;*    *skribe-load-options* ...                                        */
;*---------------------------------------------------------------------*/
(define *skribe-load-options* '())

;*---------------------------------------------------------------------*/
;*    skribe-load ...                                                  */
;*---------------------------------------------------------------------*/
(define (skribe-load file #!rest opt #!key engine path)
   (with-debug 4 'skribe-load
      (debug-item "  engine=" engine)
      (debug-item "  path=" path)
      (debug-item "  opt" opt)
      (let* ((ei (cond
		    ((not engine)
		     *skribe-engine*)
		    ((engine? engine)
		     engine)
		    ((not (symbol? engine))
		     (skribe-error 'skribe-load "Illegal engine" engine))
		    (else
		     engine)))
	     (path (cond
		      ((not path)
		       (skribe-path))
		      ((string? path)
		       (list path))
		      ((not (and (list? path) (every? string? path)))
		       (skribe-error 'skribe-load "Illegal path" path))
		      (else
		       path)))
	     (filep (find-file/path file path)))
	 (set! *skribe-load-options* opt)
	 (if (and (string? filep) (file-exists? filep))
	     (if (not (hashtable-get *skribe-loaded* filep))
		 (begin
		    (hashtable-put! *skribe-loaded* filep #t)
		    (cond
		       ((>fx *skribe-verbose* 1)
			(fprint (current-error-port)
				"  [loading file: " filep " " opt "]"))
		       ((>fx *skribe-verbose* 0)
			(fprint (current-error-port)
				"  [loading file: " filep "]")))
		    (with-input-from-file filep
		       (lambda ()
			  (skribe-eval-port (current-input-port) ei)))))
	     (skribe-error 'skribe-load
			   (format "Can't find file `~a' in path" file)
			   path)))))

;*---------------------------------------------------------------------*/
;*    skribe-load-options ...                                          */
;*---------------------------------------------------------------------*/
(define (skribe-load-options)
   *skribe-load-options*)

;*---------------------------------------------------------------------*/
;*    evaluate ...                                                     */
;*---------------------------------------------------------------------*/
(define (evaluate exp)
   (try (eval exp)
	(lambda (a p m o)
	   (evmeaning-notify-error p m o)
	   (flush-output-port (current-error-port)))))

;*---------------------------------------------------------------------*/
;*    skribe-include ...                                               */
;*---------------------------------------------------------------------*/
(define (skribe-include file . rest)
   (let* ((path (cond
		   ((or (null? rest) (null? (cdr rest)))
		    (skribe-path))
		   ((not (every? string? (cdr rest)))
		    (skribe-error 'skribe-include "Illegal path" (cdr rest)))
		   (else
		    (cdr rest))))
	  (filep (find-file/path file (if (null? path) (skribe-path) path))))
      (if (and (string? filep) (file-exists? filep))
	  (begin
	     (if (>fx *skribe-verbose* 0)
		 (fprint (current-error-port)
			 "  [including file: " filep "]"))
	     (with-input-from-file filep
		(lambda ()
		   (let loop ((exp (skribe-read (current-input-port)))
			      (res '()))
		      (if (eof-object? exp)
			  (if (and (pair? res) (null? (cdr res)))
			      (car res)
			      (reverse! res))
			  (loop (skribe-read (current-input-port))
				(cons (evaluate exp) res)))))))
	  (skribe-error 'skribe-include
			(format "Can't find file `~a 'in path" file)
			path))))

;*---------------------------------------------------------------------*/
;*    skribe-open-bib-file ...                                         */
;*---------------------------------------------------------------------*/
(define (skribe-open-bib-file file command)
   (let ((filep (find-file/path file *skribe-bib-path*)))
      (if (string? filep)
	  (begin
	     (if (>fx *skribe-verbose* 0)
		 (fprint (current-error-port) "  [loading bib: " filep "]"))
	     (open-input-file (if (string? command)
				  (string-append "| "
						 (format command filep))
				  filep)))
	  (begin
	     (skribe-warning 1
			     'bibliography
			     "Can't find bibliography -- " file)
	     #f))))
      
;*---------------------------------------------------------------------*/
;*    skribe-eval-port ...                                             */
;*---------------------------------------------------------------------*/
(define (skribe-eval-port port ei #!key (env '()))
   (with-debug 2 'skribe-eval-port
      (debug-item "ei=" ei)
      (let ((e (if (symbol? ei) (find-engine ei) ei)))
	 (debug-item "e=" e)
	 (if (not (%engine? e))
	     (skribe-error 'find-engine "Can't find engine" ei)
	     (let loop ((exp (skribe-read port)))
		(with-debug 10 'skribe-eval-port
		   (debug-item "exp=" exp))
		(if (not (eof-object? exp))
		    (begin
		       (skribe-eval (evaluate exp) e :env env)
		       (loop (skribe-read port)))))))))

;*---------------------------------------------------------------------*/
;*    skribe-eval ...                                                  */
;*---------------------------------------------------------------------*/
(define (skribe-eval a e #!key (env '()))
   (with-debug 2 'skribe-eval
      (debug-item "a=" a " e=" (%engine-ident e))
      (let ((a2 (resolve! a e env)))
	 (debug-item "resolved a=" a)
	 (let ((a3 (verify a2 e)))
	    (debug-item "verified a=" a3)
	    (output a3 e)))))

;*---------------------------------------------------------------------*/
;*    skribe-path ...                                                  */
;*---------------------------------------------------------------------*/
(define (skribe-path)
   *skribe-path*)

;*---------------------------------------------------------------------*/
;*    skribe-path-set! ...                                             */
;*---------------------------------------------------------------------*/
(define (skribe-path-set! path)
   (if (not (and (list? path) (every? string? path)))
       (skribe-error 'skribe-path-set! "Illegal path" path)
       (set! *skribe-path* path)))

;*---------------------------------------------------------------------*/
;*    skribe-image-path ...                                            */
;*---------------------------------------------------------------------*/
(define (skribe-image-path)
   *skribe-image-path*)

;*---------------------------------------------------------------------*/
;*    skribe-image-path-set! ...                                       */
;*---------------------------------------------------------------------*/
(define (skribe-image-path-set! path)
   (if (not (and (list? path) (every? string? path)))
       (skribe-error 'skribe-image-path-set! "Illegal path" path)
       (set! *skribe-image-path* path)))

;*---------------------------------------------------------------------*/
;*    skribe-bib-path ...                                              */
;*---------------------------------------------------------------------*/
(define (skribe-bib-path)
   *skribe-bib-path*)

;*---------------------------------------------------------------------*/
;*    skribe-bib-path-set! ...                                         */
;*---------------------------------------------------------------------*/
(define (skribe-bib-path-set! path)
   (if (not (and (list? path) (every? string? path)))
       (skribe-error 'skribe-bib-path-set! "Illegal path" path)
       (set! *skribe-bib-path* path)))

;*---------------------------------------------------------------------*/
;*    skribe-source-path ...                                           */
;*---------------------------------------------------------------------*/
(define (skribe-source-path)
   *skribe-source-path*)

;*---------------------------------------------------------------------*/
;*    skribe-source-path-set! ...                                      */
;*---------------------------------------------------------------------*/
(define (skribe-source-path-set! path)
   (if (not (and (list? path) (every? string? path)))
       (skribe-error 'skribe-source-path-set! "Illegal path" path)
       (set! *skribe-source-path* path)))