-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
56 lines (48 loc) · 1.34 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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "lexer.h"
int main(int argc, char **argv) {
FILE *stream;
char *line = NULL;
size_t len = 0;
ssize_t read;
struct Lexer lexer;
struct Token token;
stream = fdopen(STDIN_FILENO, "rb");
if (!stream) {
exit(EXIT_FAILURE);
}
setbuf(stdout, 0);
while(1) {
printf(">> ");
read = getline(&line, &len, stream);
if (read > 0) {
if (lexer_create(&lexer, line, line+read) != 0) {
fprintf(stderr, "Failed to create lexer!\n");
exit(EXIT_FAILURE);
}
// printf("Retrieved line of length %zu :\n", read);
// printf("%s", line);
do {
if (lexer_next_token(&lexer, &token) != 0) {
fprintf(stderr, "Lexer failed to get next Token!\n");
break;
}
printf("%s\n", token_to_string(token.type));
} while (token.type != EndOfFile);
if (lexer_destroy(&lexer) != 0) {
fprintf(stderr, "Failed to destroy lexer!\n");
exit(EXIT_FAILURE);
}
} else {
break;
}
}
free(line);
fclose(stream);
exit(EXIT_SUCCESS);
return 0;
}