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 #include "flower-proto.hh"
15 #include "drul-array.hh"
16 #include "direction.hh"
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 #define scm_is_number(x) (scm_number_p(x)==SCM_BOOL_T)
30 inline int ly_scm2int (SCM x) { return scm_num2int (x, 0, "ly_scm2int"); }
31 #define scm_to_int(x) (ly_scm2int(x))
33 inline double ly_scm2double (SCM x) { return scm_num2dbl (x, "ly_scm2double"); }
34 #define scm_to_double(x) (ly_scm2double(x))
35 #define scm_from_double(x) (scm_make_real(x))
36 #endif /* SCM_MINOR_VERSION < 7 */
38 #ifndef SMOB_FREE_RETURN_VAL
39 #define SMOB_FREE_RETURN_VAL(CL) 0
43 #define SCM_PACK(x) ((SCM) x)
47 #define SCM_UNPACK(x) (x)
50 /** Conversion functions follow the GUILE naming convention, i.e.
53 SCM ly_last (SCM list);
54 SCM ly_write2scm (SCM s);
55 SCM ly_deep_copy (SCM);
56 SCM ly_truncate_list (int k, SCM lst);
59 /* Unreliable with gcc-2.x
60 FIXME: should add check for x86 as well? */
66 /* Using this trick we cache the value of scm_str2symbol ("fooo") where
67 "fooo" is a constant string. This is done at the cost of one static
68 variable per ly_symbol2scm() use, and one boolean evaluation for
71 The overall speedup of lily is about 5% on a run of wtk1-fugue2. */
72 #define ly_symbol2scm(x) \
75 /* We store this one locally, since G++ -O2 fucks up else */ \
77 if ( __builtin_constant_p ((x))) \
80 value = cached = scm_gc_protect_object (scm_str2symbol ((x))); \
83 value = scm_str2symbol ((char*) (x)); \
87 inline SCM ly_symbol2scm(char const* x) { return scm_str2symbol ((x)); }
90 extern SCM global_lily_module;
93 TODO: rename me to ly_c_lily_module_eval
95 #define ly_scheme_function(x) \
98 /* We store this one locally, since G++ -O2 fucks up else */ \
100 if ( __builtin_constant_p ((x))) \
103 value = cached = scm_gc_protect_object (scm_eval (scm_str2symbol (x), \
104 global_lily_module)); \
107 value = scm_eval (scm_str2symbol (x), global_lily_module); \
112 String ly_scm2string (SCM s);
113 String ly_symbol2string (SCM);
114 SCM ly_offset2scm (Offset);
115 Offset ly_scm2offset (SCM);
116 SCM ly_assoc_chain (SCM key, SCM achain);
117 SCM ly_assoc_cdr (SCM key, SCM alist);
118 Interval ly_scm2interval (SCM);
119 Drul_array<Real> ly_scm2realdrul (SCM);
120 Slice int_list_to_slice (SCM l);
121 SCM ly_interval2scm (Drul_array<Real>);
122 char *ly_scm2newstr (SCM str, size_t *lenp);
124 Real robust_scm2double (SCM, double);
125 int robust_scm2int (SCM, int);
126 Drul_array<Real> robust_scm2drul (SCM, Drul_array<Real>);
127 Interval robust_scm2interval (SCM, Drul_array<Real>);
128 Offset robust_scm2offset (SCM, Offset);
130 SCM ly_quote_scm (SCM s);
131 bool type_check_assignment (SCM val, SCM sym, SCM type_symbol) ;
132 String print_scm_val (SCM val);
133 SCM ly_number2string (SCM s);
135 SCM parse_symbol_list (char const *);
136 SCM robust_list_ref(int i, SCM l);
137 SCM alist_to_hashq (SCM);
139 inline SCM ly_cdr (SCM x) { return SCM_CDR (x); }
140 inline SCM ly_car (SCM x) { return SCM_CAR (x); }
141 inline SCM ly_caar (SCM x) { return SCM_CAAR (x); }
142 inline SCM ly_cdar (SCM x) { return SCM_CDAR (x); }
143 inline SCM ly_cadr (SCM x) { return SCM_CADR (x); }
144 inline SCM ly_cddr (SCM x) { return SCM_CDDR (x); }
145 inline SCM ly_caddr (SCM x) { return SCM_CADDR (x); }
146 inline SCM ly_cdadr (SCM x) { return SCM_CDADR (x); }
147 inline SCM ly_caadr (SCM x) { return SCM_CAADR (x); }
148 inline SCM ly_cadar (SCM x) { return SCM_CADAR (x); }
149 /* inserts at front, removing dublicates */
150 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
152 return scm_acons(key, val, scm_assoc_remove_x (alist, key));
154 inline bool ly_c_pair_p (SCM x) { return SCM_NFALSEP (scm_pair_p (x)); }
155 inline bool ly_c_symbol_p (SCM x) { return SCM_SYMBOLP (x); }
156 inline bool ly_c_boolean_p (SCM x) { return SCM_BOOLP (x); }
157 inline bool ly_c_char_p (SCM x) { return SCM_CHARP (x); }
158 inline bool ly_c_number_p (SCM x) { return SCM_NUMBERP (x); }
159 inline bool ly_c_string_p (SCM x) { return scm_is_string (x); }
160 inline bool ly_c_vector_p (SCM x) { return SCM_VECTORP (x); }
161 inline bool ly_c_list_p (SCM x) { return SCM_NFALSEP (scm_list_p (x)); }
162 inline bool ly_c_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p (x)); }
163 inline bool ly_c_eq_p (SCM x, SCM y) { return SCM_EQ_P (x, y); }
164 inline bool ly_c_equal_p (SCM x, SCM y) {
165 return SCM_NFALSEP (scm_equal_p (x, y));
168 inline bool ly_scm2bool (SCM x) { return SCM_NFALSEP (x); }
169 inline char ly_scm2char (SCM x) { return SCM_CHAR(x); }
170 inline unsigned long ly_length (SCM x) {
171 return scm_num2ulong (scm_length (x), 0, "ly_length");
173 inline unsigned long ly_vector_length (SCM x) { return SCM_VECTOR_LENGTH (x); }
175 inline SCM ly_bool2scm (bool x) { return SCM_BOOL (x); }
177 inline SCM ly_append2 (SCM x1, SCM x2) {
178 return scm_append (scm_listify (x1, x2, SCM_UNDEFINED));
180 inline SCM ly_append3 (SCM x1, SCM x2, SCM x3) {
181 return scm_append (scm_listify (x1, x2, x3, SCM_UNDEFINED));
183 inline SCM ly_append4 (SCM x1, SCM x2, SCM x3, SCM x4) {
184 return scm_append (scm_listify (x1, x2, x3, x4, SCM_UNDEFINED));
188 display and print newline.
191 void ly_display_scm (SCM s);
194 void read_lily_scm_file (String);
195 void ly_c_init_guile ();
197 bool is_direction (SCM s);
198 bool is_number_pair (SCM);
202 these conversion functions also do a typecheck on the argument, and
203 return a default value if S has the wrong type.
206 Direction to_dir (SCM s);
207 bool to_boolean (SCM s);
209 void init_ly_protection ();
210 unsigned int ly_scm_hash (SCM s);
212 SCM index_get_cell (SCM cell, Direction d);
213 SCM index_set_cell (SCM cell, Direction d, SCM val);
215 SCM ly_snoc (SCM s, SCM list);
216 SCM ly_split_list (SCM s, SCM lst);
217 SCM ly_unique (SCM lst);
218 SCM ly_list_qsort_uniq_x (SCM lst);
220 SCM ly_output_formats();
221 SCM ly_kpathsea_expand_path (SCM);
226 void add_scm_init_func (void (*) ());
229 typedef SCM (*Scheme_function_unknown) ();
232 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
233 typedef SCM (*Scheme_function_0) ();
234 typedef SCM (*Scheme_function_1) (SCM);
235 typedef SCM (*Scheme_function_2) (SCM,SCM);
236 typedef SCM (*Scheme_function_3) (SCM,SCM, SCM);
238 typedef SCM (*Scheme_function_0) (...);
239 typedef SCM (*Scheme_function_1) (...);
240 typedef SCM (*Scheme_function_2) (...);
241 typedef SCM (*Scheme_function_3) (...);
246 Adds the NAME as a Scheme function, and a variable to store the SCM
247 version of the function in the static variable NAME_proc
249 #define DECLARE_SCHEME_CALLBACK(NAME, ARGS) \
250 static SCM NAME ARGS; \
251 static SCM NAME ## _proc
254 Make TYPE::FUNC available as a Scheme function.
256 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
257 SCM TYPE :: FUNC ## _proc; \
259 TYPE ## _ ## FUNC ## _init_functions () \
261 TYPE :: FUNC ## _proc = scm_c_define_gsubr (#TYPE "::" #FUNC, \
263 (Scheme_function_unknown)TYPE :: FUNC); \
264 scm_c_export (#TYPE "::" #FUNC, NULL); \
267 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, \
268 TYPE ## _ ## FUNC ## _init_functions);
272 ly_add_function_documentation (SCM proc, char const *fname,
276 #define ADD_SCM_INIT_FUNC(name, func) \
277 class name ## _scm_initter \
280 name ## _scm_initter () \
282 add_scm_init_func (func); \
284 } _ ## name ## _scm_initter; \
287 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, \
288 ARGLIST, DOCSTRING) \
289 SCM FNAME ## _proc; \
291 INITPREFIX ## init () \
293 FNAME ## _proc = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, \
294 (Scheme_function_unknown) FNAME); \
295 ly_add_function_documentation (FNAME ## _proc, PRIMNAME, #ARGLIST, \
297 scm_c_export (PRIMNAME, NULL); \
299 ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init); \
304 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
306 LY_DEFINE_WITHOUT_DECL (FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, \
309 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, \
310 ARGLIST, DOCSTRING) \
312 LY_DEFINE_WITHOUT_DECL (CLASS ## FNAME, CLASS::FNAME, PRIMNAME, REQ, OPT, \
313 VAR, ARGLIST, DOCSTRING)
315 #define get_property(x) internal_get_property (ly_symbol2scm (x))
316 #define set_property(x,y) internal_set_property (ly_symbol2scm (x), y)
318 #endif /* LILY_GUILE_HH */