4

我正在尝试进入 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);
}

可以通过这种方式使用数字来做到这一点:

电汇:

(defun return-a-number () 5.2)

用 C++ 读取和打印:

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

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

电汇:

(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。

调试

4

4 回答 4

6

(从@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 类型,该类型base-string确实使用char( base-charin lisp) 作为其字符类型。然后lisp代码读取

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

(可能有更优雅的方式进行转换,base-string但我不知道)。

在 C++ 中打印

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

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

于 2016-03-17T09:27:25.907 回答
3

根据The ECL Manual的第2.6 节字符串,我认为通过访问返回对象的字段可以找到实际的字符数组。你可以试试下面的吗?string.self

std::cout << y->string.self << std::endl;
于 2016-03-15T20:53:40.363 回答
2
std::string str {""};    
cl_object y2 = lisp("(return-a-base-string)");
//get dimension
int j = y2->string.dim;
//get pointer
ecl_character* selv = y2->string.self;
//do simple pointer addition
for(int i=0;i<j;i++){
    str += (*(selv+i));
}
//do whatever you want to str

当从 ecl_characters 构建字符串时,此代码有效

从文档中:

“ECL 定义了两种 C 类型来保存其字符:ecl_base_char 和 ecl_character。

在没有 Unicode 的情况下构建 ECL 时,它们既重合又通常匹配 unsigned char,以涵盖所需的 256 个代码。当 ECL 使用 Unicode 构建时,这两种类型不再等价,ecl_character 更大。为了使您的代码具有可移植性和面向未来的能力,请使用这两种类型来真正表达您打算做什么。”

于 2016-03-18T08:55:04.757 回答
1

在我的系统上,不需要 return-a-base-string,但我认为添加兼容性可能会很好。我使用 (ecl) 嵌入式 CLISP 16.1.2 版本。以下代码从 lisp 读取一个字符串并转换为 C++ 字符串类型 - std::string 和 c-string- 并将它们存储在 C++ 变量中:

// strings initializations: string and c-string
std::string str2 {""};     
char str_c[99] = " ";
// text read from clisp, whatever clisp function that returns string type
cl_object cl_text = lisp("(coerce (text-from-lisp X) 'base-string)");
//cl_object cl_text = lisp("(text-from-lisp X)"); // no base string conversions
// catch dimension
int cl_text_dim = cl_text->string.dim;
// complete c-string char by char
for(int ind=0;i<cl_text_dim;i++){
str_c[i] = ecl_char(cl_text,i); // ecl function to get char from cl_object
   }
str_c[cl_text_dim] ='\0'; // end of the c-string
str2 = str_c; // get the string on the other string type
std::cout <<  "Dim: " << cl_ text_dim << " C-String var: " << str_c() << " String var << str2 << std::endl;   

这是一个缓慢的过程,因为逐个字符传递,但据我所知,这是唯一的方法。希望能帮助到你。问候!

于 2016-07-22T18:15:58.900 回答