-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenizer.c
More file actions
151 lines (139 loc) · 4.07 KB
/
tokenizer.c
File metadata and controls
151 lines (139 loc) · 4.07 KB
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
#include "tokenizer.h"
#include "lizard.h"
#include "mem.h"
bool lizard_is_digit(const char *input, int i) {
if (input[i] >= '0' && input[i] <= '9') {
return true;
}
if ((input[i] == '-' || input[i] == '+') && input[i + 1] >= '0' &&
input[i + 1] <= '9') {
return true;
}
return false;
}
void lizard_add_token(list_t *list, lizard_token_type_t token_type,
char *data) {
lizard_token_list_node_t *node;
node = lizard_heap_alloc(sizeof(lizard_token_list_node_t));
node->token.type = token_type;
switch (token_type) {
case TOKEN_LEFT_PAREN:
case TOKEN_RIGHT_PAREN:
node->token.data.string = NULL;
break;
case TOKEN_SYMBOL:
node->token.data.symbol = data;
break;
case TOKEN_NUMBER:
mpz_init(node->token.data.number);
mpz_set_str(node->token.data.number, data, 10);
lizard_heap_free(data);
break;
case TOKEN_STRING:
node->token.data.string = data;
break;
}
list_append(list, &node->node);
}
list_t *lizard_tokenize(const char *input) {
list_t *list = list_create_alloc(lizard_heap_alloc, lizard_heap_free);
int i, j, k;
char *buffer;
i = 0;
while (input[i] != '\0') {
if (input[i] == ' ') {
i++;
continue;
}
if (input[i] == '(') {
lizard_add_token(list, TOKEN_LEFT_PAREN, NULL);
i++;
} else if (input[i] == ')') {
lizard_add_token(list, TOKEN_RIGHT_PAREN, NULL);
i++;
} else if (input[i] == '"') {
j = i;
i++;
while (input[i] != '"' && input[i] != '\0') {
i++;
}
if (input[i] == '\0') {
fprintf(stderr, "Error: unterminated string.\n");
exit(1);
}
buffer = lizard_heap_alloc(sizeof(char) * (long unsigned int)(i - j));
for (k = 0, j++; j < i; j++, k++) {
buffer[k] = input[j];
}
buffer[k] = '\0';
lizard_add_token(list, TOKEN_STRING, buffer);
i++;
} else if (lizard_is_digit(input, i)) {
j = i;
while (lizard_is_digit(input, i)) {
i++;
}
buffer = lizard_heap_alloc(sizeof(char) * (long unsigned int)(i - j + 1));
for (k = 0; j < i; j++, k++) {
buffer[k] = input[j];
}
buffer[k] = '\0';
lizard_add_token(list, TOKEN_NUMBER, buffer);
} else {
j = i;
if (input[i] == '\'' || input[i] == '`' || input[i] == ',' ||
input[i] == '!' || input[i] == '@' || input[i] == '#' ||
input[i] == '$' || input[i] == '%' || input[i] == '^' ||
input[i] == '&' || input[i] == '[' || input[i] == ']' ||
(input[i] == ',' && input[i + 1] == '@')) {
if (input[i] == ',' && input[i + 1] == '@') {
buffer = lizard_heap_alloc(sizeof(char) * 3);
buffer[0] = input[i];
buffer[1] = input[i + 1];
buffer[2] = '\0';
lizard_add_token(list, TOKEN_SYMBOL, buffer);
i += 2;
} else {
buffer = lizard_heap_alloc(sizeof(char) * 2);
buffer[0] = input[i];
buffer[1] = '\0';
lizard_add_token(list, TOKEN_SYMBOL, buffer);
i++;
}
} else {
while (input[i] != ' ' && input[i] != '\0' && input[i] != '(' &&
input[i] != ')' && input[i] != '\'' && input[i] != '`' &&
input[i] != ',') {
i++;
}
buffer =
lizard_heap_alloc(sizeof(char) * (long unsigned int)(i - j + 1));
for (k = 0; j < i; j++, k++) {
buffer[k] = input[j];
}
buffer[k] = '\0';
lizard_add_token(list, TOKEN_SYMBOL, buffer);
}
}
}
return list;
}
void lizard_destroy_token(list_node_t *node) {
lizard_token_list_node_t *token_node = CAST(node, lizard_token_list_node_t);
lizard_token_t *token = &token_node->token;
switch (token->type) {
case TOKEN_SYMBOL:
case TOKEN_STRING:
lizard_heap_free(token->data.string);
break;
case TOKEN_NUMBER:
mpz_clear(token->data.number);
break;
default:
break;
}
lizard_heap_free(token_node);
}
void lizard_lizard_heap_free_tokens(list_t *token_list) {
list_destroy(token_list, lizard_destroy_token);
}