]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/lily-guile.hh
lily-guile updates and CG: "Scheme->C interface" section.
[lilypond.git] / lily / include / lily-guile.hh
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1998--2011 Jan Nieuwenhuizen <janneke@gnu.org>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #ifndef LILY_GUILE_HH
21 #define LILY_GUILE_HH
22
23 #if __MINGW32__
24 #include "mingw-compatibility.hh"
25 #endif
26
27 #include <libguile.h>
28 #include <string.h>
29
30 /*
31   Hack for various MacOS incarnations.
32  */
33 #ifndef GUILE_ELLIPSIS
34 #define GUILE_ELLIPSIS
35 #endif
36
37 #include "guile-compatibility.hh"
38 #include "interval.hh"
39 #include "lily-guile-macros.hh"
40 #include "ly-module.hh"
41
42 /** Conversion functions follow the GUILE naming convention, i.e.
43     A ly_B2A (B b);  */
44
45 SCM ly_last (SCM list);
46 string ly_scm_write_string (SCM s);
47 SCM ly_deep_copy (SCM);
48 SCM ly_truncate_list (int k, SCM lst);
49
50 extern SCM global_lily_module;
51
52 string gulp_file_to_string (string fn, bool must_exist, int size);
53
54 SCM ly_string2scm (string const &s);
55 string ly_scm2string (SCM s);
56 string ly_symbol2string (SCM);
57 Rational ly_scm2rational (SCM);
58 SCM ly_rational2scm (Rational);
59 SCM ly_offset2scm (Offset);
60 Offset ly_scm2offset (SCM);
61 SCM ly_chain_assoc (SCM key, SCM achain);
62 SCM ly_chain_assoc_get (SCM key, SCM achain, SCM default_value, SCM strict_checking = SCM_BOOL_F);
63 SCM ly_assoc_cdr (SCM key, SCM alist);
64 SCM ly_assoc_get (SCM key, SCM alist, SCM default_value, SCM strict_checking = SCM_BOOL_F);
65 Interval ly_scm2interval (SCM);
66 Drul_array<Real> ly_scm2realdrul (SCM);
67 Slice int_list_to_slice (SCM l);
68 SCM ly_interval2scm (Drul_array<Real>);
69 char *ly_scm2str0 (SCM str);
70
71 Real robust_scm2double (SCM, double);
72 int robust_scm2int (SCM, int);
73 Direction robust_scm2dir (SCM, Direction);
74 Drul_array<Real> robust_scm2drul (SCM, Drul_array<Real>);
75 Drul_array<bool> robust_scm2booldrul (SCM, Drul_array<bool>);
76 Interval robust_scm2interval (SCM, Drul_array<Real>);
77 Offset robust_scm2offset (SCM, Offset);
78 string robust_scm2string (SCM, string);
79 Rational robust_scm2rational (SCM, Rational);
80
81 SCM ly_quote_scm (SCM s);
82 bool type_check_assignment (SCM val, SCM sym, SCM type_symbol);
83 string print_scm_val (SCM val);
84 SCM ly_number2string (SCM s);
85
86 SCM parse_symbol_list (char const *);
87 SCM robust_list_ref (int i, SCM l);
88 SCM alist_to_hashq (SCM);
89
90 SCM ly_alist_vals (SCM alist);
91 SCM ly_hash2alist (SCM tab);
92 SCM ly_hash_table_keys (SCM tab);
93
94 SCM ly_assoc_prepend_x (SCM alist, SCM key, SCM val);
95 // is SCM_FRACTIONP (x) now scm_is_true (scm_rational_p (x))
96 //                       or scm_is_true (scm_exact_p (x)) ?
97 inline bool ly_is_fraction (SCM x) { return SCM_FRACTIONP (x) || scm_is_integer (x); }
98
99 inline bool ly_is_list (SCM x) { return scm_is_true (scm_list_p (x)); }
100 inline bool ly_cheap_is_list (SCM x) { return scm_is_pair (x) || x == SCM_EOL; }
101 inline bool ly_is_procedure (SCM x) { return scm_is_true (scm_procedure_p (x)); }
102 inline bool ly_is_port (SCM x) { return scm_is_true (scm_port_p (x)); }
103
104 /*
105   want to take the address of this function; scm_is_symbol() is a
106   macro.
107  */
108 inline bool ly_is_symbol (SCM x) { return scm_is_symbol (x); }
109
110 inline bool ly_is_equal (SCM x, SCM y)
111 {
112   return scm_is_true (scm_equal_p (x, y));
113 }
114
115 inline bool ly_scm2bool (SCM x) { return scm_is_true (x); }
116 inline char ly_scm2char (SCM x) { return (char)SCM_CHAR (x); }
117 inline SCM ly_bool2scm (bool x) { return scm_from_bool (x); }
118
119 inline SCM ly_append2 (SCM x1, SCM x2)
120 {
121   return scm_append (scm_listify (x1, x2, SCM_UNDEFINED));
122 }
123 inline SCM ly_append3 (SCM x1, SCM x2, SCM x3)
124 {
125   return scm_append (scm_listify (x1, x2, x3, SCM_UNDEFINED));
126 }
127 inline SCM ly_append4 (SCM x1, SCM x2, SCM x3, SCM x4)
128 {
129   return scm_append (scm_listify (x1, x2, x3, x4, SCM_UNDEFINED));
130 }
131
132 /*
133   display and print newline.
134 */
135 extern "C" {
136   void ly_display_scm (SCM s);
137 }
138
139 void read_lily_scm_file (string);
140 void ly_c_init_guile ();
141
142 bool is_direction (SCM s);
143 bool is_number_pair (SCM);
144 bool is_axis (SCM);
145
146 /*
147   these conversion functions also do a typecheck on the argument, and
148   return a default value if S has the wrong type.
149 */
150
151 Direction to_dir (SCM s);
152 bool to_boolean (SCM s);
153
154 void init_ly_protection ();
155 unsigned int ly_scm_hash (SCM s);
156
157 SCM index_get_cell (SCM cell, Direction d);
158 SCM index_set_cell (SCM cell, Direction d, SCM val);
159
160 SCM ly_snoc (SCM s, SCM list);
161 SCM ly_split_list (SCM s, SCM lst);
162 SCM ly_unique (SCM lst);
163 SCM ly_list_qsort_uniq_x (SCM lst);
164
165 SCM ly_output_formats ();
166
167 /*
168   snarfing.
169 */
170 void add_scm_init_func (void ( *) ());
171
172 extern "C" {
173   typedef SCM (*Scheme_function_unknown) (GUILE_ELLIPSIS);
174 }
175
176 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
177 typedef SCM (*Scheme_function_0) ();
178 typedef SCM (*Scheme_function_1) (SCM);
179 typedef SCM (*Scheme_function_2) (SCM, SCM);
180 typedef SCM (*Scheme_function_3) (SCM, SCM, SCM);
181 #else
182 typedef SCM (*Scheme_function_0) (GUILE_ELLIPSIS);
183 typedef SCM (*Scheme_function_1) (GUILE_ELLIPSIS);
184 typedef SCM (*Scheme_function_2) (GUILE_ELLIPSIS);
185 typedef SCM (*Scheme_function_3) (GUILE_ELLIPSIS);
186 #endif
187
188 /*
189   Inline these for performance reasons.
190  */
191 #define scm_cdr ly_cdr
192 #define scm_car ly_car
193
194 #ifndef scm_is_pair
195 #define scm_is_pair ly_is_pair
196 #endif
197
198 inline SCM ly_car (SCM x) { return SCM_CAR (x); }
199 inline SCM ly_cdr (SCM x) { return SCM_CDR (x); }
200 inline bool ly_is_pair (SCM x) { return SCM_I_CONSP (x); }
201
202 #include "std-vector.hh"
203
204 template<class T>
205 SCM
206 ly_cxx_vector_to_list (vector<T> const &src)
207 {
208   SCM l = SCM_EOL;
209   for (vsize i = src.size (); i--;)
210     l = scm_cons (src[i]->self_scm (), l);
211
212   return l;
213 }
214
215 SCM ly_offsets2scm (vector<Offset> os);
216 vector<Offset> ly_scm2offsets (SCM s);
217
218 #endif /* LILY_GUILE_HH */