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"
17 #include "guile-compatibility.hh"
19 #ifndef SMOB_FREE_RETURN_VAL
20 #define SMOB_FREE_RETURN_VAL(CL) 0
24 #define SCM_PACK(x) ((SCM) x)
28 #define SCM_UNPACK(x) (x)
31 /** Conversion functions follow the GUILE naming convention, i.e.
34 SCM ly_last (SCM list);
35 SCM ly_write2scm (SCM s);
36 SCM ly_deep_copy (SCM);
37 SCM ly_truncate_list (int k, SCM lst);
40 /* Unreliable with gcc-2.x
41 FIXME: should add check for x86 as well? */
47 /* Using this trick we cache the value of scm_str2symbol ("fooo") where
48 "fooo" is a constant string. This is done at the cost of one static
49 variable per ly_symbol2scm() use, and one boolean evaluation for
52 The overall speedup of lily is about 5% on a run of wtk1-fugue2. */
53 #define ly_symbol2scm(x) \
56 /* We store this one locally, since G++ -O2 fucks up else */ \
58 if ( __builtin_constant_p ((x))) \
61 value = cached = scm_gc_protect_object (scm_str2symbol ((x))); \
64 value = scm_str2symbol ((char*) (x)); \
68 inline SCM ly_symbol2scm(char const* x) { return scm_str2symbol ((x)); }
71 extern SCM global_lily_module;
74 TODO: rename me to ly_c_lily_module_eval
76 #define ly_scheme_function(x) \
79 /* We store this one locally, since G++ -O2 fucks up else */ \
81 if ( __builtin_constant_p ((x))) \
84 value = cached = scm_gc_protect_object (scm_eval (scm_str2symbol (x), \
85 global_lily_module)); \
88 value = scm_eval (scm_str2symbol (x), global_lily_module); \
93 String ly_scm2string (SCM s);
94 String ly_symbol2string (SCM);
95 SCM ly_offset2scm (Offset);
96 Offset ly_scm2offset (SCM);
97 SCM ly_assoc_chain (SCM key, SCM achain);
98 SCM ly_assoc_cdr (SCM key, SCM alist);
99 SCM ly_assoc_get (SCM key, SCM alist, SCM def);
100 Interval ly_scm2interval (SCM);
101 Drul_array<Real> ly_scm2realdrul (SCM);
102 Slice int_list_to_slice (SCM l);
103 SCM ly_interval2scm (Drul_array<Real>);
104 char *ly_scm2newstr (SCM str, size_t *lenp);
106 Real robust_scm2double (SCM, double);
107 int robust_scm2int (SCM, int);
108 Drul_array<Real> robust_scm2drul (SCM, Drul_array<Real>);
109 Interval robust_scm2interval (SCM, Drul_array<Real>);
110 Offset robust_scm2offset (SCM, Offset);
112 SCM ly_quote_scm (SCM s);
113 bool type_check_assignment (SCM val, SCM sym, SCM type_symbol) ;
114 String print_scm_val (SCM val);
115 SCM ly_number2string (SCM s);
117 SCM parse_symbol_list (char const *);
118 SCM robust_list_ref(int i, SCM l);
119 SCM alist_to_hashq (SCM);
122 /* inserts at front, removing dublicates */
123 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
125 return scm_acons(key, val, scm_assoc_remove_x (alist, key));
127 inline bool ly_c_char_p (SCM x) { return SCM_CHARP (x); }
128 inline bool ly_c_vector_p (SCM x) { return SCM_VECTORP (x); }
129 inline bool ly_c_list_p (SCM x) { return SCM_NFALSEP (scm_list_p (x)); }
130 inline bool ly_c_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p (x)); }
131 inline bool ly_c_equal_p (SCM x, SCM y) {
132 return SCM_NFALSEP (scm_equal_p (x, y));
136 inline bool ly_scm2bool (SCM x) { return SCM_NFALSEP (x); }
137 inline char ly_scm2char (SCM x) { return SCM_CHAR(x); }
138 inline unsigned long ly_length (SCM x) {
139 return scm_num2ulong (scm_length (x), 0, "ly_length");
141 inline unsigned long ly_vector_length (SCM x) { return SCM_VECTOR_LENGTH (x); }
143 inline SCM ly_bool2scm (bool x) { return SCM_BOOL (x); }
145 inline SCM ly_append2 (SCM x1, SCM x2) {
146 return scm_append (scm_listify (x1, x2, SCM_UNDEFINED));
148 inline SCM ly_append3 (SCM x1, SCM x2, SCM x3) {
149 return scm_append (scm_listify (x1, x2, x3, SCM_UNDEFINED));
151 inline SCM ly_append4 (SCM x1, SCM x2, SCM x3, SCM x4) {
152 return scm_append (scm_listify (x1, x2, x3, x4, SCM_UNDEFINED));
156 display and print newline.
159 void ly_display_scm (SCM s);
162 void read_lily_scm_file (String);
163 void ly_c_init_guile ();
165 bool is_direction (SCM s);
166 bool is_number_pair (SCM);
170 these conversion functions also do a typecheck on the argument, and
171 return a default value if S has the wrong type.
174 Direction to_dir (SCM s);
175 bool to_boolean (SCM s);
177 void init_ly_protection ();
178 unsigned int ly_scm_hash (SCM s);
180 SCM index_get_cell (SCM cell, Direction d);
181 SCM index_set_cell (SCM cell, Direction d, SCM val);
183 SCM ly_snoc (SCM s, SCM list);
184 SCM ly_split_list (SCM s, SCM lst);
185 SCM ly_unique (SCM lst);
186 SCM ly_list_qsort_uniq_x (SCM lst);
188 SCM ly_output_formats();
189 SCM ly_kpathsea_expand_path (SCM);
194 void add_scm_init_func (void (*) ());
197 typedef SCM (*Scheme_function_unknown) ();
200 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
201 typedef SCM (*Scheme_function_0) ();
202 typedef SCM (*Scheme_function_1) (SCM);
203 typedef SCM (*Scheme_function_2) (SCM,SCM);
204 typedef SCM (*Scheme_function_3) (SCM,SCM, SCM);
206 typedef SCM (*Scheme_function_0) (...);
207 typedef SCM (*Scheme_function_1) (...);
208 typedef SCM (*Scheme_function_2) (...);
209 typedef SCM (*Scheme_function_3) (...);
214 Adds the NAME as a Scheme function, and a variable to store the SCM
215 version of the function in the static variable NAME_proc
217 #define DECLARE_SCHEME_CALLBACK(NAME, ARGS) \
218 static SCM NAME ARGS; \
219 static SCM NAME ## _proc
222 Make TYPE::FUNC available as a Scheme function.
224 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
225 SCM TYPE :: FUNC ## _proc; \
227 TYPE ## _ ## FUNC ## _init_functions () \
229 TYPE :: FUNC ## _proc = scm_c_define_gsubr (#TYPE "::" #FUNC, \
231 (Scheme_function_unknown)TYPE :: FUNC); \
232 scm_c_export (#TYPE "::" #FUNC, NULL); \
235 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, \
236 TYPE ## _ ## FUNC ## _init_functions);
240 ly_add_function_documentation (SCM proc, char const *fname,
244 #define ADD_SCM_INIT_FUNC(name, func) \
245 class name ## _scm_initter \
248 name ## _scm_initter () \
250 add_scm_init_func (func); \
252 } _ ## name ## _scm_initter; \
255 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, \
256 ARGLIST, DOCSTRING) \
257 SCM FNAME ## _proc; \
259 INITPREFIX ## init () \
261 FNAME ## _proc = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, \
262 (Scheme_function_unknown) FNAME); \
263 ly_add_function_documentation (FNAME ## _proc, PRIMNAME, #ARGLIST, \
265 scm_c_export (PRIMNAME, NULL); \
267 ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init); \
272 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
274 LY_DEFINE_WITHOUT_DECL (FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, \
277 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, \
278 ARGLIST, DOCSTRING) \
280 LY_DEFINE_WITHOUT_DECL (CLASS ## FNAME, CLASS::FNAME, PRIMNAME, REQ, OPT, \
281 VAR, ARGLIST, DOCSTRING)
283 #define get_property(x) internal_get_property (ly_symbol2scm (x))
284 #define set_property(x, y) internal_set_property (ly_symbol2scm (x), y)
286 #endif /* LILY_GUILE_HH */