From 91580b2b9ea67f6ec15214b8c3c3579e6cc2fb15 Mon Sep 17 00:00:00 2001 From: Akuli Date: Fri, 10 Jan 2025 15:41:06 +0200 Subject: [PATCH] print_to_width --- compiler/cf_graph.jou | 6 +----- compiler/structs.jou | 17 ++++++----------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/compiler/cf_graph.jou b/compiler/cf_graph.jou index 8ab803b4..674dca3e 100644 --- a/compiler/cf_graph.jou +++ b/compiler/cf_graph.jou @@ -251,11 +251,7 @@ class CfGraph: printf(" Variables:\n") for var = self->locals; var < &self->locals[self->nlocals]; var++: printf(" ") - (*var)->print() - # Pad variable names with spaces to align them. - for i = (*var)->print_width(); i < 20; i++: - putchar(' ') - # If variable name is very long, put two spaces even in that case. + (*var)->print_to_width(20) printf(" %s\n", (*var)->type->name) for blockidx = 0; blockidx < self->n_all_blocks; blockidx++: diff --git a/compiler/structs.jou b/compiler/structs.jou index 80c97f4e..e0d7890c 100644 --- a/compiler/structs.jou +++ b/compiler/structs.jou @@ -1,6 +1,7 @@ # TODO: delete this file, merge into others import "stdlib/str.jou" +import "stdlib/math.jou" import "stdlib/io.jou" import "./ast.jou" @@ -93,20 +94,14 @@ class LocalVariable: type: Type* is_argument: bool # First n variables are always the arguments - def print(self) -> int: + def print_to_width(self, width: int) -> None: if self->name[0] != '\0': - printf("%s", self->name) + printf("%-*s", width, self->name) else: - # Anonymous temporary variable created by compiler. - printf("$%d", self->id) + printf("$%-*d", max(width-1, 0), self->id) - # Return how many characters print() outputs. - def print_width(self) -> int: - if self->name[0] != '\0': - return strlen(self->name) as int - temp: byte[100] - sprintf(temp, "%d", self->id) - return 1 + (strlen(temp) as int) + def print(self) -> None: + self->print_to_width(0) class ExpressionTypes: