commit 1659953b0bf1329ab8c02b3d471bf57480783f47
parent 455d7eea841cdc8559ce5bcc77f7a85bf6dd35cc
Author: Brian Swetland <swetland@frotz.net>
Date: Sat, 14 Oct 2023 13:09:11 -0700
compiler0: improve indentation
Diffstat:
1 file changed, 9 insertions(+), 5 deletions(-)
diff --git a/compiler0.c b/compiler0.c
@@ -367,7 +367,7 @@ void emit(FILE* fp, const char *fmt, ...) {
va_end(ap);
}
-unsigned indent = 0;
+int indent = 0;
void emit_impl(const char *fmt, ...) {
va_list ap;
@@ -377,14 +377,18 @@ void emit_impl(const char *fmt, ...) {
ctx.outptr += n;
if (fmt[strlen(fmt) - 1] == '\n') {
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++;
+ // any }s reduce the indent level of the current line
+ for (unsigned n = 0; n < len; n++) {
if (ctx.outbuf[n] == '}') indent--;
}
- for (unsigned n = 0; n < indent; n++) {
+ for (int n = 0; n < indent; n++) {
fwrite(" ", 1, 4, ctx.fp_impl);
}
+ fwrite(ctx.outbuf, 1, len, ctx.fp_impl);
+ // any {s increase the indent level of the next line
+ for (unsigned n = 0; n < len; n++) {
+ if (ctx.outbuf[n] == '{') indent++;
+ }
ctx.outptr = ctx.outbuf;
ctx.outbuf[0] = 0;
}