-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.c
237 lines (213 loc) · 7.86 KB
/
eval.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
/**
* iclc - Interactive Command Line Calculator
*
* eval.c
*
* iclc is a simple, interactive, command line calculator. It
* employs and thereby demonstrates basic parsing technique.
*
* Beware, it leaks like a strainer and probably has more bugs
* than Starship Troopers.
*
*
* Copyright 2012 Patrick Haas (@p7haas)
*
* Released under the MIT and GPL licenses.
*
* ------------------------------------------------
* author: Patrick Haas
* url: https://github.com/p7haas/iclc
* source: https://github.com/p7haas/iclc
*/
#include "eval.h"
#include "store.h"
#include "snippets.h"
#include <math.h>
#include <stdarg.h>
const int MAX_PN = 255;
LONG_DOUBLE eval_emit_error(eval_context *ctx, const char *format, ...) {
// Fehlerstring aufbauen, printf-Syntax
va_list args;
va_start(args, format);
char fmtbuf[256];
vsnprintf(fmtbuf, 256, format, args);
// Fehlerstruktur füllen
eval_error *e = malloc(sizeof(eval_error));
e->__next = NULL;
e->message = stringcopy(fmtbuf);
// Letzten Eintrag in der Fehlerliste finden und e anhängen
eval_error **it = &ctx->error;
while (*it != NULL) it = &((*it)->__next);
*it = e;
// Fehlerflag setzen.
ctx->success = false;
return -NAN;
}
LONG_DOUBLE eval_expr_func(eval_context *ctx, ast_node *tree) {
expr_func_data *func_data = (expr_func_data*)tree->data;
if (strcmp(func_data->name, "sin") == 0) return sinl(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "cos") == 0) return cosl(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "tan") == 0) return tanl(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "acos") == 0) return acosl(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "asin") == 0) return asinl(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "atan") == 0) return atanl(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "cosh") == 0) return coshl(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "sinh") == 0) return sinhl(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "tanh") == 0) return tanhl(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "log") == 0) return logl(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "log10") == 0) return log10l(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "ceil") == 0) return ceill(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "fabs") == 0) return fabsl(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "floor") == 0) return floorl(eval_expr(ctx, func_data->rhs));
else if (strcmp(func_data->name, "sqrt") == 0) return sqrtl(eval_expr(ctx, func_data->rhs));
else return eval_emit_error(ctx, "Unknown function %s.", func_data->name);
}
LONG_DOUBLE eval_expr_var(eval_context *ctx, ast_node *tree) {
expr_var_data *var_data = (expr_var_data*)tree->data;
if (store_has_var(store_get(), var_data->name)) return store_get_var(store_get(), var_data->name);
else return eval_emit_error(ctx, "Undeclared variable %s.", var_data->name);
}
LONG_DOUBLE eval_expr_num(eval_context *ctx, ast_node *tree) {
expr_num_data *num_data = (expr_num_data*)tree->data;
return num_data->val;
}
LONG_DOUBLE eval_expr_binop(eval_context *ctx, ast_node *tree) {
expr_binop_data *binop_data = (expr_binop_data*)tree->data;
switch (binop_data->op) {
case OP_ADD:
return eval_expr(ctx, binop_data->lhs) + eval_expr(ctx, binop_data->rhs);
case OP_SUB:
return eval_expr(ctx, binop_data->lhs) - eval_expr(ctx, binop_data->rhs);
case OP_MUL:
return eval_expr(ctx, binop_data->lhs) * eval_expr(ctx, binop_data->rhs);
case OP_DIV:
return eval_expr(ctx, binop_data->lhs) / eval_expr(ctx, binop_data->rhs);
case OP_MOD: {
// Knuthsche "floored division"
LONG_DOUBLE a = eval_expr(ctx, binop_data->lhs);
LONG_DOUBLE n = eval_expr(ctx, binop_data->rhs);
return a-n*floor(a/n);
}
case OP_EXP:
return pow(eval_expr(ctx, binop_data->lhs), eval_expr(ctx, binop_data->rhs));
case OP_ASS: {
if (binop_data->lhs->type != EXPR_VAR) return eval_emit_error(ctx, "Cannot assign to a non-variable.");
else {
expr_var_data *var_data = (expr_var_data*)binop_data->lhs->data;
LONG_DOUBLE rhs_res = eval_expr(ctx, binop_data->rhs);
store_var(store_get(), var_data->name, rhs_res);
return rhs_res;
}
}
default: return eval_emit_error(ctx, "Operator not implemented.");
}
}
LONG_DOUBLE eval_expr_unop(eval_context *ctx, ast_node *tree) {
expr_unop_data *unop_data = (expr_unop_data*)tree->data;
switch (unop_data->op) {
case OP_POS:
return eval_expr(ctx, unop_data->rhs);
case OP_NEG:
return -1.0 * eval_expr(ctx, unop_data->rhs);
default: return eval_emit_error(ctx, "Unknown unary operator %c.", unop_data->op);
}
}
LONG_DOUBLE eval_expr_nop(eval_context *ctx, ast_node *tree) {
expr_nop_data *nop_data = (expr_nop_data*)tree->data;
return eval_expr(ctx, nop_data->next);
}
LONG_DOUBLE eval_expr(eval_context *ctx, ast_node *tree) {
if (!tree) return eval_emit_error(ctx, "Evaluation error: node is NULL.");
switch (tree->type) {
case EXPR_FUNC: return eval_expr_func(ctx, tree);
case EXPR_VAR: return eval_expr_var(ctx, tree);
case EXPR_NUM: return eval_expr_num(ctx, tree);
case EXPR_BINOP: return eval_expr_binop(ctx, tree);
case EXPR_UNOP: return eval_expr_unop(ctx, tree);
case EXPR_NOP: return eval_expr_nop(ctx, tree);
default:return eval_emit_error(ctx, "Unknown expression.");
}
}
eval_context *eval(ast_node *tree) {
eval_context *ctx = malloc(sizeof(eval_context));
ctx->success = true;
ctx->error = NULL;
ctx->result = eval_expr(ctx, tree);
return ctx;
}
void end_eval(eval_context *ctx) {
if (ctx != NULL) {
// Aufräumen
eval_error *error = ctx->error;
while (error != NULL) {
eval_error *free_error = error;
error = error->__next;
free(free_error->message);
free(free_error);
}
free(ctx);
}
}
void evalprintf(ast_node *tree) {
if (!tree) return;
switch (tree->type) {
case EXPR_FUNC: {
expr_func_data *func_data = (expr_func_data*)tree->data;
printf("%s(", func_data->name);
evalprintf(func_data->rhs);
printf(")");
break;
}
case EXPR_VAR: {
expr_var_data *var_data = (expr_var_data*)tree->data;
printf("%s", var_data->name);
break;
}
case EXPR_NUM: {
expr_num_data *num_data = (expr_num_data*)tree->data;
printf("%Lg", num_data->val);
break;
}
case EXPR_BINOP: {
expr_binop_data *binop_data = (expr_binop_data*)tree->data;
printf("(");
evalprintf(binop_data->lhs);
switch (binop_data->op) {
case OP_ADD:
printf("+"); break;
case OP_SUB:
printf("-"); break;
case OP_MUL:
printf("*"); break;
case OP_DIV:
printf("/"); break;
case OP_MOD:
printf("%%"); break;
case OP_EXP:
printf("^"); break;
case OP_ASS:
printf("="); break;
default:
printf("?"); break;
}
evalprintf(binop_data->rhs);
printf(")");
break;
}
case EXPR_UNOP: {
expr_unop_data *unop_data = (expr_unop_data*)tree->data;
printf("%c", unop_data->op);
evalprintf(unop_data->rhs);
break;
}
case EXPR_NOP: {
expr_nop_data *nop_data = (expr_nop_data*)tree->data;
evalprintf(nop_data->next);
break;
}
default:
printf("!\n");
break;
}
return;
}