-
Notifications
You must be signed in to change notification settings - Fork 0
/
argcalc.c
368 lines (314 loc) · 8.66 KB
/
argcalc.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
/*% cc -Wall -Wextra -g % -o #
* Copyright © 2022 — 2023 Artsiom Karakin <karakin2000@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#if defined(__OpenBSD__)
#include <sys/queue.h>
#include <err.h>
#else
#include "queue.h"
#include <bsd/bsd.h>
#endif
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <stdckdint.h>
enum token_type { TNUM, TOPR, TLBR, TRBR };
/* Enum's from precedence will be appearing only on operator stack */
enum precedence { SUB = 1, ADD = 2, DIV = 3, MUL = 4, LBR = -1};
enum { MIN_ARGS = 3};
struct token_list {
SIMPLEQ_ENTRY(token_list) next;
int token_type;
long long int payload;
};
SIMPLEQ_HEAD(, token_list) token_list_head;
struct operator_stack {
SLIST_ENTRY(operator_stack) next;
int operator; /* Same value as precedence */
};
SLIST_HEAD(, operator_stack) operator_stack_head;
struct rpn_queue {
SIMPLEQ_ENTRY(rpn_queue) next;
int token_type;
long long int payload;
};
SIMPLEQ_HEAD(, rpn_queue) rpn_queue_head;
struct eval_stack {
SLIST_ENTRY(eval_stack) next;
long long int num;
};
SLIST_HEAD(, eval_stack) eval_stack_head;
/*
* Add token which is either number, operator, left brace or right brace
* to token simple queue containing all tokens
* type is token type, load is number if token_type is TNUM or
* precedence if it is operator or left brace
*/
void
add_token_to_list(int t_type, long long int load)
{
struct token_list *node;
if ((node = malloc(sizeof(*node))) == NULL)
errx(1, "Couldn't allocate token");
node->token_type = t_type;
node->payload = load;
SIMPLEQ_INSERT_TAIL(&token_list_head, node, next);
}
/*
* Add token which is either number or operator to right polish notation
* queue for further use in sorting yard algorithm
*/
void
add_token_to_queue(int t_type, long long int load)
{
struct rpn_queue *node;
if ((node = malloc(sizeof(*node))) == NULL)
errx(1, "Couldn't allocate queue node");
node->token_type = t_type;
node->payload = load;
SIMPLEQ_INSERT_TAIL(&rpn_queue_head, node, next);
}
/*
* Push certain operator or left brace to operator stack used in sorting
* yard algorithm. Operator stack contains only operator's and left brace
* precedence's
*/
void
push_to_operator_stack(int operator)
{
struct operator_stack *p;
if ((p = malloc(sizeof(*p))) == NULL)
errx(1, "Couldn't allocate operator stack node");
p->operator = operator;
SLIST_INSERT_HEAD(&operator_stack_head, p, next);
}
/*
* Peek from operator stack. This means to look what is on top of
* operator stack and return it. If operator stack is empty it
* return left bracket.
* Used in sorting yard algorithm.
*/
int
peek_from_operator_stack(void)
{
int operator = LBR;
struct operator_stack *node;
if (!SLIST_EMPTY(&operator_stack_head)) {
node = SLIST_FIRST(&operator_stack_head);
operator = node->operator;
}
return operator;
}
/*
* Pop from revers polish notation operator stack. Used in sorting yard
* algorithm. May lead to segfault if operator stack is empty
*/
int
pop_from_operator_stack(void)
{
int operator;
struct operator_stack *node;
node = SLIST_FIRST(&operator_stack_head);
operator = node->operator;
SLIST_REMOVE_HEAD(&operator_stack_head, next);
free(node);
return operator;
}
/*
* Push number to evaluation stack used to calculate expression
*/
void
push_to_eval_stack(long long int num)
{
struct eval_stack *node;
if ((node = malloc(sizeof(*node))) == NULL)
errx(1, "Couldnt allocate evaluation stack node");
node->num = num;
SLIST_INSERT_HEAD(&eval_stack_head, node, next);
}
/*
* Pop number from evaluation stack. If stack is empty write error
* that tell's that there is inconsistent number of operators
*/
long long int
pop_from_eval_stack(void)
{
long long int num;
struct eval_stack *node;
if (SLIST_EMPTY(&eval_stack_head))
errx(1, "Inconsistent number of operators");
node = SLIST_FIRST(&eval_stack_head);
num = node->num;
SLIST_REMOVE_HEAD(&eval_stack_head, next);
free(node);
return num;
}
/*
* Devide op_first by op_second and handle if present
*/
long long int
devide(long long int op_first, long long int op_second)
{
long long int res;
if (op_second == 0)
errx(1, "Division by zero");
if ((op_first == LONG_MIN) && (op_second == -1))
errx(1, "Integer overflow");
res = op_first / op_second;
return res;
}
/*
* ARGument CALCulator
* Evaluate infix expression supplied as command line
* arguments as expr does it
*/
int
main(int argc, char **argv)
{
int is_digit;
const char *errstr;
SIMPLEQ_INIT(&token_list_head);
SLIST_INIT(&operator_stack_head);
SIMPLEQ_INIT(&rpn_queue_head);
SLIST_INIT(&eval_stack_head);
struct token_list *token_node;
struct rpn_queue *rpn_node;
int operator;
long long int operand_first;
long long int operand_second;
long long int operand_result;
/* Turn charaters from command line arguments into tokens */
for (int i = 1; i < argc && argc > MIN_ARGS; i++) {
for (int j = 0; argv[i][j] != '\0'; j++) {
switch (argv[i][j]) {
case '*':
add_token_to_list(TOPR, MUL);
is_digit = 0;
break;
case '/':
add_token_to_list(TOPR, DIV);
is_digit = 0;
break;
case '+':
add_token_to_list(TOPR, ADD);
is_digit = 0;
break;
case '-':
add_token_to_list(TOPR, SUB);
is_digit = 0;
break;
case '(':
add_token_to_list(TLBR, LBR);
is_digit = 0;
break;
case ')':
add_token_to_list(TRBR, 0);
is_digit = 0;
break;
case '{':
add_token_to_list(TLBR, LBR);
is_digit = 0;
break;
case '}':
add_token_to_list(TRBR, 0);
is_digit = 0;
break;
default:
/*
* Set is_digit != 0 if all charaters in argv[i]
* are digits.
*/
if (j == 0 || is_digit != 0)
is_digit = isdigit(argv[i][j]);
else
is_digit = 0;
break;
}
}
if (is_digit){
add_token_to_list(TNUM, strtonum(argv[i],\
LONG_MIN, LONG_MAX, &errstr));
if (errstr != NULL)
errx(1, "number \"%s\" is %s", argv[i], errstr);
}
}
/* Translate infix expression into reverse polish notation */
while (!SIMPLEQ_EMPTY(&token_list_head)) {
token_node = SIMPLEQ_FIRST(&token_list_head);
if (token_node->token_type == TNUM)
add_token_to_queue(TNUM, token_node->payload);
else if (token_node->token_type == TOPR) {
while (peek_from_operator_stack() > token_node->payload)
add_token_to_queue(TOPR,
pop_from_operator_stack());
push_to_operator_stack(token_node->payload);
} else if (token_node->token_type == TLBR) {
push_to_operator_stack(LBR);
} else if (token_node->token_type == TRBR) {
while (peek_from_operator_stack() != LBR)
add_token_to_queue(TOPR,
pop_from_operator_stack());
/* Pop the left bracket from the stack and discard it */
pop_from_operator_stack();
}
SIMPLEQ_REMOVE_HEAD(&token_list_head, next);
free(token_node);
}
while (!SLIST_EMPTY(&operator_stack_head)) {
add_token_to_queue(TOPR, pop_from_operator_stack());
}
/* Evaluate RPN expression using stack */
while (!SIMPLEQ_EMPTY(&rpn_queue_head)) {
rpn_node = SIMPLEQ_FIRST(&rpn_queue_head);
if (rpn_node->token_type == TNUM)
push_to_eval_stack(rpn_node->payload);
else if (rpn_node->token_type == TOPR) {
operator = rpn_node->payload;
/* We should get second operand first because we use stack */
operand_second = pop_from_eval_stack();
operand_first = pop_from_eval_stack();
switch (operator) {
case SUB:
ckd_sub(&operand_result,
operand_first, operand_second);
push_to_eval_stack(operand_result);
break;
case ADD:
ckd_add(&operand_result,
operand_first, operand_second);
push_to_eval_stack(operand_result);
break;
case DIV:
operand_result = devide(operand_first,
operand_second);
push_to_eval_stack(operand_result);
break;
case MUL:
ckd_mul(&operand_result,
operand_first, operand_second);
push_to_eval_stack(operand_result);
break;
default:
break;
}
}
SIMPLEQ_REMOVE_HEAD(&rpn_queue_head, next);
free(rpn_node);
}
if (!SLIST_EMPTY(&eval_stack_head))
printf("%lld \n", pop_from_eval_stack());
return 0;
}