-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrexp.c
213 lines (164 loc) · 4.04 KB
/
rexp.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
#include "rexp.h"
// global variable being used to trace the current position
int curr = 0;
// string_ro_enode(s, node) returns a pointer to a dynamically allocated enode
// which stored the string s in the enode.
// effects: allocates memory
static struct enode *string_to_enode(const char *s, struct enode *node) {
node = malloc(sizeof(struct enode));
node->left = NULL;
node->right = NULL;
while (s[curr] == ')' || s[curr] == ' ') {
curr += 1;
}
char a;
a = s[curr];
if ((a >= '0' && a <= '9') || a == '-') {
node->type = 'n';
int n;
sscanf(s + curr, "%d", &n);
node->number = n;
while (s[curr] != '\0' && s[curr] != ' ' && s[curr] != ')') {
curr += 1;
}
node->left = NULL;
node->right = NULL;
} else if (a == '(' ) {
curr += 1;
sscanf(s + curr, "%c", &a);
node->type = a;
curr += 2;
node->left = string_to_enode(s, node->left);
node->right = string_to_enode(s, node->right);
} else {
node->type = 'v';
int i = 0;
while (s[curr] != ' ' && s[curr] != ')' && s[curr] != '\0') {
node->id[i] = s[curr];
curr += 1;
i += 1;
}
node->id[i] = '\0';
node->left = NULL;
node->right = NULL;
}
return node;
}
// see the interface
struct rexp *string_to_rexp(const char *s) {
struct rexp *r = malloc(sizeof(struct rexp));
r->root = string_to_enode(s, r->root);
//print_rexp(r);
curr = 0;
return r;
}
// eval_enode(node, constants) evalueates the enode
static int eval_enode(struct enode *node, const struct dictionary *constants) {
if (!node) return 0;
if (node->type == 'n') {
return node->number;
} else if (node->type == 'v') {
return dict_lookup(node->id, constants);
} else if (node->type == '+') {
return eval_enode(node->left, constants) +
eval_enode(node->right, constants);
} else if (node->type == '-') {
return eval_enode(node->left, constants) -
eval_enode(node->right, constants);
} else if (node->type == '*') {
return eval_enode(node->left, constants) *
eval_enode(node->right, constants);
} else {
return eval_enode(node->left, constants) /
eval_enode(node->right, constants);
}
}
// see the interface
int rexp_eval(const struct rexp *r, const struct dictionary *constants) {
assert(r);
return eval_enode(r->root, constants);
}
// destroy_enode(node) deallocates the memory allocated to node
// effects :node is no longer valid
static void destroy_enode(struct enode *node) {
if (!node) return;
destroy_enode(node->left);
destroy_enode(node->right);
free(node);
}
// see the interface
void rexp_destroy(struct rexp *r) {
assert(r);
destroy_enode(r->root);
free(r);
}
// see the interface
void add_constant(const char *s, struct dictionary *constants) {
assert(s);
assert(constants);
int i = 0;
while (s[i] != ' ') {
++i;
}
++i;
char *key = malloc(21 * sizeof(char)); // freed below
int curr = 0;
while (s[i] != ' ') {
key[curr] = s[i];
++curr;
++i;
}
key[curr] = '\0';
i += 1;
int len = 0;
int max_len = 32;
char *temp = malloc(sizeof(char) * max_len); // freed below
while (s[i] != '\0') {
if (len == max_len) {
max_len *= 2;
temp = realloc(temp, sizeof(char) * max_len);
}
temp[len] =s[i];
++len;
++i;
}
if (len == max_len) {
max_len *= 2;
temp = realloc(temp, sizeof(char) * max_len);
}
temp[len] = '\0';
++len;
struct rexp *r = string_to_rexp(temp);
int val = rexp_eval(r, constants);
dict_add(key, val, constants);
free(temp);
free(key);
rexp_destroy(r);
}
////////////////////////////////////////////////////
// (below may not be useful for your own testing) //
////////////////////////////////////////////////////
void print_enode(const struct enode *e) {
//assert(e);
if (e->type == 'n') {
printf("%d", e->number);
} else if (e->type == 'v') {
printf("%s", e->id);
} else {
printf("(%c ", e->type);
print_enode(e->left);
printf(" ");
print_enode(e->right);
printf(")");
}
}
void print_rexp(const struct rexp *r) {
assert(r);
print_enode(r->root);
printf("\n");
}