Remove incorrect comment in read.c
[bpt/guile.git] / libguile / values.c
1 /* Copyright (C) 2000, 2001, 2006, 2008, 2009, 2011 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include "libguile/_scm.h"
24 #include "libguile/eval.h"
25 #include "libguile/feature.h"
26 #include "libguile/gc.h"
27 #include "libguile/numbers.h"
28 #include "libguile/ports.h"
29 #include "libguile/root.h"
30 #include "libguile/strings.h"
31 #include "libguile/struct.h"
32 #include "libguile/validate.h"
33
34 #include "libguile/values.h"
35
36 SCM scm_values_vtable;
37
38 /* OBJ must be a values object containing exactly two values.
39 scm_i_extract_values_2 puts those two values into *p1 and *p2. */
40 void
41 scm_i_extract_values_2 (SCM obj, SCM *p1, SCM *p2)
42 {
43 SCM values;
44
45 SCM_ASSERT_TYPE (SCM_VALUESP (obj), obj, SCM_ARG1,
46 "scm_i_extract_values_2", "values");
47 values = scm_struct_ref (obj, SCM_INUM0);
48 if (scm_ilength (values) != 2)
49 scm_wrong_type_arg_msg
50 ("scm_i_extract_values_2", SCM_ARG1, obj,
51 "a values object containing exactly two values");
52 *p1 = SCM_CAR (values);
53 *p2 = SCM_CADR (values);
54 }
55
56 static SCM
57 print_values (SCM obj, SCM pwps)
58 {
59 SCM values = scm_struct_ref (obj, SCM_INUM0);
60 SCM port = SCM_PORT_WITH_PS_PORT (pwps);
61 scm_print_state *ps = SCM_PRINT_STATE (SCM_PORT_WITH_PS_PS (pwps));
62
63 scm_puts ("#<values ", port);
64 scm_iprin1 (values, port, ps);
65 scm_puts (">", port);
66
67 return SCM_UNSPECIFIED;
68 }
69
70 SCM
71 scm_c_value_ref (SCM obj, size_t idx)
72 {
73 if (SCM_LIKELY (SCM_VALUESP (obj)))
74 {
75 SCM values = scm_struct_ref (obj, SCM_INUM0);
76 size_t i = idx;
77 while (SCM_LIKELY (scm_is_pair (values)))
78 {
79 if (i == 0)
80 return SCM_CAR (values);
81 values = SCM_CDR (values);
82 i--;
83 }
84 }
85 else if (idx == 0)
86 return obj;
87
88 scm_error (scm_out_of_range_key,
89 "scm_c_value_ref",
90 "Too few values in ~S to access index ~S",
91 scm_list_2 (obj, scm_from_unsigned_integer (idx)),
92 scm_list_1 (scm_from_unsigned_integer (idx)));
93 }
94
95 SCM_DEFINE (scm_values, "values", 0, 0, 1,
96 (SCM args),
97 "Delivers all of its arguments to its continuation. Except for\n"
98 "continuations created by the @code{call-with-values} procedure,\n"
99 "all continuations take exactly one value. The effect of\n"
100 "passing no value or more than one value to continuations that\n"
101 "were not created by @code{call-with-values} is unspecified.")
102 #define FUNC_NAME s_scm_values
103 {
104 long n;
105 SCM result;
106
107 SCM_VALIDATE_LIST_COPYLEN (1, args, n);
108 if (n == 1)
109 result = SCM_CAR (args);
110 else
111 {
112 result = scm_c_make_struct (scm_values_vtable, 0, 1, SCM_UNPACK (args));
113 }
114
115 return result;
116 }
117 #undef FUNC_NAME
118
119 void
120 scm_init_values (void)
121 {
122 SCM print = scm_c_define_gsubr ("%print-values", 2, 0, 0, print_values);
123
124 scm_values_vtable = scm_make_vtable (scm_from_locale_string ("pr"), print);
125
126 scm_add_feature ("values");
127
128 #include "libguile/values.x"
129 }
130
131 /*
132 Local Variables:
133 c-file-style: "gnu"
134 End:
135 */