-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.c
373 lines (325 loc) · 7.34 KB
/
compiler.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "array.c"
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
extern Error *error_type;
int isNumber(const char *s)
{
if (s == NULL || *s == '\0')
return false;
bool hasDigit = false;
bool hasDecimalPoint = false;
bool hasExponent = false;
bool hasSign = false;
while (*s)
{
if (*s == '-' || *s == '+')
{
if (hasSign || hasDigit || hasDecimalPoint || hasExponent)
return false; // Sign not allowed in the middle
hasSign = true;
}
else if (*s == '.')
{
if (hasDecimalPoint || hasExponent)
return false; // Multiple decimal points or already in exponent
hasDecimalPoint = true;
}
else if (*s == 'e' || *s == 'E')
{
if (hasExponent || !hasDigit)
return false;
hasExponent = true;
hasSign = false;
}
else if (*s >= '0' && *s <= '9')
{
hasDigit = true;
}
else
{
return false;
}
s++;
}
return hasDigit;
}
double logbase(double y, int b)
{
double lg;
lg = log10(b) / log10(y);
return (lg);
}
typedef struct ASTNode
{
char *value;
double arg_value;
struct ASTNode *children[2];
token_T *token;
Error *error_type;
} ASTNode;
ASTNode *createNode(char *value, token_T *token);
ASTNode *parseExpression(TokenArray *tArray, int count, int index);
ASTNode *createAST(TokenArray *tArray, int count);
ASTNode *createNode(char *value, token_T *token)
{
ASTNode *node = (ASTNode *)malloc(sizeof(ASTNode));
node->value = value;
node->children[0] = NULL;
node->children[1] = NULL;
node->token = token;
return node;
}
ASTNode *createAST(TokenArray *tArray, int count)
{
int index = 0;
return parseExpression(tArray, count, index);
}
int globalIndex = 0;
ASTNode *parseExpression(TokenArray *tArray, int count, int index)
{
token_T token = *(tArray->tokens[index]);
if (token.type == TOKEN_FUNC)
{
index++;
if (index < count && tArray->tokens[index]->type == TOKEN_LPAREN)
{
index++;
int localIndex = index;
int parenCount = 1;
int left = index;
int right = -1;
while (localIndex < count)
{
if (tArray->tokens[localIndex]->type == TOKEN_LPAREN)
{
parenCount++;
}
else if (tArray->tokens[localIndex]->type == TOKEN_RPAREN)
{
parenCount--;
}
if (parenCount == 0)
{
right = localIndex;
break;
}
localIndex++;
}
if (right == -1)
{
error_type->index = localIndex;
error_type->isError = true;
error_type->type = "Unmatched parentheses";
}
else
{
ASTNode *node = createNode(tArray->tokens[index - 2]->value,
tArray->tokens[index - 2]);
int gIndex = left;
node->children[0] = parseExpression(tArray, right, left);
int gLIndex = left;
int parentCount = 0;
int found = -1;
while (gLIndex < right)
{
if (tArray->tokens[gLIndex]->type == TOKEN_LPAREN)
{
parenCount++;
}
else if (tArray->tokens[gLIndex]->type == TOKEN_RPAREN)
{
parenCount--;
}
if (parenCount == 0 && tArray->tokens[gLIndex]->type == TOKEN_COMMA)
{
found = 1;
break;
}
gLIndex++;
}
if (found == 1)
{
node->children[1] = parseExpression(tArray, right, gLIndex + 1);
}
globalIndex = right;
return node;
}
}
else
{
error_type->index = index;
error_type->isError = true;
error_type->type = "Need atleat one argument";
}
}
else
{
if (token.type == TOKEN_ARGUMENTS)
{
ASTNode *node =
createNode(tArray->tokens[index]->value, tArray->tokens[index]);
return node;
}
}
}
void checkArgument(ASTNode *node, int l)
{
int i = 0;
if (node->children[0] != NULL)
{
i++;
}
if (node->children[1] != NULL)
{
i++;
}
if (i == l)
{
return;
}
else
{
define_error(error_type, -1, "Incorrect arguments count");
}
}
double evaluateAST(ASTNode *node, double x)
{
if (error_type->isError)
{
return 0;
}
if (strcmp(node->value, "tan") == 0)
{
checkArgument(node, 1);
double l = evaluateAST(node->children[0], x);
return tan(l);
}
if (strcmp(node->value, "cos") == 0)
{
checkArgument(node, 1);
double l = evaluateAST(node->children[0], x);
return cos(l);
}
if (strcmp(node->value, "sin") == 0)
{
checkArgument(node, 1);
double l = evaluateAST(node->children[0], x);
return sin(l);
}
if (node->token->type == TOKEN_ARGUMENTS)
{
if (strcmp(node->value, "x") == 0)
{
return x;
}
else
{
if (isNumber(node->token->value))
{
return atof(node->token->value);
}
}
}
if (strcmp(node->value, "add") == 0)
{
checkArgument(node, 2);
double l = evaluateAST(node->children[0], x);
double l2 = evaluateAST(node->children[1], x);
return l + l2;
}
if (strcmp(node->value, "minus") == 0)
{
checkArgument(node, 2);
double l = evaluateAST(node->children[0], x);
double l2 = evaluateAST(node->children[1], x);
return l - l2;
}
if (strcmp(node->value, "multiply") == 0)
{
checkArgument(node, 2);
double l = evaluateAST(node->children[0], x);
double l2 = evaluateAST(node->children[1], x);
return l * l2;
}
if (strcmp(node->value, "divide") == 0)
{
checkArgument(node, 2);
double l = evaluateAST(node->children[0], x);
double l2 = evaluateAST(node->children[1], x);
return l / l2;
}
if (strcmp(node->value, "power") == 0)
{
checkArgument(node, 2);
double l = evaluateAST(node->children[0], x);
double l2 = evaluateAST(node->children[1], x);
return pow(l, l2);
}
if (strcmp(node->value, "exp") == 0)
{
checkArgument(node, 1);
double l = evaluateAST(node->children[0], x);
return exp(l);
}
if (strcmp(node->value, "log10") == 0)
{
checkArgument(node, 1);
double l = evaluateAST(node->children[0], x);
return log10(l);
}
if (strcmp(node->value, "log") == 0 || strcmp(node->value, "ln") == 0)
{
checkArgument(node, 1);
double l = evaluateAST(node->children[0], x);
return log(l);
}
if (strcmp(node->value, "logBase") == 0)
{
checkArgument(node, 2);
double l = evaluateAST(node->children[0], x);
double l2 = evaluateAST(node->children[1], x);
return logbase(l, l2);
}
}
void display(ASTNode *ast)
{
if (ast == NULL)
{
printf("end");
return;
}
printf("%s", ast->value);
printf("\n");
printf("(");
display(ast->children[0]);
display(ast->children[1]);
printf(")\n");
}
typedef struct
{
ASTNode ast;
Error error;
} Value;
Value func(char *code, double x)
{
Value v;
lexer_T *lexer = init_lexer(code);
token_T *token = lexer_get_next_token(lexer);
TokenArray *tokenArray = createTokenArray();
addToTokenArray(tokenArray, token);
while (token != NULL)
{
token = lexer_get_next_token(lexer);
addToTokenArray(tokenArray, token);
}
int s = 0;
int token_count = tokenArray->size - 1;
ASTNode *ast = parseExpression(tokenArray, token_count, 0);
int t_index = 0;
v.ast = *ast;
return v;
}