feat: Includes are relative to input file

This commit is contained in:
overflowerror 2024-05-18 11:15:03 +02:00
parent f7adfd62b3
commit 76fb52c60c
2 changed files with 25 additions and 1 deletions

View file

@ -10,6 +10,8 @@
#define EXIT_ARGS_ERROR (3)
const char* input_filename = NULL;
extern FILE* yyin;
extern int yyparse(void);
extern int yydebug;
@ -58,6 +60,7 @@ int main(int argc, char** argv) {
if (!input) {
panic("fopen");
}
input_filename = argv[optind];
if (!output) {
output = fopen("m.gen.bf", "w+");

View file

@ -2,6 +2,7 @@
#include <string.h>
#include <stdbool.h>
#include <alloca.h>
#include <libgen.h>
#include "y.tab.h"
@ -18,6 +19,8 @@ int yylex(void);
extern char* yytext;
extern YYSTYPE yylval;
extern const char* input_filename;
static void handle_include(void) {
int token = yylex();
@ -26,12 +29,30 @@ static void handle_include(void) {
panic("preprocessor syntax error");
}
const char* filename = yylval.str;
char* input = strdup(input_filename);
char* relative_path = strdup(dirname(input));
free(input);
char* filename = strdup(yylval.str);
if (filename[0] != '/') {
strbuf_t buffer = strbuf_from(relative_path);
strbuf_append_c(buffer, '/');
strbuf_append(buffer, filename);
free(filename);
filename = strdup(buffer);
strbuf_free(buffer);
}
free(relative_path);
FILE* file = fopen(filename, "r");
if (file == NULL) {
panic("file not found");
}
free(filename);
push_file_to_yy_stack(file);
}