2 lily-guile.hh encapsulate guile
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
14 TODO: the GH interface is deprecated as of GUILE 1.6
16 Remove all gh_XXX functions.
21 /* GUILE only includes version in headers (libguile/version.h) as of
22 1.5.x. For some strange reason, they call it SCM.*VERSION.
24 Not including config.h here, saves a lot of unnecessary
28 #ifndef GUILE_MAJOR_VERSION
29 #ifdef SCM_MAJOR_VERSION
30 #define GUILE_MAJOR_VERSION SCM_MAJOR_VERSION
31 #define GUILE_MINOR_VERSION SCM_MINOR_VERSION
32 #define GUILE_PATCH_LEVEL SCM_MICRO_VERSION
38 #include "drul-array.hh"
41 /* Guile 1.4.x compatibility */
42 #if GUILE_MINOR_VERSION < 5
44 #define scm_t_bits scm_bits_t
46 #define fix_guile_1_4_gh_scm2newstr(str, lenp) gh_scm2newstr (str, (int*)lenp)
47 #define gh_scm2newstr(str, lenp) fix_guile_1_4_gh_scm2newstr (str, lenp)
49 #define fix_guile_1_4_scm_primitive_eval(form) scm_eval_3 (form, 1, SCM_EOL)
50 #define scm_primitive_eval(form) fix_guile_1_4_scm_primitive_eval (form)
52 #define scm_int2num(x) scm_long2num (x)
53 #define scm_current_module() (SCM)0
54 #define scm_set_current_module(x) (void)x
55 #define scm_c_resolve_module(x) (SCM)0
57 inline SCM scm_c_make_vector (int k, SCM val) {
58 return scm_make_vector (SCM_MAKINUM (k), val);
60 #define scm_c_define_gsubr scm_make_gsubr
61 #define scm_c_eval_string(str) gh_eval_str ((char*)str)
62 #define scm_remember_upto_here_1(s) scm_remember (&s)
63 #define scm_gc_protect_object scm_protect_object
64 #define scm_gc_unprotect_object scm_unprotect_object
65 #define scm_list_n scm_listify
66 #define SCM_STRING_CHARS SCM_CHARS
67 #define SCM_STRING_LENGTH SCM_LENGTH
68 #define SCM_SYMBOL_CHARS SCM_CHARS
69 #define SCM_SYMBOL_LENGTH SCM_LENGTH
70 #define SCM_VECTOR_LENGTH SCM_LENGTH
72 #define SMOB_FREE_RETURN_VAL(CL) sizeof(CL)
73 #define scm_done_free(x)
76 #ifndef SMOB_FREE_RETURN_VAL
77 #define SMOB_FREE_RETURN_VAL(CL) 0
81 #if GUILE_MINOR_VERSION < 7
82 #define scm_gc_unregister_collectable_memory(a,b,c) scm_done_free(b)
83 #define scm_gc_register_collectable_memory(a,b,c) scm_done_malloc(b)
84 #define SCM_VECTOR_REF(v,i) (SCM_VELTS((v))[(i)])
85 #define scm_hash_table_p scm_vector_p
89 #include "direction.hh"
90 #include "flower-proto.hh"
93 #define SCM_PACK(x) ((SCM) x)
97 #define SCM_UNPACK(x) ( x)
101 conversion functions follow the GUILE naming convention, i.e.
106 SCM ly_last (SCM list);
107 SCM ly_write2scm (SCM s);
108 SCM ly_deep_copy (SCM);
109 SCM ly_truncate_list (int k, SCM l );
115 // #define CACHE_SYMBOLS
120 todo: should add check for x86 as well
122 #define CACHE_SYMBOLS
130 Using this trick we cache the value of gh_symbol2scm ("fooo") where
131 "fooo" is a constant string. This is done at the cost of one static
132 variable per ly_symbol2scm() use, and one boolean evaluation for
135 The overall speedup of lily is about 5% on a run of wtk1-fugue2
138 #define ly_symbol2scm(x) ({ static SCM cached; \
139 SCM value = cached; /* We store this one locally, since G++ -O2 fucks up else */ \
140 if ( __builtin_constant_p ((x)))\
142 value = cached = scm_gc_protect_object (gh_symbol2scm((x)));\
144 value = gh_symbol2scm ((char*) (x)); \
147 inline SCM ly_symbol2scm(char const* x) { return gh_symbol2scm((x)); }
150 extern SCM global_lily_module;
152 #define ly_scheme_function(x) ({static SCM cached; \
153 SCM value = cached; /* We store this one locally, since G++ -O2 fucks up else */ \
154 if ( __builtin_constant_p ((x)))\
156 value = cached = scm_gc_protect_object (scm_eval(gh_symbol2scm (x), global_lily_module));\
158 value = scm_eval(gh_symbol2scm (x), global_lily_module);\
163 String ly_scm2string (SCM s);
164 String ly_symbol2string (SCM);
165 SCM ly_offset2scm (Offset);
166 Offset ly_scm2offset (SCM);
167 SCM ly_assoc_chain (SCM key, SCM achain);
168 SCM ly_assoc_cdr (SCM key, SCM alist);
169 Interval ly_scm2interval (SCM);
170 Drul_array<Real> ly_scm2realdrul (SCM);
171 Slice int_list_to_slice (SCM l);
172 SCM ly_interval2scm (Drul_array<Real>);
174 Real robust_scm2double (SCM, double);
175 int robust_scm2int (SCM, int);
176 Drul_array<Real> robust_scm2drul (SCM, Drul_array<Real>);
177 Interval robust_scm2interval (SCM, Drul_array<Real>);
178 Offset robust_scm2offset (SCM, Offset);
180 SCM ly_quote_scm (SCM s);
181 bool type_check_assignment (SCM val, SCM sym, SCM type_symbol) ;
182 String print_scm_val (SCM val);
183 SCM ly_number2string (SCM s);
185 SCM parse_symbol_list (char const *);
186 SCM robust_list_ref(int i, SCM l);
187 SCM alist_to_hashq (SCM);
189 inline SCM ly_cdr (SCM x) { return SCM_CDR (x); }
190 inline SCM ly_car (SCM x) { return SCM_CAR (x); }
191 inline SCM ly_caar (SCM x) { return SCM_CAAR (x); }
192 inline SCM ly_cdar (SCM x) { return SCM_CDAR (x); }
193 inline SCM ly_cadr (SCM x) { return SCM_CADR (x); }
194 inline SCM ly_cddr (SCM x) { return SCM_CDDR (x); }
195 inline SCM ly_caddr (SCM x) { return SCM_CADDR (x); }
196 inline SCM ly_cdadr (SCM x) { return SCM_CDADR (x); }
197 inline SCM ly_caadr (SCM x) { return SCM_CAADR (x); }
198 /* inserts at front, removing dublicates */
199 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
201 return scm_acons(key, val, scm_assoc_remove_x (alist, key));
204 #define gh_pair_p ly_pair_p
205 bool ly_pair_p (SCM x);
207 inline bool ly_pair_p (SCM x) { return SCM_NFALSEP (scm_pair_p (x)); }
209 inline bool ly_symbol_p (SCM x) { return SCM_SYMBOLP (x); }
210 inline bool ly_number_p (SCM x) { return SCM_NUMBERP (x); }
211 inline bool ly_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p (x)); }
214 display and print newline.
217 void ly_display_scm (SCM s);
222 void read_lily_scm_file (String);
223 void ly_init_guile ();
225 bool is_direction (SCM s);
226 bool is_number_pair (SCM);
230 these conversion functions also do a typecheck on the argument, and
231 return a default value if S has the wrong type.
234 Direction to_dir (SCM s);
235 bool to_boolean (SCM s);
237 void init_ly_protection ();
238 unsigned int ly_scm_hash (SCM s);
240 SCM index_get_cell (SCM cell, Direction d);
241 SCM index_set_cell (SCM cell, Direction d, SCM val);
243 SCM ly_snoc (SCM s, SCM list);
244 SCM ly_split_list (SCM s, SCM list);
245 SCM ly_unique (SCM list);
252 void add_scm_init_func (void (*) ());
255 typedef SCM (*Scheme_function_unknown) ();
258 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
259 typedef SCM (*Scheme_function_0) ();
260 typedef SCM (*Scheme_function_1) (SCM);
261 typedef SCM (*Scheme_function_2) (SCM,SCM);
262 typedef SCM (*Scheme_function_3) (SCM,SCM, SCM);
264 typedef SCM (*Scheme_function_0) (...);
265 typedef SCM (*Scheme_function_1) (...);
266 typedef SCM (*Scheme_function_2) (...);
267 typedef SCM (*Scheme_function_3) (...);
272 Adds the NAME as a Scheme function, and a variable to store the SCM
273 version of the function.
275 #define DECLARE_SCHEME_CALLBACK(NAME,ARGS) \
276 static SCM NAME ARGS; \
277 static SCM NAME ## _proc
280 Make TYPE::FUNC available as a Scheme function.
282 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
283 SCM TYPE :: FUNC ## _proc;\
285 TYPE ## _ ## FUNC ## _init_functions () \
287 TYPE :: FUNC ## _proc = gh_new_procedure ## ARGCOUNT ## _0 (#TYPE "::" #FUNC, \
288 ((Scheme_function_ ## ARGCOUNT)TYPE :: FUNC)); \
289 scm_c_export (#TYPE "::" #FUNC, NULL);\
292 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, TYPE ## _ ## FUNC ## _init_functions); \
295 void ly_add_function_documentation (SCM proc, char const * fname,
296 char const * varlist,
299 #define ADD_SCM_INIT_FUNC(name, func)\
300 class name ## _scm_initter { \
302 name ## _scm_initter () \
304 add_scm_init_func (func); \
306 } _ ## name ## _scm_initter; \
309 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
312 INITPREFIX ## init ()\
315 = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, (Scheme_function_unknown) FNAME);\
316 ly_add_function_documentation ( FNAME ## _proc ,PRIMNAME, #ARGLIST, DOCSTRING);\
317 scm_c_export (PRIMNAME,NULL);\
319 ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init);\
324 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
325 SCM FNAME ARGLIST ; \
326 LY_DEFINE_WITHOUT_DECL(FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING)
329 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
330 SCM FNAME ARGLIST ; \
331 LY_DEFINE_WITHOUT_DECL(CLASS ## FNAME, CLASS::FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING)
334 #define get_property(x) internal_get_property(ly_symbol2scm(x))
335 #define set_property(x,y) internal_set_property(ly_symbol2scm (x), y)
338 #endif /* LILY_GUILE_HH */