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