Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bshifter committed Dec 6, 2023
1 parent 3164dd7 commit 015e729
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 15 deletions.
3 changes: 3 additions & 0 deletions lib/filterx/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ filterxinclude_HEADERS = \
lib/filterx/filterx-config.h \
lib/filterx/expr-builtin.h \
lib/filterx/filterx-pipe.h
lib/filterx/filterx-pipe.h \
lib/filterx/expr-function.h


filterx_sources = \
Expand Down Expand Up @@ -56,6 +58,7 @@ filterx_sources = \
lib/filterx/filterx-config.c \
lib/filterx/filterx-pipe.c \
lib/filterx/expr-builtin.c \
lib/filterx/expr-function.c \
lib/filterx/filterx-grammar.y

BUILT_SOURCES += \
Expand Down
49 changes: 40 additions & 9 deletions lib/filterx/expr-function.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,60 @@
#include "filterx/expr-function.h"
#include "scratch-buffers.h"

typedef struct _FilterXFunction
static FilterXObject *
echo(FilterXObject *obj)
{
FilterXExpr super;
gchar *function_name;
GList *argument_expressions;
} FilterXFunction;
GString *buf = scratch_buffers_alloc();
LogMessageValueType t;

if (!filterx_object_marshal(obj, buf, &t))
goto exit;
msg_debug("FILTERX2",
evt_tag_str("value", buf->str),
evt_tag_str("type", log_msg_value_type_to_str(t)));
exit:
return NULL;
}

static FilterXObject *
_eval(FilterXExpr *s)
{
FilterXFunction *self = (FilterXFunction *) s;

// lookup self->function_name
// lookup self->function_name // TODO
// eval arguments filterx_expr_eval(FilterXExpr *self)
// call function implementation with Object list as argument, which returns a FilterXObject
exit:

FilterXExpr *expr;
for (GList *l = self->argument_expressions; l; l = l->next)
{
expr = l->data;
break;
}

// FilterXObject *res =
echo(filterx_expr_eval(expr));
return NULL;
}

static void
_free(FilterXExpr *s)
{
FilterXFunction *self = (FilterXFunction *) s;
g_free(self->function_name);
// g_list_free_all(self->argument_expressions);
// g_free(self->function_name);

// g_list_free_full(self->argument_expressions, (GDestroyNotify) filterx_expr_unref);
// filterx_expr_free_method(s);
filterx_function_free(self);
}

void
filterx_function_free(FilterXFunction *s)
{
g_free(s->function_name);
g_list_free_full(s->argument_expressions, (GDestroyNotify) filterx_expr_unref);
// s->super.free_fn(&(s->super));
filterx_expr_free_method(s->super);
}

/* NOTE: takes the object reference */
Expand All @@ -37,5 +67,6 @@ filterx_function_new(const gchar *function_name, GList *arguments)
self->super.eval = _eval;
self->super.free_fn = _free;
self->function_name = g_strdup(function_name);
self->argument_expressions = arguments;
return &self->super;
}
21 changes: 21 additions & 0 deletions lib/filterx/expr-function.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef FILTERX_EXPR_FUNCTION_H_INCLUDED
#define FILTERX_EXPR_FUNCTION_H_INCLUDED

#include "filterx/filterx-expr.h"
#include "filterx-object.h"

typedef struct _FilterXFunction
{
FilterXExpr super;
gchar *function_name;
GList *argument_expressions;
} FilterXFunction;

// typedef *FilterXObject (*FilterXFunctionProto)(int, ...);
// typedef *FilterXObject (*FilterXFunctionProto)(GList *filterx_objects);
// typedef *FilterXObject (*FilterXFunctionProto)(GPointer[]);

FilterXExpr *filterx_function_new(const gchar *function_name, GList *arguments);
void filterx_function_free(FilterXFunction *s);

#endif
20 changes: 14 additions & 6 deletions lib/filterx/filterx-grammar.ym
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "filterx/object-null.h"
#include "filterx/object-string.h"
#include "filterx/filterx-config.h"
#include "filterx/expr-function.h"

/* legacy filters */
#include "filter/filter-netmask.h"
Expand Down Expand Up @@ -86,7 +87,9 @@ construct_template_expr(LogTemplate *template)
%type <node> expr
%type <node> expr_value
%type <node> function_call
%type <ptr> argument_list
%type <node> function_add
%type <ptr> argument
%type <ptr> arguments
%type <node> literal
%type <ptr> literal_object
%type <node> builtin
Expand All @@ -110,6 +113,7 @@ stmts

stmt
: expr ';' { $$ = $1; }
;

expr
: expr_value { $$ = $1; }
Expand Down Expand Up @@ -146,15 +150,19 @@ expr_value
;

function_call
: LL_IDENTIFIER '(' argument_list ')' { $$ = NULL; /* filterx_function_new($1, $3); */ }
: LL_IDENTIFIER '(' arguments ')' { $$ = filterx_function_new($1, $3); }
;

arguments
: argument arguments { $$ = g_list_prepend($2, $1); }
| { $$ = NULL; }
;

argument_list
: expr ',' argument_list
|
argument
: expr
;

literal: literal_object { $$ = filterx_literal_new(filterx_config_freeze_object(configuration, $1)); }
literal: literal_object { $$ = filterx_literal_new(filterx_config_freeze_object(configuration, $1)); }

literal_object
: LL_NUMBER { $$ = filterx_integer_new($1); }
Expand Down

0 comments on commit 015e729

Please sign in to comment.