Expression: Printing should allow stream to be set

This commit is contained in:
shahramn 2024-07-18 15:39:29 +01:00
parent 9dc8fc792e
commit 608cfd507a
22 changed files with 108 additions and 94 deletions

View File

@ -137,8 +137,8 @@ static int execute(grib_action* a, grib_handle* h)
}
else {
grib_context_log(h->context, GRIB_LOG_ERROR, "Assertion failure: ");
grib_expression_print(h->context, self->expression, h);
printf("\n");
grib_expression_print(h->context, self->expression, h, stderr);
fprintf(stderr, "\n");
return GRIB_ASSERTION_FAILURE;
}
}

View File

@ -154,9 +154,9 @@ static int create_accessor(grib_section* p, grib_action* act, grib_loader* h)
next = a->block_false;
if (p->h->context->debug > 1) {
printf("EVALUATE create_accessor_handle ");
grib_expression_print(p->h->context, a->expression, p->h);
printf(" [%s][_if%p]\n", (next == a->block_true ? "true" : "false"), (void*)a);
fprintf(stderr, "EVALUATE create_accessor_handle ");
grib_expression_print(p->h->context, a->expression, p->h, stderr);
fprintf(stderr, " [%s][_if%p]\n", (next == a->block_true ? "true" : "false"), (void*)a);
/*grib_dump_action_branch(stdout,next,5);*/
}
@ -176,8 +176,8 @@ static int create_accessor(grib_section* p, grib_action* act, grib_loader* h)
static void print_expression_debug_info(grib_context* ctx, grib_expression* exp, grib_handle* h)
{
grib_expression_print(ctx, exp, h); /* writes to stdout without a newline */
printf("\n");
grib_expression_print(ctx, exp, h, stderr); /* writes without a newline */
fprintf(stderr, "\n");
}
static int execute(grib_action* act, grib_handle* h)
@ -240,7 +240,7 @@ static void dump(grib_action* act, FILE* f, int lvl)
grib_context_print(act->context, f, " ");
printf("if(%s) { ", act->name);
grib_expression_print(act->context, a->expression, 0);
grib_expression_print(act->context, a->expression, 0, stdout);
printf("\n");
if (a->block_true) {
@ -252,7 +252,7 @@ static void dump(grib_action* act, FILE* f, int lvl)
for (i = 0; i < lvl; i++)
grib_context_print(act->context, f, " ");
printf("else(%s) { ", act->name);
grib_expression_print(act->context, a->expression, 0);
grib_expression_print(act->context, a->expression, 0, stdout);
/* grib_context_print(act->context,f,"ELSE \n" );*/
grib_dump_action_branch(f, a->block_false, lvl + 1);
}

View File

@ -149,7 +149,7 @@ static void dump(grib_action* act, FILE* f, int lvl)
grib_context_print(act->context, f, " ");
printf("when(%s) { ", act->name);
grib_expression_print(act->context, a->expression, 0);
grib_expression_print(act->context, a->expression, 0, stdout);
printf("\n");
grib_dump_action_branch(f, a->block_true, lvl + 1);
@ -195,8 +195,8 @@ static int notify_change(grib_action* a, grib_accessor* observer, grib_accessor*
if (self->loop) {
printf("LOOP detected...\n");
printf("WHEN triggered by %s %ld\n", observed->name, lres);
grib_expression_print(observed->context, self->expression, 0);
printf("\n");
grib_expression_print(observed->context, self->expression, 0, stderr);
fprintf(stderr, "\n");
return ret;
}
#endif
@ -206,8 +206,8 @@ static int notify_change(grib_action* a, grib_accessor* observer, grib_accessor*
grib_context_log(hand->context, GRIB_LOG_DEBUG,
"------------- SECTION action %s is triggered by [%s] (%s)",
a->name, observed->name, a->debug_info ? a->debug_info : "no debug info");
//grib_expression_print(observed->context, self->expression, 0);
//fprintf(stderr, "\n");
grib_expression_print(observed->context, self->expression, 0, stderr);
fprintf(stderr, "\n");
}
if (lres)

View File

@ -830,7 +830,7 @@ int grib_expression_evaluate_long(grib_handle* h, grib_expression* g, long* resu
int grib_expression_evaluate_double(grib_handle* h, grib_expression* g, double* result);
const char* grib_expression_evaluate_string(grib_handle* h, grib_expression* g, char* buf, size_t* size, int* err);
const char* grib_expression_get_name(grib_expression* g);
void grib_expression_print(grib_context* ctx, grib_expression* g, grib_handle* f);
void grib_expression_print(grib_context* ctx, grib_expression* g, grib_handle* f, FILE*);
void grib_expression_free(grib_context* ctx, grib_expression* g);
void grib_expression_add_dependency(grib_expression* e, grib_accessor* observer);
grib_arguments* grib_arguments_new(grib_context* c, grib_expression* g, grib_arguments* n);

View File

@ -891,7 +891,7 @@ typedef int (*expression_evaluate_double_proc)(grib_expression*, grib_handle*, d
typedef const char* (*expression_evaluate_string_proc)(grib_expression*, grib_handle*, char*, size_t*, int*);
typedef const char* (*expression_get_name_proc)(grib_expression*);
typedef void (*expression_print_proc)(grib_context*, grib_expression*, grib_handle*);
typedef void (*expression_print_proc)(grib_context*, grib_expression*, grib_handle*, FILE*);
typedef void (*expression_add_dependency_proc)(grib_expression* e, grib_accessor* observer);
typedef struct grib_expression_class grib_expression_class;

View File

@ -74,12 +74,12 @@ const char* grib_expression_get_name(grib_expression* g)
return 0;
}
void grib_expression_print(grib_context* ctx, grib_expression* g, grib_handle* f)
void grib_expression_print(grib_context* ctx, grib_expression* g, grib_handle* f, FILE* out)
{
grib_expression_class* c = g->cclass;
while (c) {
if (c->print) {
c->print(ctx, g, f);
c->print(ctx, g, f, out);
return;
}
c = c->super ? *(c->super) : NULL;
@ -163,7 +163,7 @@ void grib_arguments_print(grib_context* c, grib_arguments* g, grib_handle* f)
{
if (g) {
if (g->expression)
grib_expression_print(c, g->expression, f);
grib_expression_print(c, g->expression, f, stdout);
if (g->next) {
printf(",");
grib_arguments_print(c, g->next, f);

View File

@ -42,7 +42,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*, FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static string get_name(grib_expression* e);
static int native_type(grib_expression*,grib_handle*);
@ -128,16 +128,16 @@ static string evaluate_string(grib_expression* g, grib_handle* h, char* buf, siz
return buf;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
const grib_expression_accessor* e = (grib_expression_accessor*)g;
printf("access('%s", e->name);
fprintf(out, "access('%s", e->name);
if (f) {
long s = 0;
grib_get_long(f, e->name, &s);
printf("=%ld", s);
fprintf(out, "=%ld", s);
}
printf("')");
fprintf(out, "')");
}
static void destroy(grib_context* c, grib_expression* g)

View File

@ -42,7 +42,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*, FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static int native_type(grib_expression*,grib_handle*);
static int evaluate_long(grib_expression*,grib_handle*,long*);
@ -144,14 +144,28 @@ static int evaluate_double(grib_expression* g, grib_handle* h, double* dres)
return GRIB_SUCCESS;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
grib_expression_binop* e = (grib_expression_binop*)g;
printf("binop(");
grib_expression_print(c, e->left, f);
printf(",");
grib_expression_print(c, e->right, f);
printf(")");
// Cover a subset of the most commonly used functions
// TODO(masn): Can be done in a much better way!
// e.g., the yacc parser passing in the functions name
if (e->long_func && e->long_func == grib_op_eq) {
fprintf(out, "equals(");
} else if (e->long_func && e->long_func == grib_op_ne) {
fprintf(out, "not_equals(");
} else if (e->long_func && e->long_func == grib_op_lt) {
fprintf(out, "less_than(");
} else if (e->long_func && e->long_func == grib_op_gt) {
fprintf(out, "greater_than(");
} else {
fprintf(out, "binop(");
}
grib_expression_print(c, e->left, f, out);
fprintf(out, ",");
grib_expression_print(c, e->right, f, out);
fprintf(out, ")");
}
static void destroy(grib_context* c, grib_expression* g)

View File

@ -38,7 +38,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*, FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static int native_type(grib_expression*,grib_handle*);
static int evaluate_long(grib_expression*,grib_handle*,long*);
@ -85,10 +85,10 @@ static int evaluate_double(grib_expression* g, grib_handle* h, double* dres)
return GRIB_SUCCESS;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
const grib_expression_double* e = (grib_expression_double*)g;
printf("double(%g)", e->value);
fprintf(out, "double(%g)", e->value);
}
static void destroy(grib_context* c, grib_expression* g)

View File

@ -38,7 +38,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*,FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static int native_type(grib_expression*,grib_handle*);
static int evaluate_long(grib_expression*,grib_handle*,long*);
@ -216,12 +216,12 @@ static int evaluate_long(grib_expression* g, grib_handle* h, long* lres)
return GRIB_NOT_IMPLEMENTED;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
const grib_expression_functor* e = (grib_expression_functor*)g;
printf("%s(", e->name);
fprintf(out, "%s(", e->name);
// grib_expression_print(c,e->args,f);
printf(")");
fprintf(out, ")");
}
static void destroy(grib_context* c, grib_expression* g)

View File

@ -39,7 +39,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*,FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static string get_name(grib_expression* e);
static int native_type(grib_expression*,grib_handle*);
@ -200,16 +200,16 @@ static string evaluate_string(grib_expression* g, grib_handle* h, char* buf, siz
// return buf;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
const grib_expression_is_in_dict* e = (grib_expression_is_in_dict*)g;
printf("access('%s", e->key);
fprintf(out, "access('%s", e->key);
if (f) {
long s = 0;
grib_get_long(f, e->key, &s);
printf("=%ld", s);
fprintf(out, "=%ld", s);
}
printf("')");
fprintf(out, "')");
}
grib_expression* new_is_in_dict_expression(grib_context* c, const char* name, const char* list)

View File

@ -41,7 +41,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*,FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static string get_name(grib_expression* e);
static int native_type(grib_expression*,grib_handle*);
@ -199,16 +199,16 @@ static string evaluate_string(grib_expression* g, grib_handle* h, char* buf, siz
return buf;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
const grib_expression_is_in_list* e = (grib_expression_is_in_list*)g;
printf("access('%s", e->name);
fprintf(out, "access('%s", e->name);
if (f) {
long s = 0;
grib_get_long(f, e->name, &s);
printf("=%ld", s);
fprintf(out, "=%ld", s);
}
printf("')");
fprintf(out, "')");
}
static void destroy(grib_context* c, grib_expression* g)

View File

@ -42,7 +42,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*, FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static string get_name(grib_expression* e);
static int native_type(grib_expression*,grib_handle*);
@ -140,7 +140,7 @@ static string evaluate_string(grib_expression* g, grib_handle* h, char* buf, siz
return buf;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
// grib_expression_is_integer* e = (grib_expression_is_integer*)g;
// printf("access('%s", e->name);

View File

@ -42,7 +42,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*,FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static string get_name(grib_expression* e);
static int native_type(grib_expression*,grib_handle*);
@ -123,16 +123,16 @@ static string evaluate_string(grib_expression* g, grib_handle* h, char* buf, siz
return buf;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
const grib_expression_length* e = (grib_expression_length*)g;
printf("access('%s", e->name);
fprintf(out, "access('%s", e->name);
if (f) {
long s = 0;
grib_get_long(f, e->name, &s);
printf("=%ld", s);
fprintf(out, "=%ld", s);
}
printf("')");
fprintf(out, "')");
}
static void destroy(grib_context* c, grib_expression* g)

View File

@ -39,7 +39,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*, FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static int native_type(grib_expression*,grib_handle*);
static int evaluate_long(grib_expression*,grib_handle*,long*);
@ -133,14 +133,14 @@ static int evaluate_double(grib_expression* g, grib_handle* h, double* dres)
return ret;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
grib_expression_logical_and* e = (grib_expression_logical_and*)g;
printf("(");
grib_expression_print(c, e->left, f);
printf(" && ");
grib_expression_print(c, e->right, f);
printf(")");
fprintf(out, "(");
grib_expression_print(c, e->left, f, out);
fprintf(out, " && ");
grib_expression_print(c, e->right, f, out);
fprintf(out, ")");
}
static void destroy(grib_context* c, grib_expression* g)

View File

@ -39,7 +39,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*,FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static int native_type(grib_expression*,grib_handle*);
static int evaluate_long(grib_expression*,grib_handle*,long*);
@ -134,14 +134,14 @@ static int evaluate_double(grib_expression* g, grib_handle* h, double* dres)
return ret;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
grib_expression_logical_or* e = (grib_expression_logical_or*)g;
printf("(");
grib_expression_print(c, e->left, f);
printf(" || ");
grib_expression_print(c, e->right, f);
printf(")");
fprintf(out, "(");
grib_expression_print(c, e->left, f, out);
fprintf(out, " || ");
grib_expression_print(c, e->right, f, out);
fprintf(out, ")");
}
static void destroy(grib_context* c, grib_expression* g)

View File

@ -38,7 +38,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*,FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static int native_type(grib_expression*,grib_handle*);
static int evaluate_long(grib_expression*,grib_handle*,long*);
@ -85,10 +85,10 @@ static int evaluate_double(grib_expression* g, grib_handle* h, double* dres)
return GRIB_SUCCESS;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
const grib_expression_long* e = (grib_expression_long*)g;
printf("long(%ld)", e->value);
fprintf(out, "long(%ld)", e->value);
}
static void destroy(grib_context* c, grib_expression* g)

View File

@ -37,7 +37,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*,FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static int native_type(grib_expression*,grib_handle*);
static string evaluate_string(grib_expression*,grib_handle*,char*,size_t*,int*);
@ -76,10 +76,10 @@ static const char* evaluate_string(grib_expression* g, grib_handle* h, char* buf
return e->value;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
const grib_expression_string* e = (grib_expression_string*)g;
printf("string('%s')", e->value);
fprintf(out, "string('%s')", e->value);
}
static void destroy(grib_context* c, grib_expression* g)

View File

@ -40,7 +40,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*,FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static int native_type(grib_expression*,grib_handle*);
static int evaluate_long(grib_expression*,grib_handle*,long*);
@ -128,14 +128,14 @@ static int evaluate_double(grib_expression* g, grib_handle* h, double* dres)
return ret;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
grib_expression_string_compare* e = (grib_expression_string_compare*)g;
printf("string_compare(");
grib_expression_print(c, e->left, f);
printf(",");
grib_expression_print(c, e->right, f);
printf(")");
fprintf(out, "string_compare(");
grib_expression_print(c, e->left, f, out);
fprintf(out, ",");
grib_expression_print(c, e->right, f, out);
fprintf(out, ")");
}
static void destroy(grib_context* c, grib_expression* g)

View File

@ -37,7 +37,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*,FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static int native_type(grib_expression*,grib_handle*);
static string evaluate_string(grib_expression*,grib_handle*,char*,size_t*,int*);
@ -76,10 +76,10 @@ static const char* evaluate_string(grib_expression* g, grib_handle* h, char* buf
return e->value;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
const grib_expression_sub_string* e = (grib_expression_sub_string*)g;
printf("string('%s')", e->value);
fprintf(out, "string('%s')", e->value);
}
static void destroy(grib_context* c, grib_expression* g)

View File

@ -37,7 +37,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*,FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static int native_type(grib_expression*,grib_handle*);
static int evaluate_long(grib_expression*,grib_handle*,long*);
@ -81,10 +81,10 @@ static int evaluate_double(grib_expression* g, grib_handle* h, double* dres)
return GRIB_SUCCESS;
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
printf("true(");
printf(")");
fprintf(out, "true(");
fprintf(out, ")");
}
static void destroy(grib_context* c, grib_expression* g)

View File

@ -41,7 +41,7 @@ or edit "expression.class" and rerun ./make_class.pl
typedef const char* string; /* to keep make_class.pl happy */
static void destroy(grib_context*,grib_expression* e);
static void print(grib_context*,grib_expression*,grib_handle*);
static void print(grib_context*,grib_expression*,grib_handle*,FILE*);
static void add_dependency(grib_expression* e, grib_accessor* observer);
static string get_name(grib_expression* e);
static int native_type(grib_expression*,grib_handle*);
@ -105,12 +105,12 @@ static const char* get_name(grib_expression* g)
return grib_expression_get_name(e->exp);
}
static void print(grib_context* c, grib_expression* g, grib_handle* f)
static void print(grib_context* c, grib_expression* g, grib_handle* f, FILE* out)
{
grib_expression_unop* e = (grib_expression_unop*)g;
printf("unop(");
grib_expression_print(c, e->exp, f);
printf(")");
fprintf(out, "unop(");
grib_expression_print(c, e->exp, f, out);
fprintf(out, ")");
}
static void destroy(grib_context* c, grib_expression* g)