8d9ef15b4f2cd465b04ee8d24e2215cfb2a2dacf
[bpt/guile.git] / libguile / procs.c
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
20
21 \f
22 #ifdef HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include "libguile/_scm.h"
27
28 #include "libguile/strings.h"
29 #include "libguile/vectors.h"
30 #include "libguile/smob.h"
31 #include "libguile/deprecation.h"
32
33 #include "libguile/validate.h"
34 #include "libguile/procs.h"
35 #include "libguile/procprop.h"
36 #include "libguile/objcodes.h"
37 #include "libguile/programs.h"
38 \f
39
40
41 /* {Procedures}
42 */
43
44
45 SCM_DEFINE (scm_procedure_p, "procedure?", 1, 0, 0,
46 (SCM obj),
47 "Return @code{#t} if @var{obj} is a procedure.")
48 #define FUNC_NAME s_scm_procedure_p
49 {
50 return scm_from_bool (SCM_PROGRAM_P (obj)
51 || SCM_RTL_PROGRAM_P (obj)
52 || (SCM_STRUCTP (obj) && SCM_STRUCT_APPLICABLE_P (obj))
53 || (SCM_HAS_TYP7 (obj, scm_tc7_smob)
54 && SCM_SMOB_APPLICABLE_P (obj)));
55 }
56 #undef FUNC_NAME
57
58 SCM_DEFINE (scm_thunk_p, "thunk?", 1, 0, 0,
59 (SCM obj),
60 "Return @code{#t} if @var{obj} is a thunk.")
61 #define FUNC_NAME s_scm_thunk_p
62 {
63 int req, opt, rest;
64 return scm_from_bool (scm_i_procedure_arity (obj, &req, &opt, &rest)
65 && req == 0);
66 }
67 #undef FUNC_NAME
68
69
70 /* Procedure-with-setter
71 */
72
73 static SCM pws_vtable;
74
75
76 SCM_DEFINE (scm_procedure_with_setter_p, "procedure-with-setter?", 1, 0, 0,
77 (SCM obj),
78 "Return @code{#t} if @var{obj} is a procedure with an\n"
79 "associated setter procedure.")
80 #define FUNC_NAME s_scm_procedure_with_setter_p
81 {
82 return scm_from_bool (SCM_STRUCTP (obj) && SCM_STRUCT_SETTER_P (obj));
83 }
84 #undef FUNC_NAME
85
86 SCM_DEFINE (scm_make_procedure_with_setter, "make-procedure-with-setter", 2, 0, 0,
87 (SCM procedure, SCM setter),
88 "Create a new procedure which behaves like @var{procedure}, but\n"
89 "with the associated setter @var{setter}.")
90 #define FUNC_NAME s_scm_make_procedure_with_setter
91 {
92 SCM name, ret;
93 SCM_VALIDATE_PROC (1, procedure);
94 SCM_VALIDATE_PROC (2, setter);
95 ret = scm_make_struct (pws_vtable, SCM_INUM0,
96 scm_list_2 (procedure, setter));
97
98 /* don't use procedure_name, because don't care enough to do a reverse
99 lookup */
100 name = scm_procedure_property (procedure, scm_sym_name);
101 if (scm_is_true (name))
102 scm_set_procedure_property_x (ret, scm_sym_name, name);
103 return ret;
104 }
105 #undef FUNC_NAME
106
107 SCM_DEFINE (scm_procedure, "procedure", 1, 0, 0,
108 (SCM proc),
109 "Return the procedure of @var{proc}, which must be an\n"
110 "applicable struct.")
111 #define FUNC_NAME s_scm_procedure
112 {
113 SCM_ASSERT (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc),
114 proc, SCM_ARG1, FUNC_NAME);
115 return SCM_STRUCT_PROCEDURE (proc);
116 }
117 #undef FUNC_NAME
118
119 SCM_PRIMITIVE_GENERIC (scm_setter, "setter", 1, 0, 0,
120 (SCM proc),
121 "Return the setter of @var{proc}, which must be an\n"
122 "applicable struct with a setter.")
123 #define FUNC_NAME s_scm_setter
124 {
125 if (SCM_UNLIKELY (!SCM_STRUCTP (proc)))
126 return scm_wta_dispatch_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
127 if (SCM_STRUCT_SETTER_P (proc))
128 return SCM_STRUCT_SETTER (proc);
129 if (SCM_PUREGENERICP (proc)
130 && SCM_IS_A_P (proc, scm_class_generic_with_setter))
131 /* FIXME: might not be an accessor */
132 return SCM_GENERIC_SETTER (proc);
133 return scm_wta_dispatch_1 (g_scm_setter, proc, SCM_ARG1, FUNC_NAME);
134 return SCM_BOOL_F; /* not reached */
135 }
136 #undef FUNC_NAME
137
138 \f
139 void
140 scm_init_procs ()
141 {
142 pws_vtable =
143 scm_c_make_struct (scm_applicable_struct_with_setter_vtable_vtable,
144 0,
145 1,
146 SCM_UNPACK (scm_from_latin1_symbol ("pwpw")));
147
148 #include "libguile/procs.x"
149 }
150
151 /*
152 Local Variables:
153 c-file-style: "gnu"
154 End:
155 */