]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
release: 1.3.60
[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                             "scm_eval_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_x (form);
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 /**
112    Read a file, and shove it down GUILE.  GUILE also has file read
113    functions, but you can't fiddle with the path of those.
114  */
115 void
116 read_lily_scm_file (String fn)
117 {
118   String s = global_path.find (fn);
119   if (s == "")
120     {
121       String e = _f ("can't find file: `%s'", fn);
122       e += " ";
123       e += _f ("(load path: `%s')", global_path.str ());
124       error (e);
125     }
126   else
127     progress_indication ("[" + s);
128
129
130   Simple_file_storage f(s);
131   
132   gh_eval_str ((char *) f.ch_C());
133   progress_indication ("]");
134 }
135
136
137 SCM
138 ly_gulp_file (SCM name)
139 {
140   String fn (ly_scm2string (name));
141  String s = global_path.find (fn);
142   if (s == "")
143     {
144       String e = _f ("can't find file: `%s'", fn);
145       e += " ";
146       e += _f ("(load path: `%s')", global_path.str ());
147       error (e);
148     }
149   else
150     progress_indication ("[" + s );
151
152
153   Simple_file_storage f(s);
154   SCM result = ly_str02scm (f.ch_C());
155   progress_indication ("]");
156   return result;
157 }
158
159 void
160 ly_display_scm (SCM s)
161 {
162   gh_display (s);
163   gh_newline ();
164 }
165
166 String
167 ly_scm2string (SCM s)
168 {
169   assert (gh_string_p (s));
170   int len; 
171   char * p = gh_scm2newstr (s , &len);
172   
173   String r (p);
174
175   free (p);
176   return r;
177 }
178
179 SCM
180 index_cell (SCM s, Direction d)
181 {
182   assert (d);
183   return (d == LEFT) ? gh_car  (s) : gh_cdr (s);
184 }
185
186 SCM
187 index_set_cell (SCM s, Direction d, SCM v)
188 {
189   if (d == LEFT)
190     gh_set_car_x (s, v);
191   else if (d == RIGHT)
192     gh_set_cdr_x (s, v);
193   return s;
194 }
195   
196 SCM
197 ly_warning (SCM str)
198 {
199   assert (gh_string_p (str));
200   warning ("lily-guile: " + ly_scm2string (str));
201   return SCM_BOOL_T;
202 }
203
204 SCM
205 ly_isdir_p (SCM s)
206 {
207   if (gh_number_p (s))
208     {
209       int i = gh_scm2int (s);
210       return (i>= -1 && i <= 1)  ? SCM_BOOL_T : SCM_BOOL_F; 
211     }
212   return SCM_BOOL_F;
213 }
214
215
216
217 typedef void (*Void_fptr)();
218 Array<Void_fptr> *scm_init_funcs_;
219
220 void add_scm_init_func (void (*f)())
221 {
222   if (!scm_init_funcs_)
223     scm_init_funcs_ = new Array<Void_fptr>;
224
225   scm_init_funcs_->push (f);
226 }
227
228 void
229 init_lily_guile ()
230 {
231   for (int i=scm_init_funcs_->size() ; i--;)
232     (scm_init_funcs_->elem (i)) ();
233 }
234
235 unsigned int ly_scm_hash (SCM s)
236 {
237   return scm_ihashv (s, ~1u);
238 }
239
240
241
242 bool
243 isdir_b (SCM s)
244 {
245   if (gh_number_p (s))
246     {
247       int i = gh_scm2int (s);
248       return i>= -1 && i <= 1; 
249     }
250   return false;
251 }
252
253 Direction
254 to_dir (SCM s)
255 {
256   return (Direction) gh_scm2int (s);
257 }
258
259 Interval
260 ly_scm2interval (SCM p)
261 {
262   return  Interval (gh_scm2double (gh_car (p)),
263                     gh_scm2double (gh_cdr (p)));
264 }
265
266 SCM
267 ly_interval2scm (Interval i)
268 {
269   return gh_cons (gh_double2scm (i[LEFT]),
270                   gh_double2scm (i[RIGHT]));
271 }
272
273
274 bool
275 to_boolean (SCM s)
276 {
277   return gh_boolean_p (s) && gh_scm2bool (s);
278 }
279
280 /*
281   Appendable list L: the cdr contains the list, the car the last cons
282   in the list.
283  */
284 SCM
285 appendable_list ()
286 {
287   SCM s = gh_cons (SCM_EOL, SCM_EOL);
288   gh_set_car_x (s, s);
289   
290   return s;
291 }
292
293 void
294 appendable_list_append (SCM l, SCM elt)
295 {
296   SCM newcons = gh_cons (elt, SCM_EOL);
297   
298   gh_set_cdr_x (gh_car (l), newcons);      
299   gh_set_car_x (l, newcons);
300 }
301
302
303 SCM
304 ly_offset2scm (Offset o)
305 {
306   return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]));
307 }
308
309 Offset
310 ly_scm2offset (SCM s)
311 {
312   return Offset (gh_scm2double (gh_car (s)),
313                  gh_scm2double (gh_cdr (s)));
314 }
315
316 SCM
317 ly_type (SCM exp)
318 {
319   char const  * cp = "unknown";
320   if (gh_number_p (exp))
321     {
322       cp = "number";
323     }
324   else if (gh_string_p (exp))
325     {
326       cp = "string";
327     }
328   else if (gh_procedure_p (exp))
329     {
330       cp = "procedure";
331     }
332   else if (gh_boolean_p (exp))
333     {
334       cp = "boolean";
335     }
336   else if (gh_pair_p (exp))
337     {
338       cp = "list";
339     }
340
341   return ly_str02scm (cp);
342 }
343
344 /*
345   convert without too many decimals, and leave  a space at the end.
346  */
347    
348    
349 SCM
350 ly_number2string (SCM s)
351 {
352   assert (gh_number_p (s));
353
354   char str[100];                        // ugh.
355
356   if (scm_integer_p (s))
357     {
358       Real r (gh_scm2double (s));
359
360       if (isinf (r) || isnan (r))
361         {
362           programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
363           r = 0.0;
364         }
365
366       sprintf (str, "%8.4f ", r);
367     }
368   else
369     {
370       sprintf (str, "%d ", gh_scm2int (s));
371     }
372
373   return gh_str02scm (str);
374 }
375
376 /*
377   Undef this to see if GUILE GC is causing too many swaps.
378  */
379
380 // #define TEST_GC
381
382 #ifdef TEST_GC
383 #include <libguile/gc.h>
384
385 static void *
386 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
387 {
388    fprintf(stderr, "entering sweep\n");
389 }
390
391 static void *
392 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
393 {
394    fprintf(stderr, "leaving sweep\n");
395 }
396 #endif
397
398 static void
399 init_functions ()
400 {
401   scm_make_gsubr ("ly-warn", 1, 0, 0, (SCM(*)(...))ly_warning);
402   scm_make_gsubr ("ly-gulp-file", 1,0, 0, (SCM(*)(...))ly_gulp_file);
403   scm_make_gsubr ("dir?", 1,0, 0, (SCM(*)(...))ly_isdir_p);
404   scm_make_gsubr ("ly-number->string", 1, 0,0, (SCM(*)(...)) ly_number2string);
405
406
407 #ifdef TEST_GC 
408   scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
409   scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
410 #endif
411   
412 }
413
414 ADD_SCM_INIT_FUNC(funcs, init_functions);