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