procprop.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* Copyright (C) 1995,1996,1998, 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/alist.h"
  44. #include "libguile/eval.h"
  45. #include "libguile/procs.h"
  46. #include "libguile/gsubr.h"
  47. #include "libguile/objects.h"
  48. #include "libguile/root.h"
  49. #include "libguile/vectors.h"
  50. #include "libguile/validate.h"
  51. #include "libguile/procprop.h"
  52. SCM_GLOBAL_SYMBOL (scm_sym_system_procedure, "system-procedure");
  53. SCM_GLOBAL_SYMBOL (scm_sym_arity, "arity");
  54. SCM
  55. scm_i_procedure_arity (SCM proc)
  56. {
  57. int a = 0, o = 0, r = 0;
  58. if (SCM_IMP (proc))
  59. return SCM_BOOL_F;
  60. loop:
  61. switch (SCM_TYP7 (proc))
  62. {
  63. case scm_tc7_subr_1o:
  64. o = 1;
  65. case scm_tc7_subr_0:
  66. break;
  67. case scm_tc7_subr_2o:
  68. o = 1;
  69. case scm_tc7_subr_1:
  70. case scm_tc7_cxr:
  71. case scm_tc7_contin:
  72. a += 1;
  73. break;
  74. case scm_tc7_subr_2:
  75. a += 2;
  76. break;
  77. case scm_tc7_subr_3:
  78. a += 3;
  79. break;
  80. case scm_tc7_asubr:
  81. case scm_tc7_rpsubr:
  82. case scm_tc7_lsubr:
  83. r = 1;
  84. break;
  85. case scm_tc7_lsubr_2:
  86. a += 2;
  87. r = 1;
  88. break;
  89. #ifdef CCLO
  90. case scm_tc7_cclo:
  91. if (SCM_EQ_P (SCM_CCLO_SUBR (proc), scm_f_gsubr_apply))
  92. {
  93. int type = SCM_INUM (SCM_GSUBR_TYPE (proc));
  94. a += SCM_GSUBR_REQ (type);
  95. o = SCM_GSUBR_OPT (type);
  96. r = SCM_GSUBR_REST (type);
  97. break;
  98. }
  99. proc = SCM_CCLO_SUBR (proc);
  100. a -= 1;
  101. goto loop;
  102. #endif
  103. case scm_tc7_pws:
  104. proc = SCM_PROCEDURE (proc);
  105. goto loop;
  106. case scm_tcs_closures:
  107. proc = SCM_CAR (SCM_CODE (proc));
  108. if (SCM_IMP (proc))
  109. break;
  110. while (SCM_CONSP (proc))
  111. {
  112. ++a;
  113. proc = SCM_CDR (proc);
  114. }
  115. if (SCM_NIMP (proc))
  116. r = 1;
  117. break;
  118. case scm_tcs_cons_gloc:
  119. if (SCM_OBJ_CLASS_FLAGS (proc) & SCM_CLASSF_PURE_GENERIC)
  120. {
  121. r = 1;
  122. break;
  123. }
  124. else if (!SCM_I_OPERATORP (proc))
  125. return SCM_BOOL_F;
  126. proc = (SCM_I_ENTITYP (proc)
  127. ? SCM_ENTITY_PROCEDURE (proc)
  128. : SCM_OPERATOR_PROCEDURE (proc));
  129. a -= 1;
  130. goto loop;
  131. default:
  132. return SCM_BOOL_F;
  133. }
  134. return SCM_LIST3 (SCM_MAKINUM (a),
  135. SCM_MAKINUM (o),
  136. SCM_BOOL(r));
  137. }
  138. static SCM
  139. scm_stand_in_scm_proc(SCM proc)
  140. {
  141. SCM answer;
  142. answer = scm_assoc (proc, scm_stand_in_procs);
  143. if (SCM_FALSEP (answer))
  144. {
  145. answer = scm_closure (scm_listify (SCM_EOL, SCM_BOOL_F, SCM_UNDEFINED),
  146. SCM_EOL);
  147. scm_stand_in_procs = scm_cons (scm_cons (proc, answer),
  148. scm_stand_in_procs);
  149. }
  150. else
  151. answer = SCM_CDR (answer);
  152. return answer;
  153. }
  154. SCM_DEFINE (scm_procedure_properties, "procedure-properties", 1, 0, 0,
  155. (SCM proc),
  156. "Return @var{obj}'s property list.")
  157. #define FUNC_NAME s_scm_procedure_properties
  158. {
  159. SCM_VALIDATE_PROC (1,proc);
  160. return scm_acons (scm_sym_arity, scm_i_procedure_arity (proc),
  161. SCM_PROCPROPS (SCM_CLOSUREP (proc)
  162. ? proc
  163. : scm_stand_in_scm_proc (proc)));
  164. }
  165. #undef FUNC_NAME
  166. SCM_DEFINE (scm_set_procedure_properties_x, "set-procedure-properties!", 2, 0, 0,
  167. (SCM proc, SCM new_val),
  168. "Set @var{obj}'s property list to @var{alist}.")
  169. #define FUNC_NAME s_scm_set_procedure_properties_x
  170. {
  171. if (!SCM_CLOSUREP (proc))
  172. proc = scm_stand_in_scm_proc(proc);
  173. SCM_VALIDATE_CLOSURE (1,proc);
  174. SCM_SETPROCPROPS (proc, new_val);
  175. return SCM_UNSPECIFIED;
  176. }
  177. #undef FUNC_NAME
  178. SCM_DEFINE (scm_procedure_property, "procedure-property", 2, 0, 0,
  179. (SCM p, SCM k),
  180. "Return the property of @var{obj} with name @var{key}.")
  181. #define FUNC_NAME s_scm_procedure_property
  182. {
  183. SCM assoc;
  184. if (SCM_EQ_P (k, scm_sym_arity))
  185. {
  186. SCM arity;
  187. SCM_ASSERT (SCM_NFALSEP (arity = scm_i_procedure_arity (p)),
  188. p, SCM_ARG1, FUNC_NAME);
  189. return arity;
  190. }
  191. SCM_VALIDATE_PROC (1,p);
  192. assoc = scm_sloppy_assq (k,
  193. SCM_PROCPROPS (SCM_CLOSUREP (p)
  194. ? p
  195. : scm_stand_in_scm_proc (p)));
  196. return (SCM_NIMP (assoc) ? SCM_CDR (assoc) : SCM_BOOL_F);
  197. }
  198. #undef FUNC_NAME
  199. SCM_DEFINE (scm_set_procedure_property_x, "set-procedure-property!", 3, 0, 0,
  200. (SCM p, SCM k, SCM v),
  201. "In @var{obj}'s property list, set the property named @var{key} to\n"
  202. "@var{value}.")
  203. #define FUNC_NAME s_scm_set_procedure_property_x
  204. {
  205. SCM assoc;
  206. if (!SCM_CLOSUREP (p))
  207. p = scm_stand_in_scm_proc(p);
  208. SCM_VALIDATE_CLOSURE (1,p);
  209. if (SCM_EQ_P (k, scm_sym_arity))
  210. SCM_MISC_ERROR ("arity is a read-only property", SCM_EOL);
  211. assoc = scm_sloppy_assq (k, SCM_PROCPROPS (p));
  212. if (SCM_NIMP (assoc))
  213. SCM_SETCDR (assoc, v);
  214. else
  215. SCM_SETPROCPROPS (p, scm_acons (k, v, SCM_PROCPROPS (p)));
  216. return SCM_UNSPECIFIED;
  217. }
  218. #undef FUNC_NAME
  219. void
  220. scm_init_procprop ()
  221. {
  222. #include "libguile/procprop.x"
  223. }
  224. /*
  225. Local Variables:
  226. c-file-style: "gnu"
  227. End:
  228. */