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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
#include "types.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Token {
// identifying tag
enum TokenType {
t_error,
t_size,
t_multiply,
t_divide,
t_add,
t_subtract,
t_whitespace,
t_newline,
t_number,
} type;
// change to offset + size, or line + col -- alleviated by helper structure of
// ptrs to each newline
union Data {
i32 number;
} data;
} Token_t;
// Should also add helper structure with pointers to each line
static isize f_read(FILE* stream, char** dst);
static i64 f_size(FILE* stream);
static void l_print_token(Token_t token);
static Token_t* l_read(char* text, isize text_length, Token_t* dst);
int
main(int argc, char* argv[]) {
if (argc == 2) {
FILE* f = NULL;
char* contents = NULL;
isize content_length = -1;
Token_t* tokens = NULL;
f = fopen(argv[1], "r");
if (f == NULL) {
fprintf(stdout, "! Failed to open file: %s\n", argv[1]);
}
if (f != NULL) {
content_length = f_read(f, &contents);
if (content_length > 0) {
printf("%s:%ld\n%s\n", argv[1], content_length, contents);
}
fclose(f);
}
if (content_length > 0) {
tokens = l_read(contents, strlen(contents), NULL);
free(contents);
}
if (tokens != NULL) {
printf("%d tokens\n", tokens[0].data.number);
for (int t = 0; t < tokens[0].data.number; t++) {
l_print_token(tokens[t]);
}
}
}
return 0;
}
/* Copies the contents of the file stream to *dst.
* @param stream An open file stream.
* @param dst** Destination buffer. If *dst is NULL allocates a buffer the size
* of stream using malloc.
* @return The number of bytes copied into dst.
* @error returns -1 on error.
*/
static isize
f_read(FILE* stream, char** dst) {
i64 file_sz = f_size(stream);
if (dst == NULL) {
fprintf(stderr, "! Got NULL ptr\n");
}
if (dst != NULL && *dst == NULL && file_sz > 0) {
*dst = (char*)malloc(file_sz * sizeof(char));
}
if (dst != NULL && *dst == NULL && file_sz > 0) {
fprintf(stderr, "! Failed to allocate %ld bytes\n", file_sz * sizeof(char));
}
isize bytes_read = -1;
if (dst != NULL && *dst != NULL && file_sz > 0) {
bytes_read = fread(*dst, sizeof(char), file_sz, stream);
}
if (bytes_read < file_sz) {
fprintf(stderr, "! Failed to read file, read %ld out of %ld bytes\n",
bytes_read, file_sz);
}
return bytes_read;
}
/* Returns the size in bytes and rewinds the stream.
* @param stream An open file stream.
* @return the size of the file in bytes.
* @error Returns -1 on error.
*/
static i64
f_size(FILE* stream) {
// Could ftell and rewind to previous position, but scratch that
i64 size = -1;
i32 error = fseek(stream, 0, SEEK_END);
if (error) {
// TODO: handle pipes (check errno == ESPIPE)
fprintf(stderr, "! Failed to seek file: %s\n", strerror(errno));
} else {
size = ftell(stream);
rewind(stream);
}
return size;
}
static void
l_print_token(Token_t token) {
switch (token.type) {
case t_error:
fputs(" error ", stdout);
break;
case t_size:
printf(" [%d] ", token.data.number);
break;
case t_multiply:
fputs(" * ", stdout);
break;
case t_divide:
fputs(" / ", stdout);
break;
case t_add:
fputs(" + ", stdout);
break;
case t_subtract:
fputs(" - ", stdout);
break;
case t_number:
printf("%d", token.data.number);
break;
default:
break;
}
}
/* Reads text and parses it into an array of tokens
* @param text A buffer holding the input text
* @return a pointer to dst
* @error Returns NULL on error.
*/
static Token_t*
l_read(char* text, isize text_length, Token_t* dst) {
if (text_length > 0 && dst == NULL) {
dst = (Token_t*)malloc(text_length * sizeof(Token_t));
}
if (text_length > 0 && dst == NULL) {
fprintf(stderr, "! Failed to allocate %ld bytes\n",
text_length * sizeof(Token_t));
}
if (text_length > 0 && dst != NULL) {
isize i = 0;
isize t = 1;
i32 error = 0;
dst[0].type = t_size;
dst[0].data.number = 0;
// TODO: rewrite to
// if token != last-token
// dst = token,line,col
// last-token = token
// t++;
// while (last-token != error)
do {
switch (text[i]) {
// case t_size:
// break;
case '*':
dst[t].type = t_multiply;
t++;
break;
case '/':
dst[t].type = t_divide;
t++;
break;
case t_add:
dst[t].type = t_add;
t++;
break;
case '-':
dst[t].type = t_subtract;
t++;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
dst[t].type = t_number;
dst[t].data.number = text[i] - '0';
t++;
break;
case '+':
dst[t].type = t_add;
t++;
break;
case ' ':
case '\t':
dst[t].type = t_whitespace;
t++;
break;
case '\n':
dst[t].type = t_newline;
t++;
break;
default:
dst[t].type = t_error;
t++;
error = 1;
break;
}
i++;
} while (!error && i < text_length);
dst[0].data.number = t;
}
return dst;
}
|