values.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* Copyright (C) 2000, 2001, 2006, 2008, 2009, 2011, 2012 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. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include "libguile/eval.h"
  23. #include "libguile/feature.h"
  24. #include "libguile/gc.h"
  25. #include "libguile/numbers.h"
  26. #include "libguile/ports.h"
  27. #include "libguile/root.h"
  28. #include "libguile/strings.h"
  29. #include "libguile/struct.h"
  30. #include "libguile/validate.h"
  31. #include "libguile/values.h"
  32. SCM scm_values_vtable;
  33. /* OBJ must be a values object containing exactly two values.
  34. scm_i_extract_values_2 puts those two values into *p1 and *p2. */
  35. void
  36. scm_i_extract_values_2 (SCM obj, SCM *p1, SCM *p2)
  37. {
  38. SCM values;
  39. SCM_ASSERT_TYPE (SCM_VALUESP (obj), obj, SCM_ARG1,
  40. "scm_i_extract_values_2", "values");
  41. values = scm_struct_ref (obj, SCM_INUM0);
  42. if (scm_ilength (values) != 2)
  43. scm_wrong_type_arg_msg
  44. ("scm_i_extract_values_2", SCM_ARG1, obj,
  45. "a values object containing exactly two values");
  46. *p1 = SCM_CAR (values);
  47. *p2 = SCM_CADR (values);
  48. }
  49. static SCM
  50. print_values (SCM obj, SCM pwps)
  51. {
  52. SCM values = scm_struct_ref (obj, SCM_INUM0);
  53. SCM port = SCM_PORT_WITH_PS_PORT (pwps);
  54. scm_print_state *ps = SCM_PRINT_STATE (SCM_PORT_WITH_PS_PS (pwps));
  55. scm_puts_unlocked ("#<values ", port);
  56. scm_iprin1 (values, port, ps);
  57. scm_puts_unlocked (">", port);
  58. return SCM_UNSPECIFIED;
  59. }
  60. size_t
  61. scm_c_nvalues (SCM obj)
  62. {
  63. if (SCM_LIKELY (SCM_VALUESP (obj)))
  64. return scm_ilength (scm_struct_ref (obj, SCM_INUM0));
  65. else
  66. return 1;
  67. }
  68. SCM
  69. scm_c_value_ref (SCM obj, size_t idx)
  70. {
  71. if (SCM_LIKELY (SCM_VALUESP (obj)))
  72. {
  73. SCM values = scm_struct_ref (obj, SCM_INUM0);
  74. size_t i = idx;
  75. while (SCM_LIKELY (scm_is_pair (values)))
  76. {
  77. if (i == 0)
  78. return SCM_CAR (values);
  79. values = SCM_CDR (values);
  80. i--;
  81. }
  82. }
  83. else if (idx == 0)
  84. return obj;
  85. scm_error (scm_out_of_range_key,
  86. "scm_c_value_ref",
  87. "Too few values in ~S to access index ~S",
  88. scm_list_2 (obj, scm_from_size_t (idx)),
  89. scm_list_1 (scm_from_size_t (idx)));
  90. }
  91. SCM_DEFINE (scm_values, "values", 0, 0, 1,
  92. (SCM args),
  93. "Delivers all of its arguments to its continuation. Except for\n"
  94. "continuations created by the @code{call-with-values} procedure,\n"
  95. "all continuations take exactly one value. The effect of\n"
  96. "passing no value or more than one value to continuations that\n"
  97. "were not created by @code{call-with-values} is unspecified.")
  98. #define FUNC_NAME s_scm_values
  99. {
  100. long n;
  101. SCM result;
  102. SCM_VALIDATE_LIST_COPYLEN (1, args, n);
  103. if (n == 1)
  104. result = SCM_CAR (args);
  105. else
  106. result = scm_c_make_struct (scm_values_vtable, 0, 1, SCM_UNPACK (args));
  107. return result;
  108. }
  109. #undef FUNC_NAME
  110. SCM
  111. scm_c_values (SCM *base, size_t nvalues)
  112. {
  113. SCM ret, *walk;
  114. if (nvalues == 1)
  115. return *base;
  116. for (ret = SCM_EOL, walk = base + nvalues - 1; walk >= base; walk--)
  117. ret = scm_cons (*walk, ret);
  118. return scm_values (ret);
  119. }
  120. void
  121. scm_init_values (void)
  122. {
  123. SCM print = scm_c_define_gsubr ("%print-values", 2, 0, 0, print_values);
  124. scm_values_vtable = scm_make_vtable (scm_from_locale_string ("pr"), print);
  125. scm_add_feature ("values");
  126. #include "libguile/values.x"
  127. }
  128. /*
  129. Local Variables:
  130. c-file-style: "gnu"
  131. End:
  132. */