]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/lily-guile.hh
* input/regression/quote-cyclic.ly (Module): new file.
[lilypond.git] / lily / include / lily-guile.hh
1 /*
2   lily-guile.hh encapsulate guile
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #ifndef LILY_GUILE_HH
10 #define LILY_GUILE_HH
11
12 #include <libguile.h>
13 #include "flower-proto.hh"
14
15 #include "drul-array.hh"
16 #include "direction.hh"
17
18 #if SCM_MINOR_VERSION < 7
19 /* guile-1.6.x compatibility */
20 #define scm_gc_unregister_collectable_memory(a, b, c) scm_done_free (b)
21 #define scm_gc_register_collectable_memory(a, b, c) scm_done_malloc (b)
22 #define SCM_VECTOR_REF(v,i) (SCM_VELTS ((v))[(i)])
23 #define scm_from_int(x) SCM_MAKINUM (x)
24 #define scm_is_integer(x) SCM_INUMP (x)
25 #define scm_is_string(x) SCM_STRINGP(x)
26 #define scm_hash_table_p scm_vector_p
27 #define scm_i_string_chars(x) SCM_STRING_CHARS(x)
28 #define scm_i_string_length(x) SCM_STRING_LENGTH(x)
29 inline bool ly_c_number_p (SCM x) { return SCM_NUMBERP (x); }
30 #define scm_is_number(x) (scm_number_p(x)==SCM_BOOL_T)
31 inline int ly_scm2int (SCM x) { return scm_num2int (x, 0, "ly_scm2int"); }
32 #define scm_to_int(x) (ly_scm2int(x))
33 inline bool ly_c_symbol_p (SCM x) { return SCM_SYMBOLP (x); }
34 #define scm_is_symbol(x) ly_c_symbol_p(x)
35 inline bool ly_c_boolean_p (SCM x) { return SCM_BOOLP (x); }
36 #define scm_is_bool(x) ly_c_boolean_p(x)
37 inline bool ly_c_eq_p (SCM x, SCM y) { return SCM_EQ_P (x, y); }
38 #define scm_is_eq(x,y)  (SCM_EQ_P (x, y));
39
40 inline double ly_scm2double (SCM x) { return scm_num2dbl (x, "ly_scm2double"); }
41 #define scm_to_double(x) (ly_scm2double(x))
42 #define scm_from_double(x) (scm_make_real(x))
43
44
45 #endif /* SCM_MINOR_VERSION < 7 */
46
47 #ifndef SMOB_FREE_RETURN_VAL
48 #define SMOB_FREE_RETURN_VAL(CL) 0
49 #endif
50
51 #ifndef SCM_PACK
52 #define SCM_PACK(x) ((SCM) x)
53 #endif
54
55 #ifndef SCM_UNPACK
56 #define SCM_UNPACK(x) (x)
57 #endif
58
59 /** Conversion functions follow the GUILE naming convention, i.e.
60     A ly_B2A (B b);  */
61
62 SCM ly_last (SCM list);
63 SCM ly_write2scm (SCM s);
64 SCM ly_deep_copy (SCM);
65 SCM ly_truncate_list (int k, SCM lst);
66
67 #if (__GNUC__ > 2)
68 /* Unreliable with gcc-2.x
69    FIXME: should add check for x86 as well?  */
70 #define CACHE_SYMBOLS
71 #endif
72
73 #ifdef CACHE_SYMBOLS
74
75 /* Using this trick we cache the value of scm_str2symbol ("fooo") where
76   "fooo" is a constant string. This is done at the cost of one static
77   variable per ly_symbol2scm() use, and one boolean evaluation for
78   every call.
79
80   The overall speedup of lily is about 5% on a run of wtk1-fugue2.  */
81 #define ly_symbol2scm(x) \
82 ({ \
83   static SCM cached; \
84   /* We store this one locally, since G++ -O2 fucks up else */ \
85   SCM value = cached; \
86   if ( __builtin_constant_p ((x))) \
87     { \
88       if (!cached) \
89         value = cached =  scm_gc_protect_object (scm_str2symbol ((x))); \
90     } \
91   else \
92     value = scm_str2symbol ((char*) (x)); \
93   value; \
94 })
95 #else
96 inline SCM ly_symbol2scm(char const* x) { return scm_str2symbol ((x)); }
97 #endif
98
99 extern SCM global_lily_module;
100
101 /*
102   TODO: rename me to ly_c_lily_module_eval
103  */
104 #define ly_scheme_function(x) \
105 ({ \
106   static SCM cached; \
107   /* We store this one locally, since G++ -O2 fucks up else */ \
108   SCM value = cached; \
109   if ( __builtin_constant_p ((x))) \
110     { \
111       if (!cached) \
112         value = cached = scm_gc_protect_object (scm_eval (scm_str2symbol (x), \
113                                                 global_lily_module)); \
114     } \
115   else \
116     value = scm_eval (scm_str2symbol (x), global_lily_module); \
117   value; \
118 })
119
120
121 String ly_scm2string (SCM s);
122 String ly_symbol2string (SCM);
123 SCM ly_offset2scm (Offset);
124 Offset ly_scm2offset (SCM);
125 SCM ly_assoc_chain (SCM key, SCM achain);
126 SCM ly_assoc_cdr (SCM key, SCM alist);
127 Interval ly_scm2interval (SCM);
128 Drul_array<Real> ly_scm2realdrul (SCM);
129 Slice int_list_to_slice (SCM l);
130 SCM ly_interval2scm (Drul_array<Real>);
131 char *ly_scm2newstr (SCM str, size_t *lenp);
132
133 Real robust_scm2double (SCM, double);
134 int robust_scm2int (SCM, int);
135 Drul_array<Real> robust_scm2drul (SCM, Drul_array<Real>);
136 Interval robust_scm2interval (SCM, Drul_array<Real>);
137 Offset robust_scm2offset (SCM, Offset);
138
139 SCM ly_quote_scm (SCM s);
140 bool type_check_assignment (SCM val, SCM sym,  SCM type_symbol) ;
141 String print_scm_val (SCM val);
142 SCM ly_number2string (SCM s);
143
144 SCM parse_symbol_list (char const *);
145 SCM robust_list_ref(int i, SCM l);
146 SCM alist_to_hashq (SCM);
147
148 inline SCM ly_cdr (SCM x) { return SCM_CDR (x); }
149 inline SCM ly_car (SCM x) { return SCM_CAR (x); }
150 inline SCM ly_caar (SCM x) { return SCM_CAAR (x); }
151 inline SCM ly_cdar (SCM x) { return SCM_CDAR (x); }
152 inline SCM ly_cadr (SCM x) { return SCM_CADR (x); }
153 inline SCM ly_cddr (SCM x) { return SCM_CDDR (x); }
154 inline SCM ly_caddr (SCM x) { return SCM_CADDR (x); }
155 inline SCM ly_cdadr (SCM x) { return SCM_CDADR (x); }
156 inline SCM ly_caadr (SCM x) { return SCM_CAADR (x); }
157 inline SCM ly_cadar (SCM x) { return SCM_CADAR (x); }
158 /* inserts at front, removing dublicates */
159 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
160 {
161   return scm_acons(key, val, scm_assoc_remove_x (alist, key));
162 }
163 inline bool ly_c_pair_p (SCM x) { return SCM_NFALSEP (scm_pair_p (x)); }
164 inline bool ly_c_char_p (SCM x) { return SCM_CHARP (x); }
165 inline bool ly_c_vector_p (SCM x) { return SCM_VECTORP (x); }
166 inline bool ly_c_list_p (SCM x) { return SCM_NFALSEP (scm_list_p (x)); }
167 inline bool ly_c_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p (x)); }
168 inline bool ly_c_equal_p (SCM x, SCM y) {
169   return SCM_NFALSEP (scm_equal_p (x, y));
170 }
171
172 inline bool ly_scm2bool (SCM x) { return SCM_NFALSEP (x); }
173 inline char ly_scm2char (SCM x) { return SCM_CHAR(x); }
174 inline unsigned long ly_length (SCM x) {
175   return scm_num2ulong (scm_length (x), 0, "ly_length");
176 }
177 inline unsigned long ly_vector_length (SCM x) { return SCM_VECTOR_LENGTH (x); }
178
179 inline SCM ly_bool2scm (bool x) { return SCM_BOOL (x); }
180
181 inline SCM ly_append2 (SCM x1, SCM x2) {
182   return scm_append (scm_listify (x1, x2, SCM_UNDEFINED));
183 }
184 inline SCM ly_append3 (SCM x1, SCM x2, SCM x3) {
185   return scm_append (scm_listify (x1, x2, x3, SCM_UNDEFINED));
186 }
187 inline SCM ly_append4 (SCM x1, SCM x2, SCM x3, SCM x4) {
188   return scm_append (scm_listify (x1, x2, x3, x4, SCM_UNDEFINED));
189 }
190
191 /*
192   display and print newline.
193  */
194 extern "C" {
195 void ly_display_scm (SCM s);
196 }
197
198 void read_lily_scm_file (String);
199 void ly_c_init_guile ();
200
201 bool is_direction (SCM s);
202 bool is_number_pair (SCM);
203 bool is_axis (SCM);
204
205 /*
206   these conversion functions also do a typecheck on the argument, and
207   return a default value if S has the wrong type.
208 */
209
210 Direction to_dir (SCM s);
211 bool to_boolean (SCM s);
212
213 void init_ly_protection ();
214 unsigned int ly_scm_hash (SCM s);
215
216 SCM index_get_cell (SCM cell, Direction d);
217 SCM index_set_cell (SCM cell, Direction d, SCM val);
218
219 SCM ly_snoc (SCM s, SCM list);
220 SCM ly_split_list (SCM s, SCM lst);
221 SCM ly_unique (SCM lst);
222 SCM ly_list_qsort_uniq_x (SCM lst);
223
224 SCM ly_output_formats();
225 SCM ly_kpathsea_expand_path (SCM);
226
227 /*
228   snarfing.
229  */
230 void add_scm_init_func (void (*) ());
231
232 extern "C" {
233 typedef SCM (*Scheme_function_unknown) ();
234 }
235
236 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
237 typedef SCM (*Scheme_function_0) ();
238 typedef SCM (*Scheme_function_1) (SCM);
239 typedef SCM (*Scheme_function_2) (SCM,SCM);     
240 typedef SCM (*Scheme_function_3) (SCM,SCM, SCM);        
241 #else
242 typedef SCM (*Scheme_function_0) (...);
243 typedef SCM (*Scheme_function_1) (...);
244 typedef SCM (*Scheme_function_2) (...);
245 typedef SCM (*Scheme_function_3) (...);
246 #endif
247
248
249 /*
250   Adds the NAME as a Scheme function, and a variable to store the SCM
251   version of the function in the static variable NAME_proc
252  */
253 #define DECLARE_SCHEME_CALLBACK(NAME, ARGS) \
254         static SCM NAME ARGS; \
255         static SCM NAME ## _proc
256
257 /*
258   Make TYPE::FUNC available as a Scheme function.
259  */
260 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
261 SCM TYPE :: FUNC ## _proc; \
262 void \
263 TYPE ## _ ## FUNC ## _init_functions () \
264 { \
265   TYPE :: FUNC ## _proc = scm_c_define_gsubr (#TYPE "::" #FUNC, \
266                                               (ARGCOUNT), 0, 0, \
267                           (Scheme_function_unknown)TYPE :: FUNC); \
268   scm_c_export (#TYPE "::" #FUNC, NULL); \
269 } \
270  \
271 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, \
272                    TYPE ## _ ## FUNC ## _init_functions);
273
274
275 void
276 ly_add_function_documentation (SCM proc, char const *fname,
277                                char const *varlist,
278                                char const *doc);
279
280 #define ADD_SCM_INIT_FUNC(name, func) \
281 class name ## _scm_initter \
282 { \
283 public: \
284   name ## _scm_initter () \
285   { \
286     add_scm_init_func (func); \
287   } \
288 } _ ## name ## _scm_initter; \
289 /* end define */
290
291 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, \
292                                ARGLIST, DOCSTRING) \
293 SCM FNAME ## _proc; \
294 void \
295 INITPREFIX ## init () \
296 { \
297   FNAME ## _proc = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, \
298                                        (Scheme_function_unknown) FNAME); \
299   ly_add_function_documentation (FNAME ## _proc, PRIMNAME, #ARGLIST, \
300                                  DOCSTRING); \
301   scm_c_export (PRIMNAME, NULL); \
302 } \
303 ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init); \
304 SCM \
305 FNAME ARGLIST
306
307
308 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
309 SCM FNAME ARGLIST; \
310 LY_DEFINE_WITHOUT_DECL (FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, \
311                         DOCSTRING)
312
313 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, \
314                                   ARGLIST, DOCSTRING) \
315 SCM FNAME ARGLIST; \
316 LY_DEFINE_WITHOUT_DECL (CLASS ## FNAME, CLASS::FNAME, PRIMNAME, REQ, OPT, \
317                         VAR, ARGLIST, DOCSTRING)
318
319 #define get_property(x) internal_get_property (ly_symbol2scm (x))
320 #define set_property(x,y) internal_set_property (ly_symbol2scm (x), y)
321
322 #endif /* LILY_GUILE_HH */