-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluation.c
53 lines (40 loc) · 1.25 KB
/
evaluation.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "parsing.h"
#include "lval.c"
lval eval_op(lval x, char* op, lval y) {
/* Guard for operands */
if (x.type == LVAL_ERR) {
return x;
}
if (y.type == LVAL_ERR) {
return y;
}
if(strcmp(op, "+") == 0) {return lval_num(x.num + y.num);}
if(strcmp(op, "-") == 0) {return lval_num(x.num - y.num);}
if(strcmp(op, "*") == 0) {return lval_num(x.num * y.num);}
if(strcmp(op, "/") == 0) {
return (y.num == 0)
? lval_err(LERR_DIV_ZERO)
: lval_num(x.num / y.num);
}
return lval_err(LERR_BAD_OP);
}
lval eval(mpc_ast_t* t) {
// printf("%s",t->tag);
if(strstr(t->tag, "number")) {
errno = 0;
long x = strtol(t->contents, NULL, 10);
return errno != ERANGE ? lval_num(x) : lval_err(LERR_BAD_NUM);
}
// the operator is always the second child of an ast
// printf("reading operator as : %s", t->children[1]->contents);
char *op = t->children[1]->contents;
// we store the third child in 'x'
lval x = eval(t->children[2]);
// iterate the remaining children and combining
int i = 3;
while(strstr(t->children[i]->tag, "expr")) {
x = eval_op(x, op, eval(t->children[i]));
i++;
}
return x;
}