]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/lily-guile.hh
* lily/score-engraver.cc (initialize): abort if ecrm10.pfa not
[lilypond.git] / lily / include / lily-guile.hh
1 /*
2   lily-guile.hh encapsulate guile
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #ifndef LILY_GUILE_HH
10 #define LILY_GUILE_HH
11
12 #include <libguile.h>
13 #include "flower-proto.hh"
14
15 #include "drul-array.hh"
16 #include "direction.hh"
17
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_hash_table_p scm_vector_p
26 #endif /* SCM_MINOR_VERSION < 7 */
27
28 #ifndef SMOB_FREE_RETURN_VAL
29 #define SMOB_FREE_RETURN_VAL(CL) 0
30 #endif
31
32 #ifndef SCM_PACK
33 #define SCM_PACK(x) ((SCM) x)
34 #endif
35
36 #ifndef SCM_UNPACK
37 #define SCM_UNPACK(x) (x)
38 #endif
39
40 /** Conversion functions follow the GUILE naming convention, i.e.
41     A ly_B2A (B b);  */
42
43 SCM ly_last (SCM list);
44 SCM ly_write2scm (SCM s);
45 SCM ly_deep_copy (SCM);
46 SCM ly_truncate_list (int k, SCM lst);
47
48 #if (__GNUC__ > 2)
49 /* Unreliable with gcc-2.x
50    FIXME: should add check for x86 as well?  */
51 #define CACHE_SYMBOLS
52 #endif
53
54 #ifdef CACHE_SYMBOLS
55
56 /* Using this trick we cache the value of scm_str2symbol ("fooo") where
57   "fooo" is a constant string. This is done at the cost of one static
58   variable per ly_symbol2scm() use, and one boolean evaluation for
59   every call.
60
61   The overall speedup of lily is about 5% on a run of wtk1-fugue2.  */
62 #define ly_symbol2scm(x) ({ static SCM cached; \
63  SCM value = cached;  /* We store this one locally, since G++ -O2 fucks up else */ \
64  if ( __builtin_constant_p ((x)))\
65  {  if (!cached)\
66      value = cached =  scm_gc_protect_object (scm_str2symbol((x)));\
67  } else\
68   value = scm_str2symbol ((char*) (x)); \
69   value; })
70 #else
71 inline SCM ly_symbol2scm(char const* x) { return scm_str2symbol((x)); }
72 #endif
73
74 extern SCM global_lily_module;
75
76 /*
77   TODO: rename me to ly_c_lily_module_eval
78  */
79 #define ly_scheme_function(x) ({static SCM cached; \
80  SCM value = cached;  /* We store this one locally, since G++ -O2 fucks up else */ \
81  if ( __builtin_constant_p ((x)))\
82  {  if (!cached)\
83      value = cached =  scm_gc_protect_object (scm_eval(scm_str2symbol (x), global_lily_module));\
84  } else\
85   value = scm_eval(scm_str2symbol (x), global_lily_module);\
86   value; })
87
88
89
90 String ly_scm2string (SCM s);
91 String ly_symbol2string (SCM);
92 SCM ly_offset2scm (Offset);
93 Offset ly_scm2offset (SCM);
94 SCM ly_assoc_chain (SCM key, SCM achain);
95 SCM ly_assoc_cdr (SCM key, SCM alist);
96 Interval ly_scm2interval (SCM);
97 Drul_array<Real> ly_scm2realdrul (SCM);
98 Slice int_list_to_slice (SCM l);
99 SCM ly_interval2scm (Drul_array<Real>);
100 char *ly_scm2newstr (SCM str, size_t *lenp);
101
102 Real robust_scm2double (SCM, double);
103 int robust_scm2int (SCM, int);
104 Drul_array<Real> robust_scm2drul (SCM, Drul_array<Real>);
105 Interval robust_scm2interval (SCM, Drul_array<Real>);
106 Offset robust_scm2offset (SCM, Offset);
107
108 SCM ly_quote_scm (SCM s);
109 bool type_check_assignment (SCM val, SCM sym,  SCM type_symbol) ;
110 String print_scm_val (SCM val);
111 SCM ly_number2string (SCM s);
112
113 SCM parse_symbol_list (char const *);
114 SCM robust_list_ref(int i, SCM l);
115 SCM alist_to_hashq (SCM);
116
117 inline SCM ly_cdr (SCM x) { return SCM_CDR (x); }
118 inline SCM ly_car (SCM x) { return SCM_CAR (x); }
119 inline SCM ly_caar (SCM x) { return SCM_CAAR (x); }
120 inline SCM ly_cdar (SCM x) { return SCM_CDAR (x); }
121 inline SCM ly_cadr (SCM x) { return SCM_CADR (x); }
122 inline SCM ly_cddr (SCM x) { return SCM_CDDR (x); }
123 inline SCM ly_caddr (SCM x) { return SCM_CADDR (x); }
124 inline SCM ly_cdadr (SCM x) { return SCM_CDADR (x); }
125 inline SCM ly_caadr (SCM x) { return SCM_CAADR (x); }
126 inline SCM ly_cadar (SCM x) { return SCM_CADAR (x); }
127 /* inserts at front, removing dublicates */
128 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
129 {
130   return scm_acons(key, val, scm_assoc_remove_x (alist, key));
131 }
132 inline bool ly_c_pair_p (SCM x) { return SCM_NFALSEP (scm_pair_p (x)); }
133 inline bool ly_c_symbol_p (SCM x) { return SCM_SYMBOLP (x); }
134 inline bool ly_c_boolean_p (SCM x) { return SCM_BOOLP (x); }
135 inline bool ly_c_char_p (SCM x) { return SCM_CHARP (x); }
136 inline bool ly_c_number_p (SCM x) { return SCM_NUMBERP (x); }
137 inline bool ly_c_string_p (SCM x) { return SCM_STRINGP (x); }
138 inline bool ly_c_vector_p (SCM x) { return SCM_VECTORP (x); }
139 inline bool ly_c_list_p (SCM x) { return SCM_NFALSEP (scm_list_p (x)); }
140 inline bool ly_c_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p (x)); }
141 inline bool ly_c_eq_p (SCM x, SCM y) { return SCM_EQ_P (x, y); }
142 inline bool ly_c_equal_p (SCM x, SCM y) {
143   return SCM_NFALSEP (scm_equal_p (x, y));
144 }
145
146 inline bool ly_scm2bool (SCM x) { return SCM_NFALSEP (x); }
147 inline char ly_scm2char (SCM x) { return SCM_CHAR(x); }
148 inline int ly_scm2int (SCM x) { return scm_num2int (x, 0, "ly_scm2int"); }
149 inline double ly_scm2double (SCM x) { return scm_num2dbl (x, "ly_scm2double"); }
150 inline unsigned long ly_length (SCM x) {
151   return scm_num2ulong (scm_length (x), 0, "ly_length");
152 }
153 inline unsigned long ly_vector_length (SCM x) { return SCM_VECTOR_LENGTH (x); }
154
155 inline SCM ly_bool2scm (bool x) { return SCM_BOOL (x); }
156
157 inline SCM ly_append2 (SCM x1, SCM x2) {
158   return scm_append (scm_listify (x1, x2, SCM_UNDEFINED));
159 }
160 inline SCM ly_append3 (SCM x1, SCM x2, SCM x3) {
161   return scm_append (scm_listify (x1, x2, x3, SCM_UNDEFINED));
162 }
163 inline SCM ly_append4 (SCM x1, SCM x2, SCM x3, SCM x4) {
164   return scm_append (scm_listify (x1, x2, x3, x4, SCM_UNDEFINED));
165 }
166
167 /*
168   display and print newline.
169  */
170 extern "C" {
171 void ly_display_scm (SCM s);
172 }
173
174 void read_lily_scm_file (String);
175 void ly_c_init_guile ();
176
177 bool is_direction (SCM s);
178 bool is_number_pair (SCM);
179 bool is_axis (SCM);
180
181 /*
182   these conversion functions also do a typecheck on the argument, and
183   return a default value if S has the wrong type.
184 */
185
186 Direction to_dir (SCM s);
187 bool to_boolean (SCM s);
188
189 void init_ly_protection ();
190 unsigned int ly_scm_hash (SCM s);
191
192 SCM index_get_cell (SCM cell, Direction d);
193 SCM index_set_cell (SCM cell, Direction d, SCM val);
194
195 SCM ly_snoc (SCM s, SCM list);
196 SCM ly_split_list (SCM s, SCM lst);
197 SCM ly_unique (SCM lst);
198 SCM ly_list_qsort_uniq_x (SCM lst);
199
200 SCM ly_output_formats();
201 SCM ly_kpathsea_expand_path (SCM);
202
203 /*
204   snarfing.
205  */
206 void add_scm_init_func (void (*) ());
207
208 extern "C" {
209 typedef SCM (*Scheme_function_unknown) ();
210 }
211
212 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
213 typedef SCM (*Scheme_function_0) ();
214 typedef SCM (*Scheme_function_1) (SCM);
215 typedef SCM (*Scheme_function_2) (SCM,SCM);     
216 typedef SCM (*Scheme_function_3) (SCM,SCM, SCM);        
217 #else
218 typedef SCM (*Scheme_function_0) (...);
219 typedef SCM (*Scheme_function_1) (...);
220 typedef SCM (*Scheme_function_2) (...);
221 typedef SCM (*Scheme_function_3) (...);
222 #endif
223
224
225 /*
226   Adds the NAME as a Scheme function, and a variable to store the SCM
227   version of the function in the static variable NAME_proc
228  */
229 #define DECLARE_SCHEME_CALLBACK(NAME,ARGS) \
230         static SCM NAME ARGS; \
231         static SCM NAME ## _proc
232
233 /*
234   Make TYPE::FUNC available as a Scheme function.
235  */
236 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
237 SCM TYPE :: FUNC ## _proc;\
238 void                                                            \
239 TYPE ## _ ## FUNC ## _init_functions ()                                 \
240 {                                                               \
241   TYPE :: FUNC ## _proc = scm_c_define_gsubr (#TYPE "::" #FUNC, \
242                                               (ARGCOUNT), 0, 0, \
243                           (Scheme_function_unknown)TYPE :: FUNC); \
244   scm_c_export (#TYPE "::" #FUNC, NULL);\
245 }                                                               \
246                                                                 \
247 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, TYPE ## _ ## FUNC ## _init_functions);       \
248
249
250 void ly_add_function_documentation (SCM proc, char const * fname,
251                                     char const * varlist,
252                                     char const * doc);
253
254 #define ADD_SCM_INIT_FUNC(name, func)\
255 class name ## _scm_initter {                    \
256 public:\
257   name ## _scm_initter ()                       \
258   {                                             \
259     add_scm_init_func (func);           \
260   }                                             \
261 } _ ## name ## _scm_initter;                    \
262 /* end define */
263
264 #define LY_DEFINE_WITHOUT_DECL(INITPREFIX, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
265 SCM FNAME ## _proc;\
266 void \
267 INITPREFIX ## init ()\
268 {\
269  FNAME ## _proc \
270     = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, (Scheme_function_unknown) FNAME);\
271   ly_add_function_documentation ( FNAME ## _proc ,PRIMNAME, #ARGLIST,  DOCSTRING);\
272   scm_c_export (PRIMNAME,NULL);\
273 }\
274 ADD_SCM_INIT_FUNC (INITPREFIX ## init_unique_prefix, INITPREFIX ## init);\
275 SCM \
276 FNAME ARGLIST\
277
278
279 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
280 SCM FNAME ARGLIST ; \
281 LY_DEFINE_WITHOUT_DECL(FNAME, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING)
282
283
284 #define LY_DEFINE_MEMBER_FUNCTION(CLASS, FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
285 SCM FNAME ARGLIST ; \
286 LY_DEFINE_WITHOUT_DECL(CLASS ## FNAME,  CLASS::FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING)
287
288
289 #define get_property(x) internal_get_property(ly_symbol2scm(x))
290 #define set_property(x,y) internal_set_property(ly_symbol2scm (x), y)
291
292
293 #endif /* LILY_GUILE_HH */