lilypond-1.3.80
[lilypond.git] / lily / lily-guile.cc
blob7877d3674f47e0aa274b773508eb424bf4d52ca2
1 /*
2 lily-guile.cc -- implement assorted guile functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2000 Jan Nieuwenhuizen <janneke@gnu.org>
8 Han-Wen Nienhuys <hanwen@cs.uu.nl>
9 */
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <math.h> // isinf
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"
26 SCM
27 ly_str02scm (char const*c)
29 // this all really sucks, guile should take char const* arguments!
30 return gh_str02scm ((char*)c);
36 Pass string to scm parser, evaluate one expression.
37 Return result value and #chars read.
39 Thanks to Gary Houston <ghouston@freewire.co.uk>
41 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
43 SCM
44 ly_parse_scm (char const* s, int* n)
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);
51 SCM form;
52 SCM answer = SCM_UNSPECIFIED;
54 /* Read expression from port */
55 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
56 answer = scm_eval_x (form);
59 After parsing
61 (begin (foo 1 2))
63 all seems fine, but after parsing
65 (foo 1 2)
67 read_buf has been advanced to read_pos - 1,
68 so that scm_ftell returns 1, instead of #parsed chars
72 urg: reset read_buf for scm_ftell
73 shouldn't scm_read () do this for us?
75 scm_fill_input (port);
76 SCM to = scm_ftell (port);
77 *n = gh_scm2int (to) - gh_scm2int (from);
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.
84 scm_close_port (port);
87 return answer;
90 SCM
91 ly_quote_scm (SCM s)
93 return gh_list (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
97 SCM
98 ly_symbol2scm(const char *s)
100 return gh_symbol2scm ((char *)s);
103 String
104 ly_symbol2string (SCM s)
106 assert (gh_symbol_p (s));
107 return String((Byte*)SCM_CHARS (s), (int) SCM_LENGTH(s));
111 String
112 gulp_file_to_string (String fn)
114 String s = global_path.find (fn);
115 if (s == "")
117 String e = _f ("can't find file: `%s'", fn);
118 e += " ";
119 e += _f ("(load path: `%s')", global_path.str ());
120 error (e);
122 else if (verbose_global_b)
123 progress_indication ("[" + s );
126 Simple_file_storage f(s);
127 String result (f.ch_C());
128 if (verbose_global_b)
129 progress_indication ("]");
130 return result;
134 ly_gulp_file (SCM fn)
136 return ly_str02scm (gulp_file_to_string (ly_scm2string (fn)).ch_C());
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.
144 void
145 read_lily_scm_file (String fn)
147 gh_eval_str ((char *) gulp_file_to_string (fn).ch_C());
151 void
152 ly_display_scm (SCM s)
154 gh_display (s);
155 gh_newline ();
158 String
159 ly_scm2string (SCM s)
161 assert (gh_string_p (s));
162 int len;
163 char * p = gh_scm2newstr (s , &len);
165 String r (p);
167 free (p);
168 return r;
172 index_cell (SCM s, Direction d)
174 assert (d);
175 return (d == LEFT) ? gh_car (s) : gh_cdr (s);
179 index_set_cell (SCM s, Direction d, SCM v)
181 if (d == LEFT)
182 gh_set_car_x (s, v);
183 else if (d == RIGHT)
184 gh_set_cdr_x (s, v);
185 return s;
189 ly_warning (SCM str)
191 assert (gh_string_p (str));
192 warning ("lily-guile: " + ly_scm2string (str));
193 return SCM_BOOL_T;
197 ly_isdir_p (SCM s)
199 if (gh_number_p (s))
201 int i = gh_scm2int (s);
202 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
204 return SCM_BOOL_F;
209 typedef void (*Void_fptr)();
210 Array<Void_fptr> *scm_init_funcs_;
212 void add_scm_init_func (void (*f)())
214 if (!scm_init_funcs_)
215 scm_init_funcs_ = new Array<Void_fptr>;
217 scm_init_funcs_->push (f);
220 void
221 init_lily_guile ()
223 for (int i=scm_init_funcs_->size() ; i--;)
224 (scm_init_funcs_->elem (i)) ();
227 unsigned int ly_scm_hash (SCM s)
229 return scm_ihashv (s, ~1u);
234 bool
235 isdir_b (SCM s)
237 if (gh_number_p (s))
239 int i = gh_scm2int (s);
240 return i>= -1 && i <= 1;
242 return false;
245 Direction
246 to_dir (SCM s)
248 return (Direction) gh_scm2int (s);
251 Interval
252 ly_scm2interval (SCM p)
254 return Interval (gh_scm2double (gh_car (p)),
255 gh_scm2double (gh_cdr (p)));
259 ly_interval2scm (Interval i)
261 return gh_cons (gh_double2scm (i[LEFT]),
262 gh_double2scm (i[RIGHT]));
266 bool
267 to_boolean (SCM s)
269 return gh_boolean_p (s) && gh_scm2bool (s);
273 Appendable list L: the cdr contains the list, the car the last cons
274 in the list.
277 appendable_list ()
279 SCM s = gh_cons (SCM_EOL, SCM_EOL);
280 gh_set_car_x (s, s);
282 return s;
285 void
286 appendable_list_append (SCM l, SCM elt)
288 SCM newcons = gh_cons (elt, SCM_EOL);
290 gh_set_cdr_x (gh_car (l), newcons);
291 gh_set_car_x (l, newcons);
296 ly_offset2scm (Offset o)
298 return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]));
301 Offset
302 ly_scm2offset (SCM s)
304 return Offset (gh_scm2double (gh_car (s)),
305 gh_scm2double (gh_cdr (s)));
309 ly_type (SCM exp)
311 char const * cp = "unknown";
312 if (gh_number_p (exp))
314 cp = "number";
316 else if (gh_string_p (exp))
318 cp = "string";
320 else if (gh_procedure_p (exp))
322 cp = "procedure";
324 else if (gh_boolean_p (exp))
326 cp = "boolean";
328 else if (gh_pair_p (exp))
330 cp = "list";
333 return ly_str02scm (cp);
337 convert without too many decimals, and leave a space at the end.
342 ly_number2string (SCM s)
344 assert (gh_number_p (s));
346 char str[100]; // ugh.
348 if (scm_integer_p (s) == SCM_BOOL_F)
350 Real r (gh_scm2double (s));
352 if (isinf (r) || isnan (r))
354 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
355 r = 0.0;
358 sprintf (str, "%8.4f ", r);
360 else
362 sprintf (str, "%d ", gh_scm2int (s));
365 return gh_str02scm (str);
369 Undef this to see if GUILE GC is causing too many swaps.
372 // #define TEST_GC
374 #ifdef TEST_GC
375 #include <libguile/gc.h>
377 static void *
378 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
380 fprintf(stderr, "entering sweep\n");
383 static void *
384 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
386 fprintf(stderr, "leaving sweep\n");
388 #endif
391 undefd ()
393 return SCM_UNDEFINED;
396 static void
397 init_functions ()
399 scm_make_gsubr ("ly-warn", 1, 0, 0, (SCM(*)(...))ly_warning);
400 scm_make_gsubr ("ly-gulp-file", 1,0, 0, (SCM(*)(...))ly_gulp_file);
401 scm_make_gsubr ("dir?", 1,0, 0, (SCM(*)(...))ly_isdir_p);
402 scm_make_gsubr ("undefd", 0,0, 0, (SCM(*)(...))undefd);
403 scm_make_gsubr ("ly-number->string", 1, 0,0, (SCM(*)(...)) ly_number2string);
406 #ifdef TEST_GC
407 scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
408 scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
409 #endif
413 ADD_SCM_INIT_FUNC(funcs, init_functions);
416 ly_deep_copy (SCM l)
418 if (gh_pair_p (l))
420 return gh_cons (ly_deep_copy (gh_car (l)), ly_deep_copy (gh_cdr (l)));
422 else
423 return l;