aboutsummaryrefslogtreecommitdiff
path: root/tomo.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-02-24 16:06:49 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-02-24 16:06:49 -0500
commitc2228bf9861b38f2514d0cc8a270754bb3a03dd7 (patch)
tree494d52f6e596dd00ff6912c28c4a5dfab5a8423c /tomo.c
parentdbad340c29e5911119db33d6043cc502dbe40ed1 (diff)
Rename 'nextlang'->'tomo'
Diffstat (limited to 'tomo.c')
-rw-r--r--tomo.c93
1 files changed, 93 insertions, 0 deletions
diff --git a/tomo.c b/tomo.c
new file mode 100644
index 00000000..1646f5bd
--- /dev/null
+++ b/tomo.c
@@ -0,0 +1,93 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <gc.h>
+#include <gc/cord.h>
+#include <printf.h>
+
+#include "ast.h"
+#include "builtins/string.h"
+#include "compile.h"
+#include "parse.h"
+#include "typecheck.h"
+#include "types.h"
+
+int main(int argc, char *argv[])
+{
+ if (argc < 2) return 1;
+
+ // register_printf_modifier(L"p");
+ if (register_printf_specifier('T', printf_type, printf_pointer_size))
+ errx(1, "Couldn't set printf specifier");
+ if (register_printf_specifier('W', printf_ast, printf_pointer_size))
+ errx(1, "Couldn't set printf specifier");
+
+ const char *autofmt = getenv("AUTOFMT");
+ if (!autofmt) autofmt = "indent -kr -l100 -nbbo -nut -sob";
+
+ file_t *f = load_file(argv[1]);
+
+ ast_t *ast = parse_file(f, NULL);
+
+ if (!ast)
+ errx(1, "Could not compile!");
+
+ bool verbose = (getenv("VERBOSE") && strcmp(getenv("VERBOSE"), "1") == 0);
+ if (verbose) {
+ FILE *out = popen(heap_strf("bat -P --file-name='%s'", argv[1]), "w");
+ fputs(f->text, out);
+ fclose(out);
+ }
+
+ if (verbose) {
+ FILE *out = popen("bat -P --file-name=AST", "w");
+ fputs(ast_to_str(ast), out);
+ fclose(out);
+ }
+
+ 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=program.c", autofmt), "w");
+ CORD_put(program, out);
+ fclose(out);
+ }
+
+ const char *cflags = getenv("CFLAGS");
+ if (!cflags)
+ cflags = "-std=c11 -fdollars-in-identifiers -fsanitize=signed-integer-overflow -fno-sanitize-recover";
+
+ const char *ldlibs = "-lgc -lcord -lm -L. -ltomo";
+ if (getenv("LDLIBS"))
+ ldlibs = heap_strf("%s %s", ldlibs, getenv("LDLIBS"));
+
+ const char *ldflags = "-Wl,-rpath '-Wl,$ORIGIN'";
+
+ const char *cc = getenv("CC");
+ if (!cc) cc = "tcc";
+ 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_put(program, runner);
+ fclose(runner);
+
+ return 0;
+}
+
+// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0