-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.h
144 lines (128 loc) · 2.93 KB
/
token.h
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
#ifndef COURSEWORK_TOKEN_H
#define COURSEWORK_TOKEN_H
#include <stdbool.h>
typedef enum token_type
{
left_parenthesis, // parentheses.
right_parenthesis,
addition, // binary operators.
subtraction,
multiplication,
division,
power,
negation, // unary operator.
squareroot, // functions.
log_10,
log_e,
sine,
cosine,
tangent,
constant, // operands.
variable,
end // end of input.
} token_type;
typedef struct token
{
token_type type;
char symbol; // name of a variable.
double value; // value of a variable or a constant.
} token;
// allocate memory for a token on the heap.
token* token_alloc()
{
return (token*)malloc(sizeof(token));
}
// initialise token for a variable.
void token_init_variable(token* token, char symbol)
{
token->type = variable;
token->symbol = symbol;
token->value = 0;
}
// initialise token for a constant.
void token_init_constant(token* token, double value)
{
token->type = constant;
token->symbol = '\0';
token->value = value;
}
// initialise token for anything else.
void token_init(token* token, token_type type, char symbol, double value)
{
token->type = type;
token->symbol = symbol;
token->value = value;
}
bool is_function(token token)
{
switch(token.type)
{
case squareroot:
case log_10:
case log_e:
case sine:
case cosine:
case tangent:
return true;
default:
return false;
}
}
bool is_operand(token token)
{
return token.type == constant || token.type == variable;
}
bool is_operator(token token)
{
switch (token.type)
{
case addition:
case subtraction:
case multiplication:
case division:
case power:
case negation:
return true;
default:
return false;
}
}
// is token is a unary operator?
bool is_unary(token token)
{
return token.type == negation;
}
// is token is a binary operator?
bool is_binary(token token)
{
return is_unary(token) == false; // assuming we have already determined token is an operator.
}
// operators of the same precedence which are evaluated right to left.
bool is_right_associative(token_type type)
{
return type == negation || type == power;
}
// operators of the same precedence which are evaluated left to right.
bool is_left_associative(token_type type)
{
return is_right_associative(type) == false; // assuming we have already determined token is an operator.
}
// operators with higher precedence are evaluated first.
int precedence(token_type type)
{
switch (type)
{
case negation:
case power:
return 4;
case multiplication:
case division:
return 3;
case addition:
case subtraction:
return 2;
default:
return -1; // token is not a valid operator.
}
}
#endif //COURSEWORK_TOKEN_H