tomo/nextlang.c

102 lines
2.8 KiB
C
Raw Normal View History

2024-02-04 12:23:59 -08:00
#include <stdio.h>
#include <stdlib.h>
#include <gc.h>
#include <gc/cord.h>
2024-02-07 21:52:18 -08:00
#include <printf.h>
2024-02-04 12:23:59 -08:00
#include "ast.h"
#include "parse.h"
#include "compile.h"
2024-02-07 21:52:18 -08:00
#include "types.h"
2024-02-04 12:23:59 -08:00
int main(int argc, char *argv[])
{
if (argc < 2) return 1;
2024-02-07 21:52:18 -08:00
// 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");
2024-02-04 15:04:41 -08:00
const char *autofmt = getenv("AUTOFMT");
2024-02-12 00:00:31 -08:00
if (!autofmt) autofmt = "indent -kr -l100 -nbbo -nut";
2024-02-04 15:04:41 -08:00
2024-02-11 16:06:42 -08:00
file_t *f = load_file(argv[1]);
2024-02-04 15:04:41 -08:00
2024-02-04 12:23:59 -08:00
ast_t *ast = parse_file(f, NULL);
2024-02-04 15:04:41 -08:00
if (!ast)
errx(1, "Could not compile!");
2024-02-11 11:02:00 -08:00
bool verbose = (getenv("VERBOSE") && strcmp(getenv("VERBOSE"), "1") == 0);
if (verbose) {
2024-02-11 23:10:45 -08:00
FILE *out = popen(heap_strf("bat -P --file-name='%s'", argv[1]), "w");
2024-02-10 12:36:35 -08:00
fputs(f->text, out);
fclose(out);
}
2024-02-11 11:02:00 -08:00
if (verbose) {
2024-02-11 23:10:45 -08:00
FILE *out = popen("bat -P --file-name=AST", "w");
2024-02-04 15:04:41 -08:00
fputs(ast_to_str(ast), out);
fclose(out);
}
env_t env = {.bindings = new(table_t)};
2024-02-04 18:13:50 -08:00
CORD_appendf(&env.imports, "#include \"nextlang.h\"\n");
CORD_appendf(&env.staticdefs, "static bool USE_COLOR = true;\n");
2024-02-04 15:04:41 -08:00
// Main body:
for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next)
CORD_appendf(&env.main, "%r\n", compile_statement(&env, stmt->ast));
CORD program = CORD_asprintf(
"// Generated code:\n"
"%r\n" // imports
"%r\n" // typedefs
"%r\n" // types
"//////////////////////////////////////////\n"
"%r\n" // static defs
"%r\n" // funcs
"\n"
"static void __load(void) {\n"
"%r\n" // main
"}\n"
"\n"
"int main(int argc, const char *argv[]) {\n"
"(void)argc;\n"
"(void)argv;\n"
"GC_INIT();\n"
"USE_COLOR = getenv(\"COLOR\") ? strcmp(getenv(\"COLOR\"), \"1\") == 0 : isatty(STDOUT_FILENO);\n"
"__load();\n"
"return 0;\n"
"}\n",
env.imports, env.typedefs, env.types, env.staticdefs,
env.funcs, env.main);
2024-02-04 15:04:41 -08:00
2024-02-11 11:02:00 -08:00
if (verbose) {
2024-02-11 23:10:45 -08:00
FILE *out = popen(heap_strf("%s | bat -P --file-name=program.c", autofmt), "w");
2024-02-11 11:07:49 -08:00
CORD_put(program, out);
2024-02-04 15:04:41 -08:00
fclose(out);
}
2024-02-11 19:12:21 -08:00
const char *cflags = getenv("CFLAGS");
if (!cflags)
cflags = "-std=c11";
const char *ldlibs = "-lgc -lcord -lm -L. -lnext";
2024-02-11 19:12:21 -08:00
if (getenv("LDLIBS"))
ldlibs = heap_strf("%s %s", ldlibs, getenv("LDLIBS"));
2024-02-13 17:27:19 -08:00
const char *run = heap_strf("tcc -run %s %s -", cflags, ldlibs);
2024-02-13 19:13:54 -08:00
// const char *run = heap_strf("gcc -x c %s %s -", cflags, ldlibs);
2024-02-04 15:04:41 -08:00
FILE *cc = popen(run, "w");
2024-02-11 11:07:49 -08:00
CORD_put(program, cc);
2024-02-04 15:04:41 -08:00
fclose(cc);
2024-02-04 12:23:59 -08:00
return 0;
}
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0