procs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* Copyright 1995-1997,1999-2001,2006,2008-2013,2017-2018,2020
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifdef HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #include "deprecation.h"
  19. #include "goops.h"
  20. #include "gsubr.h"
  21. #include "loader.h"
  22. #include "procprop.h"
  23. #include "programs.h"
  24. #include "smob.h"
  25. #include "strings.h"
  26. #include "struct.h"
  27. #include "symbols.h"
  28. #include "vectors.h"
  29. #include "procs.h"
  30. /* {Procedures}
  31. */
  32. SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
  33. (SCM obj),
  34. "Return @code{#t} if @var{obj} is a procedure.")
  35. #define FUNC_NAME s_scm_procedure_p
  36. {
  37. return scm_from_bool (SCM_PROGRAM_P (obj)
  38. || (SCM_STRUCTP (obj) && SCM_STRUCT_APPLICABLE_P (obj))
  39. || (SCM_HAS_TYP7 (obj, scm_tc7_smob)
  40. && SCM_SMOB_APPLICABLE_P (obj)));
  41. }
  42. #undef FUNC_NAME
  43. SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
  44. (SCM obj),
  45. "Return @code{#t} if @var{obj} is a procedure that can be "
  46. "called with zero arguments.")
  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. /* Procedure-with-setter
  55. */
  56. static SCM pws_vtable;
  57. SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
  58. (SCM obj),
  59. "Return @code{#t} if @var{obj} is a procedure with an\n"
  60. "associated setter procedure.")
  61. #define FUNC_NAME s_scm_procedure_with_setter_p
  62. {
  63. return scm_from_bool (SCM_STRUCTP (obj) && SCM_STRUCT_SETTER_P (obj));
  64. }
  65. #undef FUNC_NAME
  66. SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
  67. (SCM procedure, SCM setter),
  68. "Create a new procedure which behaves like @var{procedure}, but\n"
  69. "with the associated setter @var{setter}.")
  70. #define FUNC_NAME s_scm_make_procedure_with_setter
  71. {
  72. SCM_VALIDATE_PROC (1, procedure);
  73. SCM_VALIDATE_PROC (2, setter);
  74. return scm_make_struct_no_tail (pws_vtable, scm_list_2 (procedure, setter));
  75. }
  76. #undef FUNC_NAME
  77. SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
  78. (SCM proc),
  79. "Return the procedure of @var{proc}, which must be an\n"
  80. "applicable struct.")
  81. #define FUNC_NAME s_scm_procedure
  82. {
  83. SCM_ASSERT (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc),
  84. proc, SCM_ARG1, FUNC_NAME);
  85. return SCM_STRUCT_PROCEDURE (proc);
  86. }
  87. #undef FUNC_NAME
  88. SCM_PRIMITIVE_GENERIC (scm_setter, "setter", 1, 0, 0,
  89. (SCM proc),
  90. "Return the setter of @var{proc}, which must be an\n"
  91. "applicable struct with a setter.")
  92. #define FUNC_NAME s_scm_setter
  93. {
  94. if (SCM_UNLIKELY (!SCM_STRUCTP (proc)))
  95. return scm_wta_dispatch_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
  96. if (SCM_STRUCT_SETTER_P (proc))
  97. return SCM_STRUCT_SETTER (proc);
  98. return scm_wta_dispatch_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
  99. }
  100. #undef FUNC_NAME
  101. void
  102. scm_init_procs ()
  103. {
  104. pws_vtable =
  105. scm_c_make_struct (scm_applicable_struct_with_setter_vtable_vtable,
  106. 0,
  107. 1,
  108. SCM_UNPACK (scm_from_latin1_symbol ("pwpw")));
  109. #include "procs.x"
  110. }