-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForInNode.c
35 lines (32 loc) · 866 Bytes
/
ForInNode.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
34
35
#include "ForInNode.h"
#include "_List.h"
#include "Variable.h"
#include "Scope.h"
#include "Node.h"
#include "_Object.h"
#include "NodeSet.h"
#include <stdlib.h>
#include <string.h>
ForInNode *ForInNode_new(const char *var, Node *list, NodeSet *block) {
ForInNode *node = malloc(sizeof(ForInNode));
node->node.type = FORIN;
node->node.eval = &ForInNode_eval;
strcpy(node->var, var);
node->list = list;
node->block = block;
return node;
}
void ForInNode_delete(ForInNode *node) {
free(node);
}
_Object *ForInNode_eval(Node *node, Scope *scope) {
ForInNode *fiNode = (ForInNode *)node;
_List *items = fiNode->list->eval(fiNode->list, scope);
Variable *_x = Scope_set(scope, fiNode->var, NULL);
int i;
for(i = 0; i < _List_length(items); i++) {
Variable_set(_x, (_Object *)_List_get(items, i));
Block_eval(fiNode->block, scope);
}
return NULL;
}