Move filter files into src/
- Makefile still needs work, is not compiling
This commit is contained in:
committed by
David Adrian
parent
9693b66024
commit
8607c2574b
69
src/expression.c
Normal file
69
src/expression.c
Normal file
@ -0,0 +1,69 @@
|
||||
#include "tree.h"
|
||||
|
||||
node_t* alloc_node()
|
||||
{
|
||||
node_t *node = (node_t*) malloc(sizeof(node_t));
|
||||
memset(node, 0, sizeof(node_t));
|
||||
return node;
|
||||
}
|
||||
|
||||
node_t* make_op_node(enum operation op)
|
||||
{
|
||||
node_t* node = alloc_node();
|
||||
node->type = OP;
|
||||
node->value.op = op;
|
||||
return node;
|
||||
}
|
||||
|
||||
node_t* make_field_node(char *fieldname)
|
||||
{
|
||||
node_t *node = alloc_node();
|
||||
node->type = FIELD;
|
||||
node->value.fieldname = fieldname;
|
||||
return node;
|
||||
}
|
||||
|
||||
node_t* make_string_node(char *literal)
|
||||
{
|
||||
node_t *node = alloc_node();
|
||||
node->type = STRING;
|
||||
node->value.string_literal = literal;
|
||||
return node;
|
||||
}
|
||||
|
||||
node_t* make_int_node(int literal)
|
||||
{
|
||||
node_t *node = alloc_node();
|
||||
node->type = INT;
|
||||
node->value.int_literal = literal;
|
||||
return node;
|
||||
}
|
||||
|
||||
int evaluate_expression(node_t *root) {
|
||||
int result = 1;
|
||||
return result;
|
||||
}
|
||||
|
||||
void print_expression(node_t *root) {
|
||||
if (!root) return;
|
||||
printf("%s", "( ");
|
||||
print_expression(root->left_child);
|
||||
switch (root->type) {
|
||||
case OP:
|
||||
printf(" %i ", root->value.op);
|
||||
break;
|
||||
case FIELD:
|
||||
printf(" (%s", root->value.fieldname);
|
||||
break;
|
||||
case STRING:
|
||||
printf("%s) ", root->value.string_literal);
|
||||
break;
|
||||
case INT:
|
||||
printf(" %d) ", root->value.int_literal);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
print_expression(root->right_child);
|
||||
printf("%s", " )");
|
||||
}
|
44
src/expression.h
Normal file
44
src/expression.h
Normal file
@ -0,0 +1,44 @@
|
||||
#ifndef ZMAP_TREE_H
|
||||
#define ZMAP_TREE_H
|
||||
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
enum operation {
|
||||
GT, LT, EQ, NEQ, AND, OR, LT_EQ, GT_EQ
|
||||
};
|
||||
|
||||
enum node_type {
|
||||
OP, FIELD, STRING, INT
|
||||
};
|
||||
|
||||
union node_value {
|
||||
enum operation op;
|
||||
char *fieldname;
|
||||
char *string_literal;
|
||||
int int_literal;
|
||||
};
|
||||
|
||||
typedef struct node {
|
||||
struct node *left_child;
|
||||
struct node *right_child;
|
||||
enum node_type type;
|
||||
union node_value value;
|
||||
} node_t;
|
||||
|
||||
node_t* alloc_node();
|
||||
|
||||
node_t* make_op_node(enum operation op);
|
||||
|
||||
node_t* make_field_node(char *fieldname);
|
||||
|
||||
node_t* make_string_node(char *literal);
|
||||
|
||||
node_t* make_int_node(int literal);
|
||||
|
||||
int evaluate_expression(node_t *root);
|
||||
|
||||
void print_expression(node_t *root);
|
||||
|
||||
#endif /* ZMAP_TREE_H */
|
1229
src/zmap.c
1229
src/zmap.c
File diff suppressed because it is too large
Load Diff
22
src/zmap.l
Normal file
22
src/zmap.l
Normal file
@ -0,0 +1,22 @@
|
||||
%{
|
||||
#include <string.h>
|
||||
#include "zmap.tab.h"
|
||||
%}
|
||||
|
||||
%%
|
||||
[0-9]+ yylval.int_literal = atoi(yytext); return T_NUMBER;
|
||||
\n /* Ignore end of line */
|
||||
[ \t]+ /* Ignore whitespace */
|
||||
!= return T_NOT_EQ;
|
||||
>= return T_GT_EQ;
|
||||
"<=" return T_LT_EQ;
|
||||
&& return T_AND;
|
||||
"||" return T_OR;
|
||||
= return '=';
|
||||
">" return '>';
|
||||
"<" return '<';
|
||||
"(" return '(';
|
||||
")" return ')';
|
||||
[a-zA-Z][a-zA-Z0-9]+ yylval.string_literal = strdup(yytext); return T_FIELD;
|
||||
|
||||
%%
|
129
src/zmap.y
Normal file
129
src/zmap.y
Normal file
@ -0,0 +1,129 @@
|
||||
%{
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "expression.h"
|
||||
|
||||
void yyerror(const char *str)
|
||||
{
|
||||
fprintf(stderr,"error: %s\n",str);
|
||||
fprintf(stderr, "%s\n", "YOLO");
|
||||
}
|
||||
|
||||
int yywrap()
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
%}
|
||||
|
||||
%union {
|
||||
int int_literal;
|
||||
char *string_literal;
|
||||
struct node *expr;
|
||||
}
|
||||
|
||||
%token '(' ')' T_AND T_OR
|
||||
%token <int_literal> T_NUMBER
|
||||
%token <string_literal> T_FIELD
|
||||
%token T_NOT_EQ T_GT_EQ '>' '<' '=' T_LT_EQ
|
||||
|
||||
%left T_OR
|
||||
%left T_AND
|
||||
|
||||
%type <expr> filter
|
||||
%type <expr> number_filter
|
||||
%type <expr> string_filter
|
||||
%type <expr> filter_expr
|
||||
|
||||
|
||||
%%
|
||||
|
||||
filter_expr:
|
||||
filter_expr T_OR filter_expr
|
||||
{
|
||||
$$ = make_op_node(OR);
|
||||
$$->left_child = $1;
|
||||
$$->right_child = $3;
|
||||
print_expression($$);
|
||||
printf("%s\n", "");
|
||||
}
|
||||
| filter_expr T_AND filter_expr
|
||||
{
|
||||
$$ = make_op_node(AND);
|
||||
$$->left_child = $1;
|
||||
$$->right_child = $3;
|
||||
print_expression($$);
|
||||
printf("%s\n", "");
|
||||
}
|
||||
| '(' filter_expr ')'
|
||||
{
|
||||
$$ = $2;
|
||||
}
|
||||
| filter
|
||||
{
|
||||
$$ = $1;
|
||||
}
|
||||
;
|
||||
|
||||
filter: number_filter
|
||||
{
|
||||
$$ = $1;
|
||||
}
|
||||
| string_filter
|
||||
{
|
||||
$$ = $1;
|
||||
}
|
||||
;
|
||||
|
||||
number_filter: T_FIELD '=' T_NUMBER
|
||||
{
|
||||
printf("number_filter: %s = %d\n", $1, $3);
|
||||
$$ = make_op_node(EQ);
|
||||
$$->left_child = make_field_node($1);
|
||||
$$->right_child = make_int_node($3);
|
||||
}
|
||||
|
|
||||
T_FIELD '>' T_NUMBER
|
||||
{
|
||||
printf("number_filter: %s > %d\n", $1, $3);
|
||||
$$ = make_op_node(GT);
|
||||
$$->left_child = make_field_node($1);
|
||||
$$->right_child = make_int_node($3);
|
||||
}
|
||||
|
|
||||
T_FIELD '<' T_NUMBER
|
||||
{
|
||||
printf("number_filter: %s < %d\n", $1, $3);
|
||||
}
|
||||
|
|
||||
T_FIELD T_NOT_EQ T_NUMBER
|
||||
{
|
||||
printf("number_filter: %s != %d\n", $1, $3);
|
||||
}
|
||||
|
|
||||
T_FIELD T_GT_EQ T_NUMBER
|
||||
{
|
||||
printf("number_filter: %s >= %d\n", $1, $3);
|
||||
}
|
||||
|
|
||||
T_FIELD T_LT_EQ T_NUMBER
|
||||
{
|
||||
printf("number_filter: %s <= %d\n", $1, $3);
|
||||
}
|
||||
;
|
||||
|
||||
string_filter:
|
||||
T_FIELD '=' T_FIELD
|
||||
{
|
||||
printf("string_filter %s = %s\n", $1, $3);
|
||||
}
|
||||
|
|
||||
T_FIELD T_NOT_EQ T_FIELD
|
||||
{
|
||||
printf("string_filter: %s != %s\n", $1, $3);
|
||||
}
|
||||
;
|
||||
|
||||
%%
|
||||
|
||||
|
Reference in New Issue
Block a user