2 lily-guile.hh encapsulate guile
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
13 /* GUILE only includes version in headers (libguile/version.h) as of
14 1.5.x. For some strange reason, they call it SCM.*VERSION.
16 Not including config.h here, saves a lot of unnecessary
20 #ifndef GUILE_MAJOR_VERSION
21 #ifdef SCM_MAJOR_VERSION
22 #define GUILE_MAJOR_VERSION SCM_MAJOR_VERSION
23 #define GUILE_MINOR_VERSION SCM_MINOR_VERSION
24 #define GUILE_PATCH_LEVEL SCM_MICRO_VERSION
30 #include "drul-array.hh"
33 /* Guile 1.4.x compatibility */
34 #if GUILE_MINOR_VERSION < 5
36 #define scm_t_bits scm_bits_t
38 #define fix_guile_1_4_scm_primitive_eval(form) scm_eval_3 (form, 1, SCM_EOL)
39 #define scm_primitive_eval(form) fix_guile_1_4_scm_primitive_eval (form)
41 #define scm_int2num(x) scm_long2num (x)
42 #define scm_current_module() (SCM)0
43 #define scm_set_current_module(x) (void)x
44 #define scm_c_resolve_module(x) (SCM)0
46 inline SCM scm_c_make_vector (int k, SCM val) {
47 return scm_make_vector (SCM_MAKINUM (k), val);
49 #define scm_c_define_gsubr scm_make_gsubr
50 #define scm_remember_upto_here_1(s) scm_remember (&s)
51 #define scm_gc_protect_object scm_protect_object
52 #define scm_gc_unprotect_object scm_unprotect_object
53 #define scm_list_n scm_listify
54 #define SCM_STRING_CHARS SCM_CHARS
55 #define SCM_STRING_LENGTH SCM_LENGTH
56 #define SCM_SYMBOL_CHARS SCM_CHARS
57 #define SCM_SYMBOL_LENGTH SCM_LENGTH
58 #define SCM_VECTOR_LENGTH SCM_LENGTH
60 #define SMOB_FREE_RETURN_VAL(CL) sizeof(CL)
61 #define scm_done_free(x)
64 #ifndef SMOB_FREE_RETURN_VAL
65 #define SMOB_FREE_RETURN_VAL(CL) 0
69 #if GUILE_MINOR_VERSION < 7
70 #define scm_gc_unregister_collectable_memory(a,b,c) scm_done_free(b)
71 #define scm_gc_register_collectable_memory(a,b,c) scm_done_malloc(b)
72 #define SCM_VECTOR_REF(v,i) (SCM_VELTS((v))[(i)])
73 #define scm_hash_table_p scm_vector_p
77 #include "direction.hh"
78 #include "flower-proto.hh"
81 #define SCM_PACK(x) ((SCM) x)
85 #define SCM_UNPACK(x) ( x)
89 conversion functions follow the GUILE naming convention, i.e.
94 SCM ly_last (SCM list);
95 SCM ly_write2scm (SCM s);
96 SCM ly_deep_copy (SCM);
97 SCM ly_truncate_list (int k, SCM l );
103 // #define CACHE_SYMBOLS
108 todo: should add check for x86 as well
110 #define CACHE_SYMBOLS
118 Using this trick we cache the value of scm_str2symbol ("fooo") where
119 "fooo" is a constant string. This is done at the cost of one static
120 variable per ly_symbol2scm() use, and one boolean evaluation for
123 The overall speedup of lily is about 5% on a run of wtk1-fugue2
126 #define ly_symbol2scm(x) ({ static SCM cached; \
127 SCM value = cached; /* We store this one locally, since G++ -O2 fucks up else */ \
128 if ( __builtin_constant_p ((x)))\
130 value = cached = scm_gc_protect_object (scm_str2symbol((x)));\
132 value = scm_str2symbol ((char*) (x)); \
135 inline SCM ly_symbol2scm(char const* x) { return scm_str2symbol((x)); }
138 extern SCM global_lily_module;
141 TODO: rename me to ly_c_lily_module_eval
143 #define ly_scheme_function(x) ({static SCM cached; \
144 SCM value = cached; /* We store this one locally, since G++ -O2 fucks up else */ \
145 if ( __builtin_constant_p ((x)))\
147 value = cached = scm_gc_protect_object (scm_eval(scm_str2symbol (x), global_lily_module));\
149 value = scm_eval(scm_str2symbol (x), global_lily_module);\
154 String ly_scm2string (SCM s);
155 String ly_symbol2string (SCM);
156 SCM ly_offset2scm (Offset);
157 Offset ly_scm2offset (SCM);
158 SCM ly_assoc_chain (SCM key, SCM achain);
159 SCM ly_assoc_cdr (SCM key, SCM alist);
160 Interval ly_scm2interval (SCM);
161 Drul_array<Real> ly_scm2realdrul (SCM);
162 Slice int_list_to_slice (SCM l);
163 SCM ly_interval2scm (Drul_array<Real>);
164 char *ly_scm2newstr (SCM str, size_t *lenp);
166 Real robust_scm2double (SCM, double);
167 int robust_scm2int (SCM, int);
168 Drul_array<Real> robust_scm2drul (SCM, Drul_array<Real>);
169 Interval robust_scm2interval (SCM, Drul_array<Real>);
170 Offset robust_scm2offset (SCM, Offset);
172 SCM ly_quote_scm (SCM s);
173 bool type_check_assignment (SCM val, SCM sym, SCM type_symbol) ;
174 String print_scm_val (SCM val);
175 SCM ly_number2string (SCM s);
177 SCM parse_symbol_list (char const *);
178 SCM robust_list_ref(int i, SCM l);
179 SCM alist_to_hashq (SCM);
181 inline SCM ly_cdr (SCM x) { return SCM_CDR (x); }
182 inline SCM ly_car (SCM x) { return SCM_CAR (x); }
183 inline SCM ly_caar (SCM x) { return SCM_CAAR (x); }
184 inline SCM ly_cdar (SCM x) { return SCM_CDAR (x); }
185 inline SCM ly_cadr (SCM x) { return SCM_CADR (x); }
186 inline SCM ly_cddr (SCM x) { return SCM_CDDR (x); }
187 inline SCM ly_caddr (SCM x) { return SCM_CADDR (x); }
188 inline SCM ly_cdadr (SCM x) { return SCM_CDADR (x); }
189 inline SCM ly_caadr (SCM x) { return SCM_CAADR (x); }
190 inline SCM ly_cadar (SCM x) { return SCM_CADAR (x); }
191 /* inserts at front, removing dublicates */
192 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
194 return scm_acons(key, val, scm_assoc_remove_x (alist, key));
196 inline bool ly_c_pair_p (SCM x) { return SCM_NFALSEP (scm_pair_p (x)); }
197 inline bool ly_c_symbol_p (SCM x) { return SCM_SYMBOLP (x); }
198 inline bool ly_c_boolean_p (SCM x) { return SCM_BOOLP (x); }
199 inline bool ly_c_char_p (SCM x) { return SCM_CHARP (x); }
200 inline bool ly_c_number_p (SCM x) { return SCM_NUMBERP (x); }
201 inline bool ly_c_string_p (SCM x) { return SCM_STRINGP (x); }
202 inline bool ly_c_vector_p (SCM x) { return SCM_VECTORP (x); }
203 inline bool ly_c_list_p (SCM x) { return SCM_NFALSEP (scm_list_p (x)); }
204 inline bool ly_c_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p (x)); }
205 inline bool ly_c_eq_p (SCM x, SCM y) { return SCM_EQ_P (x, y); }
206 inline bool ly_c_equal_p (SCM x, SCM y) {
207 return SCM_NFALSEP (scm_equal_p (x, y));
210 inline bool ly_scm2bool (SCM x) { return SCM_NFALSEP (x); }
211 inline char ly_scm2char (SCM x) { return SCM_CHAR(x); }
212 inline int ly_scm2int (SCM x) { return scm_num2int (x, 0, "ly_scm2int"); }
213 inline double ly_scm2double (SCM x) { return scm_num2dbl (x, "ly_scm2double"); }
214 inline unsigned long ly_length (SCM x) {
215 return scm_num2ulong (scm_length (x), 0, "ly_length");
217 inline unsigned long ly_vector_length (SCM x) { return SCM_VECTOR_LENGTH (x); }
219 inline SCM ly_bool2scm (bool x) { return SCM_BOOL (x); }
221 inline SCM ly_append2 (SCM x1, SCM x2) {
222 return scm_append (scm_listify (x1, x2, SCM_UNDEFINED));
224 inline SCM ly_append3 (SCM x1, SCM x2, SCM x3) {
225 return scm_append (scm_listify (x1, x2, x3, SCM_UNDEFINED));
227 inline SCM ly_append4 (SCM x1, SCM x2, SCM x3, SCM x4) {
228 return scm_append (scm_listify (x1, x2, x3, x4, SCM_UNDEFINED));
232 display and print newline.
235 void ly_display_scm (SCM s);
240 void read_lily_scm_file (String);
241 void ly_c_init_guile ();
243 bool is_direction (SCM s);
244 bool is_number_pair (SCM);
248 these conversion functions also do a typecheck on the argument, and
249 return a default value if S has the wrong type.
252 Direction to_dir (SCM s);
253 bool to_boolean (SCM s);
255 void init_ly_protection ();
256 unsigned int ly_scm_hash (SCM s);
258 SCM index_get_cell (SCM cell, Direction d);
259 SCM index_set_cell (SCM cell, Direction d, SCM val);
261 SCM ly_snoc (SCM s, SCM list);
262 SCM ly_split_list (SCM s, SCM lst);
263 SCM ly_unique (SCM lst);
264 SCM ly_list_qsort_uniq_x (SCM lst);
271 void add_scm_init_func (void (*) ());
274 typedef SCM (*Scheme_function_unknown) ();
277 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
278 typedef SCM (*Scheme_function_0) ();
279 typedef SCM (*Scheme_function_1) (SCM);
280 typedef SCM (*Scheme_function_2) (SCM,SCM);
281 typedef SCM (*Scheme_function_3) (SCM,SCM, SCM);
283 typedef SCM (*Scheme_function_0) (...);
284 typedef SCM (*Scheme_function_1) (...);
285 typedef SCM (*Scheme_function_2) (...);
286 typedef SCM (*Scheme_function_3) (...);
291 Adds the NAME as a Scheme function, and a variable to store the SCM
292 version of the function in the static variable NAME_proc
294 #define DECLARE_SCHEME_CALLBACK(NAME,ARGS) \
295 static SCM NAME ARGS; \
296 static SCM NAME ## _proc
299 Make TYPE::FUNC available as a Scheme function.
301 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
302 SCM TYPE :: FUNC ## _proc;\
304 TYPE ## _ ## FUNC ## _init_functions () \
306 TYPE :: FUNC ## _proc = scm_c_define_gsubr (#TYPE "::" #FUNC, \
308 (Scheme_function_unknown)TYPE :: FUNC); \
309 scm_c_export (#TYPE "::" #FUNC, NULL);\
312 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, TYPE ## _ ## FUNC ## _init_functions); \
315 void ly_add_function_documentation (SCM proc, char const * fname,
316 char const * varlist,
319 #define ADD_SCM_INIT_FUNC(name, func)\
320 class name ## _scm_initter { \
322 name ## _scm_initter () \
324 add_scm_init_func (func); \
326 } _ ## name ## _scm_initter; \
329 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
332 INITPREFIX ## init ()\
335 = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, (Scheme_function_unknown) FNAME);\
336 ly_add_function_documentation ( FNAME ## _proc ,PRIMNAME, #ARGLIST, DOCSTRING);\
337 scm_c_export (PRIMNAME,NULL);\
339 ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init);\
344 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
345 SCM FNAME ARGLIST ; \
346 LY_DEFINE_WITHOUT_DECL(FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING)
349 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
350 SCM FNAME ARGLIST ; \
351 LY_DEFINE_WITHOUT_DECL(CLASS ## FNAME, CLASS::FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING)
354 #define get_property(x) internal_get_property(ly_symbol2scm(x))
355 #define set_property(x,y) internal_set_property(ly_symbol2scm (x), y)
358 #endif /* LILY_GUILE_HH */