2 lily-guile.hh encapsulate guile
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2003 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"
40 /* Guile 1.4.x compatibility */
41 #if GUILE_MINOR_VERSION < 5
43 #define scm_t_bits scm_bits_t
45 #define fix_guile_1_4_gh_scm2newstr(str, lenp) gh_scm2newstr (str, (int*)lenp)
46 #define gh_scm2newstr(str, lenp) fix_guile_1_4_gh_scm2newstr (str, lenp)
48 #define fix_guile_1_4_scm_primitive_eval(form) scm_eval_3 (form, 1, SCM_EOL)
49 #define scm_primitive_eval(form) fix_guile_1_4_scm_primitive_eval (form)
51 #define scm_int2num(x) scm_long2num (x)
52 #define scm_current_module() (SCM)0
53 #define scm_set_current_module(x) (void)x
54 #define scm_c_resolve_module(x) (SCM)0
56 inline SCM scm_c_make_vector (int k, SCM val) {
57 return scm_make_vector (SCM_MAKINUM (k), val);
59 #define scm_c_define_gsubr scm_make_gsubr
60 #define scm_c_eval_string(str) gh_eval_str ((char*)str)
61 #define scm_remember_upto_here_1(s) scm_remember (&s)
62 #define scm_gc_protect_object scm_protect_object
63 #define scm_gc_unprotect_object scm_unprotect_object
64 #define scm_list_n scm_listify
65 #define SCM_STRING_CHARS SCM_CHARS
66 #define SCM_STRING_LENGTH SCM_LENGTH
67 #define SCM_SYMBOL_CHARS SCM_CHARS
68 #define SCM_SYMBOL_LENGTH SCM_LENGTH
69 #define SCM_VECTOR_LENGTH SCM_LENGTH
71 #define SMOB_FREE_RETURN_VAL(CL) sizeof(CL)
72 #define scm_done_free(x)
75 #ifndef SMOB_FREE_RETURN_VAL
76 #define SMOB_FREE_RETURN_VAL(CL) 0
80 #if GUILE_MINOR_VERSION < 7
81 #define scm_gc_unregister_collectable_memory(a,b,c) scm_done_free(b)
82 #define scm_gc_register_collectable_memory(a,b,c) scm_done_malloc(b)
85 #include "direction.hh"
86 #include "flower-proto.hh"
89 #define SCM_PACK(x) ((SCM) x)
93 #define SCM_UNPACK(x) ( x)
97 conversion functions follow the GUILE naming convention, i.e.
102 SCM ly_last (SCM list);
103 SCM ly_write2scm (SCM s);
104 SCM ly_deep_copy (SCM);
105 SCM ly_truncate_list (int k, SCM l );
111 // #define CACHE_SYMBOLS
116 todo: should add check for x86 as well
118 #define CACHE_SYMBOLS
126 Using this trick we cache the value of gh_symbol2scm ("fooo") where
127 "fooo" is a constant string. This is done at the cost of one static
128 variable per ly_symbol2scm() use, and one boolean evaluation for
131 The overall speedup of lily is about 5% on a run of wtk1-fugue2
134 #define ly_symbol2scm(x) ({ static SCM cached; \
135 SCM value = cached; /* We store this one locally, since G++ -O2 fucks up else */ \
136 if ( __builtin_constant_p ((x)))\
138 value = cached = scm_gc_protect_object (gh_symbol2scm((x)));\
140 value = gh_symbol2scm ((char*) (x)); \
143 inline SCM ly_symbol2scm(char const* x) { return gh_symbol2scm((x)); }
148 String ly_scm2string (SCM s);
149 String ly_symbol2string (SCM);
150 SCM ly_offset2scm (Offset);
151 Offset ly_scm2offset (SCM);
152 SCM ly_assoc_chain (SCM key, SCM achain);
153 SCM ly_assoc_cdr (SCM key, SCM alist);
154 Interval ly_scm2interval (SCM);
155 Slice int_list_to_slice (SCM l);
156 SCM ly_interval2scm (Drul_array<Real>);
160 SCM ly_quote_scm (SCM s);
161 bool type_check_assignment (SCM val, SCM sym, SCM type_symbol) ;
162 String print_scm_val (SCM val);
163 SCM ly_number2string (SCM s);
165 SCM parse_symbol_list (char const *);
166 SCM robust_list_ref(int i, SCM l);
169 inline SCM ly_cdr (SCM x) { return SCM_CDR (x); }
170 inline SCM ly_car (SCM x) { return SCM_CAR (x); }
171 inline SCM ly_caar (SCM x) { return SCM_CAAR (x); }
172 inline SCM ly_cdar (SCM x) { return SCM_CDAR (x); }
173 inline SCM ly_cadr (SCM x) { return SCM_CADR (x); }
174 inline SCM ly_cddr (SCM x) { return SCM_CDDR (x); }
175 inline SCM ly_caddr (SCM x) { return SCM_CADDR (x); }
176 inline SCM ly_cdadr (SCM x) { return SCM_CDADR (x); }
177 inline SCM ly_caadr (SCM x) { return SCM_CAADR (x); }
178 /* inserts at front, removing dublicates */
179 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
181 return scm_acons(key, val, scm_assoc_remove_x (alist, key));
184 #define gh_pair_p ly_pair_p
185 bool ly_pair_p (SCM x);
187 inline bool ly_pair_p (SCM x) { return SCM_NFALSEP (scm_pair_p (x)); }
189 inline bool ly_symbol_p (SCM x) { return SCM_SYMBOLP (x); }
190 inline bool ly_number_p (SCM x) { return SCM_NUMBERP (x); }
191 inline bool ly_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p (x)); }
194 display and print newline.
197 void ly_display_scm (SCM s);
202 void read_lily_scm_file (String);
203 void ly_init_guile ();
205 bool is_direction (SCM s);
206 bool is_number_pair (SCM);
210 these conversion functions also do a typecheck on the argument, and
211 return a default value if S has the wrong type.
214 Direction to_dir (SCM s);
215 bool to_boolean (SCM s);
217 void init_ly_protection ();
218 unsigned int ly_scm_hash (SCM s);
220 SCM index_get_cell (SCM cell, Direction d);
221 SCM index_set_cell (SCM cell, Direction d, SCM val);
225 SCM ly_snoc (SCM s, SCM list);
226 SCM ly_split_list (SCM s, SCM list);
227 SCM ly_unique (SCM list);
234 void add_scm_init_func (void (*) ());
237 typedef SCM (*Scheme_function_unknown) ();
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);
245 typedef SCM (*Scheme_function_0) (...);
246 typedef SCM (*Scheme_function_1) (...);
247 typedef SCM (*Scheme_function_2) (...);
248 typedef SCM (*Scheme_function_3) (...);
251 #define DECLARE_SCHEME_CALLBACK(NAME,ARGS) \
252 static SCM NAME ARGS; \
253 static SCM NAME ## _proc
255 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
256 SCM TYPE :: FUNC ## _proc;\
258 TYPE ## _ ## FUNC ## _init_functions () \
260 TYPE :: FUNC ## _proc = gh_new_procedure ## ARGCOUNT ## _0 (#TYPE "::" #FUNC, \
261 ((Scheme_function_ ## ARGCOUNT)TYPE :: FUNC)); \
262 scm_c_export (#TYPE "::" #FUNC, NULL);\
265 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, TYPE ## _ ## FUNC ## _init_functions); \
268 void ly_add_function_documentation (SCM proc, char const * fname,
269 char const * varlist,
272 #define ADD_SCM_INIT_FUNC(name, func)\
273 class name ## _scm_initter { \
275 name ## _scm_initter () \
277 add_scm_init_func (func); \
279 } _ ## name ## _scm_initter; \
282 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
285 INITPREFIX ## init ()\
288 = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, (Scheme_function_unknown) FNAME);\
289 ly_add_function_documentation ( FNAME ## _proc ,PRIMNAME, #ARGLIST, DOCSTRING);\
290 scm_c_export (PRIMNAME,NULL);\
292 ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init);\
297 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
298 SCM FNAME ARGLIST ; \
299 LY_DEFINE_WITHOUT_DECL(FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING)
302 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
303 SCM FNAME ARGLIST ; \
304 LY_DEFINE_WITHOUT_DECL(CLASS ## FNAME, CLASS::FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING)
307 #endif /* LILY_GUILE_HH */