-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode_in.c
33 lines (31 loc) · 1.07 KB
/
node_in.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<assert.h>
#include<stdlib.h>
#include<memory.h>
#include "node.h"
#include "data.h"
#include "util.h"
#include "hashmap.h"
/* evaluate, allocate and return a data struct. */
Data *node_in_evaluate(Node *node, map_t context) {
LURE_ASSERT(node != NULL, "cannot evaluate against an empty node");
LURE_ASSERT(node->left != NULL, "left side of an <in> operation must not be NULL");
LURE_ASSERT(node->list != NULL, "list of an <in> node must not be NULL");
if (node->list->n_all == 0) {
return NewBoolData(false);
}
Data *leftRes = node->left->evaluate(node->left, context);
bool flag = node_list_in(node->list, leftRes, context);
leftRes->clean(leftRes); free(leftRes);
return flag ? NewBoolData(true) : NewBoolData(false);
}
/* An IN node has left and list. */
Node *NewNodeIn(Node *left, NodeList *list) {
Node *node = (Node *)calloc(1, sizeof(Node));
node->type = NodeType_BinOp;
node->data = NULL;
node->left = left;
node->right = NULL;
node->list = list;
node->evaluate = node_in_evaluate;
return node;
}