gsubr.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /* Copyright (C) 1995,1996,1997,1998,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 <stdio.h>
  21. #include "libguile/_scm.h"
  22. #include "libguile/procprop.h"
  23. #include "libguile/root.h"
  24. #include "libguile/gsubr.h"
  25. #include "libguile/deprecation.h"
  26. /*
  27. * gsubr.c
  28. * Provide `gsubrs' -- subrs taking a prescribed number of required, optional,
  29. * and rest arguments.
  30. */
  31. /* #define GSUBR_TEST */
  32. SCM_GLOBAL_SYMBOL (scm_sym_name, "name");
  33. SCM scm_f_gsubr_apply;
  34. static SCM
  35. create_gsubr (int define, const char *name,
  36. int req, int opt, int rst, SCM (*fcn)())
  37. {
  38. SCM subr;
  39. switch (SCM_GSUBR_MAKTYPE (req, opt, rst))
  40. {
  41. case SCM_GSUBR_MAKTYPE(0, 0, 0):
  42. subr = scm_c_make_subr (name, scm_tc7_subr_0, fcn);
  43. goto create_subr;
  44. case SCM_GSUBR_MAKTYPE(1, 0, 0):
  45. subr = scm_c_make_subr (name, scm_tc7_subr_1, fcn);
  46. goto create_subr;
  47. case SCM_GSUBR_MAKTYPE(0, 1, 0):
  48. subr = scm_c_make_subr (name, scm_tc7_subr_1o, fcn);
  49. goto create_subr;
  50. case SCM_GSUBR_MAKTYPE(1, 1, 0):
  51. subr = scm_c_make_subr (name, scm_tc7_subr_2o, fcn);
  52. goto create_subr;
  53. case SCM_GSUBR_MAKTYPE(2, 0, 0):
  54. subr = scm_c_make_subr (name, scm_tc7_subr_2, fcn);
  55. goto create_subr;
  56. case SCM_GSUBR_MAKTYPE(3, 0, 0):
  57. subr = scm_c_make_subr (name, scm_tc7_subr_3, fcn);
  58. goto create_subr;
  59. case SCM_GSUBR_MAKTYPE(0, 0, 1):
  60. subr = scm_c_make_subr (name, scm_tc7_lsubr, fcn);
  61. goto create_subr;
  62. case SCM_GSUBR_MAKTYPE(2, 0, 1):
  63. subr = scm_c_make_subr (name, scm_tc7_lsubr_2, fcn);
  64. create_subr:
  65. if (define)
  66. scm_define (SCM_SUBR_ENTRY(subr).name, subr);
  67. return subr;
  68. default:
  69. {
  70. SCM cclo = scm_makcclo (scm_f_gsubr_apply, 3L);
  71. SCM subr = scm_c_make_subr (name, scm_tc7_subr_0, fcn);
  72. SCM sym = SCM_SUBR_ENTRY(subr).name;
  73. if (SCM_GSUBR_MAX < req + opt + rst)
  74. {
  75. fprintf (stderr,
  76. "ERROR in scm_c_make_gsubr: too many args (%d) to %s\n",
  77. req + opt + rst, name);
  78. exit (1);
  79. }
  80. SCM_SET_GSUBR_PROC (cclo, subr);
  81. SCM_SET_GSUBR_TYPE (cclo,
  82. scm_from_int (SCM_GSUBR_MAKTYPE (req, opt, rst)));
  83. if (SCM_REC_PROCNAMES_P)
  84. scm_set_procedure_property_x (cclo, scm_sym_name, sym);
  85. if (define)
  86. scm_define (sym, cclo);
  87. return cclo;
  88. }
  89. }
  90. }
  91. SCM
  92. scm_c_make_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)())
  93. {
  94. return create_gsubr (0, name, req, opt, rst, fcn);
  95. }
  96. SCM
  97. scm_c_define_gsubr (const char *name, int req, int opt, int rst, SCM (*fcn)())
  98. {
  99. return create_gsubr (1, name, req, opt, rst, fcn);
  100. }
  101. static SCM
  102. create_gsubr_with_generic (int define,
  103. const char *name,
  104. int req,
  105. int opt,
  106. int rst,
  107. SCM (*fcn)(),
  108. SCM *gf)
  109. {
  110. SCM subr;
  111. switch (SCM_GSUBR_MAKTYPE(req, opt, rst))
  112. {
  113. case SCM_GSUBR_MAKTYPE(0, 0, 0):
  114. subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_0, fcn, gf);
  115. goto create_subr;
  116. case SCM_GSUBR_MAKTYPE(1, 0, 0):
  117. subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_1, fcn, gf);
  118. goto create_subr;
  119. case SCM_GSUBR_MAKTYPE(0, 1, 0):
  120. subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_1o, fcn, gf);
  121. goto create_subr;
  122. case SCM_GSUBR_MAKTYPE(1, 1, 0):
  123. subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_2o, fcn, gf);
  124. goto create_subr;
  125. case SCM_GSUBR_MAKTYPE(2, 0, 0):
  126. subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_2, fcn, gf);
  127. goto create_subr;
  128. case SCM_GSUBR_MAKTYPE(3, 0, 0):
  129. subr = scm_c_make_subr_with_generic (name, scm_tc7_subr_3, fcn, gf);
  130. goto create_subr;
  131. case SCM_GSUBR_MAKTYPE(0, 0, 1):
  132. subr = scm_c_make_subr_with_generic (name, scm_tc7_lsubr, fcn, gf);
  133. goto create_subr;
  134. case SCM_GSUBR_MAKTYPE(2, 0, 1):
  135. subr = scm_c_make_subr_with_generic (name, scm_tc7_lsubr_2, fcn, gf);
  136. create_subr:
  137. if (define)
  138. scm_define (SCM_SUBR_ENTRY(subr).name, subr);
  139. return subr;
  140. default:
  141. ;
  142. }
  143. scm_misc_error ("scm_c_make_gsubr_with_generic",
  144. "can't make primitive-generic with this arity",
  145. SCM_EOL);
  146. return SCM_BOOL_F; /* never reached */
  147. }
  148. SCM
  149. scm_c_make_gsubr_with_generic (const char *name,
  150. int req,
  151. int opt,
  152. int rst,
  153. SCM (*fcn)(),
  154. SCM *gf)
  155. {
  156. return create_gsubr_with_generic (0, name, req, opt, rst, fcn, gf);
  157. }
  158. SCM
  159. scm_c_define_gsubr_with_generic (const char *name,
  160. int req,
  161. int opt,
  162. int rst,
  163. SCM (*fcn)(),
  164. SCM *gf)
  165. {
  166. return create_gsubr_with_generic (1, name, req, opt, rst, fcn, gf);
  167. }
  168. SCM
  169. scm_gsubr_apply (SCM args)
  170. #define FUNC_NAME "scm_gsubr_apply"
  171. {
  172. SCM self = SCM_CAR (args);
  173. SCM (*fcn)() = SCM_SUBRF (SCM_GSUBR_PROC (self));
  174. SCM v[SCM_GSUBR_MAX];
  175. int typ = scm_to_int (SCM_GSUBR_TYPE (self));
  176. long i, n = SCM_GSUBR_REQ (typ) + SCM_GSUBR_OPT (typ) + SCM_GSUBR_REST (typ);
  177. #if 0
  178. if (n > SCM_GSUBR_MAX)
  179. scm_misc_error (FUNC_NAME,
  180. "Function ~S has illegal arity ~S.",
  181. scm_list_2 (self, scm_from_int (n)));
  182. #endif
  183. args = SCM_CDR (args);
  184. for (i = 0; i < SCM_GSUBR_REQ (typ); i++) {
  185. if (scm_is_null (args))
  186. scm_wrong_num_args (SCM_SNAME (SCM_GSUBR_PROC (self)));
  187. v[i] = SCM_CAR(args);
  188. args = SCM_CDR(args);
  189. }
  190. for (; i < SCM_GSUBR_REQ (typ) + SCM_GSUBR_OPT (typ); i++) {
  191. if (SCM_NIMP (args)) {
  192. v[i] = SCM_CAR (args);
  193. args = SCM_CDR(args);
  194. }
  195. else
  196. v[i] = SCM_UNDEFINED;
  197. }
  198. if (SCM_GSUBR_REST(typ))
  199. v[i] = args;
  200. else if (!scm_is_null (args))
  201. scm_wrong_num_args (SCM_SNAME (SCM_GSUBR_PROC (self)));
  202. switch (n) {
  203. case 2: return (*fcn)(v[0], v[1]);
  204. case 3: return (*fcn)(v[0], v[1], v[2]);
  205. case 4: return (*fcn)(v[0], v[1], v[2], v[3]);
  206. case 5: return (*fcn)(v[0], v[1], v[2], v[3], v[4]);
  207. case 6: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5]);
  208. case 7: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6]);
  209. case 8: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]);
  210. case 9: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8]);
  211. case 10: return (*fcn)(v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]);
  212. }
  213. return SCM_BOOL_F; /* Never reached. */
  214. }
  215. #undef FUNC_NAME
  216. #ifdef GSUBR_TEST
  217. /* A silly example, taking 2 required args, 1 optional, and
  218. a scm_list of rest args
  219. */
  220. SCM
  221. gsubr_21l(SCM req1, SCM req2, SCM opt, SCM rst)
  222. {
  223. scm_puts ("gsubr-2-1-l:\n req1: ", scm_cur_outp);
  224. scm_display(req1, scm_cur_outp);
  225. scm_puts ("\n req2: ", scm_cur_outp);
  226. scm_display(req2, scm_cur_outp);
  227. scm_puts ("\n opt: ", scm_cur_outp);
  228. scm_display(opt, scm_cur_outp);
  229. scm_puts ("\n rest: ", scm_cur_outp);
  230. scm_display(rst, scm_cur_outp);
  231. scm_newline(scm_cur_outp);
  232. return SCM_UNSPECIFIED;
  233. }
  234. #endif
  235. void
  236. scm_init_gsubr()
  237. {
  238. scm_f_gsubr_apply = scm_c_make_subr ("gsubr-apply", scm_tc7_lsubr,
  239. scm_gsubr_apply);
  240. #ifdef GSUBR_TEST
  241. scm_c_define_gsubr ("gsubr-2-1-l", 2, 1, 1, gsubr_21l); /* example */
  242. #endif
  243. #include "libguile/gsubr.x"
  244. }
  245. /*
  246. Local Variables:
  247. c-file-style: "gnu"
  248. End:
  249. */