]> git.donarmstrong.com Git - lilypond.git/blob - lily/include/lily-guile.hh
55aafc98c7d8cdea7102233ab888d2544626ce58
[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--2001 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
25 /* Guile 1.3.4 compatibility */
26 #if GUILE_MINOR_VERSION < 4
27
28
29 #ifndef SCM_CELL_TYPE
30 #define SCM_CELL_TYPE(X) SCM_CAR (X)
31 #endif
32
33 #ifndef SCM_CELL_WORD_1
34 #define SCM_CELL_WORD_1(X) SCM_CDR (X)
35 #endif
36
37 #define scm_bits_t SCM
38
39 #define fix_guile_1_3_4_scm_puts(scm_data, port) scm_puts ((char*)scm_data, port)
40 #define scm_puts(scm_data, port) fix_guile_1_3_4_scm_puts (scm_data, port)
41 #endif
42
43 /* Guile 1.4.x compatibility */
44 #if GUILE_MINOR_VERSION < 5
45
46 #define scm_t_bits scm_bits_t
47
48 #define fix_guile_1_4_gh_scm2newstr(str, lenp) gh_scm2newstr (str, (int*)lenp)
49 #define gh_scm2newstr(str, lenp) fix_guile_1_4_gh_scm2newstr (str, lenp)
50
51 #define fix_guile_1_4_scm_primitive_eval(form) scm_eval_3 (form, 1, SCM_EOL)
52 #define scm_primitive_eval(form) fix_guile_1_4_scm_primitive_eval (form)
53
54 #define scm_current_module() (SCM)0
55 #define scm_set_current_module(x) (void)x
56 #define scm_c_resolve_module(x) (SCM)0
57
58 #define scm_c_define_gsubr scm_make_gsubr
59 #define scm_c_eval_string(str) gh_eval_str ((char*)str)
60 #define scm_c_memq scm_sloppy_memq
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 #endif
69
70
71
72 #include "direction.hh"
73 #include "flower-proto.hh"
74
75 #ifndef SCM_PACK
76 #define SCM_PACK(x) ((SCM) x)
77
78 #endif
79 #ifndef SCM_UNPACK
80 #define SCM_UNPACK(x) ( x)
81 #endif
82
83 /*
84   conversion functions follow the GUILE naming convention, i.e.
85
86     A ly_B2A (B b);
87  */
88
89 SCM ly_last (SCM list);
90 SCM ly_str02scm (char const*c);
91 SCM ly_write2scm (SCM s);
92 SCM ly_deep_copy (SCM);
93 SCM ly_truncate_list (int k, SCM l );
94
95 #define CACHE_SYMBOLS
96 #ifdef CACHE_SYMBOLS
97
98
99 /*
100   We don't use gh_symbol2scm directly, since it has const-correctness
101   problems in GUILE 1.3.4
102   
103  */
104 SCM my_gh_symbol2scm (const char* x);
105
106 // #warning: CACHE_SYMBOLS
107
108 /*
109   Using this trick we cache the value of gh_symbol2scm ("fooo") where
110   "fooo" is a constant string. This is done at the cost of one static
111   variable per ly_symbol2scm() use, and one boolean evaluation for
112   every call.
113
114   The overall speedup of lily is about 5% on a run of wtk1-fugue2
115
116 */
117 #define ly_symbol2scm(x) ({ static SCM cached;  \
118  SCM value = cached;  /* We store this one locally, since G++ -O2 fucks up else */   \
119  if ( __builtin_constant_p ((x)))\
120  {  if (!cached)\
121      value = cached =  scm_gc_protect_object (my_gh_symbol2scm((x)));\
122  } else\
123   value = gh_symbol2scm ((char*) (x)); \
124   value; })
125 #else
126 inline SCM ly_symbol2scm(char const* x) { return gh_symbol2scm((x)); }
127 #endif 
128
129
130
131 String ly_scm2string (SCM s);
132 String ly_symbol2string (SCM);
133 SCM ly_offset2scm (Offset);
134 Offset ly_scm2offset (SCM);
135 SCM ly_assoc_chain (SCM key, SCM achain);
136 Interval ly_scm2interval (SCM);
137 SCM ly_interval2scm (Interval);
138
139 SCM ly_parse_scm (char const* s, int* n);
140 SCM ly_quote_scm (SCM s);
141 SCM ly_type (SCM);
142 bool type_check_assignment (SCM val, SCM sym,  SCM type_symbol) ;
143 SCM ly_number2string (SCM s);
144
145 SCM parse_symbol_list (char const *);
146
147 inline SCM ly_cdr (SCM x) { return SCM_CDR (x); }
148 inline SCM ly_car (SCM x) { return SCM_CAR (x); } 
149 inline SCM ly_caar (SCM x) { return SCM_CAAR (x); }
150 inline SCM ly_cdar (SCM x) { return SCM_CDAR (x); }
151 inline SCM ly_cadr (SCM x) { return SCM_CADR (x); }
152 inline SCM ly_cddr (SCM x) { return SCM_CDDR (x); }
153 inline SCM ly_caddr (SCM x) { return SCM_CADDR (x); }
154 inline SCM ly_cdadr (SCM x) { return SCM_CDADR (x); }
155 inline SCM ly_caadr (SCM x) { return SCM_CAADR (x); }
156 inline bool ly_pair_p (SCM x) { return SCM_NFALSEP (scm_pair_p (x)); }
157 inline bool ly_symbol_p (SCM x) { return SCM_SYMBOLP (x); }
158 inline bool ly_number_p (SCM x) { return SCM_NUMBERP (x); }
159 inline bool ly_procedure_p (SCM x) { return SCM_NFALSEP (scm_procedure_p(x)); }
160
161 /*
162   display and print newline.
163  */
164 extern "C" { 
165 void ly_display_scm (SCM s);
166 }
167
168 #include "array.hh"
169
170 void read_lily_scm_file (String);
171 void init_lily_guile (String dir);
172
173 bool isdir_b (SCM s);
174 bool isaxis_b (SCM s);
175 bool ly_number_pair_p (SCM);
176 bool ly_axis_p (SCM);
177
178 /*
179   these conversion functions also do a typecheck on the argument, and
180   return a default value if S has the wrong type.
181 */
182
183 Direction to_dir (SCM s);
184 bool to_boolean (SCM s);
185
186 void init_ly_protection ();
187 unsigned int ly_scm_hash (SCM s);
188
189 SCM index_cell (SCM cellp, Direction d);
190 SCM index_set_cell (SCM cellp, Direction d, SCM val);
191
192
193 /*
194   snarfing.
195  */
196 void add_scm_init_func (void (*) ());
197
198
199 typedef SCM (*Scheme_function_unknown) ();
200
201 #if __GNUC__ > 2 || __GNUC_MINOR__ >= 96
202 typedef SCM (*Scheme_function_0) ();
203 typedef SCM (*Scheme_function_1) (SCM);
204 typedef SCM (*Scheme_function_2) (SCM,SCM);      
205 #else
206 typedef SCM (*Scheme_function_0) (...);
207 typedef SCM (*Scheme_function_1) (...);
208 typedef SCM (*Scheme_function_2) (...);
209 #endif
210
211 #define DECLARE_SCHEME_CALLBACK(NAME,ARGS) \
212         static SCM NAME ARGS; \
213         static SCM NAME ## _proc
214
215 #define MAKE_SCHEME_CALLBACK(TYPE, FUNC, ARGCOUNT) \
216 SCM TYPE :: FUNC ## _proc;\
217 void                                                            \
218 TYPE ## _ ## FUNC ## _init_functions ()                                 \
219 {                                                               \
220   TYPE :: FUNC ## _proc = gh_new_procedure ## ARGCOUNT  ## _0 (#TYPE "::" #FUNC, \
221  ((Scheme_function_ ## ARGCOUNT)TYPE :: FUNC));                                 \
222 }                                                               \
223                                                                 \
224 ADD_SCM_INIT_FUNC (TYPE ## _ ## FUNC ## _callback, TYPE ## _ ## FUNC ## _init_functions);       \
225
226
227 #define ADD_SCM_INIT_FUNC(name, func)\
228 class name ## _scm_initter {                    \
229 public:\
230   name ## _scm_initter ()                       \
231   {                                             \
232     add_scm_init_func (func);           \
233   }                                             \
234 } _ ## name ## _scm_initter;                    \
235 /* end define */
236
237 #endif // LILY_GUILE_HH