forked from nick-jn/uni-project-20465
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.c
executable file
·286 lines (245 loc) · 8.52 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
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
/*The token of our assembly language.*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include "bool.h"
#include "token.h"
static bool is_number(char *str);
static bool is_valid_identifier(char *str);
#define MAX_OPS 16
#define MAX_DATA_DIRS 5
#define MAX_REGS 10
/*data directive strings*/
static const char *STR_DATA_DIRS[MAX_DATA_DIRS] = {
"data",
"string",
"struct",
"entry",
"extern"
};
/*register strings*/
static const char *STR_REGS[MAX_REGS] = {
"r0",
"r1",
"r2",
"r3",
"r4",
"r5",
"r6",
"r7",
"r8",
"r9"
};
/*Downcasts the token_type in the passed token *tok to a more generic one.*/
token_type downcast_toktype(token_type toktype) {
if (toktype >= toktype_operator_mov &&
toktype <= toktype_operator_stop) {
return toktype_operator;
}
if ((toktype >= toktype_register_0 &&
toktype <= toktype_register_9)) {
return toktype_operand_register;
}
if (toktype >= toktype_datadir_data &&
toktype <= toktype_datadir_extern) {
return toktype_datadir;
}
return toktype;
}
/*Note that input is *NOT* malloc'd. That is, token->tokstr merely
contains a pointer to the said string, create_token is not responsible
for creating it.*/
token *create_token(int starting_index, int length, char *input) {
int i;
token *new_token;
char *new_tokstr;
new_token = malloc(sizeof(token));
new_tokstr = malloc(sizeof(char)*(length+1));
if (new_token == NULL || new_tokstr == NULL) {
puts("Error, malloc failure in create_token.");
exit(1);
}
for (i = 0; i < length; i++, starting_index++) {
new_tokstr[i] = input[starting_index];
}
new_tokstr[i] = '\0';
new_token->starting_index = starting_index-length;
new_token->length = length;
new_token->tokstr = new_tokstr;
return new_token;
}
/*Frees the token. Not for use with clist (see destroy_clist_token).*/
void destroy_token(token *tok) {
if (tok != NULL) {
free(tok->tokstr);
free(tok);
}
}
/*Mallocs a new token. Note that we create a new tokstr. The function is
called extract to suggest the we *extract* a token from tokstream. That
is, it becomes independent of it.*/
token *extract_token(token *tok) {
char *string;
token *new_token;
if (tok == NULL) {
return NULL;
}
new_token = malloc(sizeof(token));
string = malloc(sizeof(char)*(tok->length+1));
if (new_token == NULL || string == NULL) {
puts("Error, malloc failure in get_next_toktype: tok or string.");
exit(1);
}
strcpy(string, tok->tokstr);
new_token->starting_index = tok->starting_index;
new_token->length = tok->length;
new_token->toktype = tok->toktype;
new_token->tokstr = string;
return new_token;
}
/*Figures out what toktype to give to the passed token.*/
token_type get_toktype(token *tok) {
int i;
char *tokstr = tok->tokstr;
token_type toktype = toktype_unknown;
if (is_number(tokstr)) {
return toktype_number;
}
if (strlen(tokstr) == 1) {
if (*tokstr == ':') {
toktype = toktype_colon;
} else if (*tokstr == ',') {
toktype = toktype_comma;
} else if (*tokstr == '.') {
toktype = toktype_dot;
} else if (*tokstr == '"') {
toktype = toktype_quote;
} else if (*tokstr == '#') {
toktype = toktype_hash;
}
} else {
for (i = 0; i < MAX_OPS; i++) { /*operator*/
if (strcmp(OPS[i].opname, tokstr) == 0) {
return toktype_operator + (i+1);
}
}
for (i = 0; i < MAX_REGS; i++) { /*register, will also detect r8-r9*/
if (strcmp(STR_REGS[i], tokstr) == 0) {
return toktype_register_0 + i;
}
}
for (i = 0; i < MAX_DATA_DIRS; i++) { /*data directive*/
if (strcmp(STR_DATA_DIRS[i], tokstr) == 0) {
return toktype_datadir + (i+1);
}
}
}
if (is_valid_identifier(tokstr)) {
return toktype_identifier;
}
return toktype;
}
/*Checks if the passed string is a number.*/
static bool is_number(char *str) {
if (*str == '-' || *str == '+') {
str++;
}
while (*str != '\0') {
if (!isdigit(*str)) {
return false;
}
str++;
}
return true;
}
/*Checks if the passed string is a valid identifier.*/
static bool is_valid_identifier(char *str) {
if (!isalpha(*str)) {
return false;
}
str++;
while (*str != '\0' && *str != '\n') {
if (!isalnum(*str)) {
return false;
}
str++;
}
return true;
}
/*Returns the string that desribes the passed token type.*/
const char *get_toktype_string(token_type toktype) {
static const char *toktype_strings[] = {
/*0*/ "dot", /*toktype_dot*/
/*1*/ "comma", /*toktype_comma*/
/*2*/ "colon", /*toktype_colon*/
/*3*/ "hash", /*toktype_hash*/
/*4*/ "quote", /*toktype_quote*/
/*5*/ "number", /*toktype_number*/
/*6*/ "label", /*toktype_label*/
/*7*/ "data directive", /*toktype_datadir*/
/*8*/ "data directive data", /*toktype_datadir_data*/
/*9*/ "data directive string", /*toktype_datadir_string*/
/*10*/ "data directive struct", /*toktype_datadir_struct*/
/*11*/ "data directive entry", /*toktype_datadir_entry*/
/*12*/ "data directive extern", /*toktype_datadir_extern*/
/*13*/ "operator", /*toktype_operator*/
/*14*/ "operator mov", /*toktype_operator_mov*/
/*15*/ "operator cmp", /*toktype_operator_cmp*/
/*16*/ "operator add", /*toktype_operator_add*/
/*17*/ "operator sub", /*toktype_operator_sub*/
/*18*/ "operator not", /*toktype_operator_not*/
/*19*/ "operator clr", /*toktype_operator_clr*/
/*20*/ "operator lea", /*toktype_operator_lea*/
/*21*/ "operator inc", /*toktype_operator_inc*/
/*22*/ "operator dec", /*toktype_operator_dec*/
/*23*/ "operator jmp", /*toktype_operator_jmp*/
/*24*/ "operator bne", /*toktype_operator_bne*/
/*25*/ "operator red", /*toktype_operator_red*/
/*26*/ "operator prn", /*toktype_operator_prn*/
/*27*/ "operator jsr", /*toktype_operator_jsr*/
/*28*/ "operator rts", /*toktype_operator_rts*/
/*29*/ "operator stop", /*toktype_operator_stop*/
/*30*/ "register", /*toktype_operand_register*/
/*31*/ "register 0", /*toktype_register_0*/
/*32*/ "register 1", /*toktype_register_1*/
/*33*/ "register 2", /*toktype_register_2*/
/*34*/ "register 3", /*toktype_register_3*/
/*35*/ "register 4", /*toktype_register_4*/
/*36*/ "register 5", /*toktype_register_5*/
/*37*/ "register 6", /*toktype_register_6*/
/*38*/ "register 7", /*toktype_register_7*/
/*39*/ "register 8", /*toktype_register_8*/
/*40*/ "register 9", /*toktype_register_9*/
/*41*/ "identifier", /*toktype_identifier*/
/*42*/ "unknown", /*toktype_unknown*/
/*43*/ "string literal", /*toktype_string*/
/*44*/ "end of line" /*toktype_EOL*/
};
return toktype_strings[toktype];
}
/*DEBUG*/
void print_token(token *tok) {
if (tok == NULL) {
printf("TOK IS NULL\n");
return;
}
printf("Token: %s\t%s\n", tok->tokstr, get_toktype_string(tok->toktype));
}
/*Destroyer for tokens in clist.*/
void destroy_clist_token(void *tok) {
token *p_tok = tok;
if (p_tok == NULL) {
return;
}
destroy_token(p_tok);
}
/*DEBUG*/
void print_clist_token(void *tok) {
token *p_tok = tok;
if (tok == NULL) {
return;
}
printf("Token: %s\t%s\n", p_tok->tokstr,
get_toktype_string(p_tok->toktype));
}