-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokenize.c
54 lines (46 loc) · 1.24 KB
/
tokenize.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
#include "tokenize.h"
vector tokenizer(char* input_text) {
vector tokens = vector_init(Token, 1);
for(int i = 0; i < strlen(input_text); i++) {
char c = input_text[i];
if(isWhiteSpace(c)) continue;
if(isOpenBracket(c) || isClosedBracket(c)) {
token new_token = token_init(Bracket, &c);
push_back(tokens, new_token);
continue;
}
if(isName(c)) {
short j = 0; char aux[100];
while(!isWhiteSpace(input_text[i])) {
aux[j] = input_text[i]; i++; j++;
}
aux[j] = 0;
char* name = (char*)malloc(j);
strcpy(name, aux);
token new_token = token_init(Name, name);
push_back(tokens, new_token);
continue;
}
if(isNumber(c)) {
int number = 0;
while(isNumber(c)) {
number *= 10;
number += c-'0';
c = input_text[++i];
}
token new_token = token_init(Number, &number);
push_back(tokens, new_token); i--;
continue;
}
if(isString(c)) {
char* str = ""; i++;
while(!isString(input_text[i]) && isCharacter(input_text[i])) {
str += input_text[i]; i++;
}
token new_token = token_init(String, str);
push_back(tokens, new_token);
continue;
}
}
return tokens;
}