]> git.donarmstrong.com Git - lilypond.git/blob - lily/lily-guile.cc
use scm_throw with ly-file-failed key for property check failures.
[lilypond.git] / lily / lily-guile.cc
1 /*
2   lily-guile.cc -- implement assorted SCM interface functions
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2007 Jan Nieuwenhuizen <janneke@gnu.org>
7   Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
9
10 #include "lily-guile.hh"
11
12 #include <cstdio>
13 #include <cstdlib>
14 #include <cstring> /* strdup, strchr */
15 #include <cctype>
16
17 using namespace std;
18
19 #include "dimensions.hh"
20 #include "direction.hh"
21 #include "file-path.hh"
22 #include "international.hh"
23 #include "libc-extension.hh"
24 #include "main.hh"
25 #include "misc.hh"
26 #include "offset.hh"
27 #include "pitch.hh"
28 #include "string-convert.hh"
29 #include "source-file.hh"
30 #include "version.hh"
31 #include "warn.hh"
32
33
34 /*
35   symbols/strings.
36  */
37 SCM
38 ly_to_symbol (SCM scm)
39 {
40   return scm_string_to_symbol (ly_to_string (scm));
41 }
42
43 SCM
44 ly_to_string (SCM scm)
45 {
46   return scm_call_3 (ly_lily_module_constant ("format"), SCM_BOOL_F,
47
48                      scm_from_locale_string ("~S"), scm);
49 }
50
51 SCM
52 ly_write2scm (SCM s)
53 {
54   SCM port = scm_mkstrport (SCM_INUM0,
55                             scm_make_string (SCM_INUM0, SCM_UNDEFINED),
56                             SCM_OPN | SCM_WRTNG,
57                             "ly_write2string");
58   //  SCM write = scm_eval_3 (ly_symbol2scm ("write"), s, SCM_EOL);
59   SCM write = scm_primitive_eval (ly_symbol2scm ("write"));
60
61   // scm_apply (write, port, SCM_EOL);
62   scm_call_2 (write, s, port);
63   return scm_strport_to_string (port);
64 }
65
66 SCM
67 ly_quote_scm (SCM s)
68 {
69   return scm_list_n (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
70 }
71
72 string
73 ly_symbol2string (SCM s)
74 {
75   /*
76     Ugh. this is not very efficient.
77   */
78   SCM str = scm_symbol_to_string (s);
79   return ly_scm2string (str);
80 }
81
82 string
83 gulp_file_to_string (string fn, bool must_exist, int size)
84 {
85   string s = global_path.find (fn);
86   if (s == "")
87     {
88       if (must_exist)
89         {
90           string e = _f ("cannot find file: `%s'", fn);
91           e += " ";
92           e += _f ("(load path: `%s')", global_path.to_string ());
93           error (e);
94           /* unreachable */
95         }
96       return s;
97     }
98
99   if (be_verbose_global)
100     progress_indication ("[" + s);
101
102   vector<char> chars = gulp_file (s, size);
103   string result (&chars[0], chars.size ());
104
105   if (be_verbose_global)
106     progress_indication ("]");
107
108   return result;
109 }
110
111 extern "C" {
112   // maybe gdb 5.0 becomes quicker if it doesn't do fancy C++ typing?
113   void
114   ly_display_scm (SCM s)
115   {
116     scm_display (s, scm_current_output_port ());
117     scm_newline (scm_current_output_port ());
118   }
119 };
120
121 /*
122   STRINGS
123  */
124 string
125 ly_scm2string (SCM str)
126 {
127   assert (scm_is_string (str));
128   return string (scm_i_string_chars (str),
129                  (int) scm_i_string_length (str));
130 }
131
132 SCM
133 ly_string2scm (string const &str)
134 {
135   return scm_from_locale_stringn (str.c_str(),
136                                   str.length ());
137 }
138
139
140 char *
141 ly_scm2newstr (SCM str, size_t *lenp)
142 {
143   SCM_ASSERT_TYPE (scm_is_string (str), str, SCM_ARG1, __FUNCTION__, "string");
144
145   size_t len = scm_i_string_length (str);
146   if (char *new_str = (char *) malloc ((len + 1) * sizeof (char)))
147     {
148       memcpy (new_str, scm_i_string_chars (str), len);
149       new_str[len] = '\0';
150
151       if (lenp)
152         *lenp = len;
153
154       return new_str;
155     }
156   return 0;
157 }
158
159
160 /*
161   PAIRS
162 */
163 SCM
164 index_get_cell (SCM s, Direction d)
165 {
166
167   assert (d);
168   return (d == LEFT) ? scm_car (s) : scm_cdr (s);
169 }
170
171 SCM
172 index_set_cell (SCM s, Direction d, SCM v)
173 {
174   if (d == LEFT)
175     scm_set_car_x (s, v);
176   else if (d == RIGHT)
177     scm_set_cdr_x (s, v);
178   return s;
179 }
180
181 bool
182 is_number_pair (SCM p)
183 {
184   return scm_is_pair (p)
185     && scm_is_number (scm_car (p)) && scm_is_number (scm_cdr (p));
186 }
187
188
189 unsigned int
190 ly_scm_hash (SCM s)
191 {
192   return scm_ihashv (s, ~1u);
193 }
194
195
196 bool
197 is_axis (SCM s)
198 {
199   if (scm_is_number (s))
200     {
201       int i = scm_to_int (s);
202       return i == 0 || i == 1;
203     }
204   return false;
205 }
206
207 bool
208 to_boolean (SCM s)
209 {
210   return scm_is_bool (s) && ly_scm2bool (s);
211 }
212
213 /*
214   DIRECTIONS
215  */
216 Direction
217 to_dir (SCM s)
218 {
219   return scm_is_integer (s) ? (Direction) scm_to_int (s) : CENTER;
220 }
221
222 Direction
223 robust_scm2dir (SCM d, Direction def)
224 {
225   if (is_direction (d))
226     def = to_dir (d);
227   return def;
228 }
229
230 bool
231 is_direction (SCM s)
232 {
233   if (scm_is_number (s))
234     {
235       int i = scm_to_int (s);
236       return i >= -1 && i <= 1;
237     }
238   return false;
239 }
240
241 /*
242   INTERVALS
243  */
244 Interval
245 ly_scm2interval (SCM p)
246 {
247   return Interval (scm_to_double (scm_car (p)), scm_to_double (scm_cdr (p)));
248 }
249
250 Drul_array<Real>
251 ly_scm2realdrul (SCM p)
252 {
253   return Drul_array<Real> (scm_to_double (scm_car (p)),
254                            scm_to_double (scm_cdr (p)));
255 }
256
257 SCM
258 ly_interval2scm (Drul_array<Real> i)
259 {
260   return scm_cons (scm_from_double (i[LEFT]), scm_from_double (i[RIGHT]));
261 }
262
263
264 Interval
265 robust_scm2interval (SCM k, Drul_array<Real> v)
266 {
267   Interval i;
268   i[LEFT] = v[LEFT];
269   i[RIGHT] = v[RIGHT];
270   if (is_number_pair (k))
271     i = ly_scm2interval (k);
272   return i;
273 }
274
275 Drul_array<Real>
276 robust_scm2drul (SCM k, Drul_array<Real> v)
277 {
278   if (is_number_pair (k))
279     v = ly_scm2interval (k);
280   return v;
281 }
282
283 Drul_array<bool>
284 robust_scm2booldrul (SCM k, Drul_array<bool> def)
285 {
286   if (scm_is_pair (k))
287     {
288       def[LEFT] = to_boolean (scm_car (k));
289       def[RIGHT] = to_boolean (scm_cdr (k));
290     }
291   return def;
292 }
293
294 /*
295   OFFSET
296 */
297 SCM
298 ly_offset2scm (Offset o)
299 {
300   return scm_cons (scm_from_double (o[X_AXIS]), scm_from_double (o[Y_AXIS]));
301 }
302
303 Offset
304 ly_scm2offset (SCM s)
305 {
306   return Offset (scm_to_double (scm_car (s)),
307                  scm_to_double (scm_cdr (s)));
308 }
309
310 Offset
311 robust_scm2offset (SCM k, Offset o)
312 {
313   if (is_number_pair (k))
314     o = ly_scm2offset (k);
315   return o;
316 }
317 SCM
318 ly_offsets2scm (vector<Offset> os)
319 {
320   SCM l = SCM_EOL;
321   SCM *tail = &l;
322   for (vsize i = 0; i < os.size (); i++)
323     {
324       *tail = scm_cons (ly_offset2scm (os[i]), SCM_EOL);
325       tail = SCM_CDRLOC(*tail);
326     }
327   return l;
328 }
329
330 vector<Offset>
331 ly_scm2offsets (SCM s)
332 {
333   vector<Offset> os;
334   for (; scm_is_pair (s); s = scm_cdr (s))
335     os.push_back (ly_scm2offset (scm_car (s)));
336   return os;
337 }
338
339
340
341
342 /*
343   ALIST
344 */
345
346 bool
347 alist_equal_p (SCM a, SCM b)
348 {
349   for (SCM s = a;
350        scm_is_pair (s); s = scm_cdr (s))
351     {
352       SCM key = scm_caar (s);
353       SCM val = scm_cdar (s);
354       SCM l = scm_assoc (key, b);
355
356       if (l == SCM_BOOL_F
357           || !ly_is_equal (scm_cdr (l), val))
358
359         return false;
360     }
361   return true;
362 }
363
364 SCM
365 ly_alist_vals (SCM alist)
366 {
367   SCM x = SCM_EOL;
368   for (SCM p = alist; scm_is_pair (p); p = scm_cdr (p))
369     x = scm_cons (scm_cdar (p), x);
370   return x;
371 }
372
373 /*
374   LISTS
375  */
376
377 /* Return I-th element, or last elt L. If I < 0, then we take the first
378    element.
379
380    PRE: length (L) > 0  */
381 SCM
382 robust_list_ref (int i, SCM l)
383 {
384   while (i-- > 0 && scm_is_pair (scm_cdr (l)))
385     l = scm_cdr (l);
386   return scm_car (l);
387 }
388
389
390 SCM
391 ly_deep_copy (SCM src)
392 {
393   if (scm_is_pair (src))
394     return scm_cons (ly_deep_copy (scm_car (src)), ly_deep_copy (scm_cdr (src)));
395   else if (scm_is_vector (src))
396     {
397       int len = scm_c_vector_length (src);
398       SCM nv = scm_c_make_vector (len, SCM_UNDEFINED);
399       for (int i = 0;i < len; i++)
400         {
401           SCM si = scm_from_int (i);
402           scm_vector_set_x (nv, si, ly_deep_copy (scm_vector_ref (src, si)));
403         }
404     }
405   return src;
406 }
407
408
409
410
411
412 string
413 print_scm_val (SCM val)
414 {
415   string realval = ly_scm2string (ly_write2scm (val));
416   if (realval.length () > 200)
417     realval = realval.substr (0, 100)
418       + "\n :\n :\n"
419       + realval.substr (realval.length () - 100);
420   return realval;
421 }
422
423 bool
424 type_check_assignment (SCM sym, SCM val, SCM type_symbol)
425 {
426   bool ok = true;
427
428   /*
429     Always succeeds.
430
431
432     TODO: should remove #f from allowed vals?
433   */
434   if (val == SCM_EOL || val == SCM_BOOL_F)
435     return ok;
436
437   if (!scm_is_symbol (sym))
438 #if 0
439     return false;
440 #else
441   /*
442     This is used for autoBeamSettings.
443
444     TODO: deprecate the use of \override and \revert for
445     autoBeamSettings?
446
447     or use a symbol autoBeamSettingS?
448   */
449   return true;
450 #endif
451
452   SCM type = scm_object_property (sym, type_symbol);
453
454   if (type != SCM_EOL && !ly_is_procedure (type))
455     {
456       warning (_f ("cannot find property type-check for `%s' (%s).",
457                    ly_symbol2string (sym).c_str (),
458                    ly_symbol2string (type_symbol).c_str ())
459                + "  " + _ ("perhaps a typing error?"));
460
461       /* Be strict when being anal :) */
462       if (do_internal_type_checking_global)
463         scm_throw (ly_symbol2scm ("ly-file-failed"), scm_list_3 (ly_symbol2scm ("typecheck"),
464                                                                  sym, val));
465
466       warning (_ ("doing assignment anyway"));
467     }
468   else
469     {
470       if (val != SCM_EOL
471           && ly_is_procedure (type)
472           && scm_call_1 (type, val) == SCM_BOOL_F)
473         {
474           ok = false;
475           SCM typefunc = ly_lily_module_constant ("type-name");
476           SCM type_name = scm_call_1 (typefunc, type);
477
478           warning (_f ("type check for `%s' failed; value `%s' must be of type `%s'",
479                        ly_symbol2string (sym).c_str (),
480                        print_scm_val (val),
481                        ly_scm2string (type_name).c_str ()));
482           progress_indication ("\n");
483         }
484     }
485   return ok;
486 }
487
488 /* some SCM abbrevs
489
490 zijn deze nou handig?
491 zijn ze er al in scheme, maar heten ze anders? */
492
493 /* Remove doubles from (sorted) list */
494 SCM
495 ly_unique (SCM list)
496 {
497   SCM unique = SCM_EOL;
498   for (SCM i = list; scm_is_pair (i); i = scm_cdr (i))
499     {
500       if (!scm_is_pair (scm_cdr (i))
501           || !ly_is_equal (scm_car (i), scm_cadr (i)))
502         unique = scm_cons (scm_car (i), unique);
503     }
504   return scm_reverse_x (unique, SCM_EOL);
505 }
506
507
508 /* Split list at member s, removing s.
509    Return (BEFORE . AFTER)  */
510 SCM
511 ly_split_list (SCM s, SCM list)
512 {
513   SCM before = SCM_EOL;
514   SCM after = list;
515   for (; scm_is_pair (after);)
516     {
517       SCM i = scm_car (after);
518       after = scm_cdr (after);
519       if (ly_is_equal (i, s))
520         break;
521       before = scm_cons (i, before);
522     }
523   return scm_cons (scm_reverse_x (before, SCM_EOL), after);
524 }
525
526 void
527 taint (SCM *)
528 {
529   /*
530     nop.
531   */
532 }
533
534 /*
535   display stuff without using stack
536 */
537 SCM
538 display_list (SCM s)
539 {
540   SCM p = scm_current_output_port ();
541
542   scm_puts ("(", p);
543   for (; scm_is_pair (s); s = scm_cdr (s))
544     {
545       scm_display (scm_car (s), p);
546       scm_puts (" ", p);
547     }
548   scm_puts (")", p);
549   return SCM_UNSPECIFIED;
550 }
551
552 Slice
553 int_list_to_slice (SCM l)
554 {
555   Slice s;
556   s.set_empty ();
557   for (; scm_is_pair (l); l = scm_cdr (l))
558     if (scm_is_number (scm_car (l)))
559       s.add_point (scm_to_int (scm_car (l)));
560   return s;
561 }
562
563 Real
564 robust_scm2double (SCM k, double x)
565 {
566   if (scm_is_number (k))
567     x = scm_to_double (k);
568   return x;
569 }
570
571
572 string
573 robust_scm2string (SCM k, string s)
574 {
575   if (scm_is_string (k))
576     s = ly_scm2string (k);
577   return s;
578 }
579
580 int
581 robust_scm2int (SCM k, int o)
582 {
583   if (scm_integer_p (k) == SCM_BOOL_T)
584     o = scm_to_int (k);
585   return o;
586 }
587
588
589 SCM
590 ly_rational2scm (Rational r)
591 {
592   return scm_divide (scm_from_int (r.numerator ()), scm_from_int (r.denominator ()));
593 }
594
595
596 Rational
597 ly_scm2rational (SCM r)
598 {
599   return Rational (scm_to_int (scm_numerator (r)),
600                    scm_to_int (scm_denominator (r)));
601 }
602
603
604 SCM
605 alist_to_hashq (SCM alist)
606 {
607   int i = scm_ilength (alist);
608   if (i < 0)
609     return scm_c_make_hash_table (0);
610
611   SCM tab = scm_c_make_hash_table (i);
612   for (SCM s = alist; scm_is_pair (s); s = scm_cdr (s))
613     {
614       SCM pt = scm_cdar (s);
615       scm_hashq_set_x (tab, scm_caar (s), pt);
616     }
617   return tab;
618 }
619
620 SCM
621 ly_hash2alist (SCM tab)
622 {
623   SCM func = ly_lily_module_constant ("hash-table->alist");
624   return scm_call_1 (func, tab);
625 }
626
627 int
628 procedure_arity (SCM proc)
629 {
630   assert (ly_is_procedure (proc));
631   SCM arity = scm_procedure_property (proc,
632                                       ly_symbol2scm ("arity"));
633
634   SCM fixed = scm_car (arity);
635   return scm_to_int (fixed);
636 }
637
638 /*
639   C++ interfacing.
640  */
641
642 string
643 mangle_cxx_identifier (string cxx_id)
644 {
645   if (cxx_id.substr (0, 3) == "ly_")
646     cxx_id = cxx_id.replace (0, 3, "ly:");
647   else
648     {
649       cxx_id = String_convert::to_lower (cxx_id);
650       cxx_id = "ly:" + cxx_id;
651     }
652   if (cxx_id.substr (cxx_id.length () - 2) == "_p")
653     cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "?");
654   else if (cxx_id.substr (cxx_id.length () - 2) == "_x")
655     cxx_id = cxx_id.replace (cxx_id.length () - 2, 1, "!");
656
657   cxx_id = replace_all (cxx_id, '_', '-');
658   return cxx_id;
659 }
660
661
662
663 SCM
664 ly_string_array_to_scm (vector<string> a)
665 {
666   SCM s = SCM_EOL;
667   for (vsize i = a.size (); i ; i--)
668     s = scm_cons (ly_symbol2scm (a[i - 1].c_str ()), s);
669   return s;
670 }
671
672 /* SYMBOLS is a whitespace separated list.  */
673 SCM
674 parse_symbol_list (char const *symbols)
675 {
676   while (isspace (*symbols))
677     *symbols++;
678   string s = symbols;
679   replace_all (s, '\n', ' ');
680   replace_all (s, '\t', ' ');
681   return ly_string_array_to_scm (string_split (s, ' '));
682 }
683
684
685 bool
686 ly_is_fraction (SCM x)
687 {
688   return SCM_FRACTIONP(x);
689 }
690
691 struct ly_t_double_cell
692 {
693   SCM a;
694   SCM b;
695   SCM c;
696   SCM d;
697 };
698
699 /* inserts at front, removing duplicates */
700 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val)
701 {
702   return scm_acons (key, val, scm_assoc_remove_x (alist, key));
703 }
704