2 lily-guile.cc -- implement assorted guile functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2002 Jan Nieuwenhuizen <janneke@gnu.org>
8 Han-Wen Nienhuys <hanwen@cs.uu.nl>
14 #include <math.h> /* isinf */
15 #include <string.h> /* strdup, strchr */
16 #include "libc-extension.hh"
17 #include "lily-guile.hh"
19 #include "simple-file-storage.hh"
20 #include "file-path.hh"
22 #include "direction.hh"
24 #include "interval.hh"
30 return ly_car (scm_last_pair (list));
34 ly_str02scm (char const*c)
36 // this all really sucks, guile should take char const* arguments!
37 return gh_str02scm ((char*)c);
44 SCM port = scm_mkstrport (SCM_INUM0,
45 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
48 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
49 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
51 // scm_apply (write, port, SCM_EOL);
52 gh_call2 (write, s, port);
53 return scm_strport_to_string (port);
58 Pass string to scm parser, evaluate one expression.
59 Return result value and #chars read.
61 Thanks to Gary Houston <ghouston@freewire.co.uk>
63 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
66 ly_parse_scm (char const* s, int* n)
68 SCM str = ly_str02scm (s);
69 SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
71 SCM from = scm_ftell (port);
74 SCM answer = SCM_UNSPECIFIED;
76 /* Read expression from port */
77 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
78 answer = scm_primitive_eval (form);
85 all seems fine, but after parsing
89 read_buf has been advanced to read_pos - 1,
90 so that scm_ftell returns 1, instead of #parsed chars
94 urg: reset read_buf for scm_ftell
95 shouldn't scm_read () do this for us?
97 scm_fill_input (port);
98 SCM to = scm_ftell (port);
99 *n = gh_scm2int (to) - gh_scm2int (from);
101 /* Don't close the port here; if we re-enter this function via a
102 continuation, then the next time we enter it, we'll get an error.
103 It's a string port anyway, so there's no advantage to closing it
106 scm_close_port (port);
115 return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
121 ly_symbol2string (SCM s)
123 assert (gh_symbol_p (s));
124 return String ((Byte*)SCM_STRING_CHARS (s), (int) SCM_STRING_LENGTH (s));
129 gulp_file_to_string (String fn)
131 String s = global_path.find (fn);
134 String e = _f ("can't find file: `%s'", fn);
136 e += _f ("(load path: `%s')", global_path.str ());
139 else if (verbose_global_b)
140 progress_indication ("[" + s);
143 Simple_file_storage f (s);
144 String result (f.ch_C ());
145 if (verbose_global_b)
146 progress_indication ("]");
151 ly_gulp_file (SCM fn)
153 return ly_str02scm (gulp_file_to_string (ly_scm2string (fn)).ch_C ());
158 Read a file, and shove it down GUILE. GUILE also has file read
159 functions, but you can't fiddle with the path of those.
162 read_lily_scm_file (String fn)
164 gh_eval_str ((char *) gulp_file_to_string (fn).ch_C ());
168 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
170 ly_display_scm (SCM s)
178 ly_scm2string (SCM s)
180 assert (gh_string_p (s));
183 char *p = gh_scm2newstr (s , &len);
192 index_cell (SCM s, Direction d)
195 return (d == LEFT) ? ly_car (s) : ly_cdr (s);
199 index_set_cell (SCM s, Direction d, SCM v)
211 assert (gh_string_p (str));
212 warning ("lily-guile: " + ly_scm2string (str));
221 int i = gh_scm2int (s);
222 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
228 ly_number_pair_p (SCM p)
230 return gh_pair_p (p) && gh_number_p (ly_car (p)) && gh_number_p (ly_cdr (p));
236 return gh_number_p (a) && (gh_scm2int (a) == 0 || gh_scm2int (a) == 1);
239 typedef void (*Void_fptr) ();
240 Array<Void_fptr> *scm_init_funcs_;
242 void add_scm_init_func (void (*f) ())
244 if (!scm_init_funcs_)
245 scm_init_funcs_ = new Array<Void_fptr>;
247 scm_init_funcs_->push (f);
250 extern void init_cxx_function_smobs ();
253 prepend_load_path (String p )
257 "(set! %%load-path (cons \"%s\" %%load-path))", p.ch_C());
259 scm_c_eval_string (s);
263 init_lily_guile (String p )
265 prepend_load_path (p);
267 // todo: junk this. We should make real modules iso. just loading files.
268 prepend_load_path (p + "/scm/");
270 SCM last_mod = scm_current_module ();
271 scm_set_current_module (scm_c_resolve_module ("guile"));
273 init_cxx_function_smobs ();
274 for (int i=scm_init_funcs_->size () ; i--;)
275 (scm_init_funcs_->elem (i)) ();
277 if (verbose_global_b)
278 progress_indication ("\n");
279 read_lily_scm_file ("lily.scm");
281 scm_set_current_module (last_mod);
284 unsigned int ly_scm_hash (SCM s)
286 return scm_ihashv (s, ~1u);
296 int i = gh_scm2int (s);
297 return i>= -1 && i <= 1;
308 int i = gh_scm2int (s);
309 return i== 0 || i == 1;
318 return (Direction) gh_scm2int (s);
322 ly_scm2interval (SCM p)
324 return Interval (gh_scm2double (ly_car (p)),
325 gh_scm2double (ly_cdr (p)));
329 ly_interval2scm (Interval i)
331 return gh_cons (gh_double2scm (i[LEFT]),
332 gh_double2scm (i[RIGHT]));
341 return gh_boolean_p (s) && gh_scm2bool (s);
345 Appendable list L: the cdr contains the list, the car the last cons
351 SCM s = gh_cons (SCM_EOL, SCM_EOL);
358 appendable_list_append (SCM l, SCM elt)
360 SCM newcons = gh_cons (elt, SCM_EOL);
362 gh_set_cdr_x (ly_car (l), newcons);
363 gh_set_car_x (l, newcons);
368 ly_offset2scm (Offset o)
370 return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm (o[Y_AXIS]));
374 ly_scm2offset (SCM s)
376 return Offset (gh_scm2double (ly_car (s)),
377 gh_scm2double (ly_cdr (s)));
383 char const * cp = "unknown";
384 if (gh_number_p (exp))
388 else if (gh_string_p (exp))
392 else if (gh_procedure_p (exp))
396 else if (gh_boolean_p (exp))
400 else if (gh_pair_p (exp))
405 return ly_str02scm (cp);
409 convert without too many decimals, and leave a space at the end.
414 ly_number2string (SCM s)
416 assert (gh_number_p (s));
418 char str[400]; // ugh.
420 if (scm_exact_p (s) == SCM_BOOL_F)
422 Real r (gh_scm2double (s));
424 if (isinf (r) || isnan (r))
426 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
430 sprintf (str, "%8.4f ", r);
434 sprintf (str, "%d ", gh_scm2int (s));
437 return ly_str02scm (str);
441 Undef this to see if GUILE GC is causing too many swaps.
447 #include <libguile/gc.h>
450 greet_sweep (void *dummy1, void *dummy2, void *dummy3)
452 fprintf (stderr, "entering sweep\n");
456 wave_sweep_goodbye (void *dummy1, void *dummy2, void *dummy3)
458 fprintf (stderr, "leaving sweep\n");
463 #include "version.hh"
467 char const* vs = "\' (" MAJOR_VERSION " " MINOR_VERSION " " PATCH_LEVEL " " MY_PATCH_LEVEL ")" ;
469 return gh_eval_str ((char*)vs);
475 scm_c_define_gsubr ("ly-warn", 1, 0, 0,
476 (Scheme_function_unknown)ly_warning);
477 scm_c_define_gsubr ("ly-version", 0, 0, 0,
478 (Scheme_function_unknown)ly_version);
479 scm_c_define_gsubr ("ly-gulp-file", 1,0, 0,
480 (Scheme_function_unknown)ly_gulp_file);
481 scm_c_define_gsubr ("dir?", 1,0, 0, (Scheme_function_unknown)ly_isdir_p);
482 scm_c_define_gsubr ("ly-number->string", 1, 0,0,
483 (Scheme_function_unknown) ly_number2string);
487 scm_c_hook_add (&scm_before_mark_c_hook, greet_sweep, 0, 0);
488 scm_c_hook_add (&scm_before_sweep_c_hook, wave_sweep_goodbye, 0, 0);
492 ADD_SCM_INIT_FUNC (funcs, init_functions);
499 return gh_cons (ly_deep_copy (ly_car (l)), ly_deep_copy (ly_cdr (l)));
509 ly_assoc_chain (SCM key, SCM achain)
511 if (gh_pair_p (achain))
513 SCM handle = scm_assoc (key, ly_car (achain));
514 if (gh_pair_p (handle))
517 return ly_assoc_chain (key, ly_cdr (achain));
523 /* looks the key up in the cdrs of the alist-keys
524 - ignoring the car and ignoring non-pair keys.
525 Returns first match found, i.e.
533 I would like (ly_assoc_cdr 1) to return 12 - because it's the first
534 element with the cdr of the key = 1. In other words (alloc_cdr key)
537 (alloc (anything . key))
543 ly_assoc_cdr (SCM key, SCM alist)
545 if (gh_pair_p (alist)) {
546 SCM trykey = ly_caar(alist);
547 if(gh_pair_p(trykey) && to_boolean(scm_equal_p(key,ly_cdr(trykey))))
548 return ly_car(alist);
550 return ly_assoc_cdr (key, ly_cdr (alist));
557 LIST has the form "sym1 sym2 sym3"
560 parse_symbol_list (const char * list)
562 char * s = strdup (list);
564 SCM create_list = SCM_EOL;
570 char *next = strchr (s, ' ');
574 create_list = gh_cons (ly_symbol2scm (s), create_list);
584 ly_truncate_list (int k, SCM l )
594 for (; gh_pair_p (s) && k--; s = ly_cdr (s))
599 gh_set_cdr_x (s, SCM_EOL);
605 SCM my_gh_symbol2scm (const char* x)
607 return gh_symbol2scm ((char*)x);
611 print_scm_val (SCM val)
613 String realval = ly_scm2string (ly_write2scm (val));
614 if (realval.length_i () > 200)
615 realval = realval.left_str (100) + "\n :\n :\n" + realval.right_str (100);
621 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
629 TODO: should remove #f from allowed vals?
631 if (val == SCM_EOL || val == SCM_BOOL_F)
635 SCM type_p = SCM_EOL;
637 if (gh_symbol_p (sym))
638 type_p = scm_object_property (sym, type_symbol);
640 if (type_p != SCM_EOL && !gh_procedure_p (type_p))
642 warning (_f ("Can't find property type-check for `%s' (%s). Perhaps you made a typing error? Doing assignment anyway.",
643 ly_symbol2string (sym).ch_C (),
644 ly_symbol2string (type_symbol).ch_C ()
651 && gh_procedure_p (type_p)
652 && gh_call1 (type_p, val) == SCM_BOOL_F)
654 SCM errport = scm_current_error_port ();
656 SCM typefunc = scm_primitive_eval (ly_symbol2scm ("type-name"));
657 SCM type_name = gh_call1 (typefunc, type_p);
660 scm_puts (_f ("Type check for `%s' failed; value `%s' must be of type `%s'",
661 ly_symbol2string (sym).ch_C (),
663 ly_scm2string (type_name).ch_C ()).ch_C (),
665 scm_puts ("\n", errport);