#include #include #include #include #include #include "ast.h" #include "parse.h" #include "compile.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 -nut | bat --file-name=out.c"; setenv("SSSPATH", ".", 0); sss_file_t *f = sss_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 --file-name='%s'", argv[1]), "w"); fputs(f->text, out); fclose(out); } if (verbose) { FILE *out = popen("bat --file-name=AST", "w"); fputs(ast_to_str(ast), out); fclose(out); } CORD header = "#include \"nextlang.h\"\n\n"; // Predeclare types: for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) { switch (stmt->ast->tag) { case StructDef: case EnumDef: { header = CORD_cat(header, compile(stmt->ast)); break; } default: break; } } // Predeclare funcs: for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) { switch (stmt->ast->tag) { case FunctionDef: { auto fndef = Match(stmt->ast, FunctionDef); CORD_sprintf(&header, "%rstatic %r %r(", header, fndef->ret_type ? compile_type(fndef->ret_type) : "void", compile(fndef->name)); for (arg_ast_t *arg = fndef->args; arg; arg = arg->next) { CORD_sprintf(&header, "%r%r %s", header, compile_type(arg->type), arg->name); if (arg->next) header = CORD_cat(header, ", "); } header = CORD_cat(header, ");\n"); break; } default: break; } } CORD program = CORD_cat(header, "\n/////////////////////////////////////////////////////////////////////////\n\n" "bool USE_COLOR = true;"); // Declare funcs: for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) { switch (stmt->ast->tag) { case FunctionDef: { CORD_sprintf(&program, "%r\n\n%r", program, compile(stmt->ast)); break; } default: break; } } // Main body: program = CORD_cat(program, "\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" "\n"); for (ast_list_t *stmt = Match(ast, Block)->statements; stmt; stmt = stmt->next) { switch (stmt->ast->tag) { case FunctionDef: case StructDef: case EnumDef: break; default: { program = CORD_cat(program, compile(stmt->ast)); program = CORD_cat(program, ";\n"); break; } } } program = CORD_cat(program, "\nreturn 0;\n}\n"); if (verbose) { FILE *out = popen(autofmt, "w"); CORD_put(program, out); fclose(out); } const char *flags = getenv("CFLAGS"); if (!flags) flags = "-std=c11 -lm -lgc -lcord"; const char *run = heap_strf(verbose ? "tcc %s -run - | bat --file-name=output.txt" : "tcc %s -run -", flags); FILE *cc = popen(run, "w"); CORD_put(program, cc); fclose(cc); return 0; } // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0