-
Notifications
You must be signed in to change notification settings - Fork 1
/
token_struct.h
308 lines (213 loc) · 8.92 KB
/
token_struct.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
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
#ifndef TOKEN_STRUCT_H
#define TOKEN_STRUCT_H
#include <stdio.h>
#include <stdbool.h>
/* ------------------------------------------------ */
/* Prototype definitions */
/* ------------------------------------------------ */
/* Token Node */
typedef struct strToken TokenNode;
/* TokenList */
typedef struct strTokenList TokenList;
/* Symbol Node */
typedef struct strSymbolNode SymbolNode;
/* Symbol Table */
typedef struct strSymbolTable SymbolTable;
/* Instruction Node */
typedef struct strInstructionNode InstructionNode;
/* Instruction Queue */
typedef struct strInstructionQueue InstructionQueue;
/* ------------------------------------------------ */
/* Token Node Structure */
/* ------------------------------------------------ */
typedef struct strToken{
/* Root token of the given node */
TokenNode *root_token;
/* Token String */
/* Store token string node */
char *token_str;
/* Lexical String */
/* Store value read by lexical */
char *lex_str;
/* TokenType */
int token_type;
/* Child Nodes */
TokenList *child_list;
} TokenNode, *ptrTokenNode;
/*------------ Token Structure Methods ------------ */
/* Creates a new token node */
TokenNode* newTokenNode(int token_type);
/* Free a token */
bool deleteTokenNode(ptrTokenNode *token_node, bool deleteChilds);
/* Add Token String */
bool nodeAddTokenStr(TokenNode *token_node, char *token_str);
/* Add Lexical String */
bool nodeAddLexStr(TokenNode *token_node, char *lex_str);
/* Add Root Token */
bool nodeAddRootToken(TokenNode *token_node, TokenNode *root_token);
/* ------------------------------------------------ */
/* Token List Structure */
/* ------------------------------------------------ */
typedef struct strTokenList{
/* Number of childs */
int length;
/* Size of the list */
int size;
/* List of Nodes */
TokenNode **items;
} TokenList, *ptrTokenList;
/* --------------- Token List Methods ------------- */
/* Initialize a list of tokens */
TokenList* newTokenList();
/* Free a token list */
bool deleteTokenList(ptrTokenList *token_list, bool deleteChilds);
/* Add an item to a list of tokens */
bool listAddToken(TokenList *token_list, TokenNode *token);
/* Retrieve a token by it's given index */
TokenNode* listGetTokenByIndex(TokenList *token_list, int index);
/* Retrieve a list token with a given type */
TokenList* listGetTokensByType(TokenList *token_list, int token_type);
/* ------------------------------------------------ */
/* Symbol Node Structure */
/* ------------------------------------------------ */
typedef struct strSymbolNode{
/* Name of the symbol */
char *symbol_name;
/* Start adress */
int symbol_address;
/* Symbol Size in bytes */
int symbol_size;
/* Type of the symbol */
int symbol_type;
/* Symbol type byte size */
int symbol_type_size;
/* Root SymbolTable */
SymbolTable *root_symbol_table;
} SymbolNode, *ptrSymbolNode;
/* -------------- Symbol Node Methods ------------- */
/* Create a new symbol node. */
SymbolNode* newSymbolNode( SymbolTable *root_symbol_table, char *symbol_name,
int symbol_address, int symbol_type);
/* Free a symbol node */
bool deleteSymbolNode(ptrSymbolNode *symbol_node);
/* Compare symbol name */
bool symbolEqualsName(SymbolNode *symbol, char *symbol_name);
/* Get symbol address of a symbol node */
int symbolNodeGetSymbolAdress(SymbolNode *symbol_node);
/* Get symbol size of a given symbol node */
int symbolNodeGetSymbolSize(SymbolNode *symbol_node);
/* Get define a symbol type into a register instruction */
InstructionNode* symbolNodeGetDefineInstruction(SymbolNode *symbol_node);
/* Get load a symbol into a register instruction */
InstructionNode* symbolNodeGetLoadInstruction(SymbolNode *symbol_node);
/* Get store a symbol into a register instruction */
InstructionNode* symbolNodeGetStoreInstruction(SymbolNode *symbol_node);
/* Get load a symbol type into a register instruction */
InstructionNode* symbolNodeGetLoadTypeInstruction(SymbolNode *symbol_node);
/* Get store a symbol type into a register instruction */
InstructionNode* symbolNodeGetStoreTypeInstruction(SymbolNode *symbol_node);
/* ------------------------------------------------ */
/* Symbol Table Structure */
/* ------------------------------------------------ */
typedef struct strSymbolTable{
/* Size of the structure */
int size;
/* Qunantity of elements present on this table */
int length;
/* Start Address */
int start_address;
/* Address Shift */
int shift_address;
/* Previous Scope */
SymbolTable *previous_scope;
/* Brother Symbol Table */
SymbolTable *brother_table;
/* Register type */
int register_type;
/* Items present on the table */
SymbolNode **items;
} SymbolTable, *ptrSymbolTable;
/* ------------- Symbol Table Methods ------------- */
/* Create a new global Symbol Table */
SymbolTable* newGlobalSymbolTable();
/* Create a new SymbolTable */
SymbolTable* newSymbolTable(SymbolTable *symbol_table, int register_type);
/* Free a symbol table */
bool deleteSymbolTable(ptrSymbolTable *symbol_table);
/* Add a new symbol to symbol table */
bool symbolTableAddSymbol( SymbolTable *symbol_table, char *symbol_name,
int symbol_type);
/* Add a new logical symbol node to symbol table */
bool symbolTableAddLogicSymbol(SymbolTable *symbol_table);
/* Check if a symbol of a given name is present on the table */
bool symbolTableContains(SymbolTable *symbol_table, char *symbol_name);
/* Get symbol node by name */
SymbolNode* symbolTableGetSymbolNodeByName( SymbolTable *symbol_table,
char *symbol_name);
/* Pop last symbol */
bool symbolTablePopVar(SymbolTable *symbol_table);
/* Push a virtual memory */
bool symbolTablePushVar(SymbolTable *symbol_table);
/* Add a brother table */
bool symbolTableAddBrother(SymbolTable *symbol_table, SymbolTable *brother_table);
/* ------------------------------------------------ */
/* Instruction Node Structure */
/* ------------------------------------------------ */
typedef struct strInstructionNode{
/* Instruction String */
char *instruction;
/* Length of the instruction string */
int length;
} InstructionNode, *ptrInstructionNode;
/* ---------- Instruction Node Methods ----------- */
/* Return a new Instruction Node */
InstructionNode *newInstructionNode(char* instruction_string, bool copyInstruction);
/* Free a instruction node */
bool deleteInstructionNode(ptrInstructionNode *instruction_node);
/* Print a instruction node on a given file */
bool instructionNodeFilePrint(FILE *_output_file, InstructionNode *instruction);
/* Get instruction length */
int instructionNodeLength(InstructionNode *instruction);
/* ------------------------------------------------ */
/* Instruction Queue Structure */
/* ------------------------------------------------ */
typedef struct strInstructionQueue{
/* Instructions Array */
InstructionNode **instructions;
/* Structure size */
int size;
/* Quantity of elements */
int length;
} InstructionQueue, *ptrInstructionQueue;
/* ---------- Instruction Queue Methods ----------- */
/* Return a new Instruction Queue */
InstructionQueue* newInstructionQueue();
/* Free a instruction queue */
bool deleteInstructionQueue(ptrInstructionQueue *instruction_queue);
/* Add a new instruction to instruction queue */
bool instructionQueueEnqueueInstruction(InstructionQueue *instruction_queue,
char *instruction_string,
bool copyInstruction);
/* Add a new instruction node structure to instruction queue */
bool instructionQueueEnqueueInstructionNode(InstructionQueue *instruction_queue,
InstructionNode *instruction_node);
/* Print a instruction queue on a given file */
bool instructionQueueFilePrint(FILE *_output_file, InstructionQueue *instruction);
/* ------------------------------------------------ */
/* Utilities */
/* ------------------------------------------------ */
/* Size of blocks in structures */
#define DEFAULT_BLOCK_SIZE 10
/* Standard size of the type and size of the type descriptor */
#define DEFAULT_SYMBOL_SIZE 4
#define DEFAULT_SYMBOL_TYPE_SIZE 0
/* Newline char */
#define NEWLINE_CHAR "\n"
/* Tab char */
#define TAB_CHAR "\t"
/* Empty String */
#define EMPTY_STRING ""
/* Variable Size in Bytes */
#define BYTE_VARIABLE_SIZE 4
/* ------------------------------------------------ */
#endif