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