OpenCog Framework  Branch: master, revision 6f0b7fc776b08468cf1b74aa9db028f387b4f0c0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SchemeSmob.cc
Go to the documentation of this file.
1 /*
2  * SchemeSmob.c
3  *
4  * Scheme small objects (SMOBS) for opencog -- core functions.
5  *
6  * Copyright (c) 2008, 2013, 2014, 2015 Linas Vepstas <linas@linas.org>
7  */
8 
9 #ifdef HAVE_GUILE
10 
11 #include <cstddef>
12 #include <libguile.h>
13 
15 #include "SchemePrimitive.h"
16 #include "SchemeSmob.h"
17 
18 using namespace opencog;
19 
39 scm_t_bits SchemeSmob::cog_misc_tag;
40 std::atomic_flag SchemeSmob::is_inited = ATOMIC_FLAG_INIT;
42 
44 {
45  if (is_inited.test_and_set()) return;
46 
48  scm_c_define_module("opencog", register_procs, NULL);
49  scm_c_use_module("opencog");
50 
51  atomspace_fluid = scm_make_fluid();
52  atomspace_fluid = scm_permanent_object(atomspace_fluid);
53  _radix_ten = scm_from_int8(10);
54 }
55 
57 {
58  init();
59 }
60 
62 {
64 }
65 
66 /* ============================================================== */
67 
69 {
70  // A SMOB type for everything, incuding atoms.
71  cog_misc_tag = scm_make_smob_type ("opencog-misc", sizeof (scm_t_bits));
72  scm_set_smob_print (cog_misc_tag, print_misc);
73  scm_set_smob_equalp (cog_misc_tag, equalp_misc);
74  // scm_set_smob_mark (cog_misc_tag, mark_misc);
75  scm_set_smob_free (cog_misc_tag, free_misc);
76 }
77 
78 /* ============================================================== */
79 
80 SCM SchemeSmob::equalp_misc(SCM a, SCM b)
81 {
82  // If they're not something we know about, let scheme sort it out.
83  // (Actualy, this should never happen ...)
84  if (not SCM_SMOB_PREDICATE(SchemeSmob::cog_misc_tag, a))
85  return scm_equal_p(a, b);
86 
87  // If the types don't match, they can't be equal.
88  scm_t_bits ta = SCM_SMOB_FLAGS(a);
89  scm_t_bits tb = SCM_SMOB_FLAGS(b);
90  if (ta != tb)
91  return SCM_BOOL_F;
92 
93  switch (ta)
94  {
95  default: // Should never happen.
96  case 0: // Should never happen.
97  return SCM_BOOL_F;
98  case COG_AS:
99  {
100  AtomSpace* as = (AtomSpace *) SCM_SMOB_DATA(a);
101  AtomSpace* bs = (AtomSpace *) SCM_SMOB_DATA(b);
102  scm_remember_upto_here_1(a);
103  scm_remember_upto_here_1(b);
104  /* Just a simple pointer comparison */
105  if (as == bs) return SCM_BOOL_T;
106  return SCM_BOOL_F;
107  }
108  case COG_AV:
109  {
110  AttentionValue* av = (AttentionValue *) SCM_SMOB_DATA(a);
111  AttentionValue* bv = (AttentionValue *) SCM_SMOB_DATA(b);
112  scm_remember_upto_here_1(a);
113  scm_remember_upto_here_1(b);
114  if (av == bv) return SCM_BOOL_T;
115  if (*av == *bv) return SCM_BOOL_T;
116  return SCM_BOOL_F;
117  }
118  case COG_EXTEND:
119  {
120  // We compare pointers here, only.
121  PrimitiveEnviron* av = (PrimitiveEnviron *) SCM_SMOB_DATA(a);
122  PrimitiveEnviron* bv = (PrimitiveEnviron *) SCM_SMOB_DATA(b);
123  scm_remember_upto_here_1(a);
124  scm_remember_upto_here_1(b);
125  if (av == bv) return SCM_BOOL_T;
126  return SCM_BOOL_F;
127  }
128  case COG_HANDLE:
129  {
130  Handle ha(scm_to_handle(a));
131  Handle hb(scm_to_handle(b));
132  if (ha == hb) return SCM_BOOL_T;
133  return SCM_BOOL_F;
134  }
135  case COG_TV:
136  {
137  TruthValue* av = (TruthValue *) SCM_SMOB_DATA(a);
138  TruthValue* bv = (TruthValue *) SCM_SMOB_DATA(b);
139  scm_remember_upto_here_1(a);
140  scm_remember_upto_here_1(b);
141  if (av == bv) return SCM_BOOL_T;
142  if (*av == *bv) return SCM_BOOL_T;
143  return SCM_BOOL_F;
144  }
145  }
146 }
147 
148 /* ============================================================== */
149 
150 void SchemeSmob::throw_exception(const char *msg, const char * func)
151 {
152  if (msg) {
153  // Should we even bother to log this?
154  logger().info("Guile caught C++ exception: %s", msg);
155 
156  // scm_misc_error(fe->get_name(), msg, SCM_EOL);
157  scm_throw(
158  scm_from_utf8_symbol("C++-EXCEPTION"),
159  scm_cons(
160  scm_from_utf8_string(func),
161  scm_cons(
162  scm_from_utf8_string(msg),
163  SCM_EOL)));
164  // Hmm. scm_throw never returns.
165  }
166  else
167  {
168  // scm_misc_error(fe->get_name(), "unknown C++ exception", SCM_EOL);
169  scm_error_scm(
170  scm_from_utf8_symbol("C++ exception"),
171  scm_from_utf8_string(func),
172  scm_from_utf8_string("unknown C++ exception"),
173  SCM_EOL,
174  SCM_EOL);
175  logger().error("Guile caught unknown C++ exception");
176  }
177 }
178 
179 /* ============================================================== */
180 
181 #ifdef HAVE_GUILE2
182  #define C(X) ((scm_t_subr) X)
183 #else
184  #define C(X) ((SCM (*) ()) X)
185 #endif
186 
188 {
189  register_proc("cog-atom", 1, 0, 0, C(ss_atom));
190  register_proc("cog-handle", 1, 0, 0, C(ss_handle));
191  register_proc("cog-undefined-handle", 0, 0, 0, C(ss_undefined_handle));
192  register_proc("cog-new-node", 2, 0, 1, C(ss_new_node));
193  register_proc("cog-new-link", 1, 0, 1, C(ss_new_link));
194  register_proc("cog-node", 2, 0, 1, C(ss_node));
195  register_proc("cog-link", 1, 0, 1, C(ss_link));
196  register_proc("cog-delete", 1, 0, 1, C(ss_delete));
197  register_proc("cog-delete-recursive", 1, 0, 1, C(ss_delete_recursive));
198  register_proc("cog-purge", 1, 0, 1, C(ss_purge));
199  register_proc("cog-purge-recursive", 1, 0, 1, C(ss_purge_recursive));
200  register_proc("cog-atom?", 1, 0, 1, C(ss_atom_p));
201  register_proc("cog-node?", 1, 0, 1, C(ss_node_p));
202  register_proc("cog-link?", 1, 0, 1, C(ss_link_p));
203 
204  // property setters on atoms
205  register_proc("cog-set-av!", 2, 0, 0, C(ss_set_av));
206  register_proc("cog-set-tv!", 2, 0, 0, C(ss_set_tv));
207  register_proc("cog-inc-vlti!", 1, 0, 0, C(ss_inc_vlti));
208  register_proc("cog-dec-vlti!", 1, 0, 0, C(ss_dec_vlti));
209 
210  // property getters on atoms
211  register_proc("cog-name", 1, 0, 0, C(ss_name));
212  register_proc("cog-type", 1, 0, 0, C(ss_type));
213  register_proc("cog-arity", 1, 0, 0, C(ss_arity));
214  register_proc("cog-incoming-set", 1, 0, 0, C(ss_incoming_set));
215  register_proc("cog-outgoing-set", 1, 0, 0, C(ss_outgoing_set));
216  register_proc("cog-tv", 1, 0, 0, C(ss_tv));
217  register_proc("cog-av", 1, 0, 0, C(ss_av));
218 
219  // Truth-values
220  register_proc("cog-new-stv", 2, 0, 0, C(ss_new_stv));
221  register_proc("cog-new-ctv", 3, 0, 0, C(ss_new_ctv));
222  register_proc("cog-new-itv", 3, 0, 0, C(ss_new_itv));
223  register_proc("cog-new-ptv", 3, 0, 0, C(ss_new_ptv));
224  register_proc("cog-new-ftv", 2, 0, 0, C(ss_new_ftv));
225  register_proc("cog-tv?", 1, 0, 0, C(ss_tv_p));
226  register_proc("cog-stv?", 1, 0, 0, C(ss_stv_p));
227  register_proc("cog-ctv?", 1, 0, 0, C(ss_ctv_p));
228  register_proc("cog-itv?", 1, 0, 0, C(ss_itv_p));
229  register_proc("cog-ptv?", 1, 0, 0, C(ss_ptv_p));
230  register_proc("cog-ftv?", 1, 0, 0, C(ss_ftv_p));
231  register_proc("cog-tv->alist", 1, 0, 0, C(ss_tv_get_value));
232 
233  // Atom Spaces
234  register_proc("cog-new-atomspace", 0, 1, 0, C(ss_new_as));
235  register_proc("cog-atomspace?", 1, 0, 0, C(ss_as_p));
236  register_proc("cog-atomspace", 0, 0, 0, C(ss_get_as));
237  register_proc("cog-set-atomspace!", 1, 0, 0, C(ss_set_as));
238 
239  // Attention values
240  register_proc("cog-new-av", 3, 0, 0, C(ss_new_av));
241  register_proc("cog-av?", 1, 0, 0, C(ss_av_p));
242  register_proc("cog-av->alist", 1, 0, 0, C(ss_av_get_value));
243 
244  // AttentionalFocus
245  register_proc("cog-af-boundary", 0, 0, 0, C(ss_af_boundary));
246  register_proc("cog-set-af-boundary!", 1, 0, 0, C(ss_set_af_boundary));
247  register_proc("cog-af", 0, 0, 0, C(ss_af));
248 
249  // Atom types
250  register_proc("cog-get-types", 0, 0, 0, C(ss_get_types));
251  register_proc("cog-type->int", 1, 0, 0, C(ss_get_type));
252  register_proc("cog-type?", 1, 0, 0, C(ss_type_p));
253  register_proc("cog-node-type?", 1, 0, 0, C(ss_node_type_p));
254  register_proc("cog-link-type?", 1, 0, 0, C(ss_link_type_p));
255  register_proc("cog-get-subtypes", 1, 0, 0, C(ss_get_subtypes));
256  register_proc("cog-subtype?", 2, 0, 0, C(ss_subtype_p));
257 
258  // Iterators
259  register_proc("cog-map-type", 2, 0, 0, C(ss_map_type));
260 }
261 
262 void SchemeSmob::register_proc(const char* name, int req, int opt, int rst, scm_t_subr fcn)
263 {
264  scm_c_define_gsubr(name, req, opt, rst, fcn);
265  scm_c_export(name, NULL);
266 }
267 
268 #endif
269 /* ===================== END OF FILE ============================ */
static SCM ss_set_af_boundary(SCM)
Definition: SchemeSmobAF.cc:47
static SCM ss_av_get_value(SCM)
static SCM ss_purge_recursive(SCM, SCM)
#define C(X)
Definition: SchemeSmob.cc:184
static SCM ss_af_boundary(void)
Definition: SchemeSmobAF.cc:38
static SCM ss_node_type_p(SCM)
static SCM ss_ptv_p(SCM)
static SCM ss_get_types(void)
void opencog_guile_init(void)
Definition: SchemeSmob.cc:61
static SCM ss_set_as(SCM)
static SCM ss_inc_vlti(SCM)
static int print_misc(SCM, SCM, scm_print_state *)
static SCM ss_subtype_p(SCM, SCM)
static SCM ss_node(SCM, SCM, SCM)
static SCM ss_new_av(SCM, SCM, SCM)
Definition: SchemeSmobAV.cc:80
static SCM ss_arity(SCM)
static void throw_exception(const char *, const char *)
Definition: SchemeSmob.cc:150
static SCM ss_new_ptv(SCM, SCM, SCM)
static scm_t_bits cog_misc_tag
Definition: SchemeSmob.h:59
static SCM ss_incoming_set(SCM)
static SCM ss_purge(SCM, SCM)
static SCM ss_delete(SCM, SCM)
static SCM ss_node_p(SCM)
static SCM ss_get_type(SCM)
static SCM ss_name(SCM)
static SCM ss_new_link(SCM, SCM)
static void init_smob_type(void)
Definition: SchemeSmob.cc:68
static void init()
Definition: SchemeSmob.cc:43
static SCM ss_tv_get_value(SCM)
static SCM ss_as_p(SCM)
Definition: SchemeSmobAS.cc:84
static std::atomic_flag is_inited
Definition: SchemeSmob.h:53
static SCM ss_new_ctv(SCM, SCM, SCM)
static SCM ss_av(SCM)
static SCM ss_av_p(SCM)
static SCM ss_undefined_handle(void)
static Handle scm_to_handle(SCM)
static SCM ss_atom(SCM)
static SCM ss_type(SCM)
static SCM ss_atom_p(SCM)
static void register_proc(const char *, int, int, int, scm_t_subr)
Definition: SchemeSmob.cc:262
static SCM ss_link_p(SCM)
static SCM ss_map_type(SCM, SCM)
static SCM ss_ctv_p(SCM)
static SCM ss_new_ftv(SCM, SCM)
static SCM ss_stv_p(SCM)
static SCM ss_type_p(SCM)
static void register_procs(void *)
Definition: SchemeSmob.cc:187
static SCM ss_new_itv(SCM, SCM, SCM)
static SCM ss_itv_p(SCM)
static SCM ss_dec_vlti(SCM)
static SCM ss_delete_recursive(SCM, SCM)
static SCM ss_outgoing_set(SCM)
static SCM ss_new_stv(SCM, SCM)
static SCM ss_af(void)
Definition: SchemeSmobAF.cc:61
static SCM ss_set_tv(SCM, SCM)
static SCM _radix_ten
Definition: SchemeSmob.h:81
static SCM ss_set_av(SCM, SCM)
static SCM ss_handle(SCM)
static SCM ss_tv_p(SCM)
static SCM ss_new_node(SCM, SCM, SCM)
static SCM atomspace_fluid
Definition: SchemeSmob.h:181
static SCM ss_get_subtypes(SCM)
static SCM ss_new_as(SCM)
Definition: SchemeSmobAS.cc:69
static SCM ss_ftv_p(SCM)
static SCM equalp_misc(SCM, SCM)
Definition: SchemeSmob.cc:80
static SCM ss_link(SCM, SCM)
static SCM ss_link_type_p(SCM)
static SCM ss_tv(SCM)
static size_t free_misc(SCM)
Definition: SchemeSmobGC.cc:57
static SCM ss_get_as(void)