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