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 #include "interval.hh"
15 #include "guile-compatibility.hh"
17 #ifndef SMOB_FREE_RETURN_VAL
18 #define SMOB_FREE_RETURN_VAL(CL) 0
22 #define SCM_PACK(x) ((SCM) x)
26 #define SCM_UNPACK(x) (x)
29 /** Conversion functions follow the GUILE naming convention, i.e.
32 SCM ly_last (SCM list);
33 SCM ly_write2scm (SCM s);
34 SCM ly_deep_copy (SCM);
35 SCM ly_truncate_list (int k, SCM lst);
37 SCM ly_to_string (SCM scm);
38 SCM ly_to_symbol (SCM scm);
41 /* Unreliable with gcc-2.x
42 FIXME: should add check for x86 as well? */
48 /* Using this trick we cache the value of scm_str2symbol ("fooo") where
49 "fooo" is a constant string. This is done at the cost of one static
50 variable per ly_symbol2scm() use, and one boolean evaluation for
53 The overall speedup of lily is about 5% on a run of wtk1-fugue2. */
54 #define ly_symbol2scm(x) \
57 /* We store this one locally, since G++ -O2 fucks up else */ \
59 if ( __builtin_constant_p ((x))) \
62 value = cached = scm_gc_protect_object (scm_str2symbol ((x))); \
65 value = scm_str2symbol ((char*) (x)); \
69 inline SCM ly_symbol2scm(char const* x) { return scm_str2symbol ((x)); }
72 extern SCM global_lily_module;
75 TODO: rename me to ly_c_lily_module_eval
77 #define ly_lily_module_constant(x) \
80 /* We store this one locally, since G++ -O2 fucks up else */ \
82 if ( __builtin_constant_p ((x))) \
85 value = cached = scm_gc_protect_object (scm_eval (scm_str2symbol (x), \
86 global_lily_module)); \
89 value = scm_eval (scm_str2symbol (x), global_lily_module); \
93 String gulp_file_to_string (String fn, bool must_exist);
95 String ly_scm2string (SCM s);
96 String ly_symbol2string (SCM);
97 SCM ly_offset2scm (Offset);
98 Offset ly_scm2offset (SCM);
99 SCM ly_chain_assoc (SCM key, SCM achain);
100 SCM ly_chain_assoc_get (SCM key, SCM achain, SCM dfault);
101 SCM ly_assoc_cdr (SCM key, SCM alist);
102 SCM ly_assoc_get (SCM key, SCM alist, SCM def);
103 Interval ly_scm2interval (SCM);
104 Drul_array<Real> ly_scm2realdrul (SCM);
105 Slice int_list_to_slice (SCM l);
106 SCM ly_interval2scm (Drul_array<Real>);
107 char *ly_scm2newstr (SCM str, size_t *lenp);
109 Real robust_scm2double (SCM, double);
110 int robust_scm2int (SCM, int);
111 Drul_array<Real> robust_scm2drul (SCM, Drul_array<Real>);
112 Interval robust_scm2interval (SCM, Drul_array<Real>);
113 Offset robust_scm2offset (SCM, Offset);
115 SCM ly_quote_scm (SCM s);
116 bool type_check_assignment (SCM val, SCM sym, SCM type_symbol) ;
117 String print_scm_val (SCM val);
118 SCM ly_number2string (SCM s);
120 SCM parse_symbol_list (char const *);
121 SCM robust_list_ref(int i, SCM l);
122 SCM alist_to_hashq (SCM);
125 /* inserts at front, removing dublicates */
126 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
128 return scm_acons(key, val, scm_assoc_remove_x (alist, key));
130 inline bool ly_c_char_p (SCM x) { return SCM_CHARP (x); }
131 inline bool ly_c_vector_p (SCM x) { return SCM_VECTORP (x); }
132 inline bool ly_c_list_p (SCM x) { return SCM_NFALSEP (scm_list_p (x)); }
133 inline bool ly_c_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p (x)); }
134 inline bool ly_c_equal_p (SCM x, SCM y) {
135 return SCM_NFALSEP (scm_equal_p (x, y));
139 inline bool ly_scm2bool (SCM x) { return SCM_NFALSEP (x); }
140 inline char ly_scm2char (SCM x) { return SCM_CHAR(x); }
141 inline unsigned long ly_length (SCM x) {
142 return scm_num2ulong (scm_length (x), 0, "ly_length");
144 inline unsigned long ly_vector_length (SCM x) { return SCM_VECTOR_LENGTH (x); }
146 inline SCM ly_bool2scm (bool x) { return SCM_BOOL (x); }
148 inline SCM ly_append2 (SCM x1, SCM x2) {
149 return scm_append (scm_listify (x1, x2, SCM_UNDEFINED));
151 inline SCM ly_append3 (SCM x1, SCM x2, SCM x3) {
152 return scm_append (scm_listify (x1, x2, x3, SCM_UNDEFINED));
154 inline SCM ly_append4 (SCM x1, SCM x2, SCM x3, SCM x4) {
155 return scm_append (scm_listify (x1, x2, x3, x4, SCM_UNDEFINED));
159 display and print newline.
162 void ly_display_scm (SCM s);
165 void read_lily_scm_file (String);
166 void ly_c_init_guile ();
168 bool is_direction (SCM s);
169 bool is_number_pair (SCM);
173 these conversion functions also do a typecheck on the argument, and
174 return a default value if S has the wrong type.
177 Direction to_dir (SCM s);
178 bool to_boolean (SCM s);
180 void init_ly_protection ();
181 unsigned int ly_scm_hash (SCM s);
183 SCM index_get_cell (SCM cell, Direction d);
184 SCM index_set_cell (SCM cell, Direction d, SCM val);
186 SCM ly_snoc (SCM s, SCM list);
187 SCM ly_split_list (SCM s, SCM lst);
188 SCM ly_unique (SCM lst);
189 SCM ly_list_qsort_uniq_x (SCM lst);
191 SCM ly_output_formats();
192 SCM ly_kpathsea_find_file (SCM);
197 void add_scm_init_func (void (*) ());
200 typedef SCM (*Scheme_function_unknown) ();
203 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
204 typedef SCM (*Scheme_function_0) ();
205 typedef SCM (*Scheme_function_1) (SCM);
206 typedef SCM (*Scheme_function_2) (SCM,SCM);
207 typedef SCM (*Scheme_function_3) (SCM,SCM, SCM);
209 typedef SCM (*Scheme_function_0) (...);
210 typedef SCM (*Scheme_function_1) (...);
211 typedef SCM (*Scheme_function_2) (...);
212 typedef SCM (*Scheme_function_3) (...);
217 Adds the NAME as a Scheme function, and a variable to store the SCM
218 version of the function in the static variable NAME_proc
220 #define DECLARE_SCHEME_CALLBACK(NAME, ARGS) \
221 static SCM NAME ARGS; \
222 static SCM NAME ## _proc
225 Make TYPE::FUNC available as a Scheme function.
227 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
228 SCM TYPE :: FUNC ## _proc; \
230 TYPE ## _ ## FUNC ## _init_functions () \
232 TYPE :: FUNC ## _proc = scm_c_define_gsubr (#TYPE "::" #FUNC, \
234 (Scheme_function_unknown)TYPE :: FUNC); \
235 scm_c_export (#TYPE "::" #FUNC, NULL); \
238 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, \
239 TYPE ## _ ## FUNC ## _init_functions);
243 ly_add_function_documentation (SCM proc, char const *fname,
247 #define ADD_SCM_INIT_FUNC(name, func) \
248 class name ## _scm_initter \
251 name ## _scm_initter () \
253 add_scm_init_func (func); \
255 } _ ## name ## _scm_initter; \
258 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, \
259 ARGLIST, DOCSTRING) \
260 SCM FNAME ## _proc; \
262 INITPREFIX ## init () \
264 FNAME ## _proc = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, \
265 (Scheme_function_unknown) FNAME); \
266 ly_add_function_documentation (FNAME ## _proc, PRIMNAME, #ARGLIST, \
268 scm_c_export (PRIMNAME, NULL); \
270 ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init); \
275 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
277 LY_DEFINE_WITHOUT_DECL (FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, \
280 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, \
281 ARGLIST, DOCSTRING) \
283 LY_DEFINE_WITHOUT_DECL (CLASS ## FNAME, CLASS::FNAME, PRIMNAME, REQ, OPT, \
284 VAR, ARGLIST, DOCSTRING)
286 #define get_property(x) internal_get_property (ly_symbol2scm (x))
287 #define set_property(x, y) internal_set_property (ly_symbol2scm (x), y)
289 #endif /* LILY_GUILE_HH */