procs.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* Copyright (C) 1995, 1996, 1997, 1999, 2000, 2002 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program 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
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. * Boston, MA 02111-1307 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include <stdio.h>
  42. #include "libguile/_scm.h"
  43. #include "libguile/objects.h"
  44. #include "libguile/strings.h"
  45. #include "libguile/vectors.h"
  46. #include "libguile/validate.h"
  47. #include "libguile/procs.h"
  48. /* {Procedures}
  49. */
  50. scm_subr_entry *scm_subr_table;
  51. /* libguile contained approx. 700 primitive procedures on 24 Aug 1999. */
  52. int scm_subr_table_size = 0;
  53. int scm_subr_table_room = 750;
  54. SCM
  55. scm_make_subr_opt (const char *name, int type, SCM (*fcn) (), int set)
  56. {
  57. SCM symcell;
  58. register SCM z;
  59. int entry;
  60. if (scm_subr_table_size == scm_subr_table_room)
  61. {
  62. scm_sizet new_size = scm_subr_table_room * 3 / 2;
  63. void *new_table
  64. = scm_must_realloc ((char *) scm_subr_table,
  65. sizeof (scm_subr_entry) * scm_subr_table_room,
  66. sizeof (scm_subr_entry) * new_size,
  67. "scm_subr_table");
  68. scm_subr_table = new_table;
  69. scm_subr_table_room = new_size;
  70. }
  71. SCM_NEWCELL (z);
  72. symcell = set ? scm_sysintern0 (name) : scm_intern0 (name);
  73. entry = scm_subr_table_size;
  74. scm_subr_table[entry].handle = z;
  75. scm_subr_table[entry].name = SCM_CAR (symcell);
  76. scm_subr_table[entry].generic = 0;
  77. scm_subr_table[entry].properties = SCM_EOL;
  78. scm_subr_table[entry].documentation = SCM_BOOL_F;
  79. SCM_SET_SUBRF (z, fcn);
  80. SCM_SET_CELL_TYPE (z, (entry << 8) + type);
  81. scm_subr_table_size++;
  82. if (set)
  83. SCM_SETCDR (symcell, z);
  84. return z;
  85. }
  86. /* This function isn't currently used since subrs are never freed. */
  87. /* *fixme* Need mutex here. */
  88. void
  89. scm_free_subr_entry (SCM subr)
  90. {
  91. int entry = SCM_SUBRNUM (subr);
  92. /* Move last entry in table to the free position */
  93. scm_subr_table[entry] = scm_subr_table[scm_subr_table_size - 1];
  94. SCM_SET_SUBRNUM (scm_subr_table[entry].handle, entry);
  95. scm_subr_table_size--;
  96. }
  97. SCM
  98. scm_make_subr (const char *name, int type, SCM (*fcn) ())
  99. {
  100. return scm_make_subr_opt (name, type, fcn, 1);
  101. }
  102. SCM
  103. scm_make_subr_with_generic (const char *name, int type, SCM (*fcn) (), SCM *gf)
  104. {
  105. SCM subr = scm_make_subr_opt (name, type, fcn, 1);
  106. scm_subr_table[scm_subr_table_size - 1].generic = gf;
  107. return subr;
  108. }
  109. void
  110. scm_mark_subr_table ()
  111. {
  112. int i;
  113. for (i = 0; i < scm_subr_table_size; ++i)
  114. {
  115. SCM_SETGC8MARK (scm_subr_table[i].name);
  116. if (scm_subr_table[i].generic && *scm_subr_table[i].generic)
  117. scm_gc_mark (*scm_subr_table[i].generic);
  118. if (SCM_NIMP (scm_subr_table[i].properties))
  119. scm_gc_mark (scm_subr_table[i].properties);
  120. if (SCM_NIMP (scm_subr_table[i].documentation))
  121. scm_gc_mark (scm_subr_table[i].documentation);
  122. }
  123. }
  124. #ifdef CCLO
  125. SCM
  126. scm_makcclo (SCM proc, long len)
  127. {
  128. SCM s;
  129. SCM_NEWCELL (s);
  130. SCM_DEFER_INTS;
  131. SCM_SETCHARS (s, scm_must_malloc (len * sizeof (SCM), "compiled-closure"));
  132. SCM_SETLENGTH (s, len, scm_tc7_cclo);
  133. while (--len)
  134. SCM_VELTS (s)[len] = SCM_UNSPECIFIED;
  135. SCM_CCLO_SUBR (s) = proc;
  136. SCM_ALLOW_INTS;
  137. return s;
  138. }
  139. /* Undocumented debugging procedure */
  140. #ifdef GUILE_DEBUG
  141. SCM_DEFINE (scm_make_cclo, "make-cclo", 2, 0, 0,
  142. (SCM proc, SCM len),
  143. "")
  144. #define FUNC_NAME s_scm_make_cclo
  145. {
  146. return scm_makcclo (proc, SCM_INUM (len));
  147. }
  148. #undef FUNC_NAME
  149. #endif
  150. #endif
  151. SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
  152. (SCM obj),
  153. "")
  154. #define FUNC_NAME s_scm_procedure_p
  155. {
  156. if (SCM_NIMP (obj))
  157. switch (SCM_TYP7 (obj))
  158. {
  159. case scm_tcs_cons_gloc:
  160. if (!SCM_I_OPERATORP (obj))
  161. break;
  162. case scm_tcs_closures:
  163. case scm_tc7_contin:
  164. case scm_tcs_subrs:
  165. #ifdef CCLO
  166. case scm_tc7_cclo:
  167. #endif
  168. case scm_tc7_pws:
  169. return SCM_BOOL_T;
  170. default:
  171. return SCM_BOOL_F;
  172. }
  173. return SCM_BOOL_F;
  174. }
  175. #undef FUNC_NAME
  176. SCM_DEFINE (scm_closure_p, "closure?", 1, 0, 0,
  177. (SCM obj),
  178. "")
  179. #define FUNC_NAME s_scm_closure_p
  180. {
  181. return SCM_BOOL(SCM_CLOSUREP (obj));
  182. }
  183. #undef FUNC_NAME
  184. SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
  185. (SCM obj),
  186. "")
  187. #define FUNC_NAME s_scm_thunk_p
  188. {
  189. if (SCM_NIMP (obj))
  190. {
  191. again:
  192. switch (SCM_TYP7 (obj))
  193. {
  194. case scm_tcs_closures:
  195. if (SCM_NULLP (SCM_CAR (SCM_CODE (obj))))
  196. return SCM_BOOL_T;
  197. case scm_tc7_subr_0:
  198. case scm_tc7_subr_1o:
  199. case scm_tc7_lsubr:
  200. case scm_tc7_rpsubr:
  201. case scm_tc7_asubr:
  202. #ifdef CCLO
  203. case scm_tc7_cclo:
  204. #endif
  205. return SCM_BOOL_T;
  206. case scm_tc7_pws:
  207. obj = SCM_PROCEDURE (obj);
  208. goto again;
  209. default:
  210. ;
  211. }
  212. }
  213. return SCM_BOOL_F;
  214. }
  215. #undef FUNC_NAME
  216. /* Only used internally. */
  217. int
  218. scm_subr_p (SCM obj)
  219. {
  220. if (SCM_NIMP (obj))
  221. switch (SCM_TYP7 (obj))
  222. {
  223. case scm_tcs_subrs:
  224. return 1;
  225. default:
  226. ;
  227. }
  228. return 0;
  229. }
  230. SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
  231. (SCM proc),
  232. "Return the documentation string associated with @code{proc}. By\n"
  233. "convention, if a procedure contains more than one expression and the\n"
  234. "first expression is a string constant, that string is assumed to contain\n"
  235. "documentation for that procedure.")
  236. #define FUNC_NAME s_scm_procedure_documentation
  237. {
  238. SCM code;
  239. SCM_ASSERT (SCM_EQ_P (scm_procedure_p (proc), SCM_BOOL_T)
  240. && SCM_NIMP (proc) && SCM_TYP7 (proc) != scm_tc7_contin,
  241. proc, SCM_ARG1, FUNC_NAME);
  242. switch (SCM_TYP7 (proc))
  243. {
  244. case scm_tcs_closures:
  245. code = SCM_CDR (SCM_CODE (proc));
  246. if (SCM_IMP (SCM_CDR (code)))
  247. return SCM_BOOL_F;
  248. code = SCM_CAR (code);
  249. if (SCM_IMP (code))
  250. return SCM_BOOL_F;
  251. if (SCM_STRINGP (code))
  252. return code;
  253. default:
  254. return SCM_BOOL_F;
  255. /*
  256. case scm_tcs_subrs:
  257. #ifdef CCLO
  258. case scm_tc7_cclo:
  259. #endif
  260. */
  261. }
  262. }
  263. #undef FUNC_NAME
  264. /* Procedure-with-setter
  265. */
  266. SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
  267. (SCM obj),
  268. "")
  269. #define FUNC_NAME s_scm_procedure_with_setter_p
  270. {
  271. return SCM_BOOL(SCM_PROCEDURE_WITH_SETTER_P (obj));
  272. }
  273. #undef FUNC_NAME
  274. SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
  275. (SCM procedure, SCM setter),
  276. "")
  277. #define FUNC_NAME s_scm_make_procedure_with_setter
  278. {
  279. SCM z;
  280. SCM_VALIDATE_PROC (1, procedure);
  281. SCM_VALIDATE_PROC (2, setter);
  282. SCM_NEWCELL2 (z);
  283. SCM_ENTER_A_SECTION;
  284. SCM_SET_CELL_OBJECT_1 (z, procedure);
  285. SCM_SET_CELL_OBJECT_2 (z, setter);
  286. SCM_SET_CELL_TYPE (z, scm_tc7_pws);
  287. SCM_EXIT_A_SECTION;
  288. return z;
  289. }
  290. #undef FUNC_NAME
  291. SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
  292. (SCM proc),
  293. "")
  294. #define FUNC_NAME s_scm_procedure
  295. {
  296. SCM_VALIDATE_NIM (1, proc);
  297. if (SCM_PROCEDURE_WITH_SETTER_P (proc))
  298. return SCM_PROCEDURE (proc);
  299. else if (SCM_STRUCTP (proc))
  300. {
  301. SCM_ASSERT (SCM_I_OPERATORP (proc), proc, SCM_ARG1, FUNC_NAME);
  302. return proc;
  303. }
  304. SCM_WRONG_TYPE_ARG (1, proc);
  305. return SCM_BOOL_F; /* not reached */
  306. }
  307. #undef FUNC_NAME
  308. SCM_GPROC (s_setter, "setter", 1, 0, 0, scm_setter, g_setter);
  309. SCM
  310. scm_setter (SCM proc)
  311. {
  312. SCM_GASSERT1 (SCM_NIMP (proc), g_setter, proc, SCM_ARG1, s_setter);
  313. if (SCM_PROCEDURE_WITH_SETTER_P (proc))
  314. return SCM_SETTER (proc);
  315. else if (SCM_STRUCTP (proc))
  316. {
  317. SCM setter;
  318. SCM_GASSERT1 (SCM_I_OPERATORP (proc),
  319. g_setter, proc, SCM_ARG1, s_setter);
  320. setter = (SCM_I_ENTITYP (proc)
  321. ? SCM_ENTITY_SETTER (proc)
  322. : SCM_OPERATOR_SETTER (proc));
  323. if (SCM_NIMP (setter))
  324. return setter;
  325. /* fall through */
  326. }
  327. SCM_WTA_DISPATCH_1 (g_setter, proc, SCM_ARG1, s_setter);
  328. return SCM_BOOL_F; /* not reached */
  329. }
  330. void
  331. scm_init_subr_table ()
  332. {
  333. scm_subr_table
  334. = ((scm_subr_entry *)
  335. scm_must_malloc (sizeof (scm_subr_entry) * scm_subr_table_room,
  336. "scm_subr_table"));
  337. }
  338. void
  339. scm_init_procs ()
  340. {
  341. #include "libguile/procs.x"
  342. }
  343. /*
  344. Local Variables:
  345. c-file-style: "gnu"
  346. End:
  347. */