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