aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-03-03 14:45:36 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-03-03 14:45:36 -0500
commit09b1d07d8bf9c712339d94653ebf077c94623a62 (patch)
tree7d294fd942f5ad31a6802d46c8ec786aef4415de
parent7237cac2e558cf557260553823b39bc8e5e64171 (diff)
Clean up transpiling into separate .c/.h files
-rw-r--r--compile.c1
-rw-r--r--tomo.c69
2 files changed, 45 insertions, 25 deletions
diff --git a/compile.c b/compile.c
index ab72a6ad..6794ffe8 100644
--- a/compile.c
+++ b/compile.c
@@ -1085,6 +1085,7 @@ module_code_t compile_file(ast_t *ast)
return (module_code_t){
.header=CORD_all(
+ "#pragma once\n",
// CORD_asprintf("#line 0 %r\n", Str__quoted(ast->file->filename, false)),
env->code->imports, "\n",
env->code->typedefs, "\n",
diff --git a/tomo.c b/tomo.c
index f51bc7c8..5f540bf2 100644
--- a/tomo.c
+++ b/tomo.c
@@ -63,27 +63,13 @@ int main(int argc, char *argv[])
}
module_code_t module = compile_file(ast);
-
- CORD program = CORD_all(
- "// File: ", f->filename, ".h\n",
- module.header,
- "\n",
- "// File: ", f->filename, ".c\n",
- module.c_file,
- "\n",
- "int main(int argc, const char *argv[]) {\n"
- "(void)argc;\n"
- "(void)argv;\n"
- "GC_INIT();\n"
- "detect_color();\n"
- "$load();\n"
- "return 0;\n"
- "}\n"
- );
if (verbose) {
- FILE *out = popen(heap_strf("%s | bat -P --file-name=%s.c", autofmt, f->filename), "w");
- CORD_put(program, out);
+ FILE *out = popen(heap_strf("%s | bat -P --file-name=%s.h", autofmt, f->filename), "w");
+ CORD_put(module.header, out);
+ pclose(out);
+ out = popen(heap_strf("%s | bat -P --file-name=%s.c", autofmt, f->filename), "w");
+ CORD_put(CORD_all("#include \"", f->filename, "\"\n\n", module.c_file), out);
pclose(out);
}
@@ -105,25 +91,58 @@ int main(int argc, char *argv[])
const char *run = streq(cc, "tcc") ? heap_strf("tcc -run %s %s %s -", cflags, ldflags, ldlibs)
: heap_strf("gcc -x c %s %s %s - -o program && ./program", cflags, ldflags, ldlibs);
FILE *runner = popen(run, "w");
+
+ CORD program = CORD_all(
+ "// File: ", f->filename, ".h\n",
+ module.header,
+ "\n",
+ "// File: ", f->filename, ".c\n",
+ module.c_file,
+ "\n",
+ "int main(int argc, const char *argv[]) {\n"
+ "(void)argc;\n"
+ "(void)argv;\n"
+ "GC_INIT();\n"
+ "detect_color();\n"
+ "$load();\n"
+ "return 0;\n"
+ "}\n"
+ );
+
CORD_put(program, runner);
int status = pclose(runner);
return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE;
}
case MODE_TRANSPILE: {
- FILE *prog = popen(heap_strf("%s > %s.c", autofmt, f->filename), "w");
- CORD_put(program, prog);
+ FILE *prog = popen(heap_strf("%s > %s.h", autofmt, f->filename), "w");
+ CORD_put(module.header, prog);
int status = pclose(prog);
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
+ printf("Transpiled to %s.h\n", f->filename);
+ else
+ return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE;
+
+ prog = popen(heap_strf("%s > %s.c", autofmt, f->filename), "w");
+ CORD_put(CORD_all("#include \"", f->filename, ".h\"\n\n", module.c_file), prog);
+ status = pclose(prog);
+ if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
printf("Transpiled to %s.c\n", f->filename);
return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE;
}
case MODE_EXPANDED_TRANSPILE: {
- const char *cc = getenv("CC");
- if (!cc) cc = "tcc";
- FILE *prog = popen(heap_strf("%s -x c %s -E - | %s > %s.c", cc, cflags, autofmt, f->filename), "w");
- CORD_put(program, prog);
+ FILE *prog = popen(heap_strf("%s -x c %s -E - | %s > %s.h", cc, cflags, autofmt, f->filename), "w");
+ CORD_put(module.header, prog);
int status = pclose(prog);
if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
+ printf("Transpiled to %s.h\n", f->filename);
+ else
+ return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE;
+
+ prog = popen(heap_strf("%s -x c %s -E - | %s > %s.c", cc, cflags, autofmt, f->filename), "w");
+ CORD_put(CORD_all("#include \"", f->filename, ".h\"\n\n", module.c_file), prog);
+ CORD_put(module.c_file, prog);
+ status = pclose(prog);
+ if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
printf("Transpiled to %s.c\n", f->filename);
return WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE;
}