procs.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. return scm_from_bool (SCM_PROGRAM_P (obj)
  39. || (SCM_STRUCTP (obj) && SCM_STRUCT_APPLICABLE_P (obj))
  40. || (SCM_HAS_TYP7 (obj, scm_tc7_smob)
  41. && SCM_SMOB_APPLICABLE_P (obj)));
  42. }
  43. #undef FUNC_NAME
  44. SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
  45. (SCM obj),
  46. "Return @code{#t} if @var{obj} is a thunk.")
  47. #define FUNC_NAME s_scm_thunk_p
  48. {
  49. int req, opt, rest;
  50. return scm_from_bool (scm_i_procedure_arity (obj, &req, &opt, &rest)
  51. && req == 0);
  52. }
  53. #undef FUNC_NAME
  54. SCM_SYMBOL (sym_documentation, "documentation");
  55. SCM_DEFINE (scm_procedure_documentation, "procedure-documentation", 1, 0, 0,
  56. (SCM proc),
  57. "Return the documentation string associated with @code{proc}. By\n"
  58. "convention, if a procedure contains more than one expression and the\n"
  59. "first expression is a string constant, that string is assumed to contain\n"
  60. "documentation for that procedure.")
  61. #define FUNC_NAME s_scm_procedure_documentation
  62. {
  63. SCM_VALIDATE_PROC (SCM_ARG1, proc);
  64. return scm_procedure_property (proc, sym_documentation);
  65. }
  66. #undef FUNC_NAME
  67. /* Procedure-with-setter
  68. */
  69. static SCM pws_vtable;
  70. SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
  71. (SCM obj),
  72. "Return @code{#t} if @var{obj} is a procedure with an\n"
  73. "associated setter procedure.")
  74. #define FUNC_NAME s_scm_procedure_with_setter_p
  75. {
  76. return scm_from_bool (SCM_STRUCTP (obj) && SCM_STRUCT_SETTER_P (obj));
  77. }
  78. #undef FUNC_NAME
  79. SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
  80. (SCM procedure, SCM setter),
  81. "Create a new procedure which behaves like @var{procedure}, but\n"
  82. "with the associated setter @var{setter}.")
  83. #define FUNC_NAME s_scm_make_procedure_with_setter
  84. {
  85. SCM name, ret;
  86. SCM_VALIDATE_PROC (1, procedure);
  87. SCM_VALIDATE_PROC (2, setter);
  88. ret = scm_make_struct (pws_vtable, SCM_INUM0,
  89. scm_list_2 (procedure, setter));
  90. /* don't use procedure_name, because don't care enough to do a reverse
  91. lookup */
  92. name = scm_procedure_property (procedure, scm_sym_name);
  93. if (scm_is_true (name))
  94. scm_set_procedure_property_x (ret, scm_sym_name, name);
  95. return ret;
  96. }
  97. #undef FUNC_NAME
  98. SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
  99. (SCM proc),
  100. "Return the procedure of @var{proc}, which must be an\n"
  101. "applicable struct.")
  102. #define FUNC_NAME s_scm_procedure
  103. {
  104. SCM_ASSERT (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc),
  105. proc, SCM_ARG1, FUNC_NAME);
  106. return SCM_STRUCT_PROCEDURE (proc);
  107. }
  108. #undef FUNC_NAME
  109. SCM_PRIMITIVE_GENERIC (scm_setter, "setter", 1, 0, 0,
  110. (SCM proc),
  111. "Return the setter of @var{proc}, which must be an\n"
  112. "applicable struct with a setter.")
  113. #define FUNC_NAME s_scm_setter
  114. {
  115. if (SCM_UNLIKELY (!SCM_STRUCTP (proc)))
  116. return scm_wta_dispatch_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
  117. if (SCM_STRUCT_SETTER_P (proc))
  118. return SCM_STRUCT_SETTER (proc);
  119. if (SCM_PUREGENERICP (proc)
  120. && SCM_IS_A_P (proc, scm_class_generic_with_setter))
  121. /* FIXME: might not be an accessor */
  122. return SCM_GENERIC_SETTER (proc);
  123. return scm_wta_dispatch_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
  124. return SCM_BOOL_F; /* not reached */
  125. }
  126. #undef FUNC_NAME
  127. void
  128. scm_init_procs ()
  129. {
  130. pws_vtable =
  131. scm_c_make_struct (scm_applicable_struct_with_setter_vtable_vtable,
  132. 0,
  133. 1,
  134. SCM_UNPACK (scm_from_latin1_symbol ("pwpw")));
  135. #include "libguile/procs.x"
  136. }
  137. /*
  138. Local Variables:
  139. c-file-style: "gnu"
  140. End:
  141. */