-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.c
34 lines (29 loc) · 916 Bytes
/
interpreter.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
#include "interpreter.h"
static long
clisp_eval_operator(long x, char* op, long y)
{
if (strcmp(op, "+") == 0) { return x + y; }
if (strcmp(op, "-") == 0) { return x - y; }
if (strcmp(op, "*") == 0) { return x * y; }
if (strcmp(op, "/") == 0) { return x / y; }
return 0;
}
long
clisp_eval(mpc_ast_t *node)
{
/* If this node is a number, we know it has no children and can be returned directly */
if (strstr(node->tag, "number")) {
return atoi(node->contents);
}
/* second child is always the operator */
char *op = node->children[1]->contents;
/* grab the third child and store it */
long x = clisp_eval(node->children[2]);
/* iterate the remaining children to combine them */
int i = 3;
while (strstr(node->children[i]->tag, "expr")) {
x = clisp_eval_operator(x, op, clisp_eval(node->children[i]));
i++;
}
return x;
}