147 lines
2.8 KiB
C
147 lines
2.8 KiB
C
#include <assert.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "error.h"
|
|
#include "parser.h"
|
|
#include "tokenizer.h"
|
|
#include "vector.h"
|
|
|
|
[[noreturn]] static void usage(void);
|
|
|
|
static VEC(char) read_file(char const *);
|
|
static void read_into_vec(VEC(char) *, FILE *);
|
|
|
|
static int print_tokens(SPAN(char), SPAN(token));
|
|
|
|
enum mode { TOKENIZE, PARSE };
|
|
|
|
int main(int argc, char **argv) {
|
|
argv0 = argv[0];
|
|
|
|
// TODO: proper argument parsing
|
|
if (argc != 3) {
|
|
usage();
|
|
}
|
|
|
|
auto subcommand = argv[1];
|
|
enum mode mode;
|
|
if (!strcmp(subcommand, "tokenize")) {
|
|
mode = TOKENIZE;
|
|
} else if (!strcmp(subcommand, "parse")) {
|
|
mode = PARSE;
|
|
} else {
|
|
usage();
|
|
}
|
|
|
|
auto path = argv[2];
|
|
|
|
auto source = read_file(path);
|
|
|
|
auto source_span = VEC_AS_SPAN(char, source);
|
|
|
|
auto tokens = tokenize(source_span);
|
|
|
|
if (mode == TOKENIZE) {
|
|
return print_tokens(source_span, VEC_AS_SPAN(token, tokens));
|
|
}
|
|
|
|
auto parsed = parse(VEC_AS_SPAN(token, tokens));
|
|
VEC_FREE(tokens);
|
|
|
|
bool error = false;
|
|
for (size_t i = 0; i < LENGTH(parsed.nodes); i++) {
|
|
auto node = AT(parsed.nodes, i);
|
|
if (node.type == NODE_ERROR) {
|
|
fprintf(
|
|
stderr, "%s:%zu:%zu error\n", path, node.at.line, node.at.column
|
|
);
|
|
error = true;
|
|
}
|
|
}
|
|
|
|
return error;
|
|
}
|
|
|
|
static void usage(void) {
|
|
fprintf(stderr, "usage: %s tokenize <file>\n", argv0);
|
|
fprintf(stderr, " %s parse <file>\n", argv0);
|
|
exit(1);
|
|
}
|
|
|
|
static VEC(char) read_file(char const *path) {
|
|
VEC(char) buffer = {};
|
|
|
|
if (!strcmp(path, "-")) {
|
|
read_into_vec(&buffer, stdin);
|
|
|
|
if (ferror(stdin)) {
|
|
die("failure reading stdin");
|
|
}
|
|
|
|
} else {
|
|
FILE *f = fopen(path, "r");
|
|
if (!f) {
|
|
die("cannot open file \"%s\"", path);
|
|
}
|
|
|
|
read_into_vec(&buffer, f);
|
|
|
|
if (ferror(f)) {
|
|
die("failure reading file \"%s\"", path);
|
|
}
|
|
|
|
fclose(f);
|
|
}
|
|
|
|
return buffer;
|
|
}
|
|
|
|
static void read_into_vec(VEC(char) *buffer, FILE *f) {
|
|
for (;;) {
|
|
VEC_ENSURE_FREE_SPACE(*buffer);
|
|
auto free_space = VEC_FREE_SPACE(char, *buffer);
|
|
|
|
auto bytes_read = fread(&AT(free_space, 0), 1, LENGTH(free_space), f);
|
|
if (bytes_read == 0) {
|
|
break;
|
|
}
|
|
|
|
VEC_RESIZE(*buffer, LENGTH(*buffer) + bytes_read);
|
|
}
|
|
}
|
|
|
|
static int print_tokens(SPAN(char) source, SPAN(token) tokens) {
|
|
bool error = false;
|
|
|
|
for (size_t i = 0; i < LENGTH(tokens); i++) {
|
|
auto tok = AT(tokens, i);
|
|
printf("%zu:%zu ", tok.at.line, tok.at.column);
|
|
|
|
switch (tok.id) {
|
|
case TOK_EOF: printf("EOF"); break;
|
|
case TOK_ERROR:
|
|
printf("(error) ");
|
|
error = true;
|
|
break;
|
|
case TOK_IDENTIFIER: printf("(identifier) "); break;
|
|
case TOK_NATURAL: printf("(natural) "); break;
|
|
default: printf("%d ", tok.id);
|
|
}
|
|
|
|
auto token_text =
|
|
SLICE(source, tok.at.offset, tok.at.offset + tok.length);
|
|
for (size_t j = 0; j < LENGTH(token_text); j++) {
|
|
putchar(AT(token_text, j));
|
|
}
|
|
|
|
printf("\n");
|
|
}
|
|
|
|
if (fflush(stdout)) {
|
|
die("fflush(stdout)");
|
|
}
|
|
|
|
return error;
|
|
}
|