-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt-expr.c
executable file
·299 lines (275 loc) · 8.85 KB
/
chatgpt-expr.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <math.h> // For power operator
// Define a structure for expression tree nodes
typedef struct Node {
char op; // Operator or '\0' if it's a number, variable, or pointer
int value; // Used if it's a number
char var_name; // Used if it's a variable
char member_name[64]; // For struct member access
struct Node *left;
struct Node *right;
} Node;
// Define a structure for variable storage
typedef struct Variable {
char name;
int value;
void *struct_value; // Pointer to a structure (for struct support)
void *pointer_value; // Pointer for handling pointers
} Variable;
Variable variables[26]; // Support for 26 single-letter variables (a-z)
// Forward declarations
Node *expression();
Node *term();
Node *factor();
Node *unary();
Node *logical();
Node *boolean_expr();
const char *input; // Input string for the parser
// Function to consume whitespace
void skip_whitespace() {
while (isspace(*input)) {
input++;
}
}
// Function to match a specific character
void match(char expected) {
skip_whitespace();
if (*input == expected) {
input++;
} else {
fprintf(stderr, "Error: Expected '%c'\n", expected);
exit(1);
}
}
// Create a new tree node
Node *new_node(char op, int value, Node *left, Node *right) {
Node *node = (Node *)malloc(sizeof(Node));
if (!node) {
fprintf(stderr, "Error: Memory allocation failed\n");
exit(1);
}
node->op = op;
node->value = value;
node->var_name = '\0';
node->member_name[0] = '\0';
node->left = left;
node->right = right;
return node;
}
// Create a new variable node
Node *new_var_node(char name) {
Node *node = (Node *)malloc(sizeof(Node));
if (!node) {
fprintf(stderr, "Error: Memory allocation failed\n");
exit(1);
}
node->op = '\0';
node->value = 0;
node->var_name = name;
node->member_name[0] = '\0';
node->left = NULL;
node->right = NULL;
return node;
}
// Create a new struct member access or pointer dereference node
Node *new_member_or_pointer_node(char op, Node *left, const char *member_name) {
Node *node = (Node *)malloc(sizeof(Node));
if (!node) {
fprintf(stderr, "Error: Memory allocation failed\n");
exit(1);
}
node->op = op;
node->value = 0;
node->var_name = '\0';
strncpy(node->member_name, member_name, sizeof(node->member_name) - 1);
node->member_name[sizeof(node->member_name) - 1] = '\0';
node->left = left;
node->right = NULL;
return node;
}
// Parse a number, variable, or pointer
Node *number_or_variable() {
skip_whitespace();
if (isdigit(*input)) {
int result = 0;
while (isdigit(*input)) {
result = result * 10 + (*input - '0');
input++;
}
return new_node('\0', result, NULL, NULL);
} else if (isalpha(*input)) {
char var_name = *input;
input++;
Node *var_node = new_var_node(var_name);
while (*input == '.' || (*input == '-' && *(input + 1) == '>') || *input == '*') {
if (*input == '*') {
input++;
var_node = new_node('*', 0, var_node, NULL);
} else {
char op = *input;
input += (op == '-') ? 2 : 1; // Skip '.' or '->'
char member_name[64];
int i = 0;
while (isalnum(*input) || *input == '_') {
if (i < sizeof(member_name) - 1) {
member_name[i++] = *input;
}
input++;
}
member_name[i] = '\0';
var_node = new_member_or_pointer_node(op, var_node, member_name);
}
}
return var_node;
} else {
fprintf(stderr, "Error: Expected a number or variable\n");
exit(1);
}
}
// Parse a factor (a number, variable, or parenthesized expression or a power operation)
Node *factor() {
skip_whitespace();
if (*input == '(') {
match('(');
Node *result = expression();
match(')');
return result;
} else {
Node *base = number_or_variable();
if (*input == '^') {
input++;
Node *exponent = factor();
return new_node('^', 0, base, exponent);
}
return base;
}
}
// Parse a unary operator
Node *unary() {
skip_whitespace();
if (*input == '-' || *input == '+' || *input == '*') {
char op = *input;
input++;
Node *operand = unary();
return new_node(op, 0, operand, NULL);
}
return factor();
}
// Parse a term (a unary optionally followed by '*', '/', or '%')
Node *term() {
Node *result = unary();
while (*input == '*' || *input == '/' || *input == '%') {
char op = *input;
input++;
Node *right = unary();
result = new_node(op, 0, result, right);
}
return result;
}
// Parse a boolean expression (a term optionally followed by relational operators)
Node *boolean_expr() {
Node *result = term();
while (*input == '<' || *input == '>' || *input == '=' || (*input == '!' && *(input + 1) == '=')) {
char op = *input;
input++;
if (op == '=' || op == '!') {
match('=');
op = (op == '=') ? 'E' : 'N'; // 'E' for '==' and 'N' for '!='
}
Node *right = term();
result = new_node(op, 0, result, right);
}
return result;
}
// Parse a logical expression (a boolean expression optionally followed by '&&' or '||')
Node *logical() {
Node *result = boolean_expr();
while ((*input == '&' && *(input + 1) == '&') || (*input == '|' && *(input + 1) == '|')) {
char op = (*input == '&') ? 'A' : 'O'; // 'A' for '&&', 'O' for '||'
input += 2;
Node *right = boolean_expr();
result = new_node(op, 0, result, right);
}
return result;
}
// Parse an expression (a logical expression optionally followed by '+' or '-')
Node *expression() {
return logical();
}
// Evaluate the expression tree
int evaluate(Node *node) {
if (!node) return 0;
if (node->op == '\0') {
if (node->var_name != '\0') {
int idx = node->var_name - 'a';
Variable *var = &variables[idx];
if (node->left && node->left->op == '*') { // Dereference pointer
return *((int *)var->pointer_value);
}
return var->value;
}
return node->value;
}
int left_val = node->left ? evaluate(node->left) : 0;
int right_val = node->right ? evaluate(node->right) : 0;
switch (node->op) {
int evaluate(Node *node) {
if (!node) return 0;
if (node->op == '\0') {
if (node->var_name != '\0') {
int idx = node->var_name - 'a';
Variable *var = &variables[idx];
if (node->left && node->left->op == '*') { // Dereference pointer
if (!var->pointer_value) {
fprintf(stderr, "Error: Null pointer dereference\n");
exit(1);
}
return *((int *)var->pointer_value);
}
return var->value;
}
return node->value;
}
int left_val = node->left ? evaluate(node->left) : 0;
int right_val = node->right ? evaluate(node->right) : 0;
switch (node->op) {
case '+': return left_val + right_val;
case '-': return left_val - right_val;
case '*': return left_val * right_val;
case '/':
if (right_val == 0) {
fprintf(stderr, "Error: Division by zero\n");
exit(1);
}
return left_val / right_val;
case '%':
if (right_val == 0) {
fprintf(stderr, "Error: Division by zero\n");
exit(1);
}
return left_val % right_val;
case '^': return pow(left_val, right_val);
case '<': return left_val < right_val;
case '>': return left_val > right_val;
case 'E': return left_val == right_val; // '==' operator
case 'N': return left_val != right_val; // '!=' operator
case 'A': return left_val && right_val; // '&&' operator
case 'O': return left_val || right_val; // '||' operator
case '.':
case '-': // Struct member access
if (node->op == '-' && node->left && node->left->op == '*') {
void *ptr = evaluate(node->left);
return *(int *)((char *)ptr + offsetof(struct_type, node->member_name)); // Adjust as needed
}
fprintf(stderr, "Error: Unsupported struct member access\n");
exit(1);
default:
fprintf(stderr, "Error: Unknown operator '%c'\n", node->op);
exit(1);
}
return 0; // Should never reach here
}