evalext.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Copyright (C) 1998,1999,2000,2001, 2003 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice. */
  41. #include "libguile/_scm.h"
  42. #include "libguile/eval.h"
  43. #include "libguile/macros.h"
  44. #include "libguile/modules.h"
  45. #include "libguile/fluids.h"
  46. #include "libguile/validate.h"
  47. #include "libguile/evalext.h"
  48. SCM_SYMBOL (scm_sym_setter, "setter");
  49. SCM
  50. scm_m_generalized_set_x (SCM xorig, SCM env SCM_UNUSED)
  51. {
  52. SCM x = SCM_CDR (xorig);
  53. SCM_ASSYNT (2 == scm_ilength (x), scm_s_expression, scm_s_set_x);
  54. if (SCM_SYMBOLP (SCM_CAR (x)))
  55. return scm_cons (SCM_IM_SET_X, x);
  56. else if (SCM_CONSP (SCM_CAR (x)))
  57. return scm_cons (scm_list_2 (scm_sym_setter, SCM_CAAR (x)),
  58. scm_append (scm_list_2 (SCM_CDAR (x), SCM_CDR (x))));
  59. else
  60. scm_misc_error (scm_s_set_x, scm_s_variable, SCM_EOL);
  61. }
  62. SCM_DEFINE (scm_definedp, "defined?", 1, 1, 0,
  63. (SCM sym, SCM env),
  64. "Return @code{#t} if @var{sym} is defined in the lexical "
  65. "environment @var{env}. When @var{env} is not specified, "
  66. "look in the top-level environment as defined by the "
  67. "current module.")
  68. #define FUNC_NAME s_scm_definedp
  69. {
  70. SCM var;
  71. SCM_VALIDATE_SYMBOL (1,sym);
  72. if (SCM_UNBNDP (env))
  73. var = scm_sym2var (sym, scm_current_module_lookup_closure (),
  74. SCM_BOOL_F);
  75. else
  76. {
  77. SCM frames = env;
  78. register SCM b;
  79. for (; SCM_NIMP (frames); frames = SCM_CDR (frames))
  80. {
  81. SCM_ASSERT (SCM_CONSP (frames), env, SCM_ARG2, FUNC_NAME);
  82. b = SCM_CAR (frames);
  83. if (SCM_NFALSEP (scm_procedure_p (b)))
  84. break;
  85. SCM_ASSERT (SCM_CONSP (b), env, SCM_ARG2, FUNC_NAME);
  86. for (b = SCM_CAR (b); SCM_NIMP (b); b = SCM_CDR (b))
  87. {
  88. if (SCM_NCONSP (b))
  89. {
  90. if (SCM_EQ_P (b, sym))
  91. return SCM_BOOL_T;
  92. else
  93. break;
  94. }
  95. if (SCM_EQ_P (SCM_CAR (b), sym))
  96. return SCM_BOOL_T;
  97. }
  98. }
  99. var = scm_sym2var (sym,
  100. SCM_NIMP (frames) ? SCM_CAR (frames) : SCM_BOOL_F,
  101. SCM_BOOL_F);
  102. }
  103. return (SCM_FALSEP (var) || SCM_UNBNDP (SCM_VARIABLE_REF (var))
  104. ? SCM_BOOL_F
  105. : SCM_BOOL_T);
  106. }
  107. #undef FUNC_NAME
  108. SCM_SYNTAX (s_undefine, "undefine", scm_makacro, scm_m_undefine);
  109. SCM
  110. scm_m_undefine (SCM x, SCM env)
  111. {
  112. SCM arg1 = x;
  113. x = SCM_CDR (x);
  114. SCM_ASSYNT (SCM_TOP_LEVEL (env), "bad placement ", s_undefine);
  115. SCM_ASSYNT (SCM_CONSP (x) && SCM_NULLP (SCM_CDR (x)),
  116. scm_s_expression, s_undefine);
  117. x = SCM_CAR (x);
  118. SCM_ASSYNT (SCM_SYMBOLP (x), scm_s_variable, s_undefine);
  119. arg1 = scm_sym2var (x, scm_env_top_level (env), SCM_BOOL_F);
  120. SCM_ASSYNT (SCM_NFALSEP (arg1) && !SCM_UNBNDP (SCM_VARIABLE_REF (arg1)),
  121. "variable already unbound ", s_undefine);
  122. SCM_VARIABLE_SET (arg1, SCM_UNDEFINED);
  123. #ifdef SICP
  124. return x;
  125. #else
  126. return SCM_UNSPECIFIED;
  127. #endif
  128. }
  129. SCM_REGISTER_PROC (s_map_in_order, "map-in-order", 2, 0, 1, scm_map);
  130. #define scm_tcs_struct scm_tcs_cons_gloc
  131. SCM_DEFINE (scm_self_evaluating_p, "self-evaluating?", 1, 0, 0,
  132. (SCM obj),
  133. "Return #t for objects which Guile considers self-evaluating")
  134. #define FUNC_NAME s_scm_self_evaluating_p
  135. {
  136. switch (SCM_ITAG3 (obj))
  137. {
  138. case scm_tc3_int_1:
  139. case scm_tc3_int_2:
  140. /* inum */
  141. return SCM_BOOL_T;
  142. case scm_tc3_imm24:
  143. /* characters, booleans, other immediates */
  144. return SCM_BOOL (!SCM_NULLP (obj));
  145. case scm_tc3_cons:
  146. switch (SCM_TYP7 (obj))
  147. {
  148. case scm_tcs_closures:
  149. case scm_tc7_vector:
  150. case scm_tc7_wvect:
  151. #ifdef HAVE_ARRAYS
  152. case scm_tc7_bvect:
  153. case scm_tc7_byvect:
  154. case scm_tc7_svect:
  155. case scm_tc7_ivect:
  156. case scm_tc7_uvect:
  157. case scm_tc7_fvect:
  158. case scm_tc7_dvect:
  159. case scm_tc7_cvect:
  160. #ifdef HAVE_LONG_LONGS
  161. case scm_tc7_llvect:
  162. #endif
  163. #endif
  164. case scm_tc7_string:
  165. case scm_tc7_smob:
  166. case scm_tc7_cclo:
  167. case scm_tc7_pws:
  168. case scm_tcs_subrs:
  169. case scm_tcs_struct:
  170. return SCM_BOOL_T;
  171. default:
  172. return SCM_BOOL_F;
  173. }
  174. }
  175. SCM_MISC_ERROR ("Internal error: Object ~S has unknown type",
  176. scm_list_1 (obj));
  177. return SCM_UNSPECIFIED; /* never reached */
  178. }
  179. #undef FUNC_NAME
  180. #undef scm_tcs_struct
  181. void
  182. scm_init_evalext ()
  183. {
  184. scm_make_synt (scm_s_set_x, scm_makmmacro, scm_m_generalized_set_x);
  185. #include "libguile/evalext.x"
  186. }
  187. /*
  188. Local Variables:
  189. c-file-style: "gnu"
  190. End:
  191. */