Skip to content

Commit b360914

Browse files
tzuohannclaude
andcommitted
halcompile: man pages give array ranges as 00..size-1
Pins and params of an array sized by an expression were documented as 00..personality; the last index is personality-1. Expressions that are not a plain name are bracketed: 00..(personality & 0xff)-1. Affects the man pages of bitmerge, bitslice, carousel, demux, gantry, limit_axis, lincurve, logic, max31855, moveoff, multiswitch, output_buffer and radiobutton. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 92d74da commit b360914

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

‎src/hal/utils/halcompile.g‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,26 @@ def to_hal_man(s):
10811081
s = re.sub("#+", lambda m: "__" + "M" * len(m.group(0)) + "__", s)
10821082
return s
10831083

1084+
def wraps(s):
1085+
"""True when the first '(' of s closes at its last character."""
1086+
depth = 0
1087+
for i, ch in enumerate(s):
1088+
depth += {'(': 1, ')': -1}.get(ch, 0)
1089+
if depth == 0:
1090+
return i == len(s) - 1
1091+
return False
1092+
1093+
def last_index(size):
1094+
"""The highest index of a pin/param array whose size is the expression
1095+
`size`: arrays run 00..size-1 (issue #3519). An expression that is not a
1096+
plain name is bracketed so the -1 applies to all of it."""
1097+
size = str(size).strip()
1098+
while size.startswith('(') and size.endswith(')') and wraps(size):
1099+
size = size[1:-1].strip()
1100+
if not re.match(r'^\w+$', size):
1101+
size = '(%s)' % size
1102+
return '%s-1' % size
1103+
10841104
# Generate an adoc file from the embedded declarations, defines and
10851105
# documentation.
10861106
def document(filename, outfilebase):
@@ -1195,7 +1215,7 @@ def document(filename, outfilebase):
11951215
if array:
11961216
sz = name.count("#")
11971217
if isinstance(array, tuple):
1198-
print(" (%s=%0*d..%s)" % ("M" * sz, sz, 0, array[1]), end=' ', file=f)
1218+
print(" (%s=%0*d..%s)" % ("M" * sz, sz, 0, last_index(array[1])), end=' ', file=f)
11991219
else:
12001220
print(" (%s=%0*d..%0*d)" % ("M" * sz, sz, 0, sz, array-1), end=' ', file=f)
12011221
if personality:
@@ -1218,7 +1238,7 @@ def document(filename, outfilebase):
12181238
if array:
12191239
sz = name.count("#")
12201240
if isinstance(array, tuple):
1221-
print(" (%s=%0*d..%s)" % ("M" * sz, sz, 0, array[1]), end=' ', file=f)
1241+
print(" (%s=%0*d..%s)" % ("M" * sz, sz, 0, last_index(array[1])), end=' ', file=f)
12221242
else:
12231243
print(" (%s=%0*d..%0*d)" % ("M" * sz, sz, 0, sz, array-1), end=' ', file=f)
12241244
if personality:

0 commit comments

Comments
 (0)