Initial commit
This commit is contained in:
40
main.c
Normal file
40
main.c
Normal file
@@ -0,0 +1,40 @@
|
||||
#include "parse.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, const char **argv) {
|
||||
if (argc < 2) {
|
||||
printf("Not enough args!\n");
|
||||
return 1;
|
||||
}
|
||||
FILE *in = fopen(argv[1], "r");
|
||||
fseek(in, 0, SEEK_END);
|
||||
off_t length = ftello(in);
|
||||
rewind(in);
|
||||
char *text = malloc(length);
|
||||
fread(text, 1, length, in);
|
||||
fclose(in);
|
||||
TokenStream stream;
|
||||
TokenStreamError err;
|
||||
Token token;
|
||||
token_stream_init(&stream, text, length);
|
||||
while (!token_stream_is_eof(&stream)) {
|
||||
token_stream_next(&stream, &token, &err);
|
||||
if (token_stream_error_set(&err)) {
|
||||
printf("Error at %zu:%zu: %s\n", err.pos.line, err.pos.column,
|
||||
err.message);
|
||||
free(text);
|
||||
return 1;
|
||||
} else {
|
||||
char *ts = malloc(token.length + 1);
|
||||
memcpy(ts, &text[token.pos.offset], token.length);
|
||||
ts[token.length] = '\0';
|
||||
printf("%s: %s\n", token_type_to_string(token.type), ts);
|
||||
free(ts);
|
||||
}
|
||||
}
|
||||
free(text);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user