2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1998--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 LilyPond is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 LilyPond is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
21 #include "lily-guile.hh"
25 #include <cstring> /* strdup, strchr */
29 #include "dimensions.hh"
30 #include "direction.hh"
31 #include "file-path.hh"
32 #include "international.hh"
33 #include "libc-extension.hh"
38 #include "string-convert.hh"
39 #include "source-file.hh"
42 #include "lily-imports.hh"
51 ly_scm_write_string (SCM s)
53 SCM port = scm_mkstrport (SCM_INUM0,
54 scm_make_string (SCM_INUM0, SCM_UNDEFINED),
57 // SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
58 SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
60 // scm_apply (write, port, SCM_EOL);
61 scm_call_2 (write, s, port);
62 return ly_scm2string (scm_strport_to_string (port));
68 return scm_list_2 (ly_symbol2scm ("quote"), s);
72 ly_symbol2string (SCM s)
75 Ugh. this is not very efficient.
77 return ly_scm2string (scm_symbol_to_string (s));
81 robust_symbol2string (SCM sym, const string &str)
83 return scm_is_symbol (sym) ? ly_symbol2string (sym) : str;
87 gulp_file_to_string (const string &fn, bool must_exist, int size)
89 string s = global_path.find (fn);
94 string e = _f ("cannot find file: `%s'", fn);
96 e += _f ("(load path: `%s')", global_path.to_string ());
103 debug_output ("[" + s, true);
105 vector<char> chars = gulp_file (s, size);
106 string result (&chars[0], chars.size ());
108 debug_output ("]\n", false);
114 // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
116 ly_display_scm (SCM s)
118 scm_display (s, scm_current_output_port ());
119 scm_newline (scm_current_output_port ());
127 ly_scm2string (SCM str)
129 assert (scm_is_string (str));
131 size_t len = scm_c_string_length (str);
135 scm_to_locale_stringbuf (str, &result.at (0), len);
141 ly_string2scm (string const &str)
143 return scm_from_locale_stringn (str.c_str (),
148 ly_scm2str0 (SCM str)
150 return scm_to_utf8_string (str);
157 index_get_cell (SCM s, Direction d)
160 return (d == LEFT) ? scm_car (s) : scm_cdr (s);
164 index_set_cell (SCM s, Direction d, SCM v)
167 scm_set_car_x (s, v);
169 scm_set_cdr_x (s, v);
174 is_number_pair (SCM p)
176 return scm_is_pair (p)
177 && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
183 return scm_ihashv (s, ~1u);
189 if (scm_is_integer (s))
191 int i = scm_to_int (s);
192 return i == 0 || i == 1;
200 return scm_is_bool (s) && ly_scm2bool (s);
209 return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
213 robust_scm2dir (SCM d, Direction def)
215 if (is_direction (d))
223 if (scm_is_number (s))
225 int i = scm_to_int (s);
226 return i >= -1 && i <= 1;
235 ly_scm2interval (SCM p)
237 return Interval (scm_to_double (scm_car (p)),
238 scm_to_double (scm_cdr (p)));
242 ly_scm2realdrul (SCM p)
244 return Drul_array<Real> (scm_to_double (scm_car (p)),
245 scm_to_double (scm_cdr (p)));
249 ly_interval2scm (Drul_array<Real> i)
251 return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
255 robust_scm2interval (SCM k, Drul_array<Real> v)
260 if (is_number_pair (k))
261 i = ly_scm2interval (k);
266 robust_scm2drul (SCM k, Drul_array<Real> v)
268 if (is_number_pair (k))
269 v = ly_scm2interval (k);
274 robust_scm2booldrul (SCM k, Drul_array<bool> def)
278 def[LEFT] = to_boolean (scm_car (k));
279 def[RIGHT] = to_boolean (scm_cdr (k));
288 ly_offset2scm (Offset o)
290 return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
294 ly_scm2offset (SCM s)
296 return Offset (scm_to_double (scm_car (s)),
297 scm_to_double (scm_cdr (s)));
301 robust_scm2offset (SCM k, Offset o)
303 if (is_number_pair (k))
304 o = ly_scm2offset (k);
308 ly_offsets2scm (vector<Offset> os)
312 for (vsize i = 0; i < os.size (); i++)
314 *tail = scm_cons (ly_offset2scm (os[i]), SCM_EOL);
315 tail = SCM_CDRLOC (*tail);
321 ly_scm2offsets (SCM s)
324 for (; scm_is_pair (s); s = scm_cdr (s))
325 os.push_back (ly_scm2offset (scm_car (s)));
333 ly_alist_vals (SCM alist)
336 for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
337 x = scm_cons (scm_cdar (p), x);
345 /* Return I-th element, or last elt L. If I < 0, then we take the first
348 PRE: length (L) > 0 */
350 robust_list_ref (int i, SCM l)
352 while (i-- > 0 && scm_is_pair (scm_cdr (l)))
358 ly_deep_copy (SCM src)
360 if (scm_is_pair (src))
365 res = scm_cons (ly_deep_copy (scm_car (src)), res);
368 while (scm_is_pair (src));
369 // Oh, come on, GUILE. Why do you require the second argument
370 // of scm_reverse_x to be a proper list? That makes no sense.
371 // return scm_reverse_x (res, ly_deep_copy (src));
373 res = scm_reverse_x (res, SCM_EOL);
374 scm_set_cdr_x (last_cons, ly_deep_copy (src));
377 if (scm_is_vector (src))
379 int len = scm_c_vector_length (src);
380 SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
381 for (int i = 0; i < len; i++)
383 SCM si = scm_from_int (i);
384 scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
392 print_scm_val (SCM val)
394 string realval = ly_scm_write_string (val);
395 if (realval.length () > 200)
396 realval = realval.substr (0, 100)
398 + realval.substr (realval.length () - 100);
403 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
406 // If undefined, some internal function caused it...should never happen.
407 assert (!SCM_UNBNDP (val));
408 if (!scm_is_symbol (sym))
411 SCM type = scm_object_property (sym, type_symbol);
413 if (!scm_is_null (type) && !ly_is_procedure (type))
415 warning (_f ("cannot find property type-check for `%s' (%s).",
416 ly_symbol2string (sym).c_str (),
417 ly_symbol2string (type_symbol).c_str ())
418 + " " + _ ("perhaps a typing error?"));
420 /* Be strict when being anal :) */
421 if (do_internal_type_checking_global)
422 scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
425 warning (_ ("skipping assignment"));
433 TODO: should remove #f from allowed vals?
435 if (scm_is_null (val) || scm_is_false (val))
438 if (!scm_is_null (val)
439 && ly_is_procedure (type)
440 && scm_is_false (scm_call_1 (type, val)))
442 SCM type_name = Lily::type_name (type);
444 warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
445 ly_symbol2string (sym).c_str (),
447 ly_scm2string (type_name).c_str ()));
448 progress_indication ("\n");
455 ly_wrong_smob_arg (bool pred (SCM), SCM var, int number, const char *fun)
457 string type = predicate_to_typename ((void *) pred);
460 // Uh oh. unsmob<T> delivered 0, yet
461 // unsmob<T> delivers true. This means that unsmob<T> is a
462 // matching check from a base class of T, but var is of an
463 // incompatible derived type.
464 type = string (_ ("Wrong kind of ")).append (type);
466 scm_wrong_type_arg_msg (mangle_cxx_identifier (fun).c_str (),
467 number, var, type.c_str ());
473 zijn deze nou handig?
474 zijn ze er al in scheme, maar heten ze anders? */
476 /* Remove doubles from (sorted) list */
480 SCM unique = SCM_EOL;
481 for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
483 if (!scm_is_pair (scm_cdr (i))
484 || !ly_is_equal (scm_car (i), scm_cadr (i)))
485 unique = scm_cons (scm_car (i), unique);
487 return scm_reverse_x (unique, SCM_EOL);
490 /* Split list at member s, removing s.
491 Return (BEFORE . AFTER) */
493 ly_split_list (SCM s, SCM list)
495 SCM before = SCM_EOL;
497 for (; scm_is_pair (after);)
499 SCM i = scm_car (after);
500 after = scm_cdr (after);
501 if (ly_is_equal (i, s))
503 before = scm_cons (i, before);
505 return scm_cons (scm_reverse_x (before, SCM_EOL), after);
517 display stuff without using stack
522 SCM p = scm_current_output_port ();
525 for (; scm_is_pair (s); s = scm_cdr (s))
527 scm_display (scm_car (s), p);
531 return SCM_UNSPECIFIED;
535 int_list_to_slice (SCM l)
539 for (; scm_is_pair (l); l = scm_cdr (l))
540 if (scm_is_number (scm_car (l)))
541 s.add_point (scm_to_int (scm_car (l)));
546 robust_scm2double (SCM k, double x)
548 if (scm_is_number (k))
549 x = scm_to_double (k);
554 ly_scm2floatvector (SCM l)
557 for (SCM s = l; scm_is_pair (s); s = scm_cdr (s))
558 floats.push_back (robust_scm2double (scm_car (s), 0.0));
563 ly_floatvector2scm (vector<Real> v)
567 for (vsize i = 0; i < v.size (); i++)
569 *tail = scm_cons (scm_from_double (v[i]), SCM_EOL);
570 tail = SCM_CDRLOC (*tail);
576 robust_scm2string (SCM k, const string &s)
578 if (scm_is_string (k))
579 return ly_scm2string (k);
584 robust_scm2int (SCM k, int o)
586 if (scm_is_integer (k))
592 robust_scm2vsize (SCM k, vsize o)
594 if (scm_is_integer (k))
596 int i = scm_to_int (k);
604 ly_rational2scm (Rational r)
606 if (r.is_infinity ())
608 if (r > Rational (0))
611 return scm_difference (scm_inf (), SCM_UNDEFINED);
614 return scm_divide (scm_from_int64 (r.numerator ()),
615 scm_from_int64 (r.denominator ()));
619 ly_scm2rational (SCM r)
621 if (scm_is_true (scm_inf_p (r)))
623 if (scm_is_true (scm_positive_p (r)))
637 return Rational (scm_to_int64 (scm_numerator (r)),
638 scm_to_int64 (scm_denominator (r)));
642 robust_scm2rational (SCM n, Rational rat)
644 if (ly_is_rational (n))
645 return ly_scm2rational (n);
651 ly_is_rational (SCM n)
653 return (scm_is_real (n)
654 && (scm_is_true (scm_exact_p (n))
655 || scm_is_true (scm_inf_p (n))));
659 alist_to_hashq (SCM alist)
661 int i = scm_ilength (alist);
663 return scm_c_make_hash_table (0);
665 SCM tab = scm_c_make_hash_table (i);
666 for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
668 SCM pt = scm_cdar (s);
669 scm_hashq_set_x (tab, scm_caar (s), pt);
675 ly_hash2alist (SCM tab)
677 return Lily::hash_table_to_alist (tab);
685 mangle_cxx_identifier (string cxx_id)
687 if (cxx_id.substr (0, 3) == "ly_")
688 cxx_id = cxx_id.replace (0, 3, "ly:");
691 cxx_id = String_convert::to_lower (cxx_id);
692 cxx_id = "ly:" + cxx_id;
694 if (cxx_id.substr (cxx_id.length () - 2) == "_p")
695 cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "?");
696 else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
697 cxx_id = cxx_id.replace (cxx_id.length () - 2, 2, "!");
699 replace_all (&cxx_id, "_less?", "<?");
700 replace_all (&cxx_id, "_2_", "->");
701 replace_all (&cxx_id, "__", "::");
702 replace_all (&cxx_id, '_', '-');
708 ly_string_array_to_scm (vector<string> a)
711 for (vsize i = a.size (); i; i--)
712 s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
716 /* SYMBOLS is a whitespace separated list. */
718 parse_symbol_list (char const *symbols)
720 while (isspace (*symbols))
723 replace_all (&s, '\n', ' ');
724 replace_all (&s, '\t', ' ');
725 replace_all (&s, " ", " ");
726 return ly_string_array_to_scm (string_split (s, ' '));
730 struct ly_t_double_cell
738 /* inserts at front, removing duplicates */
739 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
741 return scm_acons (key, val, scm_assoc_remove_x (alist, key));