Skip to content

Commit

Permalink
Separate indentation handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nickdrozd committed Aug 7, 2024
1 parent dce5ee9 commit 52bf1bc
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions tools/c.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ def make_instruction(
pr: Color,
sh: Shift,
tr: State | None,
indent: int,
*,
binary: bool = True,
) -> str:
) -> list[str]:
lines = [
make_comment(st, co),
make_shift(sh),
Expand All @@ -59,7 +58,11 @@ def make_instruction(
lines.append(
make_trans(tr))

return ('\n' + (' ' * indent)).join(lines)
return lines


def indent(space: int, lines: list[str]) -> str:
return ('\n' + (' ' * space)).join(lines)


def make_if_else(st: State, in0: Instr, in1: Instr) -> str:
Expand All @@ -70,8 +73,8 @@ def make_if_else(st: State, in0: Instr, in1: Instr) -> str:
return make_while(st, in0, in1)

return IF_TEMPLATE.format(
make_instruction(st, 0, *in0, 6),
make_instruction(st, 1, *in1, 6),
indent(6, make_instruction(st, 0, *in0)),
indent(6, make_instruction(st, 1, *in1)),
)


Expand All @@ -94,14 +97,18 @@ def make_while(st: State, in0: Instr, in1: Instr) -> str:

if tr0 == st:
test = 'BLANK'
loop = make_instruction(st, 0, pr0, sh0, None, 4)
rest = make_instruction(st, 1, *in1, 2)
loop = make_instruction(st, 0, pr0, sh0, None)
rest = make_instruction(st, 1, *in1)
else:
test = '!BLANK'
loop = make_instruction(st, 1, pr1, sh1, None, 4)
rest = make_instruction(st, 0, *in0, 2)
loop = make_instruction(st, 1, pr1, sh1, None)
rest = make_instruction(st, 0, *in0)

return WHILE_TEMPLATE.format(test, loop, rest)
return WHILE_TEMPLATE.format(
test,
indent(4, loop),
indent(2, rest),
)


WHILE_TEMPLATE = \
Expand Down Expand Up @@ -134,7 +141,7 @@ def make_n_way_switch(state: State, instrs: tuple[Instr, ...]) -> str:
def make_case(st: State, co: Color, instr: Instr) -> str:
return CASE_TEMPLATE.format(
co,
make_instruction(st, co, *instr, 6, binary = False),
indent(6, make_instruction(st, co, *instr, binary = False)),
)


Expand Down

0 comments on commit 52bf1bc

Please sign in to comment.