加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

common-lisp – 将字符串从ECL传递给C

发布时间:2020-12-16 10:12:33 所属栏目:百科 来源:网络整理
导读:我正试图进入嵌入在C中的Common Lisp的迷人世界.我的问题是我无法从c读取和打印由ECL中定义的lisp函数返回的字符串. 在C中我有这个函数来运行任意的Lisp表达式: cl_object lisp(const std::string call) { return cl_safe_eval(c_string_to_object(call.c_s
我正试图进入嵌入在C中的Common Lisp的迷人世界.我的问题是我无法从c读取和打印由ECL中定义的lisp函数返回的字符串.

在C中我有这个函数来运行任意的Lisp表达式:

cl_object lisp(const std::string & call) {
    return cl_safe_eval(c_string_to_object(call.c_str()),Cnil,Cnil);
}

我可以用这种方式用数字来做:

ECL:

(defun return-a-number () 5.2)

在C中阅读和打印:

auto x = ecl_to_float(lisp("(return-a-number)"));
std::cout << "The number is " << x << std::endl;

一切都设置好并且工作正常,但我不知道用字符串而不是数字来做.这是我尝试过的:

ECL:

(defun return-a-string () "Hello")

C :

cl_object y = lisp("(return-a-string)");
 std::cout << "A string: " << y << std::endl;

打印字符串的结果是这样的:

一个字符串:0x3188b00

我猜是字符串的地址.

这是捕获调试器和y cl_object的内容. y-> string.self类型是一个ecl_character.

Debug

解决方法

(从@ coredump的回答开始,string.self字段提供结果.)

string.self字段被定义为类型ecl_character *(ecl / object.h),它似乎在ecl / config.h中以int类型给出(尽管我怀疑这是稍微依赖于平台的).因此,您将无法像打字符一样打印它.

我发现对我有用的方法是将其重新解释为wchar_t(即unicode字符).不幸的是,我有理由相信这不可移植,并且取决于ecl的配置方式和C编译器.

// basic check that this should work
static_assert(sizeof(ecl_character)==sizeof(wchar_t),"sizes must be the same");

std::wcout << "A string: " << reinterpret_cast<wchar_t*>(y->string.self) << std::endl;
// prints hello,as required
// note the use of wcout

另一种方法是使用lisp类型的基本字符串,它使用char(lisp中的base-char)作为其字符类型.然后读取lisp代码

(defun return-a-base-string ()
    (coerce "Hello" 'base-string))

(可能有更优雅的方式来转换为基本字符串,但我不知道它们).

用C打印

cl_object y2 = lisp("(return-a-base-string)");
std::cout << "Another: " << y2->base_string.self << std::endl;

(注意你不能在同一个程序中混用wcout和cout)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读