-
Notifications
You must be signed in to change notification settings - Fork 0
/
sexpr.c
241 lines (220 loc) · 6.11 KB
/
sexpr.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "sexpr.h"
#define ILLEGAL -999999999
struct s_token* global_it;
void show_token(struct s_token* head) {
if (head == NULL) {
printf("Invalid Expression\n");
return;
}
struct s_token* it = head;
printf("(");
while (it != NULL) {
printf("%s", it->val);
it = it->next;
if (it != NULL) {
printf(" ");
}
}
printf(")\n");
}
struct s_token* build_expr(const char* exp) {
const char* const_op = "()+-*/:";
int expr_len = strlen(exp);
int expr_space_len = 0;
char* expr_space = (char*)malloc(expr_len * 3 + 1);
for (int i = 0; i < expr_len; i++) {
if (strchr(const_op, exp[i]) != NULL) {
expr_space[expr_space_len++] = ' ';
}
expr_space[expr_space_len++] = exp[i];
if (strchr(const_op, exp[i]) != NULL) {
expr_space[expr_space_len++] = ' ';
}
}
expr_space[expr_space_len++] = '\0';
char* tok = strtok(expr_space, " ");
struct s_token* head = NULL;
struct s_token* tail = NULL;
while (tok != NULL) {
struct s_token* new = create_token(tok);
if (head == NULL) {
head = tail = new;
} else {
tail->next = new;
tail = new;
}
tok = strtok(NULL, " ");
}
free(expr_space);
expr_space = NULL;
return head;
}
int eval_recursive() {
if (global_it == NULL) {
return ILLEGAL;
}
int result = ILLEGAL;
int first = 0;
int second = 0;
if (is_number(global_it->val) == 1) {
result = atoi(global_it->val);
global_it = global_it->next;
} else if (strcmp(global_it->val, "(") == 0) {
global_it = global_it->next;
result = eval_recursive();
if (global_it == NULL || strcmp(global_it->val, ")") != 0) { // Illegal
result = ILLEGAL;
} else {
global_it = global_it->next;
}
} else if (strcmp(global_it->val, "+") == 0) {
global_it = global_it->next;
first = eval_recursive();
if (first == ILLEGAL) {
return ILLEGAL;
}
second = eval_recursive();
if (second == ILLEGAL) {
return ILLEGAL;
}
result = first + second;
} else if (strcmp(global_it->val, "-") == 0) {
global_it = global_it->next;
first = eval_recursive();
if (first == ILLEGAL) {
return ILLEGAL;
}
second = eval_recursive();
if (second == ILLEGAL) {
return ILLEGAL;
}
result = first - second;
} else if (strcmp(global_it->val, "*") == 0) {
global_it = global_it->next;
first = eval_recursive();
if (first == ILLEGAL) {
return ILLEGAL;
}
second = eval_recursive();
if (second == ILLEGAL) {
return ILLEGAL;
}
result = first * second;
} else if (strcmp(global_it->val, "/") == 0) {
global_it = global_it->next;
first = eval_recursive();
if (first == ILLEGAL) {
return ILLEGAL;
}
second = eval_recursive();
if (second == ILLEGAL) {
return ILLEGAL;
}
result = first / second;
} else {
result = ILLEGAL;
}
return result;
}
struct s_token* parse_recursive() {
if (global_it == NULL) {
return NULL;
}
struct s_token* head = NULL;
struct s_token* tail = NULL;
struct s_token* new = NULL;
int new_val;
char buf[100];
while (global_it != NULL && strcmp(global_it->val, ")")) {
if (strcmp(global_it->val, "(") == 0) {
global_it = global_it->next;
new = parse_recursive();
if (global_it == NULL) {
return NULL;
}
if (strcmp(global_it->val, ")") != 0) { // Illegal
return NULL;
}
global_it = global_it->next;
} else if (strcmp(global_it->val, ":") == 0) {
global_it = global_it->next;
} else if (strcmp(global_it->val, "eval") == 0) {
global_it = global_it->next;
if (global_it != NULL && !strcmp(global_it->val, ":")) {
global_it = global_it->next;
}
new_val = eval_recursive();
if (new_val == ILLEGAL) {
return NULL;
}
sprintf(buf, "%d", new_val);
new = create_token(buf);
} else {
new = create_token(global_it->val);
global_it = global_it->next;
}
if (head == NULL) {
head = tail = new;
} else {
tail->next = new;
tail = get_tail(tail);
}
}
return head;
}
struct s_token* parse_expr(struct s_token* head) {
global_it = head;
struct s_token* result = parse_recursive();
return result;
}
struct s_token* create_token(const char* val) {
struct s_token* new = (struct s_token*)malloc(sizeof(struct s_token));
new->next = NULL;
new->val = (char*)malloc(strlen(val) + 1); // NUL terminator
memcpy(new->val, val, strlen(val) + 1); // NUL terminator
return new;
}
struct s_token* get_tail(struct s_token* cur) {
if (cur == NULL) {
return NULL;
}
struct s_token* it = cur;
while (it->next != NULL) {
it = it->next;
}
return it;
}
int is_number(const char* s) {
int len = strlen(s);
for (int i = 0; i < len; i++) {
if (i == 0 && s[i] == '-') {
continue;
}
if (s[i] <'0' || s[i] > '9') {
return 0;
}
}
return 1;
}
void destroy_expr(struct s_token* head) {
struct s_token* it = head;
while (it != NULL) {
head = it->next;
it->next = NULL;
free(it->val);
it->val = NULL;
free(it);
it = head;
}
head = NULL;
}
void evaluate_exp(const char* exp) {
struct s_token* expr = build_expr(exp);
struct s_token* expr_evaluated = parse_expr(expr);
show_token(expr_evaluated);
destroy_expr(expr);
destroy_expr(expr_evaluated);
}