procs.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* Copyright (C) 1995,1996,1997,1999,2000,2001, 2006, 2008, 2009, 2010, 2011 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 License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * 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
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.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. #include "libguile/procprop.h"
  29. #include "libguile/objcodes.h"
  30. #include "libguile/programs.h"
  31. /* {Procedures}
  32. */
  33. SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
  34. (SCM obj),
  35. "Return @code{#t} if @var{obj} is a procedure.")
  36. #define FUNC_NAME s_scm_procedure_p
  37. {
  38. if (SCM_NIMP (obj))
  39. switch (SCM_TYP7 (obj))
  40. {
  41. case scm_tcs_struct:
  42. if (!((SCM_OBJ_CLASS_FLAGS (obj) & SCM_CLASSF_PURE_GENERIC)
  43. || SCM_STRUCT_APPLICABLE_P (obj)))
  44. break;
  45. case scm_tc7_program:
  46. return SCM_BOOL_T;
  47. case scm_tc7_smob:
  48. return scm_from_bool (SCM_SMOB_DESCRIPTOR (obj).apply);
  49. default:
  50. return SCM_BOOL_F;
  51. }
  52. return SCM_BOOL_F;
  53. }
  54. #undef FUNC_NAME
  55. SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
  56. (SCM obj),
  57. "Return @code{#t} if @var{obj} is a thunk.")
  58. #define FUNC_NAME s_scm_thunk_p
  59. {
  60. int req, opt, rest;
  61. return scm_from_bool (scm_i_procedure_arity (obj, &req, &opt, &rest)
  62. && req == 0);
  63. }
  64. #undef FUNC_NAME
  65. SCM_SYMBOL (sym_documentation, "documentation");
  66. SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
  67. (SCM proc),
  68. "Return the documentation string associated with @code{proc}. By\n"
  69. "convention, if a procedure contains more than one expression and the\n"
  70. "first expression is a string constant, that string is assumed to contain\n"
  71. "documentation for that procedure.")
  72. #define FUNC_NAME s_scm_procedure_documentation
  73. {
  74. SCM_VALIDATE_PROC (SCM_ARG1, proc);
  75. return scm_procedure_property (proc, sym_documentation);
  76. }
  77. #undef FUNC_NAME
  78. /* Procedure-with-setter
  79. */
  80. static SCM pws_vtable;
  81. SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
  82. (SCM obj),
  83. "Return @code{#t} if @var{obj} is a procedure with an\n"
  84. "associated setter procedure.")
  85. #define FUNC_NAME s_scm_procedure_with_setter_p
  86. {
  87. return scm_from_bool (SCM_STRUCTP (obj) && SCM_STRUCT_SETTER_P (obj));
  88. }
  89. #undef FUNC_NAME
  90. SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
  91. (SCM procedure, SCM setter),
  92. "Create a new procedure which behaves like @var{procedure}, but\n"
  93. "with the associated setter @var{setter}.")
  94. #define FUNC_NAME s_scm_make_procedure_with_setter
  95. {
  96. SCM name, ret;
  97. SCM_VALIDATE_PROC (1, procedure);
  98. SCM_VALIDATE_PROC (2, setter);
  99. ret = scm_make_struct (pws_vtable, SCM_INUM0,
  100. scm_list_2 (procedure, setter));
  101. /* don't use procedure_name, because don't care enough to do a reverse
  102. lookup */
  103. name = scm_procedure_property (procedure, scm_sym_name);
  104. if (scm_is_true (name))
  105. scm_set_procedure_property_x (ret, scm_sym_name, name);
  106. return ret;
  107. }
  108. #undef FUNC_NAME
  109. SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
  110. (SCM proc),
  111. "Return the procedure of @var{proc}, which must be an\n"
  112. "applicable struct.")
  113. #define FUNC_NAME s_scm_procedure
  114. {
  115. SCM_ASSERT (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc),
  116. proc, SCM_ARG1, FUNC_NAME);
  117. return SCM_STRUCT_PROCEDURE (proc);
  118. }
  119. #undef FUNC_NAME
  120. SCM_PRIMITIVE_GENERIC (scm_setter, "setter", 1, 0, 0,
  121. (SCM proc),
  122. "Return the setter of @var{proc}, which must be an\n"
  123. "applicable struct with a setter.")
  124. #define FUNC_NAME s_scm_setter
  125. {
  126. SCM_GASSERT1 (SCM_STRUCTP (proc), g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
  127. if (SCM_STRUCT_SETTER_P (proc))
  128. return SCM_STRUCT_SETTER (proc);
  129. if (SCM_PUREGENERICP (proc)
  130. && SCM_IS_A_P (proc, scm_class_generic_with_setter))
  131. /* FIXME: might not be an accessor */
  132. return SCM_GENERIC_SETTER (proc);
  133. SCM_WTA_DISPATCH_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
  134. return SCM_BOOL_F; /* not reached */
  135. }
  136. #undef FUNC_NAME
  137. void
  138. scm_init_procs ()
  139. {
  140. pws_vtable =
  141. scm_c_make_struct (scm_applicable_struct_with_setter_vtable_vtable,
  142. 0,
  143. 1,
  144. SCM_UNPACK (scm_from_latin1_symbol ("pwpw")));
  145. #include "libguile/procs.x"
  146. }
  147. /*
  148. Local Variables:
  149. c-file-style: "gnu"
  150. End:
  151. */