-
Notifications
You must be signed in to change notification settings - Fork 0
/
psa_utils.c
271 lines (252 loc) · 5.52 KB
/
psa_utils.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
/**
* @file utils.c
* @author Anastasia Butok (xbutok00@vutbr.cz)
* @brief Utility functions for PSA.
* @version 0.1
* @date 2023-11-24
*
* @copyright Copyright (c) 2023
* Project: IFJ compiler
*
*/
#include "psa.h"
uint32_t handleToUInt32(PSA_Token *handle, unsigned int len)
{
uint32_t result = 0;
for (unsigned int i = 0; i < len; i++)
{
result = result << 8 | (char)(handle[i].type);
}
return result;
}
uint32_t reverseHandleTypesToUInt32(Expression_type *types, unsigned int len)
{
uint32_t result = 0;
for (int i = len - 1; i >= 0; i--)
{
result = result << 8 | (char)types[i];
}
return result;
}
Expression_type getTypeFromToken(Token_type token)
{
switch (token)
{
case TOKEN_INT:
return (Expression_type)TYPE_INT;
case TOKEN_DOUBLE:
case TOKEN_EXP:
return (Expression_type)TYPE_DOUBLE;
case TOKEN_BOOL:
return (Expression_type)TYPE_BOOL;
case TOKEN_STRING:
return (Expression_type)TYPE_STRING;
case TOKEN_NIL:
return (Expression_type)TYPE_NIL;
default:
return (Expression_type)TYPE_INVALID;
}
}
bool isTokenLiteral(Token_type token)
{
switch (token)
{
case TOKEN_INT:
case TOKEN_DOUBLE:
case TOKEN_EXP:
case TOKEN_BOOL:
case TOKEN_STRING:
case TOKEN_NIL:
return true;
default:
return false;
}
}
bool isTokenOperand(Token_type token)
{
switch (token)
{
case TOKEN_INT:
case TOKEN_DOUBLE:
case TOKEN_EXP:
case TOKEN_BOOL:
case TOKEN_STRING:
case TOKEN_EXPRSN:
case TOKEN_IDENTIFICATOR:
case TOKEN_FUNC_ID:
return true;
default:
return false;
}
}
bool isTokenBinaryOperator(Token_type token)
{
switch (token)
{
case TOKEN_MUL:
case TOKEN_DIV:
case TOKEN_PLUS:
case TOKEN_MINUS:
case TOKEN_EQ:
case TOKEN_NEQ:
case TOKEN_LESS:
case TOKEN_MORE:
case TOKEN_LESS_EQ:
case TOKEN_MORE_EQ:
case TOKEN_AND:
case TOKEN_OR:
case TOKEN_BINARY_OPERATOR:
return true;
default:
return false;
}
}
bool isTokenBracket(Token_type token)
{
switch (token)
{
case TOKEN_L_BRACKET:
case TOKEN_R_BRACKET:
return true;
default:
return false;
}
}
bool canTokenBeStartOfExpression(Token_type token)
{
switch (token)
{
case TOKEN_INT:
case TOKEN_DOUBLE:
case TOKEN_EXP:
case TOKEN_BOOL:
case TOKEN_STRING:
case TOKEN_NOT:
case TOKEN_L_BRACKET:
case TOKEN_IDENTIFICATOR:
return true;
default:
return false;
}
}
bool canTokenBeEndOfExpression(Token_type token)
{
switch (token)
{
case TOKEN_INT:
case TOKEN_DOUBLE:
case TOKEN_EXP:
case TOKEN_BOOL:
case TOKEN_STRING:
case TOKEN_R_BRACKET:
case TOKEN_IDENTIFICATOR:
return true;
default:
return false;
}
}
bool canTypeBeNil(Expression_type type)
{
switch (type)
{
case TYPE_INT_NIL:
case TYPE_DOUBLE_NIL:
case TYPE_BOOL_NIL:
case TYPE_STRING_NIL:
case TYPE_NIL:
return true;
default:
return false;
}
}
char getOperationChar(Token_type token)
{
switch (token)
{
case TOKEN_MUL:
return '*';
case TOKEN_DIV:
return '/';
case TOKEN_PLUS:
return '+';
case TOKEN_MINUS:
return '-';
case TOKEN_EQ:
return '=';
case TOKEN_NEQ:
return '!';
case TOKEN_LESS:
return '<';
case TOKEN_MORE:
return '>';
case TOKEN_LESS_EQ:
return 'L';
case TOKEN_MORE_EQ:
return 'M';
case TOKEN_AND:
return '&';
case TOKEN_OR:
return '|';
default:
return ' ';
}
}
Expression_type removeTypeNil(Expression_type expr_type)
{
switch (expr_type)
{
case TYPE_INT_NIL:
return TYPE_INT;
case TYPE_DOUBLE_NIL:
return TYPE_DOUBLE;
case TYPE_BOOL_NIL:
return TYPE_BOOL;
case TYPE_STRING_NIL:
return TYPE_STRING;
default:
return TYPE_INVALID;
}
}
Instruction_list tokenTypeToStackInstruction(Token_type tt)
{
switch (tt)
{
case TOKEN_MUL:
return (Instruction_list){.inst = {MULS}, .len = 1};
case TOKEN_DIV:
return (Instruction_list){.inst = {DIVS}, .len = 1};
case TOKEN_PLUS:
return (Instruction_list){.inst = {ADDS}, .len = 1};
case TOKEN_MINUS:
return (Instruction_list){.inst = {SUBS}, .len = 1};
case TOKEN_EQ:
return (Instruction_list){.inst = {EQS}, .len = 1};
case TOKEN_NEQ:
return (Instruction_list){.inst = {EQS, NOTS}, .len = 2};
case TOKEN_LESS:
return (Instruction_list){.inst = {LTS}, .len = 1};
case TOKEN_MORE:
return (Instruction_list){.inst = {GTS}, .len = 1};
case TOKEN_LESS_EQ:
return (Instruction_list){.inst = {GTS, NOTS}, .len = 2};
case TOKEN_MORE_EQ:
return (Instruction_list){.inst = {LTS, NOTS}, .len = 2};
case TOKEN_AND:
return (Instruction_list){.inst = {ANDS}, .len = 1};
case TOKEN_OR:
return (Instruction_list){.inst = {ORS}, .len = 1};
case TOKEN_NOT:
return (Instruction_list){.inst = {NOTS}, .len = 1};
default:
return (Instruction_list){.inst = {EMPTY}, .len = 1};
}
}
char *hadleToString(PSA_Token *handle, unsigned int handle_len)
{
char *result = calloc(100, sizeof(char));
for (unsigned int i = 0; i < handle_len; i++)
{
strcat(result, handle[i].token_value);
}
return result;
}