Skip to content

Commit

Permalink
print_to_width
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Jan 10, 2025
1 parent a9faf0f commit 91580b2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 16 deletions.
6 changes: 1 addition & 5 deletions compiler/cf_graph.jou
Original file line number Diff line number Diff line change
Expand Up @@ -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++:
Expand Down
17 changes: 6 additions & 11 deletions compiler/structs.jou
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 91580b2

Please sign in to comment.