procs.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright (C) 1995, 1996, 1997, 1999, 2000, 2001, 2006, 2008, 2009,
  2. * 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include "libguile/_scm.h"
  23. #include "libguile/strings.h"
  24. #include "libguile/vectors.h"
  25. #include "libguile/smob.h"
  26. #include "libguile/deprecation.h"
  27. #include "libguile/validate.h"
  28. #include "libguile/procs.h"
  29. #include "libguile/procprop.h"
  30. #include "libguile/loader.h"
  31. #include "libguile/programs.h"
  32. /* {Procedures}
  33. */
  34. SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
  35. (SCM obj),
  36. "Return @code{#t} if @var{obj} is a procedure.")
  37. #define FUNC_NAME s_scm_procedure_p
  38. {
  39. return scm_from_bool (SCM_PROGRAM_P (obj)
  40. || (SCM_STRUCTP (obj) && SCM_STRUCT_APPLICABLE_P (obj))
  41. || (SCM_HAS_TYP7 (obj, scm_tc7_smob)
  42. && SCM_SMOB_APPLICABLE_P (obj)));
  43. }
  44. #undef FUNC_NAME
  45. SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
  46. (SCM obj),
  47. "Return @code{#t} if @var{obj} is a thunk.")
  48. #define FUNC_NAME s_scm_thunk_p
  49. {
  50. int req, opt, rest;
  51. return scm_from_bool (scm_i_procedure_arity (obj, &req, &opt, &rest)
  52. && req == 0);
  53. }
  54. #undef FUNC_NAME
  55. /* Procedure-with-setter
  56. */
  57. static SCM pws_vtable;
  58. SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
  59. (SCM obj),
  60. "Return @code{#t} if @var{obj} is a procedure with an\n"
  61. "associated setter procedure.")
  62. #define FUNC_NAME s_scm_procedure_with_setter_p
  63. {
  64. return scm_from_bool (SCM_STRUCTP (obj) && SCM_STRUCT_SETTER_P (obj));
  65. }
  66. #undef FUNC_NAME
  67. SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
  68. (SCM procedure, SCM setter),
  69. "Create a new procedure which behaves like @var{procedure}, but\n"
  70. "with the associated setter @var{setter}.")
  71. #define FUNC_NAME s_scm_make_procedure_with_setter
  72. {
  73. SCM_VALIDATE_PROC (1, procedure);
  74. SCM_VALIDATE_PROC (2, setter);
  75. return scm_make_struct (pws_vtable, SCM_INUM0,
  76. scm_list_2 (procedure, setter));
  77. }
  78. #undef FUNC_NAME
  79. SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
  80. (SCM proc),
  81. "Return the procedure of @var{proc}, which must be an\n"
  82. "applicable struct.")
  83. #define FUNC_NAME s_scm_procedure
  84. {
  85. SCM_ASSERT (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc),
  86. proc, SCM_ARG1, FUNC_NAME);
  87. return SCM_STRUCT_PROCEDURE (proc);
  88. }
  89. #undef FUNC_NAME
  90. SCM_PRIMITIVE_GENERIC (scm_setter, "setter", 1, 0, 0,
  91. (SCM proc),
  92. "Return the setter of @var{proc}, which must be an\n"
  93. "applicable struct with a setter.")
  94. #define FUNC_NAME s_scm_setter
  95. {
  96. if (SCM_UNLIKELY (!SCM_STRUCTP (proc)))
  97. return scm_wta_dispatch_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
  98. if (SCM_STRUCT_SETTER_P (proc))
  99. return SCM_STRUCT_SETTER (proc);
  100. return scm_wta_dispatch_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
  101. }
  102. #undef FUNC_NAME
  103. void
  104. scm_init_procs ()
  105. {
  106. pws_vtable =
  107. scm_c_make_struct (scm_applicable_struct_with_setter_vtable_vtable,
  108. 0,
  109. 1,
  110. SCM_UNPACK (scm_from_latin1_symbol ("pwpw")));
  111. #include "libguile/procs.x"
  112. }
  113. /*
  114. Local Variables:
  115. c-file-style: "gnu"
  116. End:
  117. */