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>
15 #include "libc-extension.hh"
16 #include "lily-guile.hh"
18 #include "simple-file-storage.hh"
19 #include "file-path.hh"
21 #include "direction.hh"
25 ly_str02scm (char const*c
)
27 // this all really sucks, guile should take char const* arguments!
28 return gh_str02scm ((char*)c
);
32 ly_eval_str (String s
)
34 // this all really sucks, guile should take char const* arguments!
35 return gh_eval_str ((char*)s
.ch_C ());
40 Pass string to scm parser, evaluate one expression.
41 Return result value and #chars read.
43 Thanks to Gary Houston <ghouston@freewire.co.uk>
45 Need guile-1.3.4 (>1.3 anyway) for ftell on str ports -- jcn
48 ly_parse_scm (char const* s
, int* n
)
50 SCM str
= gh_str02scm ((char*)s
);
51 SCM port
= scm_mkstrport (SCM_INUM0
, str
, SCM_OPN
| SCM_RDNG
,
53 SCM from
= scm_ftell (port
);
56 SCM answer
= SCM_UNSPECIFIED
;
58 /* Read expression from port */
59 if (!SCM_EOF_OBJECT_P (form
= scm_read (port
)))
60 answer
= scm_eval_x (form
);
67 all seems fine, but after parsing
71 read_buf has been advanced to read_pos - 1,
72 so that scm_ftell returns 1, instead of #parsed chars
76 urg: reset read_buf for scm_ftell
77 shouldn't scm_read () do this for us?
79 scm_fill_input (port
);
80 SCM to
= scm_ftell (port
);
81 *n
= gh_scm2int (to
) - gh_scm2int (from
);
83 /* Don't close the port here; if we re-enter this function via a
84 continuation, then the next time we enter it, we'll get an error.
85 It's a string port anyway, so there's no advantage to closing it
88 scm_close_port (port);
97 return gh_list (ly_symbol2scm ("quote"), s
, SCM_UNDEFINED
);
102 ly_symbol2scm(const char *s
)
104 return gh_symbol2scm ((char *)s
);
108 ly_symbol2string (SCM s
)
110 assert (gh_symbol_p (s
));
111 return String((Byte
*)SCM_CHARS (s
), (int) SCM_LENGTH(s
));
116 Read a file, and shove it down GUILE. GUILE also has file read
117 functions, but you can't fiddle with the path of those.
120 read_lily_scm_file (String fn
)
122 String s
= global_path
.find (fn
);
125 String e
= _f ("Can't find file: `%s'", fn
);
127 e
+= _f ("(load path: `%s')", global_path
.str ());
131 progress_indication ("[" + s
);
134 Simple_file_storage
f(s
);
136 ly_eval_str ((char *) f
.ch_C());
137 progress_indication ("]");
142 ly_gulp_file (SCM name
)
144 String
fn (ly_scm2string (name
));
145 String s
= global_path
.find (fn
);
148 String e
= _f ("Can't find file: `%s'", fn
);
150 e
+= _f ("(load path: `%s')", global_path
.str ());
154 progress_indication ("[" + s
);
157 Simple_file_storage
f(s
);
158 SCM result
= ly_str02scm (f
.ch_C());
159 progress_indication ("]");
164 ly_display_scm (SCM s
)
171 ly_scm2string (SCM s
)
173 assert (gh_string_p (s
));
175 char * p
= gh_scm2newstr (s
, &len
);
184 index_cell (SCM s
, Direction d
)
187 return (d
== LEFT
) ? gh_car (s
) : gh_cdr (s
);
191 index_set_cell (SCM s
, Direction d
, SCM v
)
203 assert (gh_string_p (str
));
204 warning ("lily-guile: " + ly_scm2string (str
));
213 int i
= gh_scm2int (s
);
214 return (i
>= -1 && i
<= 1) ? SCM_BOOL_T
: SCM_BOOL_F
;
223 scm_make_gsubr ("ly-warn", 1, 0, 0, (SCM(*)(...))ly_warning
);
224 scm_make_gsubr ("ly-gulp-file", 1,0, 0, (SCM(*)(...))ly_gulp_file
);
225 scm_make_gsubr ("dir?", 1,0, 0, (SCM(*)(...))ly_isdir_p
);
228 ADD_SCM_INIT_FUNC(funcs
, init_functions
);
231 typedef void (*Void_fptr
)();
232 Array
<Void_fptr
> *scm_init_funcs_
;
234 void add_scm_init_func (void (*f
)())
236 if (!scm_init_funcs_
)
237 scm_init_funcs_
= new Array
<Void_fptr
>;
239 scm_init_funcs_
->push (f
);
245 for (int i
=scm_init_funcs_
->size() ; i
--;)
246 (scm_init_funcs_
->elem (i
)) ();
249 unsigned int ly_scm_hash (SCM s
)
251 return scm_ihashv (s
, ~1u);
261 int i
= gh_scm2int (s
);
262 return i
>= -1 && i
<= 1;
270 return (Direction
) gh_scm2int (s
);
277 return gh_int2scm (i
);
284 scm_to (SCM s
, int* )
286 return gh_number_p (s
) ? gh_scm2int (s
) : 0;
292 return gh_double2scm (r
);
296 scm_to (SCM s
, Real
* )
298 return gh_number_p (s
) ? gh_scm2double (s
) : 0;
304 return gh_boolean_p (s
) && gh_scm2bool (s
);
308 Appendable list L: the cdr contains the list, the car the last cons
314 SCM s
= gh_cons (SCM_EOL
, SCM_EOL
);
321 appendable_list_append (SCM l
, SCM elt
)
323 SCM newcons
= gh_cons (elt
, SCM_EOL
);
325 gh_set_cdr_x (gh_car (l
), newcons
);
326 gh_set_car_x (l
, newcons
);
333 return gh_cons (gh_double2scm (o
[X_AXIS
]), gh_double2scm(o
[Y_AXIS
]));
337 scm_to (SCM s
, Offset
*)
339 return Offset (gh_scm2double (gh_car (s
)),
340 gh_scm2double (gh_cdr (s
)));
346 char const * cp
= "unknown";
347 if (gh_number_p (exp
))
351 else if (gh_string_p (exp
))
355 else if (gh_procedure_p (exp
))
359 else if (gh_boolean_p (exp
))
363 else if (gh_pair_p (exp
))
368 return ly_str02scm (cp
);