-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
270 lines (227 loc) · 6.68 KB
/
main.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define TOKEN_EOF 1
#define TOKEN_INT 2
#define TOKEN_ADD 3
#define TOKEN_SUB 4
#define TOKEN_MUL 5
#define TOKEN_DIV 6
#define TOKEN_LPAREN 7
#define TOKEN_RPAREN 8
/********************\
| * LEXER * |
\********************/
struct Lexer {
int position;
char* input;
};
struct Token {
int type;
char value[100];
};
struct Token lexer_get_next_token(struct Lexer* self) {
struct Token token = {0, ""};
while (self->input[self->position] != '\0') {
if (isdigit(self->input[self->position])) {
token.type = TOKEN_INT;
int i = 0;
while (isdigit(self->input[self->position])) {
token.value[i] = self->input[self->position];
i++;
self->position++;
}
token.value[i] = '\0';
break;
} else if (self->input[self->position] == '+') {
token.type = TOKEN_ADD;
self->position++;
break;
} else if (self->input[self->position] == '-') {
token.type = TOKEN_SUB;
self->position++;
break;
} else if (self->input[self->position] == '*') {
token.type = TOKEN_MUL;
self->position++;
break;
} else if (self->input[self->position] == '/') {
token.type = TOKEN_DIV;
self->position++;
break;
} else if (self->input[self->position] == '(') {
token.type = TOKEN_LPAREN;
self->position++;
break;
} else if (self->input[self->position] == ')') {
token.type = TOKEN_RPAREN;
self->position++;
break;
} else {
self->position++;
}
}
if (self->input[self->position] == '\0') {
token.type = TOKEN_EOF;
}
return token;
}
struct Token lexer_peek_next_token(struct Lexer* self) {
int position = self->position;
struct Token token = lexer_get_next_token(self);
self->position = position;
return token;
}
struct Token lexer_eat_token(struct Lexer* self, int type) {
struct Token token = lexer_get_next_token(self);
if (token.type != type) {
printf("expected: %d, got: %d\n", type, token.type);
printf("Invalid syntax\n");
exit(1);
}
return token;
}
struct Token lexer_eat_token_value(struct Lexer* self, int type, char* value) {
struct Token token = lexer_get_next_token(self);
if (token.type != type || strcmp(token.value, value) != 0) {
printf("Invalid syntax\n");
exit(1);
}
return token;
}
/********************\
| * PARSER * |
\********************/
struct Parser {
struct Lexer* lexer;
struct Token current_token;
};
struct Node {
int type;
int value;
struct Node* left;
struct Node* right;
};
struct Parser parser_init(struct Lexer* lexer) {
struct Parser parser = {lexer, lexer_get_next_token(lexer)};
return parser;
}
void parser_eat(struct Parser* self, int type) {
if (self->current_token.type == type) {
self->current_token = lexer_get_next_token(self->lexer);
} else {
printf("Invalid syntax\n");
exit(1);
}
}
struct Node* parse_int(struct Parser* self) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->type = TOKEN_INT;
node->value = atoi(self->current_token.value);
parser_eat(self, TOKEN_INT);
return node;
}
struct Node* parser_expr(struct Parser* self); // forward declaration because c is a pain
struct Node* parser_factor(struct Parser* self) {
if (self->current_token.type == TOKEN_INT) {
struct Node* node = parse_int(self);
return node;
}
if (self->current_token.type == TOKEN_LPAREN) {
parser_eat(self, TOKEN_LPAREN);
struct Node* node = parser_expr(self);
parser_eat(self, TOKEN_RPAREN);
return node;
}
}
struct Node* parser_term(struct Parser* self) {
struct Node* node = parser_factor(self);
while ((self->current_token.type == TOKEN_MUL) || (self->current_token.type == TOKEN_DIV)) {
struct Token token = self->current_token;
parser_eat(self, token.type);
struct Node* right = parser_factor(self);
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->type = token.type;
new_node->left = node;
new_node->right = right;
node = new_node;
}
return node;
}
struct Node* parser_expr(struct Parser* self) {
struct Node* node = parser_term(self);
while ((self->current_token.type == TOKEN_ADD) || (self->current_token.type == TOKEN_SUB)) {
struct Token token = self->current_token;
parser_eat(self, token.type);
struct Node* right = parser_term(self);
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
new_node->type = token.type;
new_node->left = node;
new_node->right = right;
node = new_node;
}
return node;
}
/********************\
| * MAIN * |
\********************/
void print_node(struct Node node) {
if (node.type == TOKEN_INT) {
printf("%d", node.value);
} else {
print_node(*node.left);
if (node.type == TOKEN_ADD) {
printf("+");
} else if (node.type == TOKEN_SUB) {
printf("-");
} else if (node.type == TOKEN_MUL) {
printf("*");
} else if (node.type == TOKEN_DIV) {
printf("/");
}
print_node(*node.right);
}
}
void free_node(struct Node* node) {
if (node->type != TOKEN_INT) {
free_node(node->left);
free_node(node->right);
}
free(node);
}
int interpret(struct Node* node) {
if (node->type == TOKEN_INT) {
return node->value;
} else {
int left = interpret(node->left);
int right = interpret(node->right);
if (node->type == TOKEN_ADD) {
return left + right;
} else if (node->type == TOKEN_SUB) {
return left - right;
} else if (node->type == TOKEN_MUL) {
return left * right;
} else if (node->type == TOKEN_DIV) {
return left / right;
}
}
printf("Invalid syntax\n");
exit(1);
}
int main() {
// get user input
printf("Enter an expression: ");
char input[100];
fgets(input, 100, stdin);
// tokenize
struct Lexer lexer = {0, input};
// parse the input
struct Parser parser = parser_init(&lexer);
struct Node* ast = parser_expr(&parser);
// interpret
printf("Result: %d\n", interpret(ast));
// free the memory
free_node(ast);
return 0;
}