forked from hjsong-pknu/calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.c
47 lines (41 loc) · 1.03 KB
/
token.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
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "token.h"
/* Array of operators */
struct operator oplist[] = { {'+', OP_PLUS, 1, 1},
{'-', OP_MINUS, 1, 1},
{'*', OP_MULTIPLY, 2, 2},
{'/', OP_DIVIDE, 2, 2},
{'^', OP_POWER, 3, 3},
{'%', OP_MOD, 2, 2},
{')', OP_RPAREN, 0, 0},
{'(', OP_LPAREN, -1, 4},
{0, 0, 0, 0} };
char * GetNextToken(char * input, struct token * t) {
while ( *input && isspace(*input) ) /* Skip leading whitespace */
++input;
if ( *input == 0 ) /* Check for end of input */
return NULL;
if ( isdigit(*input) ) { /* Token is an operand */
t->type = TOK_OPERAND;
t->value = strtol(input, &input, 0);
}
else { /* Token is an operator */
int n = 0, found = 0;
t->type = TOK_OPERATOR;
while ( !found && oplist[n].symbol ) {
if ( oplist[n].symbol == *input ) {
t->value = oplist[n].value;
found = 1;
}
++n;
}
if ( !found ) {
printf("Bad operator: %c\n", *input);
exit(EXIT_FAILURE);
}
++input;
}
return input;
}