2002-07-20 Han-Wen <hanwen@cs.uu.nl>
[bpt/guile.git] / libguile / evalext.c
index 6228372..16b3ed5 100644 (file)
@@ -1,4 +1,4 @@
-/*     Copyright (C) 1998 Free Software Foundation, Inc.
+/* Copyright (C) 1998,1999,2000,2001 Free Software Foundation, Inc.
  * 
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * If you write modifications of your own for GUILE, it is your choice
  * whether to permit this exception to apply to your modifications.
  * If you do not wish that, delete this exception notice.  */
+
+
 \f
 
-#include "_scm.h"
-#include "eval.h"
-#include "macros.h"
+#include "libguile/_scm.h"
+#include "libguile/eval.h"
+#include "libguile/macros.h"
+#include "libguile/modules.h"
+#include "libguile/fluids.h"
 
-#include "evalext.h"
+#include "libguile/validate.h"
+#include "libguile/evalext.h"
 
-SCM_PROC (s_definedp, "defined?", 1, 0, 0, scm_definedp);
+SCM_SYMBOL (scm_sym_setter, "setter");
 
 SCM 
-scm_definedp (sym)
-     SCM sym;
+scm_m_generalized_set_x (SCM xorig, SCM env SCM_UNUSED)
 {
-  SCM vcell;
+  SCM x = SCM_CDR (xorig);
+  SCM_ASSYNT (2 == scm_ilength (x), scm_s_expression, scm_s_set_x);
+  if (SCM_SYMBOLP (SCM_CAR (x)))
+    return scm_cons (SCM_IM_SET_X, x);
+  else if (SCM_CONSP (SCM_CAR (x)))
+    return scm_cons (scm_list_2 (scm_sym_setter, SCM_CAAR (x)),
+                    scm_append (scm_list_2 (SCM_CDAR (x), SCM_CDR (x))));
+  else
+    scm_misc_error (scm_s_set_x, scm_s_variable, SCM_EOL);
+}
 
-  if (SCM_ISYMP (sym))
-    return SCM_BOOL_T;
+SCM_DEFINE (scm_definedp, "defined?", 1, 1, 0,
+            (SCM sym, SCM env),
+           "Return @code{#t} if @var{sym} is defined in the lexical "
+           "environment @var{env}.  When @var{env} is not specified, "
+           "look in the top-level environment as defined by the "
+           "current module.")
+#define FUNC_NAME s_scm_definedp
+{
+  SCM var;
 
-  SCM_ASSERT (SCM_NIMP (sym) && SCM_SYMBOLP (sym), sym, SCM_ARG1, s_definedp);
+  SCM_VALIDATE_SYMBOL (1, sym);
 
-  vcell = scm_sym2vcell(sym,
-                       SCM_CDR (scm_top_level_lookup_closure_var),
-                       SCM_BOOL_F);
-  return (vcell == SCM_BOOL_F || SCM_UNBNDP(SCM_CDR(vcell))) ? 
-      SCM_BOOL_F : SCM_BOOL_T;
+  if (SCM_UNBNDP (env))
+    var = scm_sym2var (sym, scm_current_module_lookup_closure (),
+                        SCM_BOOL_F);
+  else
+    {
+      SCM frames = env;
+      register SCM b;
+      for (; SCM_NIMP (frames); frames = SCM_CDR (frames))
+       {
+         SCM_ASSERT (SCM_CONSP (frames), env, SCM_ARG2, FUNC_NAME);
+         b = SCM_CAR (frames);
+         if (SCM_NFALSEP (scm_procedure_p (b)))
+           break;
+         SCM_ASSERT (SCM_CONSP (b), env, SCM_ARG2, FUNC_NAME);
+         for (b = SCM_CAR (b); SCM_NIMP (b); b = SCM_CDR (b))
+           {
+             if (SCM_NCONSP (b))
+               {
+                 if (SCM_EQ_P (b, sym))
+                   return SCM_BOOL_T;
+                 else
+                   break;
+               }
+             if (SCM_EQ_P (SCM_CAR (b), sym))
+               return SCM_BOOL_T;
+           }
+       }
+      var = scm_sym2var (sym,
+                        SCM_NIMP (frames) ? SCM_CAR (frames) : SCM_BOOL_F,
+                        SCM_BOOL_F);
+    }
+             
+  return (SCM_FALSEP (var) || SCM_UNBNDP (SCM_VARIABLE_REF (var))
+         ? SCM_BOOL_F
+         : SCM_BOOL_T);
 }
+#undef FUNC_NAME
 
-static char s_undefine[] = "undefine";
+
+SCM_SYNTAX (s_undefine, "undefine", scm_makacro, scm_m_undefine);
 
 SCM
-scm_m_undefine (x, env)
-     SCM x, env;
+scm_m_undefine (SCM x, SCM env)
 {
   SCM arg1 = x;
   x = SCM_CDR (x);
-  SCM_ASSYNT (SCM_TOP_LEVEL (env), arg1, "bad placement ", s_undefine);
-  SCM_ASSYNT (SCM_NIMP (x) && SCM_CONSP (x) && SCM_CDR (x) == SCM_EOL,
-             arg1, scm_s_expression, s_undefine);
+  SCM_ASSYNT (SCM_TOP_LEVEL (env), "bad placement ", s_undefine);
+  SCM_ASSYNT (SCM_CONSP (x) && SCM_NULLP (SCM_CDR (x)),
+             scm_s_expression, s_undefine);
   x = SCM_CAR (x);
-  SCM_ASSYNT (SCM_NIMP (x) && SCM_SYMBOLP (x), arg1, scm_s_variable, s_undefine);
-  arg1 = scm_sym2vcell (x, scm_env_top_level (env), SCM_BOOL_F);
-  SCM_ASSYNT (SCM_NFALSEP (arg1) && !SCM_UNBNDP (SCM_CDR (arg1)),
-             x, "variable already unbound ", s_undefine);
-#if 0
-#ifndef SCM_RECKLESS
-  if (SCM_NIMP (SCM_CDR (arg1)) && ((SCM) SCM_SNAME (SCM_CDR (arg1)) == x))
-    scm_warn ("undefining built-in ", SCM_CHARS (x));
-  else
-#endif
-    if (5 <= scm_verbose && SCM_UNDEFINED != SCM_CDR (arg1))
-      scm_warn ("redefining ", SCM_CHARS (x));
-#endif
-  SCM_SETCDR (arg1, SCM_UNDEFINED);
+  SCM_ASSYNT (SCM_SYMBOLP (x), scm_s_variable, s_undefine);
+  arg1 = scm_sym2var (x, scm_env_top_level (env), SCM_BOOL_F);
+  SCM_ASSYNT (SCM_NFALSEP (arg1) && !SCM_UNBNDP (SCM_VARIABLE_REF (arg1)),
+             "variable already unbound ", s_undefine);
+  SCM_VARIABLE_SET (arg1, SCM_UNDEFINED);
 #ifdef SICP
-  return SCM_CAR (arg1);
+  return x;
 #else
   return SCM_UNSPECIFIED;
 #endif
 }
 
-static char s_sequence_to_list[] = "sequence->list";
-
-SCM
-scm_m_sequence_to_list (SCM x, SCM env)
-{
-  SCM res = SCM_EOL;
-  SCM_ASSYNT (SCM_NULLP (SCM_CDR (x))
-             || (SCM_NIMP (SCM_CDR (x) && SCM_ECONSP (SCM_CDR (x)))),
-             x, scm_s_expression, s_sequence_to_list);
-  while (SCM_NNULLP (x = SCM_CDR (x)))
-    res = scm_cons (SCM_XEVALCAR (x, env), res);
-  return res;
-}
-
-SCM_PROC (s_serial_map, "serial-map", 2, 0, 1, scm_map);
+SCM_REGISTER_PROC (s_map_in_order, "map-in-order", 2, 0, 1, scm_map);
 
 void 
 scm_init_evalext ()
 {
-  scm_make_synt (s_undefine, scm_makacro, scm_m_undefine);
-  scm_make_synt (s_sequence_to_list, scm_makacro, scm_m_sequence_to_list);
-#include "evalext.x"
+  scm_make_synt (scm_s_set_x, scm_makmmacro, scm_m_generalized_set_x);
+#include "libguile/evalext.x"
 }
+
+/*
+  Local Variables:
+  c-file-style: "gnu"
+  End:
+*/