115 lines
2.7 KiB
C
115 lines
2.7 KiB
C
#include "parse.h"
|
|
#include "report.h"
|
|
#include <errno.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
typedef struct {
|
|
char* input_file;
|
|
char* output_file;
|
|
} Args;
|
|
|
|
static int parse_args(Args* args, int argc, char** argv);
|
|
|
|
static char* read_text_file(const char* filename);
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
Args args;
|
|
if (parse_args(&args, argc, argv) != 0) {
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
char* text = read_text_file(args.input_file);
|
|
if (!text) {
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
Parser* parser = parser_new(args.input_file, text);
|
|
|
|
size_t lines_capacity = 1024;
|
|
PLine** lines = malloc(sizeof(PLine*) * lines_capacity);
|
|
size_t lines_size = 0;
|
|
|
|
while (!parser_done(parser)) {
|
|
if (parser_next_is_const(parser)) {
|
|
PConst* stmt = parser_parse_const(parser);
|
|
|
|
} else if (parser_next_is_include(parser)) {
|
|
PInclude* stmt = parser_parse_include(parser);
|
|
|
|
} else {
|
|
PLabel* label;
|
|
while ((label = parser_parse_label(parser)) != nullptr) { }
|
|
}
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int parse_args(Args* args, int argc, char** argv)
|
|
{
|
|
*args = (Args) {
|
|
.input_file = nullptr,
|
|
.output_file = nullptr,
|
|
};
|
|
|
|
int i = 1;
|
|
while (i < argc) {
|
|
if (strcmp(argv[i], "-o") == 0) {
|
|
i += 1;
|
|
if (i >= argc) {
|
|
fprintf(stderr, FMT_ERROR("expected filename after '-o'"));
|
|
return 1;
|
|
}
|
|
args->output_file = argv[i];
|
|
i += 1;
|
|
} else if (argv[i][0] == '-') {
|
|
fprintf(stderr, FMT_ERROR("unrecognized argument '%s'"), argv[i]);
|
|
return 1;
|
|
} else {
|
|
if (args->input_file != nullptr) {
|
|
fprintf(stderr, FMT_ERROR("multiple input files"));
|
|
return 1;
|
|
}
|
|
args->input_file = argv[i];
|
|
i += 1;
|
|
}
|
|
}
|
|
|
|
if (!args->input_file) {
|
|
fprintf(stderr, FMT_ERROR("no input file"));
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
char* read_text_file(const char* filename)
|
|
{
|
|
FILE* fp = fopen(filename, "r");
|
|
if (!fp) {
|
|
fprintf(stderr,
|
|
FMT_ERROR("could not open file '%s' for reading: %s"),
|
|
filename,
|
|
strerror(errno));
|
|
return NULL;
|
|
}
|
|
fseek(fp, 0L, SEEK_END);
|
|
size_t file_size = (size_t)ftell(fp);
|
|
rewind(fp);
|
|
|
|
char* text = calloc(file_size + 1, sizeof(char));
|
|
size_t bytes_read = fread(text, sizeof(char), file_size, fp);
|
|
fclose(fp);
|
|
if (bytes_read != file_size) {
|
|
fprintf(stderr,
|
|
FMT_ERROR("could not read input file '%s': %s"),
|
|
filename,
|
|
strerror(errno));
|
|
return NULL;
|
|
}
|
|
return text;
|
|
}
|