*/
#ifdef HAVE_GUILE
#include
#include
#include
#include
#include "SchemeEval.h"
#include "SchemeSmob.h"
using namespace opencog;
/**
* do_apply -- apply named function func to arguments in ListLink
* It is assumed that varargs is a ListLink, containing a list of
* atom handles. This list is unpacked, and then the fuction func
* is applied to them. If the function returns an atom handle, then
* this is returned.
*/
Handle SchemeEval::do_apply(const std::string &func, Handle varargs)
{
// Apply the function to the args
SCM sresult = do_apply_scm (func, varargs);
// If the result is a handle, return the handle.
if (!SCM_SMOB_PREDICATE(SchemeSmob::cog_handle_tag, sresult))
{
return Handle::UNDEFINED;
}
return SchemeSmob::scm_to_handle(sresult);
}
/**
* do_apply_scm -- apply named function func to arguments in ListLink
* It is assumed that varargs is a ListLink, containing a list of
* atom handles. This list is unpacked, and then the fuction func
* is applied to them. The SCM value returned by the function is returned.
*/
SCM SchemeEval::do_apply_scm( const std::string& func, Handle varargs )
{
SCM sfunc = scm_from_locale_symbol(func.c_str());
SCM expr = SCM_EOL;
// If there were args, pass the args to the function.
boost::shared_ptr largs = cogserver().getAtomSpace()->cloneLink(varargs);
if (largs)
{
const std::vector &oset = largs->getOutgoingSet();
size_t sz = oset.size();
for (int i=sz-1; i>=0; i--)
{
Handle h = oset[i];
SCM sh = SchemeSmob::handle_to_scm(h);
expr = scm_cons(sh, expr);
}
}
expr = scm_cons(sfunc, expr);
return do_scm_eval(expr);
}
#endif
/* ===================== END OF FILE ============================ */