-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathculator.c
391 lines (354 loc) · 8.52 KB
/
culator.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* culator -- A simple infix notation floating-point cli calculator.
* Based on Per Vognsen's Bitwise day 3: https://youtu.be/0woxSWjWsb8
* By Olaf Bernstein <camel-cdr@protonmail.com>
*/
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "arg.h"
#define MAX_FUNC_ARGS 256
#define SIZEOF_ARR(arr) (sizeof(arr) / sizeof(*arr))
#define REAL_FMT "Lg"
typedef long double real;
enum TokenKind {
TOKEN_VAL,
TOKEN_ADD,
TOKEN_SUB,
TOKEN_MUL,
TOKEN_DIV,
TOKEN_POW,
TOKEN_FUNC,
TOKEN_CONST,
TOKEN_LPARAM,
TOKEN_RPARAM,
TOKEN_COMMA,
TOKEN_EOF,
NUM_TOKENS
};
struct Token {
enum TokenKind kind;
char const *start, *end;
union {
real val;
struct {
int nargs;
real (*func)(real *reals);
} func;
} as;
};
#define X(name, func, nargs, args) \
static real name##_real(real *x) { return func args; }
#include "functions.c"
/* implement custom functions */
static real torad(real *x) { return x[0] * 3.141592653589793238462643383279502884L / 180.0L; }
static struct {
char *name;
int nargs;
real (*func)(real *reals);
} functions[] = {
#define X(name, func, nargs, args) \
{ #name, nargs, name##_real },
#include "functions.c"
/* register custom functions */
{ "torad", 1, torad },
};
static struct {
char *name;
real val;
} constants[] = {
{ "pi", 3.141592653589793238462643383279502884L },
{ "e", 2.718281828459045235360287471352662498L },
{ "M_E", 2.718281828459045235360287471352662498L },
{ "M_LOG2E", 1.442695040888963407359924681001892137L },
{ "M_LOG10E", 0.434294481903251827651128918916605082L },
{ "M_LN2", 0.693147180559945309417232121458176568L },
{ "M_LN10", 2.302585092994045684017991454684364208L },
{ "M_PI", 3.141592653589793238462643383279502884L },
{ "M_PI_2", 1.570796326794896619231321691639751442L },
{ "M_PI_4", 0.785398163397448309615660845819875721L },
{ "M_1_PI", 0.318309886183790671537767526745028724L },
{ "M_2_PI", 0.636619772367581343075535053490057448L },
{ "M_2_SQRTPI", 1.128379167095512573896158903121545172L },
{ "M_SQRT2", 1.414213562373095048801688724209698079L },
{ "M_SQRT1_2", 0.707106781186547524400844362104849039L },
/* add custom constants */
};
static int precision = 15;
static struct Token token = {0};
static char const *stream;
static void
error(char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, "ERROR: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
exit(EXIT_FAILURE);
}
static void
warning(char const *fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, "WARNING: ");
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
}
static void
next_token(void)
{
repeat:
token.start = stream;
switch (*stream) {
case ' ': case '\n': case '\r': case '\t': case '\v':
while (isspace(*stream)) {
++stream;
}
goto repeat;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9': {
while (isdigit(*stream)) { ++stream;}
if (*stream == '.') { ++stream; }
while (isdigit(*stream)) { ++stream; }
token.kind = TOKEN_VAL;
sscanf(token.start, "%"REAL_FMT, &token.as.val);
break;
}
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
case 'v': case 'w': case 'x': case 'y': case 'z':
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
case 'V': case 'W': case 'X': case 'Y': case 'Z':
case '_': {
size_t i, len;
while (isalnum(*stream) || *stream == '_') {
++stream;
}
len = stream - token.start;
for (i = 0; i < SIZEOF_ARR(constants); ++i) {
if (strlen(constants[i].name) == len && strncmp(token.start, constants[i].name, len) == 0) {
token.kind = TOKEN_CONST;
token.as.val = constants[i].val;
goto end;
}
}
for (i = 0; i < SIZEOF_ARR(functions); ++i) {
if (strlen(functions[i].name) == len && strncmp(token.start, functions[i].name, len) == 0) {
token.kind = TOKEN_FUNC;
token.as.func.func = functions[i].func;
token.as.func.nargs = functions[i].nargs;
goto end;
}
}
warning("Unknown name '%.*s', skipping", len, token.start);
break;
}
case '*':
token.kind = TOKEN_MUL;
if (*++stream == '*') { case '^':
token.kind = TOKEN_POW;
++stream;
}
break;
case '+': token.kind = TOKEN_ADD; ++stream; break;
case '-': token.kind = TOKEN_SUB; ++stream; break;
case '/': token.kind = TOKEN_DIV; ++stream; break;
case '(': token.kind = TOKEN_LPARAM; ++stream; break;
case ')': token.kind = TOKEN_RPARAM; ++stream; break;
case ',': token.kind = TOKEN_COMMA; ++stream; break;
case '\0': token.kind = TOKEN_EOF; ++stream; break;
default:
warning("Invalid '%c' token, skipping", *stream);
++stream;
goto repeat;
}
end:
token.end = stream;
}
static char const *tokenToNames[NUM_TOKENS] = {
"Val",
"+",
"-",
"*",
"/",
"^",
"Func",
"Const",
"(",
")",
",",
"EOF",
};
#define is_token(k) (token.kind == k)
#define match_token(k) (is_token(k) ? (next_token(), 1) : 0)
#define expect_token(k) \
(is_token(k) \
? (next_token(), 1) \
: (error("Expected token '%s', got '%s'", tokenToNames[k], tokenToNames[token.kind]), 0))
static real parse_expr(void);
static real
parse_expr4(void)
{
if (is_token(TOKEN_VAL)) {
real val = token.as.val;
next_token();
return val;
} else if (is_token(TOKEN_CONST)) {
real val = token.as.val;
next_token();
return val;
} else if (is_token(TOKEN_FUNC)) {
static real args[MAX_FUNC_ARGS] = {0};
int i, nargs = token.as.func.nargs;
real (*func)(real *) = token.as.func.func;
next_token();
expect_token(TOKEN_LPARAM);
for (i = 0; i < nargs-1; ++i) {
args[i] = parse_expr();
expect_token(TOKEN_COMMA);
}
args[nargs - 1] = parse_expr();
expect_token(TOKEN_RPARAM);
return func(args);
} else if (match_token(TOKEN_LPARAM)) {
real val = parse_expr();
expect_token(TOKEN_RPARAM);
return val;
} else {
error("Unexpected token");
return 0;
}
}
static real
parse_expr3(void)
{
if (match_token(TOKEN_SUB))
return -parse_expr3();
else if (match_token(TOKEN_ADD))
return parse_expr3();
else
return parse_expr4();
}
static real
parse_expr2(void)
{
real val = parse_expr3();
while (is_token(TOKEN_POW)) {
next_token();
val = pow(val, parse_expr3());
}
return val;
}
static real
parse_expr1(void)
{
real val = parse_expr2();
while (is_token(TOKEN_MUL) || is_token(TOKEN_DIV)) {
enum TokenKind op = token.kind;
next_token();
if (op == TOKEN_MUL) {
val *= parse_expr2();
} else {
assert(op == TOKEN_DIV);
val /= parse_expr2();
}
}
return val;
}
static real
parse_expr(void)
{
real val = parse_expr1();
while (is_token(TOKEN_ADD) || is_token(TOKEN_SUB)) {
enum TokenKind op = token.kind;
next_token();
if (op == TOKEN_ADD) {
val += parse_expr1();
} else {
assert(op == TOKEN_SUB);
val -= parse_expr1();
}
}
return val;
}
static void
parse_str(char const *str)
{
static struct Token nul = {0};
token = nul;
stream = str;
next_token();
if (!is_token(TOKEN_EOF))
printf("%.*"REAL_FMT "\n", precision, parse_expr());
}
static int
parse_expr_stdin()
{
static char *str = NULL;
static size_t len = 0, cap = 0;
while (1) {
if (len >= cap) {
cap = cap * 2 + 128;
str = realloc(str, cap);
}
while (len < cap) {
int c = getchar();
switch (c) {
case '\n': goto end;
case EOF: return 0;
}
str[len++] = c;
}
}
end:
str[len] = '\0';
len = 0;
parse_str(str);
return 1;
}
static void
usage(char *argv0)
{
printf("usage: %s [OPTIONS] [EXPRESSION ...]\n", argv0);
puts("A simple infix notation floating-point cli calculator.");
puts("Reads from stdin if no EXPRESSION is given.\n");
puts(" -p, --precision=NUM quit after NUM outputs");
puts(" -h, --help display this help and exit");
}
int
main(int argc, char **argv)
{
int i;
char *argv0 = argv[0];
ARG_BEGIN {
if (ARG_LONG("help")) case 'h': case '?': {
usage(argv0);
return EXIT_SUCCESS;
} else if (ARG_LONG("precision")) case 'p': {
precision = atoi(ARG_VAL());
} else { default:
fprintf(stderr,
"%s: invalid option '%s'\n"
"Try '%s --help' for more information.\n",
argv0, *argv, argv0);
return EXIT_FAILURE;
}
} ARG_END;
if (argc > 0) {
for (i = 0; i < argc; ++i)
parse_str(argv[i]);
} else {
while (parse_expr_stdin());
}
return EXIT_SUCCESS;
}