-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
133 lines (111 loc) · 2.91 KB
/
main.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
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int get_len(char* input) {
const char* current = input;
for (;; ++current) {
if (*current == '\0') {
return current - input;
}
}
}
struct token {
char* string;
int length;
};
struct token_list_wrap {
struct token* token_ptr;
int length;
};
// return 1 if input char is space or nulterminator
// || input == '\n' // newline is needed
int is_whitespace(char input) {
if (input == ' ' || input == '\0') {
return 1;
} else {
return 0;
}
}
// tokenizer (lexer?)
struct token_list_wrap* scan(char* input) {
int length = get_len(input) + 1;
int token_counter = -1;
struct token* token_list = malloc(sizeof(struct token) * length);
int cur_len = 0;
char* cur_beg = input;
for (int i = 0; i < length; i++) {
cur_len++;
if (!(isalpha(input[i]) || isdigit(input[i]) || input[i] == '_')) {
// register token
token_counter++;
token_list[token_counter].string = cur_beg;
token_list[token_counter].length = cur_len - 1;
cur_len = 0;
// register token i
token_counter++;
token_list[token_counter].string = input + i;
token_list[token_counter].length = 1;
cur_beg = input + i + 1;
}
}
// remove whitespaces
int tcr = 0;
for (int i = 0; i < token_counter; i++) {
if (token_list[i].length < 1) {
continue;
} else if (is_whitespace(token_list[i].string[0])) {
continue;
} else {
token_list[tcr].string = token_list[i].string;
token_list[tcr].length = token_list[i].length;
tcr++;
}
}
token_list = realloc(token_list, sizeof(struct token) * tcr);
struct token_list_wrap* ret = malloc(sizeof(struct token_list_wrap));
ret->token_ptr = token_list;
ret->length = tcr;
return ret;
}
char* types[2] = {"int", "float"};
int is_type(char* val) {
for (int i = 0; i < 2; i++) {
if (val == types[i]) {
return 1;
}
}
return 0;
}
// TODO
void parsing_begin(struct token* tokens, int lenght) {
if (is_type(tokens->string)) {
// call function for var or func
printf("todo func handler\n");
}
}
int main() {
FILE* infile;
char* buffer;
long numbytes;
infile = fopen("main.c", "r");
fseek(infile, 0L, SEEK_END);
numbytes = ftell(infile);
fseek(infile, 0L, SEEK_SET);
buffer = (char*)calloc(numbytes, sizeof(char));
fread(buffer, sizeof(char), numbytes, infile);
fclose(infile);
struct token_list_wrap* res;
res = scan(buffer);
printf("total: %i tokens\n", res->length);
char* print_str = malloc(sizeof(char) * 50);
for (int i = 0; i < res->length; i++) {
strncpy(print_str, (res->token_ptr + i)->string, (res->token_ptr + i)->length);
print_str[(res->token_ptr + i)->length] = '\0';
printf("len: %i\n", (res->token_ptr + i)->length);
printf("|%s|\n", print_str);
}
printf("\n");
parsing_begin(res->token_ptr, res->length);
return 0;
}