From: Lukas Krickl Date: Sun, 10 Dec 2023 09:15:05 +0000 (+0100) Subject: WIP: #include X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=23d4006a62b0cb6a6de22e11dfe105de51a5d947;p=ulas%2F.git WIP: #include --- diff --git a/include/ulas.h b/include/ulas.h index e6b2b67..c598e56 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -34,6 +34,7 @@ #define ULAS_PPSTR_IFNDEF "#ifndef" #define ULAS_PPSTR_ENDMACRO "#endmacro" #define ULAS_PPSTR_UNDEF "#undefine" +#define ULAS_PPSTR_INCLUDE "#include" #define ULAS_ASMSTR_ORG ".org" #define ULAS_ASMSTR_SET ".set" @@ -268,7 +269,9 @@ enum ulas_ppdirs { // ifndef name ULAS_PPDIR_IFNDEF, // endif - ULAS_PPDIR_ENDIF + ULAS_PPDIR_ENDIF, + // include "filename" + ULAS_PPDIR_INCLUDE, }; enum ulas_ppdefs { diff --git a/src/ulas.c b/src/ulas.c index a7c85cb..cc5253e 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -840,14 +840,14 @@ int ulas_preprocline(struct ulas_preproc *pp, FILE *dst, FILE *src, char *line = ulas_preprocexpand(pp, raw_line, &n); const char *pline = line; - const char *dirstrs[] = {ULAS_PPSTR_DEF, ULAS_PPSTR_MACRO, - ULAS_PPSTR_IFDEF, ULAS_PPSTR_IFNDEF, - ULAS_PPSTR_ENDIF, ULAS_PPSTR_ENDMACRO, - ULAS_PPSTR_UNDEF, NULL}; + const char *dirstrs[] = { + ULAS_PPSTR_DEF, ULAS_PPSTR_MACRO, ULAS_PPSTR_IFDEF, + ULAS_PPSTR_IFNDEF, ULAS_PPSTR_ENDIF, ULAS_PPSTR_ENDMACRO, + ULAS_PPSTR_UNDEF, ULAS_PPSTR_INCLUDE, NULL}; enum ulas_ppdirs dirs[] = {ULAS_PPDIR_DEF, ULAS_PPDIR_MACRO, ULAS_PPDIR_IFDEF, ULAS_PPDIR_IFNDEF, ULAS_PPDIR_ENDIF, ULAS_PPDIR_ENDMACRO, - ULAS_PPDIR_UNDEF}; + ULAS_PPDIR_UNDEF, ULAS_PPDIR_INCLUDE}; enum ulas_ppdirs found_dir = ULAS_PPDIR_NONE; @@ -995,6 +995,33 @@ found: break; } + case ULAS_PPDIR_INCLUDE: { + int rc = found_dir; + char *path = ulas_strexpr(&pline, strlen(pline), &rc); + if (rc == -1 || !path) { + return rc; + } + FILE *f = ulas_incpathfopen(path, "re"); + if (!f) { + return -1; + } + char *prev_path = ulas.filename; + int prev_lines = ulas.line; + + ulas.filename = strdup(path); + ulas.line = 0; + + FILE *tmp = tmpfile(); + rc = ulas_preproc(tmp, f); + + ulas.filename = prev_path; + ulas.line = prev_lines; + + free(ulas.filename); + fclose(f); + fclose(tmp); + return rc; + } default: // this should not happen! break; diff --git a/tests/t0.s b/tests/t0.s index 36daff4..2dbd3f0 100644 --- a/tests/t0.s +++ b/tests/t0.s @@ -95,3 +95,5 @@ l2: .str "test1", "test2" .incbin "tests/inc.bin" +#include "tests/t1.s" + nop diff --git a/tests/t1.s b/tests/t1.s new file mode 100644 index 0000000..e8a929e --- /dev/null +++ b/tests/t1.s @@ -0,0 +1,2 @@ + nop + halt