procs.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Copyright 1995-1997,1999-2001,2006,2008-2013,2017-2018
  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 thunk.")
  46. #define FUNC_NAME s_scm_thunk_p
  47. {
  48. int req, opt, rest;
  49. return scm_from_bool (scm_i_procedure_arity (obj, &req, &opt, &rest)
  50. && req == 0);
  51. }
  52. #undef FUNC_NAME
  53. /* Procedure-with-setter
  54. */
  55. static SCM pws_vtable;
  56. SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
  57. (SCM obj),
  58. "Return @code{#t} if @var{obj} is a procedure with an\n"
  59. "associated setter procedure.")
  60. #define FUNC_NAME s_scm_procedure_with_setter_p
  61. {
  62. return scm_from_bool (SCM_STRUCTP (obj) && SCM_STRUCT_SETTER_P (obj));
  63. }
  64. #undef FUNC_NAME
  65. SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
  66. (SCM procedure, SCM setter),
  67. "Create a new procedure which behaves like @var{procedure}, but\n"
  68. "with the associated setter @var{setter}.")
  69. #define FUNC_NAME s_scm_make_procedure_with_setter
  70. {
  71. SCM_VALIDATE_PROC (1, procedure);
  72. SCM_VALIDATE_PROC (2, setter);
  73. return scm_make_struct_no_tail (pws_vtable, scm_list_2 (procedure, setter));
  74. }
  75. #undef FUNC_NAME
  76. SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
  77. (SCM proc),
  78. "Return the procedure of @var{proc}, which must be an\n"
  79. "applicable struct.")
  80. #define FUNC_NAME s_scm_procedure
  81. {
  82. SCM_ASSERT (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc),
  83. proc, SCM_ARG1, FUNC_NAME);
  84. return SCM_STRUCT_PROCEDURE (proc);
  85. }
  86. #undef FUNC_NAME
  87. SCM_PRIMITIVE_GENERIC (scm_setter, "setter", 1, 0, 0,
  88. (SCM proc),
  89. "Return the setter of @var{proc}, which must be an\n"
  90. "applicable struct with a setter.")
  91. #define FUNC_NAME s_scm_setter
  92. {
  93. if (SCM_UNLIKELY (!SCM_STRUCTP (proc)))
  94. return scm_wta_dispatch_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
  95. if (SCM_STRUCT_SETTER_P (proc))
  96. return SCM_STRUCT_SETTER (proc);
  97. return scm_wta_dispatch_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
  98. }
  99. #undef FUNC_NAME
  100. void
  101. scm_init_procs ()
  102. {
  103. pws_vtable =
  104. scm_c_make_struct (scm_applicable_struct_with_setter_vtable_vtable,
  105. 0,
  106. 1,
  107. SCM_UNPACK (scm_from_latin1_symbol ("pwpw")));
  108. #include "procs.x"
  109. }