spl

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 1a20d3678d76dca73e53ac14f644a7e8e71ec982
parent 1048fd39d56027d7931cb24f80a5d57636eb9b01
Author: Brian Swetland <swetland@frotz.net>
Date:   Thu, 12 Oct 2023 13:36:58 -0700

compiler0: some simple indentation

This makes the generated impl.c files a bit more readable

Diffstat:
Mcompiler0.c | 12+++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/compiler0.c b/compiler0.c @@ -363,6 +363,8 @@ void emit(FILE* fp, const char *fmt, ...) { va_end(ap); } +unsigned indent = 0; + void emit_impl(const char *fmt, ...) { va_list ap; va_start(ap, fmt); @@ -370,7 +372,15 @@ void emit_impl(const char *fmt, ...) { va_end(ap); ctx.outptr += n; if (fmt[strlen(fmt) - 1] == '\n') { - fwrite(ctx.outbuf, 1, ctx.outptr - ctx.outbuf, ctx.fp_impl); + unsigned len = ctx.outptr - ctx.outbuf; + fwrite(ctx.outbuf, 1, len, ctx.fp_impl); + for (unsigned n = 0; n - len; n++) { + if (ctx.outbuf[n] == '{') indent++; + if (ctx.outbuf[n] == '}') indent--; + } + for (unsigned n = 0; n < indent; n++) { + fwrite(" ", 1, 4, ctx.fp_impl); + } ctx.outptr = ctx.outbuf; ctx.outbuf[0] = 0; }