-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.c
84 lines (83 loc) · 2.17 KB
/
evaluate.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//Code for evaluating an expression tree with static memory allocation
#include<stdio.h>
#include "evaluate.h"
//#include "exptree.c" //comment before use
static int MEMORY[26];
int evalCode(tnode *t){
switch(t->ntype){
case OPERATOR:
return calculate(t);
case FUNCTION:
if(t->val)
return read(t->l);
else
return write(t->l);
case CONNECTION:
evalCode(t->l);
evalCode(t->r);
return 0;
case CONTROL:
evalCtrl(t);
return 0;
}
}
int read(tnode *t){
printf("In:");
scanf("%d",&MEMORY[getVar(t,LVAL)]);
return 0;
}
int calculate(tnode *exp){
if(exp->ntype){
switch(exp->var){
case '+':return calculate(exp->l)+calculate(exp->r);
case '-':return calculate(exp->l)-calculate(exp->r);
case '*':return calculate(exp->l)*calculate(exp->r);
case '/':return calculate(exp->l)/calculate(exp->r);
case '%':return calculate(exp->l)%calculate(exp->r);
case '<':if(calculate(exp->l)<calculate(exp->r))return 1;
return 0;
case '>':if(calculate(exp->l)>calculate(exp->r))return 1;
return 0;
case '=':
MEMORY[getVar(exp->l,LVAL)]=calculate(exp->r);return getVar(exp->l,RVAL);
}
}
switch(exp->dtype){
case NUMERIC:return exp->val;
case IDENTIFIER:return getVar(exp,RVAL);
}
}
int write(tnode *t){
int data=calculate(t);
printf("Out:%d\n",data);
return 0;
}
int getVar(tnode *varNode,int val){
int i;
if(val)
i=MEMORY[varNode->var-'a'];
else
i=varNode->var-'a';
return i;
}
int evalCtrl(tnode *t){
switch(t->dtype){
case SIMPLE_IF:
if(evalCode(t->l)){
evalCode(t->r);
return 1;
}
return 0;
case IF_ELSE:
if(evalCode(t->l))
evalCode(t->r);
else
evalCode(t->e);
case WHILE_LOOP:
if(evalCode(t->l)){
evalCode(t->r);
evalCtrl(t);
}
return 0;
}
}