-
Notifications
You must be signed in to change notification settings - Fork 2
/
calc.c
258 lines (231 loc) · 5.24 KB
/
calc.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
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <setjmp.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <errno.h>
#include "calc.h"
#define when(n) break; case n
#define otherwise break; default
#define err(args...) fprintf(stderr, args)
#define CRITICAL ?:(err("Error"),/*perror(NULL), */exit(1), NULL)
#define ALLOC(name) name = malloc(sizeof(*name)) CRITICAL
// GLOBALS
Num ans = 0;
int haveAns = 0;
jmp_buf env;
void throw(int err) {
//todo: allow defining error strings somehow
// maybe have throw take err message string,
// or, allow allocating messages beforehand and use int handles.
// this could make, for example, translation easier
// as well as encouraging reuse of error messages
}
OpDef* addOp(OpDef** listp, OpDef* def) {
//printf("adding op %s\n", def->name);
listp CRITICAL;
def CRITICAL;
def->name CRITICAL;
def->func CRITICAL;
OpDef* ALLOC(new);
*new = *def;
// keep list sorted by descending name length
OpDef* prev = NULL;
size_t len = strlen(def->name);
OpDef* next = *listp;
while (next && strlen(next->name) > len) {
prev = next;
next = next->next;
}
// insert item
if (prev)
prev->next = new;
new->next = next;
if (next == *listp) // if replacing list head
*listp = new;
return new;
}
/*
this is broken now because of sorting
void addAlias(OpDef** list, Str name) {
list CRITICAL;
name CRITICAL;
addOp(list, *list)->name = name;
}*/
OpDef* prefix = NULL;
OpDef* infix = NULL;
OpDef* literal = NULL;
OpDef* format = NULL;
DEF_LIT(input,
if (isatty(0))
err("input number: ");
Num res;
if (scanDL(&res, stdin)==1)
return res;
THROW(4);
);
DEF_LIT(ans,
return haveAns ? ans : lit_input(str);
);
DEF_FMT(decimal,
DLprint(num, 10);
);
AUTORUN {
ADD_OP(literal, "i", lit_input, "get number from stdin");
ADD_OP(literal, "a", lit_ans, "value of previous expression");
ADD_OP(format, "", fmt_decimal, "default number printer");
}
local Op search(Str* str, OpDef* ops) {
for (; ops; ops=ops->next) {
int len = strlen(ops->name);
if (strncmp(*str, ops->name, len)==0) {
*str += len;
return ops->func;
}
}
return NULL;
}
local Num readExpr(Str*, int);
local Num readValue(Str* str, int depth) {
// Start group
if ((*str)[0]==' ') {
(*str)++;
return readExpr(str, (depth ?: 1)+1);
}
// literal/vars
Op op = search(str, literal);
if (op)
return op(str);
// decimal
if (((*str)[0]>='0' && (*str)[0]<='9') || (*str)[0]=='.') {
Str end;
Num num = strtoDL(*str, &end);
if (end) {
*str = end;
return num;
}
}
// Prefix Operator
op = search(str, prefix);
if (op)
return op(readValue(str, depth ?: 1), 0);
// Implicit value (top level only)
// "+1" is treated as "a+1", etc.
// this only works if the expression is not otherwise valid.
// so, "-1" is not treated as "a-1", etc.
if (!depth)
return lit_ans(str);
// Error
THROW(1);
}
local Num readAfter(Str* str, int depth, Num acc) {
// End group
if ((*str)[0]==' ') {
(*str)++;
if (depth>1)
return acc;
}
if ((*str)[0]=='\0')
return acc;
// Infix Operator
Op op = search(str, infix);
if (op) {
Num v = readValue(str, depth ?: 1);
return readAfter(str, depth ?: 1, op(acc, v));
}
// Error
THROW(2);
}
local Num readExpr(Str* str, int depth) {
Num acc = readValue(str, depth);
return readAfter(str, depth ?: 1, acc);
}
local int doline(Str line, int interactive) {
if (!line)
return 3;
if (interactive)
add_history(line);
//IF (line[0]=='\0')
// continue;
Str expr;
int result = setjmp(env);
if (!result) {
expr = line;
Op printer = search(&expr, format) CRITICAL;
Num res = readExpr(&expr, 0); //can longjump
ans = res;
haveAns = 1;
if (interactive)
printf("> ");
printer((Num)res, (int)interactive);
putchar('\n');
return 0;
} else { //returned via longjump
switch (result) {
when(1):
err("! Error: expected value (number");
OpDef* op;
for (op=literal; op; op=op->next) {
err(", %s", op->name);
}
err(") or prefix operator (");
for (op=prefix; op; op=op->next) {
if (op!=prefix)
err(", ");
err("%s", op->name);
}
err(")");
when(2):
err("! Error: expected operator (");
for (op=infix; op; op=op->next) {
if (op!=infix)
err(", ");
err("%s", op->name);
}
err(")");
when(4):
err("! Error: expected number from stdin");
otherwise:
err("! Unknown error: %d", result);
}
err("\n");
err("! %.*s⟨here⟩%s\n", (int)(expr-line), line, expr);
}
return result;
}
int main(int argc, Str* argv) {
if (argc == 2)
return doline(argv[1], 0);
if (!isatty(0)) {
Str line = NULL;
size_t n = 0;
ssize_t c = 0;
int err = 0;
while (1) {
c = getline(&line, &n, stdin);
if (c<0) {
err = err ?: errno;
break;
}
if (line[c-1]=='\n')
line[c-1] = '\0';
err = err ?: doline(line, 0);
}
free(line);
return err;
}
using_history();
while (1) {
Str line = readline("<< ");
if (!line)
break;
doline(line, 1);
free(line);
}
return 0;
}
//todo: allow parentheses. these are stronger than space precedence.
//(....) will always be a group (unless it contains more parentheses) regardless of the amount of whitespace.