-
Notifications
You must be signed in to change notification settings - Fork 1
/
tokenizer.c
184 lines (153 loc) · 4.51 KB
/
tokenizer.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
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "tokenizer.h"
// max chars that a token can represent
#define MAX_TOKEN_SIZE 30
int is_number(char *str, int len)
{
for (int i = 0; i < len; ++i)
if (!isdigit(str[i]) && str[i] != '.')
return 0;
return 1;
}
void free_token(token *t)
{
if (t->value.data != NULL && (t->type == literal_string || t->type == func_call || t->type == quote))
{
if (t->type == quote)
free_token(t->value.data);
else
free(t->value.data);
}
free(t);
}
token* copy_token(token* origin)
{
token *output = malloc(sizeof(token));
output->type = origin->type;
if (origin->type == literal_number)
output->value.literal = origin->value.literal;
else if (origin->type == literal_string || origin->type == func_call || origin->type == func_def)
{
int len = strlen((char*)origin->value.data) + 1;
output->value.data = malloc(sizeof(char) * len);
memcpy(output->value.data, origin->value.data, len);
}
else if (origin->type == quote)
{
output->value.data = copy_token((token*)origin->value.data);
}
return output;
}
token* create_literal(char *str, int len)
{
token *t = malloc(sizeof(token));
t->value.data = NULL;
if (strncmp(str, "proc", len) == 0)
{
t->type = func_def;
}
else if (strncmp(str, "end", len) == 0)
{
t->type = func_end;
}
else if (str[0] == '"' && str[len - 1] == '"') // string
{
t->type = literal_string;
t->value.data = malloc(sizeof(char) * len - 1);
memcpy(t->value.data, str + 1, len - 1);
((char*)t->value.data)[len - 2] = '\0';
}
else if (is_number(str, len))
{
t->type = literal_number;
t->value.literal = (float) atof(str);
// WARN: possible buffer overflow
// TODO: Write a safer atof
}
else
{
// Assume it's a func call
t->type = func_call;
t->value.data = malloc(sizeof(char) * (len + 1));
memcpy(t->value.data, str, len);
((char*)t->value.data)[len] = '\0';
}
return t;
}
token_list* tokenize(char *program, int len, int *token_count)
{
token_list *root = NULL,
*currentNode = NULL;
char buffer[MAX_TOKEN_SIZE];
int insideComment = 0,
insideQuote = 0,
insideWrap = 0,
bufferCounter = 0;
for (int i = 0; i < len; ++i)
{
if (program[i] == '\n' || program[i] == '\r')
{
insideComment = 0;
}
if (program[i] == ';')
insideComment = 1;
if (insideComment) continue;
if (program[i] == '"')
insideQuote = !insideQuote;
if ((program[i] == ' ' || program[i] == '\t' || program[i] == '\n' || program[i] == '\r') && !insideQuote)
{
if (bufferCounter > 0)
{
token *thisToken;
buffer[bufferCounter] = '\0';
thisToken = create_literal(buffer, bufferCounter);
if (insideWrap)
{
token *wrapper = malloc(sizeof(token));
wrapper->type = quote;
wrapper->value.data = thisToken;
thisToken = wrapper;
}
token_list *node = malloc(sizeof(token_list));
node->data = thisToken;
node->next = NULL;
if (currentNode != NULL)
currentNode->next = node;
else root = node;
currentNode = node;
(*token_count)++;
}
bufferCounter = 0;
insideWrap = 0;
continue;
}
if (program[i] == '@')
insideWrap++;
else
buffer[bufferCounter++] = program[i];
}
if (bufferCounter > 0)
{
token *thisToken;
buffer[bufferCounter] = '\0';
thisToken = create_literal(buffer, bufferCounter);
if (insideWrap)
{
token *wrapper = malloc(sizeof(token));
wrapper->type = quote;
wrapper->value.data = thisToken;
thisToken = wrapper;
}
token_list *node = malloc(sizeof(token_list));
node->data = thisToken;
node->next = NULL;
if (currentNode != NULL)
currentNode->next = node;
else root = node;
currentNode = node;
(*token_count)++;
}
return root;
}