/* * Foundational functions for C code output. * * Copyright © 2020-2025 Samuel Lidén Borell * * SPDX-License-Identifier: EUPL-1.2+ */ #include #include #include "compiler.h" #include "out.h" const char *outfile_path = NULL; FILE *outfile = NULL; int indentlevel = 0; NORETURN void io_error(void) { perror(outfile_path); exit(EXIT_FAILURE); } NORETURN void ast_error(const char *msg) { fprintf(stderr, "bootstrap compiler: internal error: %s\n", msg); exit(EXIT_FAILURE); } void outc(char c) { if (fputc(c, outfile) == EOF) { io_error(); } } void indent(void) { int spaces = 4*indentlevel; while (spaces--) outc(' '); } static void outvf_(const char *fmt, va_list ap) { if (vfprintf(outfile, fmt, ap) < 0) { io_error(); } } void outf(const char *fmt, ...) { va_list ap; va_start(ap, fmt); outvf_(fmt, ap); va_end(ap); } void beginf(const char *fmt, ...) { va_list ap; va_start(ap, fmt); indent(); indentlevel++; outvf_(fmt, ap); va_end(ap); } void endf(const char *fmt, ...) { va_list ap; va_start(ap, fmt); indentlevel--; indent(); outvf_(fmt, ap); va_end(ap); } void indentf(const char *fmt, ...) { va_list ap; va_start(ap, fmt); indent(); outvf_(fmt, ap); va_end(ap); }