-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoken.c
100 lines (76 loc) · 2.22 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
#include <malloc.h>
#include <string.h>
#include "token.h"
TokenList * initializeTokenList(TokenList * tkList) {
tkList = malloc(sizeof(TokenList));
tkList->length = 0;
tkList->first = NULL;
return tkList;
}
void printTokenList(TokenList * tl){
if(tl->first == NULL){
printf("The Token List is empty!!\n\n");
}
Token * token = tl->first;
while(token != NULL){
printToken(token);
token = token->next;
printf("--------------------------------------------------\n");
}
printf("Token List size: %d\n", tl->length);
return;
}
int appendToken(TokenList * list, Token * newToken) {
Token * token;
if(list == NULL) return 0;
newToken->tokenPosition = list->length;
if(list->first == NULL) {
list->first = newToken;
} else {
token = (Token*) list->first;
while(token->next != NULL){
token = token->next;
}
token->next = newToken;
}
list->length++;
return 1;
}
Token * getNextToken(TokenList * list) {
Token * ret;
if(list == NULL || list->first == NULL) {
return NULL;
}
ret = list->first;
list->first = list->first->next;
list->length--;
return ret;
}
Token * createToken ( char * start, char * end, int lineNumber, int position, TokenType type) {
char * content;
size_t tokenLength;
Token * token = malloc(sizeof(Token));
token->tokenLength = ((end - start) / sizeof(char)) +1;
token->content = malloc(token->tokenLength);
memcpy(token->content, start, (token->tokenLength));
token->type = type;
token->position = position;
token->lineNumber = lineNumber;
TokenValue * val = malloc(sizeof(TokenValue));
//TODO hacen lo mismo por ahora
if(type == LITERAL_NUMBER ){
val->string = start;
} else {
val->string = start;
}
//TODO: Checkear esto, Token tiene un TokenValue en lugar de un apuntador
token->value = *val;
//token->line
//token->lineLength
token->next = NULL;
return token;
}
void printToken(Token *token) {
printf("Token: %s \t Type: %d \t Length: %d \n", token->content, token->type, (int)token->tokenLength);
return;
}