procs.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* Copyright (C) 1995,1996,1997,1999,2000,2001, 2006, 2008 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public
  5. * License as published by the Free Software Foundation; either
  6. * version 2.1 of the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include "libguile/_scm.h"
  21. #include "libguile/objects.h"
  22. #include "libguile/strings.h"
  23. #include "libguile/vectors.h"
  24. #include "libguile/smob.h"
  25. #include "libguile/deprecation.h"
  26. #include "libguile/validate.h"
  27. #include "libguile/procs.h"
  28. /* {Procedures}
  29. */
  30. scm_t_subr_entry *scm_subr_table;
  31. /* libguile contained approx. 700 primitive procedures on 24 Aug 1999. */
  32. /* Increased to 800 on 2001-05-07 -- Guile now has 779 primitives on
  33. startup, 786 with guile-readline. 'martin */
  34. long scm_subr_table_size = 0;
  35. long scm_subr_table_room = 800;
  36. SCM
  37. scm_c_make_subr (const char *name, long type, SCM (*fcn) ())
  38. {
  39. register SCM z;
  40. long entry;
  41. if (scm_subr_table_size == scm_subr_table_room)
  42. {
  43. long new_size = scm_subr_table_room * 3 / 2;
  44. void *new_table
  45. = scm_realloc ((char *) scm_subr_table,
  46. sizeof (scm_t_subr_entry) * new_size);
  47. scm_subr_table = new_table;
  48. scm_subr_table_room = new_size;
  49. }
  50. entry = scm_subr_table_size;
  51. z = scm_cell ((entry << 8) + type, (scm_t_bits) fcn);
  52. scm_subr_table[entry].handle = z;
  53. scm_subr_table[entry].name = scm_from_locale_symbol (name);
  54. scm_subr_table[entry].generic = 0;
  55. scm_subr_table[entry].properties = SCM_EOL;
  56. scm_subr_table_size++;
  57. return z;
  58. }
  59. SCM
  60. scm_c_define_subr (const char *name, long type, SCM (*fcn) ())
  61. {
  62. SCM subr = scm_c_make_subr (name, type, fcn);
  63. scm_define (SCM_SUBR_ENTRY(subr).name, subr);
  64. return subr;
  65. }
  66. /* This function isn't currently used since subrs are never freed. */
  67. /* *fixme* Need mutex here. */
  68. void
  69. scm_free_subr_entry (SCM subr)
  70. {
  71. long entry = SCM_SUBRNUM (subr);
  72. /* Move last entry in table to the free position */
  73. scm_subr_table[entry] = scm_subr_table[scm_subr_table_size - 1];
  74. SCM_SET_SUBRNUM (scm_subr_table[entry].handle, entry);
  75. scm_subr_table_size--;
  76. }
  77. SCM
  78. scm_c_make_subr_with_generic (const char *name,
  79. long type, SCM (*fcn) (), SCM *gf)
  80. {
  81. SCM subr = scm_c_make_subr (name, type, fcn);
  82. SCM_SUBR_ENTRY(subr).generic = gf;
  83. return subr;
  84. }
  85. SCM
  86. scm_c_define_subr_with_generic (const char *name,
  87. long type, SCM (*fcn) (), SCM *gf)
  88. {
  89. SCM subr = scm_c_make_subr_with_generic (name, type, fcn, gf);
  90. scm_define (SCM_SUBR_ENTRY(subr).name, subr);
  91. return subr;
  92. }
  93. void
  94. scm_mark_subr_table ()
  95. {
  96. long i;
  97. for (i = 0; i < scm_subr_table_size; ++i)
  98. {
  99. scm_gc_mark (scm_subr_table[i].name);
  100. if (scm_subr_table[i].generic && *scm_subr_table[i].generic)
  101. scm_gc_mark (*scm_subr_table[i].generic);
  102. if (SCM_NIMP (scm_subr_table[i].properties))
  103. scm_gc_mark (scm_subr_table[i].properties);
  104. }
  105. }
  106. #ifdef CCLO
  107. SCM
  108. scm_makcclo (SCM proc, size_t len)
  109. {
  110. scm_t_bits *base = scm_gc_malloc (len * sizeof (scm_t_bits),
  111. "compiled closure");
  112. unsigned long i;
  113. SCM s;
  114. for (i = 0; i < len; ++i)
  115. base [i] = SCM_UNPACK (SCM_UNSPECIFIED);
  116. s = scm_cell (SCM_MAKE_CCLO_TAG (len), (scm_t_bits) base);
  117. SCM_SET_CCLO_SUBR (s, proc);
  118. return s;
  119. }
  120. /* Undocumented debugging procedure */
  121. #ifdef GUILE_DEBUG
  122. SCM_DEFINE (scm_make_cclo, "make-cclo", 2, 0, 0,
  123. (SCM proc, SCM len),
  124. "Create a compiled closure for @var{proc}, which reserves\n"
  125. "@var{len} objects for its usage.")
  126. #define FUNC_NAME s_scm_make_cclo
  127. {
  128. return scm_makcclo (proc, scm_to_size_t (len));
  129. }
  130. #undef FUNC_NAME
  131. #endif
  132. #endif
  133. SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
  134. (SCM obj),
  135. "Return @code{#t} if @var{obj} is a procedure.")
  136. #define FUNC_NAME s_scm_procedure_p
  137. {
  138. if (SCM_NIMP (obj))
  139. switch (SCM_TYP7 (obj))
  140. {
  141. case scm_tcs_struct:
  142. if (!SCM_I_OPERATORP (obj))
  143. break;
  144. case scm_tcs_closures:
  145. case scm_tcs_subrs:
  146. #ifdef CCLO
  147. case scm_tc7_cclo:
  148. #endif
  149. case scm_tc7_pws:
  150. return SCM_BOOL_T;
  151. case scm_tc7_smob:
  152. return scm_from_bool (SCM_SMOB_DESCRIPTOR (obj).apply);
  153. default:
  154. return SCM_BOOL_F;
  155. }
  156. return SCM_BOOL_F;
  157. }
  158. #undef FUNC_NAME
  159. SCM_DEFINE (scm_closure_p, "closure?", 1, 0, 0,
  160. (SCM obj),
  161. "Return @code{#t} if @var{obj} is a closure.")
  162. #define FUNC_NAME s_scm_closure_p
  163. {
  164. return scm_from_bool (SCM_CLOSUREP (obj));
  165. }
  166. #undef FUNC_NAME
  167. SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
  168. (SCM obj),
  169. "Return @code{#t} if @var{obj} is a thunk.")
  170. #define FUNC_NAME s_scm_thunk_p
  171. {
  172. if (SCM_NIMP (obj))
  173. {
  174. again:
  175. switch (SCM_TYP7 (obj))
  176. {
  177. case scm_tcs_closures:
  178. return scm_from_bool (!scm_is_pair (SCM_CLOSURE_FORMALS (obj)));
  179. case scm_tc7_subr_0:
  180. case scm_tc7_subr_1o:
  181. case scm_tc7_lsubr:
  182. case scm_tc7_rpsubr:
  183. case scm_tc7_asubr:
  184. #ifdef CCLO
  185. case scm_tc7_cclo:
  186. #endif
  187. return SCM_BOOL_T;
  188. case scm_tc7_pws:
  189. obj = SCM_PROCEDURE (obj);
  190. goto again;
  191. default:
  192. ;
  193. }
  194. }
  195. return SCM_BOOL_F;
  196. }
  197. #undef FUNC_NAME
  198. /* Only used internally. */
  199. int
  200. scm_subr_p (SCM obj)
  201. {
  202. if (SCM_NIMP (obj))
  203. switch (SCM_TYP7 (obj))
  204. {
  205. case scm_tcs_subrs:
  206. return 1;
  207. default:
  208. ;
  209. }
  210. return 0;
  211. }
  212. SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
  213. (SCM proc),
  214. "Return the documentation string associated with @code{proc}. By\n"
  215. "convention, if a procedure contains more than one expression and the\n"
  216. "first expression is a string constant, that string is assumed to contain\n"
  217. "documentation for that procedure.")
  218. #define FUNC_NAME s_scm_procedure_documentation
  219. {
  220. SCM code;
  221. SCM_ASSERT (scm_is_true (scm_procedure_p (proc)),
  222. proc, SCM_ARG1, FUNC_NAME);
  223. switch (SCM_TYP7 (proc))
  224. {
  225. case scm_tcs_closures:
  226. code = SCM_CLOSURE_BODY (proc);
  227. if (scm_is_null (SCM_CDR (code)))
  228. return SCM_BOOL_F;
  229. code = SCM_CAR (code);
  230. if (scm_is_string (code))
  231. return code;
  232. else
  233. return SCM_BOOL_F;
  234. default:
  235. return SCM_BOOL_F;
  236. /*
  237. case scm_tcs_subrs:
  238. #ifdef CCLO
  239. case scm_tc7_cclo:
  240. #endif
  241. */
  242. }
  243. }
  244. #undef FUNC_NAME
  245. /* Procedure-with-setter
  246. */
  247. SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
  248. (SCM obj),
  249. "Return @code{#t} if @var{obj} is a procedure with an\n"
  250. "associated setter procedure.")
  251. #define FUNC_NAME s_scm_procedure_with_setter_p
  252. {
  253. return scm_from_bool(SCM_PROCEDURE_WITH_SETTER_P (obj));
  254. }
  255. #undef FUNC_NAME
  256. SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
  257. (SCM procedure, SCM setter),
  258. "Create a new procedure which behaves like @var{procedure}, but\n"
  259. "with the associated setter @var{setter}.")
  260. #define FUNC_NAME s_scm_make_procedure_with_setter
  261. {
  262. SCM_VALIDATE_PROC (1, procedure);
  263. SCM_VALIDATE_PROC (2, setter);
  264. return scm_double_cell (scm_tc7_pws,
  265. SCM_UNPACK (procedure),
  266. SCM_UNPACK (setter), 0);
  267. }
  268. #undef FUNC_NAME
  269. SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
  270. (SCM proc),
  271. "Return the procedure of @var{proc}, which must be either a\n"
  272. "procedure with setter, or an operator struct.")
  273. #define FUNC_NAME s_scm_procedure
  274. {
  275. SCM_VALIDATE_NIM (1, proc);
  276. if (SCM_PROCEDURE_WITH_SETTER_P (proc))
  277. return SCM_PROCEDURE (proc);
  278. else if (SCM_STRUCTP (proc))
  279. {
  280. SCM_ASSERT (SCM_I_OPERATORP (proc), proc, SCM_ARG1, FUNC_NAME);
  281. return proc;
  282. }
  283. SCM_WRONG_TYPE_ARG (1, proc);
  284. return SCM_BOOL_F; /* not reached */
  285. }
  286. #undef FUNC_NAME
  287. SCM_GPROC (s_setter, "setter", 1, 0, 0, scm_setter, g_setter);
  288. SCM
  289. scm_setter (SCM proc)
  290. {
  291. SCM_GASSERT1 (SCM_NIMP (proc), g_setter, proc, SCM_ARG1, s_setter);
  292. if (SCM_PROCEDURE_WITH_SETTER_P (proc))
  293. return SCM_SETTER (proc);
  294. else if (SCM_STRUCTP (proc))
  295. {
  296. SCM setter;
  297. SCM_GASSERT1 (SCM_I_OPERATORP (proc),
  298. g_setter, proc, SCM_ARG1, s_setter);
  299. setter = (SCM_I_ENTITYP (proc)
  300. ? SCM_ENTITY_SETTER (proc)
  301. : SCM_OPERATOR_SETTER (proc));
  302. if (SCM_NIMP (setter))
  303. return setter;
  304. /* fall through */
  305. }
  306. SCM_WTA_DISPATCH_1 (g_setter, proc, SCM_ARG1, s_setter);
  307. return SCM_BOOL_F; /* not reached */
  308. }
  309. void
  310. scm_init_subr_table ()
  311. {
  312. scm_subr_table
  313. = ((scm_t_subr_entry *)
  314. scm_malloc (sizeof (scm_t_subr_entry) * scm_subr_table_room));
  315. }
  316. void
  317. scm_init_procs ()
  318. {
  319. #include "libguile/procs.x"
  320. }
  321. /*
  322. Local Variables:
  323. c-file-style: "gnu"
  324. End:
  325. */