]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
69fef2e645d1632a6fb93c3329c07f25f7a35d2a
[lilypond.git] / lily / lily-guile.cc
1 /*
2   lily-guile.cc -- implement assorted guile functions
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2000 Jan Nieuwenhuizen <janneke@gnu.org>
7
8   Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 */
10
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h>               // isinf
15
16 #include "libc-extension.hh"
17 #include "lily-guile.hh"
18 #include "main.hh"
19 #include "simple-file-storage.hh"
20 #include "file-path.hh"
21 #include "debug.hh"
22 #include "direction.hh"
23 #include "offset.hh"
24 #include "interval.hh"
25
26 SCM
27 ly_str02scm (char const*c)
28 {
29   // this all really sucks, guile should take char const* arguments!
30   return gh_str02scm ((char*)c);
31 }
32
33
34
35 /*
36   Pass string to scm parser, evaluate one expression.
37   Return result value and #chars read.
38
39   Thanks to Gary Houston <ghouston@freewire.co.uk>
40
41   Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
42 */
43 SCM
44 ly_parse_scm (char const* s, int* n)
45 {
46   SCM str = gh_str02scm ((char*)s);
47   SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
48                             "ly_eval_scm_0str");
49   SCM from = scm_ftell (port);
50
51   SCM form;
52   SCM answer = SCM_UNSPECIFIED;
53
54   /* Read expression from port */
55   if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
56     answer = scm_eval_3 (form, 1, SCM_EOL);
57
58   /*
59    After parsing
60
61        (begin (foo 1 2))
62
63    all seems fine, but after parsing
64
65        (foo 1 2)
66
67    read_buf has been advanced to read_pos - 1,
68    so that scm_ftell returns 1, instead of #parsed chars
69    */
70   
71   /*
72     urg: reset read_buf for scm_ftell
73     shouldn't scm_read () do this for us?
74   */
75   scm_fill_input (port);
76   SCM to = scm_ftell (port);
77   *n = gh_scm2int (to) - gh_scm2int (from);
78
79   /* Don't close the port here; if we re-enter this function via a
80      continuation, then the next time we enter it, we'll get an error.
81      It's a string port anyway, so there's no advantage to closing it
82      early.
83
84      scm_close_port (port);
85   */
86
87   return answer;
88 }
89
90 SCM
91 ly_quote_scm (SCM s)
92 {
93   return gh_list (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
94 }
95
96
97 SCM
98 ly_symbol2scm(const char *s)
99 {
100   return gh_symbol2scm ((char *)s);
101 }
102
103 String
104 ly_symbol2string (SCM s)
105 {
106   assert (gh_symbol_p (s));
107   return String((Byte*)SCM_CHARS (s), (int) SCM_LENGTH(s));
108 }
109
110
111 String
112 gulp_file_to_string (String fn)
113 {
114   String s = global_path.find (fn);
115   if (s == "")
116     {
117       String e = _f ("can't find file: `%s'", fn);
118       e += " ";
119       e += _f ("(load path: `%s')", global_path.str ());
120       error (e);
121     }
122   else if (verbose_global_b)
123     progress_indication ("[" + s );
124
125
126   Simple_file_storage f(s);
127   String result (f.ch_C());
128   if (verbose_global_b)
129     progress_indication ("]");
130   return result;
131 }
132
133 SCM
134 ly_gulp_file (SCM fn)
135 {
136   return ly_str02scm (gulp_file_to_string (ly_scm2string (fn)).ch_C());
137 }
138
139
140 /**
141    Read a file, and shove it down GUILE.  GUILE also has file read
142    functions, but you can't fiddle with the path of those.
143  */
144 void
145 read_lily_scm_file (String fn)
146 {
147   gh_eval_str ((char *) gulp_file_to_string (fn).ch_C());
148 }
149
150 extern "C" {
151   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
152 void
153 ly_display_scm (SCM s)
154 {
155   gh_display (s);
156   gh_newline ();
157 }
158 };
159
160 String
161 ly_scm2string (SCM s)
162 {
163   assert (gh_string_p (s));
164   int len; 
165   char * p = gh_scm2newstr (s , &len);
166   
167   String r (p);
168
169   free (p);
170   return r;
171 }
172
173 SCM
174 index_cell (SCM s, Direction d)
175 {
176   assert (d);
177   return (d == LEFT) ? gh_car  (s) : gh_cdr (s);
178 }
179
180 SCM
181 index_set_cell (SCM s, Direction d, SCM v)
182 {
183   if (d == LEFT)
184     gh_set_car_x (s, v);
185   else if (d == RIGHT)
186     gh_set_cdr_x (s, v);
187   return s;
188 }
189   
190 SCM
191 ly_warning (SCM str)
192 {
193   assert (gh_string_p (str));
194   warning ("lily-guile: " + ly_scm2string (str));
195   return SCM_BOOL_T;
196 }
197
198 SCM
199 ly_isdir_p (SCM s)
200 {
201   if (gh_number_p (s))
202     {
203       int i = gh_scm2int (s);
204       return (i>= -1 && i <= 1)  ? SCM_BOOL_T : SCM_BOOL_F; 
205     }
206   return SCM_BOOL_F;
207 }
208
209
210
211 typedef void (*Void_fptr)();
212 Array<Void_fptr> *scm_init_funcs_;
213
214 void add_scm_init_func (void (*f)())
215 {
216   if (!scm_init_funcs_)
217     scm_init_funcs_ = new Array<Void_fptr>;
218
219   scm_init_funcs_->push (f);
220 }
221
222 void
223 init_lily_guile ()
224 {
225   for (int i=scm_init_funcs_->size() ; i--;)
226     (scm_init_funcs_->elem (i)) ();
227 }
228
229 unsigned int ly_scm_hash (SCM s)
230 {
231   return scm_ihashv (s, ~1u);
232 }
233
234
235
236 bool
237 isdir_b (SCM s)
238 {
239   if (gh_number_p (s))
240     {
241       int i = gh_scm2int (s);
242       return i>= -1 && i <= 1; 
243     }
244   return false;
245 }
246
247 Direction
248 to_dir (SCM s)
249 {
250   return (Direction) gh_scm2int (s);
251 }
252
253 Interval
254 ly_scm2interval (SCM p)
255 {
256   return  Interval (gh_scm2double (gh_car (p)),
257                     gh_scm2double (gh_cdr (p)));
258 }
259
260 SCM
261 ly_interval2scm (Interval i)
262 {
263   return gh_cons (gh_double2scm (i[LEFT]),
264                   gh_double2scm (i[RIGHT]));
265 }
266
267
268 bool
269 to_boolean (SCM s)
270 {
271   return gh_boolean_p (s) && gh_scm2bool (s);
272 }
273
274 /*
275   Appendable list L: the cdr contains the list, the car the last cons
276   in the list.
277  */
278 SCM
279 appendable_list ()
280 {
281   SCM s = gh_cons (SCM_EOL, SCM_EOL);
282   gh_set_car_x (s, s);
283   
284   return s;
285 }
286
287 void
288 appendable_list_append (SCM l, SCM elt)
289 {
290   SCM newcons = gh_cons (elt, SCM_EOL);
291   
292   gh_set_cdr_x (gh_car (l), newcons);      
293   gh_set_car_x (l, newcons);
294 }
295
296
297 SCM
298 ly_offset2scm (Offset o)
299 {
300   return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]));
301 }
302
303 Offset
304 ly_scm2offset (SCM s)
305 {
306   return Offset (gh_scm2double (gh_car (s)),
307                  gh_scm2double (gh_cdr (s)));
308 }
309
310 SCM
311 ly_type (SCM exp)
312 {
313   char const  * cp = "unknown";
314   if (gh_number_p (exp))
315     {
316       cp = "number";
317     }
318   else if (gh_string_p (exp))
319     {
320       cp = "string";
321     }
322   else if (gh_procedure_p (exp))
323     {
324       cp = "procedure";
325     }
326   else if (gh_boolean_p (exp))
327     {
328       cp = "boolean";
329     }
330   else if (gh_pair_p (exp))
331     {
332       cp = "list";
333     }
334
335   return ly_str02scm (cp);
336 }
337
338 /*
339   convert without too many decimals, and leave  a space at the end.
340  */
341    
342    
343 SCM
344 ly_number2string (SCM s)
345 {
346   assert (gh_number_p (s));
347
348   char str[100];                        // ugh.
349
350   if (scm_integer_p (s) == SCM_BOOL_F)
351     {
352       Real r (gh_scm2double (s));
353
354       if (isinf (r) || isnan (r))
355         {
356           programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
357           r = 0.0;
358         }
359
360       sprintf (str, "%8.4f ", r);
361     }
362   else
363     {
364       sprintf (str, "%d ", gh_scm2int (s));
365     }
366
367   return gh_str02scm (str);
368 }
369
370 /*
371   Undef this to see if GUILE GC is causing too many swaps.
372  */
373
374 // #define TEST_GC
375
376 #ifdef TEST_GC
377 #include <libguile/gc.h>
378
379 static void *
380 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
381 {
382    fprintf(stderr, "entering sweep\n");
383 }
384
385 static void *
386 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
387 {
388    fprintf(stderr, "leaving sweep\n");
389 }
390 #endif
391
392 SCM
393 undefd ()
394 {
395   return SCM_UNDEFINED;
396 }
397
398
399 static void
400 init_functions ()
401 {
402   scm_make_gsubr ("ly-warn", 1, 0, 0, (SCM(*)())ly_warning);
403   scm_make_gsubr ("ly-gulp-file", 1,0, 0, (SCM(*)())ly_gulp_file);
404   scm_make_gsubr ("dir?", 1,0, 0, (SCM(*)())ly_isdir_p);
405   scm_make_gsubr ("undefd", 0,0, 0, (SCM(*)())undefd);  
406   scm_make_gsubr ("ly-number->string", 1, 0,0, (SCM(*)()) ly_number2string);
407
408
409 #ifdef TEST_GC 
410   scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
411   scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
412 #endif
413 }
414
415 ADD_SCM_INIT_FUNC(funcs, init_functions);
416
417 SCM
418 ly_deep_copy (SCM l)
419 {
420   if (gh_pair_p (l))
421     {
422       return gh_cons (ly_deep_copy (gh_car (l)), ly_deep_copy (gh_cdr (l)));
423     }
424   else
425     return l;
426 }
427
428