2 lily-guile.cc -- implement assorted guile functions
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2000 Jan Nieuwenhuizen <janneke@gnu.org>
8 Han-Wen Nienhuys <hanwen@cs.uu.nl>
14 #include <math.h> // isinf
16 #include "libc-extension.hh"
17 #include "lily-guile.hh"
19 #include "simple-file-storage.hh"
20 #include "file-path.hh"
22 #include "direction.hh"
26 ly_str02scm (char const*c)
28 // this all really sucks, guile should take char const* arguments!
29 return gh_str02scm ((char*)c);
33 ly_eval_str (String s)
35 // this all really sucks, guile should take char const* arguments!
36 return gh_eval_str ((char*)s.ch_C ());
41 Pass string to scm parser, evaluate one expression.
42 Return result value and #chars read.
44 Thanks to Gary Houston <ghouston@freewire.co.uk>
46 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
49 ly_parse_scm (char const* s, int* n)
51 SCM str = gh_str02scm ((char*)s);
52 SCM port = scm_mkstrport (SCM_INUM0, str, SCM_OPN | SCM_RDNG,
54 SCM from = scm_ftell (port);
57 SCM answer = SCM_UNSPECIFIED;
59 /* Read expression from port */
60 if (!SCM_EOF_OBJECT_P (form = scm_read (port)))
61 answer = scm_eval_x (form);
68 all seems fine, but after parsing
72 read_buf has been advanced to read_pos - 1,
73 so that scm_ftell returns 1, instead of #parsed chars
77 urg: reset read_buf for scm_ftell
78 shouldn't scm_read () do this for us?
80 scm_fill_input (port);
81 SCM to = scm_ftell (port);
82 *n = gh_scm2int (to) - gh_scm2int (from);
84 /* Don't close the port here; if we re-enter this function via a
85 continuation, then the next time we enter it, we'll get an error.
86 It's a string port anyway, so there's no advantage to closing it
89 scm_close_port (port);
98 return gh_list (ly_symbol2scm ("quote"), s, SCM_UNDEFINED);
103 ly_symbol2scm(const char *s)
105 return gh_symbol2scm ((char *)s);
109 ly_symbol2string (SCM s)
111 assert (gh_symbol_p (s));
112 return String((Byte*)SCM_CHARS (s), (int) SCM_LENGTH(s));
117 Read a file, and shove it down GUILE. GUILE also has file read
118 functions, but you can't fiddle with the path of those.
121 read_lily_scm_file (String fn)
123 String s = global_path.find (fn);
126 String e = _f ("Can't find file: `%s'", fn);
128 e += _f ("(load path: `%s')", global_path.str ());
132 progress_indication ("[" + s);
135 Simple_file_storage f(s);
137 ly_eval_str ((char *) f.ch_C());
138 progress_indication ("]");
143 ly_gulp_file (SCM name)
145 String fn (ly_scm2string (name));
146 String s = global_path.find (fn);
149 String e = _f ("Can't find file: `%s'", fn);
151 e += _f ("(load path: `%s')", global_path.str ());
155 progress_indication ("[" + s );
158 Simple_file_storage f(s);
159 SCM result = ly_str02scm (f.ch_C());
160 progress_indication ("]");
165 ly_display_scm (SCM s)
172 ly_scm2string (SCM s)
174 assert (gh_string_p (s));
176 char * p = gh_scm2newstr (s , &len);
185 index_cell (SCM s, Direction d)
188 return (d == LEFT) ? gh_car (s) : gh_cdr (s);
192 index_set_cell (SCM s, Direction d, SCM v)
204 assert (gh_string_p (str));
205 warning ("lily-guile: " + ly_scm2string (str));
214 int i = gh_scm2int (s);
215 return (i>= -1 && i <= 1) ? SCM_BOOL_T : SCM_BOOL_F;
222 typedef void (*Void_fptr)();
223 Array<Void_fptr> *scm_init_funcs_;
225 void add_scm_init_func (void (*f)())
227 if (!scm_init_funcs_)
228 scm_init_funcs_ = new Array<Void_fptr>;
230 scm_init_funcs_->push (f);
236 for (int i=scm_init_funcs_->size() ; i--;)
237 (scm_init_funcs_->elem (i)) ();
240 unsigned int ly_scm_hash (SCM s)
242 return scm_ihashv (s, ~1u);
252 int i = gh_scm2int (s);
253 return i>= -1 && i <= 1;
261 return (Direction) gh_scm2int (s);
268 return gh_int2scm (i);
275 scm_to (SCM s, int* )
277 return gh_number_p (s) ? gh_scm2int (s) : 0;
283 return gh_double2scm (r);
287 scm_to (SCM s, Real* )
289 return gh_number_p (s) ? gh_scm2double (s) : 0;
295 return gh_boolean_p (s) && gh_scm2bool (s);
299 Appendable list L: the cdr contains the list, the car the last cons
305 SCM s = gh_cons (SCM_EOL, SCM_EOL);
312 appendable_list_append (SCM l, SCM elt)
314 SCM newcons = gh_cons (elt, SCM_EOL);
316 gh_set_cdr_x (gh_car (l), newcons);
317 gh_set_car_x (l, newcons);
324 return gh_cons (gh_double2scm (o[X_AXIS]), gh_double2scm(o[Y_AXIS]));
328 scm_to (SCM s, Offset*)
330 return Offset (gh_scm2double (gh_car (s)),
331 gh_scm2double (gh_cdr (s)));
337 char const * cp = "unknown";
338 if (gh_number_p (exp))
342 else if (gh_string_p (exp))
346 else if (gh_procedure_p (exp))
350 else if (gh_boolean_p (exp))
354 else if (gh_pair_p (exp))
359 return ly_str02scm (cp);
363 convert without too many decimals, and leave a space at the end.
368 ly_number2string (SCM s)
370 assert (gh_number_p (s));
372 char str[100]; // ugh.
374 if (scm_integer_p (s))
376 Real r (gh_scm2double (s));
378 if (isinf (r) || isnan (r))
380 programming_error ("Infinity or NaN encountered while converting Real number; setting to zero.");
384 sprintf (str, "%8.4f ", r);
388 sprintf (str, "%d ", gh_scm2int (s));
391 return gh_str02scm (str);
398 scm_make_gsubr ("ly-warn", 1, 0, 0, (SCM(*)(...))ly_warning);
399 scm_make_gsubr ("ly-gulp-file", 1,0, 0, (SCM(*)(...))ly_gulp_file);
400 scm_make_gsubr ("dir?", 1,0, 0, (SCM(*)(...))ly_isdir_p);
401 scm_make_gsubr ("ly-number->string", 1, 0,0, (SCM(*)(...)) ly_number2string);
404 ADD_SCM_INIT_FUNC(funcs, init_functions);