forked from HuoLanguage/huo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenizer.c
84 lines (82 loc) · 2.74 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
#include <string.h>
#include <stddef.h>
#include <assert.h>
#include "constants.h"
struct Tokens * tokenize(struct String file, struct Tokens *content){
assert(string_is_sane(&file));
int counter = 0;
while (counter < file.length){
char c = file.body[counter];
if(c != ' ' && c != '\n'){
struct Token t = {
.data = {
.length = 0,
.body = NULL
}
};
assert(string_is_sane(&t.data));
if(is_a_open_parens(c)){
t.type = 'o';
}
else if(is_a_close_parens(c)){
t.type = 'c';
}
else if(is_a_bracket(c)){
t.type = 'b';
}
else if(is_a_end_bracket(c)){
t.type = 'e';
}
else if(is_a_number(c)){
t.type = 'n';
while(is_a_number(c) || c == dot_const){
RESIZE(t.data.body, t.data.length+1);
t.data.body[t.data.length] = file.body[counter];
t.data.length++;
counter++;
c = file.body[counter];
}
RESIZE(t.data.body, t.data.length+1);
t.data.body[t.data.length] = 0;
assert(string_is_sane(&t.data));
counter--;
}
else if(is_a_quote(c)){
t.type = 's';
counter++;
char s = file.body[counter];
while(!is_a_quote(s)){
RESIZE(t.data.body, t.data.length+1);
t.data.body[t.data.length] = file.body[counter];
t.data.length++;
counter++;
s = file.body[counter];
}
RESIZE(t.data.body, t.data.length+1);
t.data.body[t.data.length] = 0;
assert(string_is_sane(&t.data));
}
else if(is_a_function(c)){
t.type = c;
}
else if(is_a_letter(c)){
t.type = 'k';
while(is_a_letter(c)){
RESIZE(t.data.body, t.data.length+1);
t.data.body[t.data.length] = file.body[counter];
t.data.length++;
counter++;
c = file.body[counter];
}
RESIZE(t.data.body, t.data.length+1);
t.data.body[t.data.length] = 0;
assert(string_is_sane(&t.data));
counter--;
}
content->tokens[content->length] = t;
content->length++;
}
counter++;
}
return content;
}