]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/lily-guile.hh
* Documentation/windows/zlily-profile.sh:
[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--2002 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #ifndef LILY_GUILE_HH
10 #define LILY_GUILE_HH
11
12 #define SCM_VOIDP_TEST
13
14 #include <libguile.h>
15
16 /*
17   TODO: the  GH interface is deprecated as of GUILE 1.6
18
19   Remove all gh_XXX functions.
20  */
21 #include <guile/gh.h>
22
23 #include "config.h"
24 #include "drul-array.hh"
25
26
27 /* Guile 1.3.4 compatibility */
28 #if GUILE_MINOR_VERSION < 4
29
30
31 #ifndef SCM_CELL_TYPE
32 #define SCM_CELL_TYPE(X) SCM_CAR (X)
33 #endif
34
35 #ifndef SCM_CELL_WORD_1
36 #define SCM_CELL_WORD_1(X) SCM_CDR (X)
37 #endif
38
39 #define scm_bits_t SCM
40
41 #define fix_guile_1_3_4_scm_puts(scm_data, port) scm_puts ((char*)scm_data, port)
42 #define scm_puts(scm_data, port) fix_guile_1_3_4_scm_puts (scm_data, port)
43 #endif
44
45 /* Guile 1.4.x compatibility */
46 #if GUILE_MINOR_VERSION < 5
47
48 #define scm_t_bits scm_bits_t
49
50 #define fix_guile_1_4_gh_scm2newstr(str, lenp) gh_scm2newstr (str, (int*)lenp)
51 #define gh_scm2newstr(str, lenp) fix_guile_1_4_gh_scm2newstr (str, lenp)
52
53 #define fix_guile_1_4_scm_primitive_eval(form) scm_eval_3 (form, 1, SCM_EOL)
54 #define scm_primitive_eval(form) fix_guile_1_4_scm_primitive_eval (form)
55
56 #define scm_current_module() (SCM)0
57 #define scm_set_current_module(x) (void)x
58 #define scm_c_resolve_module(x) (SCM)0
59
60 #define scm_c_define_gsubr scm_make_gsubr
61 #define scm_c_eval_string(str) gh_eval_str ((char*)str)
62 #define scm_c_memq scm_sloppy_memq
63 #define scm_gc_protect_object scm_protect_object
64 #define scm_gc_unprotect_object scm_unprotect_object
65 #define scm_list_n scm_listify
66 #define SCM_STRING_CHARS SCM_CHARS
67 #define SCM_STRING_LENGTH SCM_LENGTH
68 #define SCM_SYMBOL_CHARS SCM_CHARS
69 #define SCM_SYMBOL_LENGTH SCM_LENGTH
70 #endif
71
72
73 #include "direction.hh"
74 #include "flower-proto.hh"
75
76 #ifndef SCM_PACK
77 #define SCM_PACK(x) ((SCM) x)
78
79 #endif
80 #ifndef SCM_UNPACK
81 #define SCM_UNPACK(x) ( x)
82 #endif
83
84 /*
85   conversion functions follow the GUILE naming convention, i.e.
86
87     A ly_B2A (B b);
88  */
89
90 SCM ly_last (SCM list);
91 SCM ly_str02scm (char const*c);
92 SCM ly_write2scm (SCM s);
93 SCM ly_deep_copy (SCM);
94 SCM ly_truncate_list (int k, SCM l );
95
96
97 #if (__GNUC__ > 2)
98 /*
99   todo: should add check for x86 as well
100  */
101 #define CACHE_SYMBOLS
102 #endif
103
104
105 /*
106   We don't use gh_symbol2scm directly, since it has const-correctness
107   problems in GUILE 1.3.4
108   
109  */
110 SCM my_gh_symbol2scm (const char* x);
111
112 #ifdef CACHE_SYMBOLS
113
114
115 /*
116   Using this trick we cache the value of gh_symbol2scm ("fooo") where
117   "fooo" is a constant string. This is done at the cost of one static
118   variable per ly_symbol2scm() use, and one boolean evaluation for
119   every call.
120
121   The overall speedup of lily is about 5% on a run of wtk1-fugue2
122
123 */
124 #define ly_symbol2scm(x) ({ static SCM cached;  \
125  SCM value = cached;  /* We store this one locally, since G++ -O2 fucks up else */   \
126  if ( __builtin_constant_p ((x)))\
127  {  if (!cached)\
128      value = cached =  scm_gc_protect_object (my_gh_symbol2scm((x)));\
129  } else\
130   value = gh_symbol2scm ((char*) (x)); \
131   value; })
132 #else
133 inline SCM ly_symbol2scm(char const* x) { return my_gh_symbol2scm((x)); }
134 #endif 
135
136
137
138 String ly_scm2string (SCM s);
139 String ly_symbol2string (SCM);
140 SCM ly_offset2scm (Offset);
141 Offset ly_scm2offset (SCM);
142 SCM ly_assoc_chain (SCM key, SCM achain);
143 SCM ly_assoc_cdr (SCM key, SCM alist);
144 Interval ly_scm2interval (SCM);
145 SCM ly_interval2scm (Drul_array<Real>);
146
147
148 SCM ly_parse_scm (char const* s, int* n);
149 SCM ly_quote_scm (SCM s);
150 SCM ly_type (SCM);
151 bool type_check_assignment (SCM val, SCM sym,  SCM type_symbol) ;
152 String print_scm_val (SCM val);
153 SCM ly_number2string (SCM s);
154
155 SCM parse_symbol_list (char const *);
156
157 inline SCM ly_cdr (SCM x) { return SCM_CDR (x); }
158 inline SCM ly_car (SCM x) { return SCM_CAR (x); } 
159 inline SCM ly_caar (SCM x) { return SCM_CAAR (x); }
160 inline SCM ly_cdar (SCM x) { return SCM_CDAR (x); }
161 inline SCM ly_cadr (SCM x) { return SCM_CADR (x); }
162 inline SCM ly_cddr (SCM x) { return SCM_CDDR (x); }
163 inline SCM ly_caddr (SCM x) { return SCM_CADDR (x); }
164 inline SCM ly_cdadr (SCM x) { return SCM_CDADR (x); }
165 inline SCM ly_caadr (SCM x) { return SCM_CAADR (x); }
166 /* inserts at front, removing dublicates */
167 inline SCM ly_assoc_front_x(SCM alist, SCM key, SCM val)
168 {
169   return scm_acons(key, val, scm_assoc_remove_x (alist, key));
170 }
171 #ifdef PARANOID
172 #define gh_pair_p ly_pair_p
173 bool ly_pair_p (SCM x);
174 #else
175 inline bool ly_pair_p (SCM x) { return SCM_NFALSEP (scm_pair_p (x)); }
176 #endif
177 inline bool ly_symbol_p (SCM x) { return SCM_SYMBOLP (x); }
178 inline bool ly_number_p (SCM x) { return SCM_NUMBERP (x); }
179 inline bool ly_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p(x)); }
180
181 /*
182   display and print newline.
183  */
184 extern "C" { 
185 void ly_display_scm (SCM s);
186 }
187
188 #include "array.hh"
189
190 void read_lily_scm_file (String);
191 void init_lily_guile (String dir);
192
193 bool ly_dir_p (SCM s);
194 bool ly_number_pair_p (SCM);
195 bool ly_axis_p (SCM);
196
197 /*
198   these conversion functions also do a typecheck on the argument, and
199   return a default value if S has the wrong type.
200 */
201
202 Direction to_dir (SCM s);
203 bool to_boolean (SCM s);
204
205 void init_ly_protection ();
206 unsigned int ly_scm_hash (SCM s);
207
208 SCM index_cell (SCM cellp, Direction d);
209 SCM index_set_cell (SCM cellp, Direction d, SCM val);
210
211
212
213 SCM ly_snoc (SCM s, SCM list);
214 SCM ly_split_list (SCM s, SCM list);
215 SCM ly_unique (SCM list);
216 /*
217   snarfing.
218  */
219 void add_scm_init_func (void (*) ());
220
221
222 typedef SCM (*Scheme_function_unknown) ();
223
224 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
225 typedef SCM (*Scheme_function_0) ();
226 typedef SCM (*Scheme_function_1) (SCM);
227 typedef SCM (*Scheme_function_2) (SCM,SCM);      
228 typedef SCM (*Scheme_function_3) (SCM,SCM, SCM);         
229 #else
230 typedef SCM (*Scheme_function_0) (...);
231 typedef SCM (*Scheme_function_1) (...);
232 typedef SCM (*Scheme_function_2) (...);
233 typedef SCM (*Scheme_function_3) (...);
234 #endif
235
236 #define DECLARE_SCHEME_CALLBACK(NAME,ARGS) \
237         static SCM NAME ARGS; \
238         static SCM NAME ## _proc
239
240 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
241 SCM TYPE :: FUNC ## _proc;\
242 void                                                            \
243 TYPE ## _ ## FUNC ## _init_functions ()                                 \
244 {                                                               \
245   TYPE :: FUNC ## _proc = gh_new_procedure ## ARGCOUNT  ## _0 (#TYPE "::" #FUNC, \
246  ((Scheme_function_ ## ARGCOUNT)TYPE :: FUNC));                                 \
247 }                                                               \
248                                                                 \
249 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, TYPE ## _ ## FUNC ## _init_functions);       \
250
251
252 void ly_add_function_documentation (char const * fname,
253                                     char const * varlist,
254                                     char const * doc);
255
256 #define ADD_SCM_INIT_FUNC(name, func)\
257 class name ## _scm_initter {                    \
258 public:\
259   name ## _scm_initter ()                       \
260   {                                             \
261     add_scm_init_func (func);           \
262   }                                             \
263 } _ ## name ## _scm_initter;                    \
264 /* end define */
265
266 #define LY_DEFINE(FNAME, PRIMNAME, REQ, OPT, VAR, ARGLIST, DOCSTRING) \
267 SCM FNAME ARGLIST ; \
268 SCM FNAME ## _proc;\
269 void \
270 FNAME ## init ()\
271 {\
272  FNAME ## _proc \
273     = scm_c_define_gsubr (PRIMNAME,REQ, OPT, VAR, (Scheme_function_unknown) FNAME);\
274   ly_add_function_documentation (PRIMNAME, #ARGLIST,  DOCSTRING);\
275 }\
276 ADD_SCM_INIT_FUNC (FNAME ## init_unique_prefix, FNAME ## init);\
277 SCM \
278 FNAME ARGLIST\
279
280
281
282 #endif // LILY_GUILE_HH