lilypond-1.3.145
[lilypond.git] / lily / lily-guile.cc
blob04bb47cdd3c47dcd3bc60d402a22595521afa094
1 /*
2 lily-guile.cc -- implement assorted guile functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2001 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_last (SCM list)
29 return gh_car (scm_last_pair (list));
32 SCM
33 ly_str02scm (char const*c)
35 // this all really sucks, guile should take char const* arguments!
36 return gh_str02scm ((char*)c);
40 SCM
41 ly_write2scm (SCM s)
43 SCM port = scm_mkstrport (SCM_INUM0,
44 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
45 SCM_OPN | SCM_WRTNG,
46 "ly_write2string");
47 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
48 SCM write = scm_eval2 (ly_symbol2scm ("write"), SCM_EOL);
50 // scm_apply (write, port, SCM_EOL);
51 gh_call2 (write, s, port);
52 return scm_strport_to_string (port);
57 Pass string to scm parser, evaluate one expression.
58 Return result value and #chars read.
60 Thanks to Gary Houston <ghouston@freewire.co.uk>
62 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
64 SCM
65 ly_parse_scm (char const* s, int* n)
67 SCM str = ly_str02scm (s);
68 SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
69 "ly_eval_scm_0str");
70 SCM from = scm_ftell (port);
72 SCM form;
73 SCM answer = SCM_UNSPECIFIED;
75 /* Read expression from port */
76 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
77 answer = scm_eval_3 (form, 1, SCM_EOL); // guh?
80 After parsing
82 (begin (foo 1 2))
84 all seems fine, but after parsing
86 (foo 1 2)
88 read_buf has been advanced to read_pos - 1,
89 so that scm_ftell returns 1, instead of #parsed chars
93 urg: reset read_buf for scm_ftell
94 shouldn't scm_read () do this for us?
96 scm_fill_input (port);
97 SCM to = scm_ftell (port);
98 *n = gh_scm2int (to) - gh_scm2int (from);
100 /* Don't close the port here; if we re-enter this function via a
101 continuation, then the next time we enter it, we'll get an error.
102 It's a string port anyway, so there's no advantage to closing it
103 early.
105 scm_close_port (port);
108 return answer;
112 ly_quote_scm (SCM s)
114 return gh_list (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
119 ly_symbol2scm (const char *s)
121 return gh_symbol2scm ((char *)s);
125 String
126 ly_symbol2string (SCM s)
128 assert (gh_symbol_p (s));
129 return String ((Byte*)SCM_CHARS (s), (int) SCM_LENGTH (s));
133 String
134 gulp_file_to_string (String fn)
136 String s = global_path.find (fn);
137 if (s == "")
139 String e = _f ("can't find file: `%s'", fn);
140 e += " ";
141 e += _f ("(load path: `%s')", global_path.str ());
142 error (e);
144 else if (verbose_global_b)
145 progress_indication ("[" + s);
148 Simple_file_storage f (s);
149 String result (f.ch_C ());
150 if (verbose_global_b)
151 progress_indication ("]");
152 return result;
156 ly_gulp_file (SCM fn)
158 return ly_str02scm (gulp_file_to_string (ly_scm2string (fn)).ch_C ());
163 Read a file, and shove it down GUILE. GUILE also has file read
164 functions, but you can't fiddle with the path of those.
166 void
167 read_lily_scm_file (String fn)
169 gh_eval_str ((char *) gulp_file_to_string (fn).ch_C ());
172 extern "C" {
173 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
174 void
175 ly_display_scm (SCM s)
177 gh_display (s);
178 gh_newline ();
182 String
183 ly_scm2string (SCM s)
185 assert (gh_string_p (s));
186 int len;
187 char * p = gh_scm2newstr (s , &len);
189 String r (p);
191 free (p);
192 return r;
196 index_cell (SCM s, Direction d)
198 assert (d);
199 return (d == LEFT) ? gh_car (s) : gh_cdr (s);
203 index_set_cell (SCM s, Direction d, SCM v)
205 if (d == LEFT)
206 gh_set_car_x (s, v);
207 else if (d == RIGHT)
208 gh_set_cdr_x (s, v);
209 return s;
213 ly_warning (SCM str)
215 assert (gh_string_p (str));
216 warning ("lily-guile: " + ly_scm2string (str));
217 return SCM_BOOL_T;
221 ly_isdir_p (SCM s)
223 if (gh_number_p (s))
225 int i = gh_scm2int (s);
226 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
228 return SCM_BOOL_F;
233 typedef void (*Void_fptr) ();
234 Array<Void_fptr> *scm_init_funcs_;
236 void add_scm_init_func (void (*f) ())
238 if (!scm_init_funcs_)
239 scm_init_funcs_ = new Array<Void_fptr>;
241 scm_init_funcs_->push (f);
243 extern void init_cxx_function_smobs ();
245 void
246 init_lily_guile ()
248 init_cxx_function_smobs ();
249 for (int i=scm_init_funcs_->size () ; i--;)
250 (scm_init_funcs_->elem (i)) ();
253 unsigned int ly_scm_hash (SCM s)
255 return scm_ihashv (s, ~1u);
260 bool
261 isdir_b (SCM s)
263 if (gh_number_p (s))
265 int i = gh_scm2int (s);
266 return i>= -1 && i <= 1;
268 return false;
271 Direction
272 to_dir (SCM s)
274 return (Direction) gh_scm2int (s);
277 Interval
278 ly_scm2interval (SCM p)
280 return Interval (gh_scm2double (gh_car (p)),
281 gh_scm2double (gh_cdr (p)));
285 ly_interval2scm (Interval i)
287 return gh_cons (gh_double2scm (i[LEFT]),
288 gh_double2scm (i[RIGHT]));
294 bool
295 to_boolean (SCM s)
297 return gh_boolean_p (s) && gh_scm2bool (s);
301 Appendable list L: the cdr contains the list, the car the last cons
302 in the list.
305 appendable_list ()
307 SCM s = gh_cons (SCM_EOL, SCM_EOL);
308 gh_set_car_x (s, s);
310 return s;
313 void
314 appendable_list_append (SCM l, SCM elt)
316 SCM newcons = gh_cons (elt, SCM_EOL);
318 gh_set_cdr_x (gh_car (l), newcons);
319 gh_set_car_x (l, newcons);
324 ly_offset2scm (Offset o)
326 return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm (o[Y_AXIS]));
329 Offset
330 ly_scm2offset (SCM s)
332 return Offset (gh_scm2double (gh_car (s)),
333 gh_scm2double (gh_cdr (s)));
337 ly_type (SCM exp)
339 char const * cp = "unknown";
340 if (gh_number_p (exp))
342 cp = "number";
344 else if (gh_string_p (exp))
346 cp = "string";
348 else if (gh_procedure_p (exp))
350 cp = "procedure";
352 else if (gh_boolean_p (exp))
354 cp = "boolean";
356 else if (gh_pair_p (exp))
358 cp = "list";
361 return ly_str02scm (cp);
365 convert without too many decimals, and leave a space at the end.
370 ly_number2string (SCM s)
372 assert (gh_number_p (s));
374 char str[400]; // ugh.
376 if (scm_integer_p (s) == SCM_BOOL_F)
378 Real r (gh_scm2double (s));
380 if (isinf (r) || isnan (r))
382 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
383 r = 0.0;
386 sprintf (str, "%8.4f ", r);
388 else
390 sprintf (str, "%d ", gh_scm2int (s));
393 return ly_str02scm (str);
397 Undef this to see if GUILE GC is causing too many swaps.
400 // #define TEST_GC
402 #ifdef TEST_GC
403 #include <libguile/gc.h>
405 static void *
406 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
408 fprintf (stderr, "entering sweep\n");
411 static void *
412 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
414 fprintf (stderr, "leaving sweep\n");
416 #endif
419 #include "version.hh"
421 ly_version ()
423 char const* vs = "\' (" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
426 return gh_eval_str ((char*)vs);
429 static void
430 init_functions ()
432 scm_make_gsubr ("ly-warn", 1, 0, 0, (Scheme_function_unknown)ly_warning);
433 scm_make_gsubr ("ly-version", 0, 0, 0, (Scheme_function_unknown)ly_version);
434 scm_make_gsubr ("ly-gulp-file", 1,0, 0, (Scheme_function_unknown)ly_gulp_file);
435 scm_make_gsubr ("dir?", 1,0, 0, (Scheme_function_unknown)ly_isdir_p);
437 scm_make_gsubr ("ly-number->string", 1, 0,0, (Scheme_function_unknown) ly_number2string);
440 #ifdef TEST_GC
441 scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
442 scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
443 #endif
446 ADD_SCM_INIT_FUNC (funcs, init_functions);
449 ly_deep_copy (SCM l)
451 if (gh_pair_p (l))
453 return gh_cons (ly_deep_copy (gh_car (l)), ly_deep_copy (gh_cdr (l)));
455 else
456 return l;
463 ly_assoc_chain (SCM key, SCM achain)
465 if (gh_pair_p (achain))
467 SCM handle = scm_assoc (key, gh_car (achain));
468 if (gh_pair_p (handle))
469 return handle;
470 else
471 return ly_assoc_chain (key, gh_cdr (achain));
473 else
474 return SCM_BOOL_F;