mirror of
https://github.com/sigmasternchen/macrofuck
synced 2025-03-15 23:28:55 +00:00
feat: Includes are relative to input file
This commit is contained in:
parent
f7adfd62b3
commit
76fb52c60c
2 changed files with 25 additions and 1 deletions
compiler/src
|
@ -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+");
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue