gpt4 book ai didi

c - 如何将C代码转换为旧的Visual Studio 2005,特别是TinyExpr源代码

转载 作者:行者123 更新时间:2023-11-30 14:35:28 24 4
gpt4 key购买 nike

我想转换源代码TinyExpr ( https://github.com/codeplea/tinyexpr ) 在Visual Studio 2005 的 C 兼容代码(我认为它是符合 C89/90)。特别是我在转换这个宏时遇到问题(存在于tinyexpr.c中):

#define NEW_EXPR(type, ...) new_expr((type), (const te_expr*[]){__VA_ARGS__})

与:

typedef struct te_expr {
int type;
union {double value; const double *bound; const void *function;};
void *parameters[1];
} te_expr;

static te_expr *new_expr(const int type, const te_expr *parameters[]) {
...
}

static te_expr *base(state *s) {
...
}

问题出现在调用中:NEW_EXPR(TE_FUNCTION1 | TE_FLAG_PURE, base(s));我有一个:

error C2059: sintax error : '{'

最佳答案

正如我在评论中所说,在旧版编译器中确实没有很好的方法来做到这一点。

一种可能的方法是使用包装器可变参数函数,该函数动态创建数组,调用真正的 new_expr 函数,然后释放临时数组。

也许是这样的

te_expr *new_expr_va(const int type, ...) {
re_expr *result = NULL; /* The resulting expression structure to return */
size_t argument_count = 0; /* The number of te_expr arguments passed */

/*
* We do two passes over the arguments, once to get the number of them,
* and once to get the actual values.
*/

/* First get the number of arguments */
{
va_list va;

va_start(va, type);

/* Loop until we get a null pointer */
while (va_arg(va, te_expr *) != NULL)
++argument_count;

va_end(va);
}

/* Now allocate the array */
te_expr **parameters = malloc(argument_count * sizeof(te_expr *));

/* And populate the array */
{
va_list va;
size_t index = 0;
te_expr *expr;

va_start(va, type);

/* Get all arguments and add to the allocated array */
while ((expr = va_arg(va, te_expr *)) != NULL)
parameters[index++] = expr;

va_end(va);
}

/* Now we call the actual function */
result = new_expr(type, parameters);

/* Free the memory we allocated for the array */
free(parameters);

/* And return the result */
return result;
}

可以使用如

/* Argument list must be terminated by a NULL */
new_expr_va(TE_FUNCTION1 | TE_FLAG_PURE, base(s), NULL);

请注意,这一切都未经测试,并且没有任何类型的错误检查。

关于c - 如何将C代码转换为旧的Visual Studio 2005,特别是TinyExpr源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58517576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com