WIP: #include
authorLukas Krickl <lukas@krickl.dev>
Sun, 10 Dec 2023 09:15:05 +0000 (10:15 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sun, 10 Dec 2023 09:15:05 +0000 (10:15 +0100)
include/ulas.h
src/ulas.c
tests/t0.s
tests/t1.s [new file with mode: 0644]

index e6b2b67cea83d4ec21a25599f500d8a46fd1e3cd..c598e56e94a8b74122f5cf6e3953b9279fb6d091 100644 (file)
@@ -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 {
index a7c85cb90d873e89dc42dddba75aff300dd9dbb1..cc5253efdc4c2387deceee1a047f50a7f80c7557 100644 (file)
@@ -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;
index 36daff4bb8c3c81ba4aedf7f857f7d4f91daa485..2dbd3f0b6eba59b64a268aaf7aac205c0de3768e 100644 (file)
@@ -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 (file)
index 0000000..e8a929e
--- /dev/null
@@ -0,0 +1,2 @@
+  nop
+  halt