Skip to content

Fix RangeLiteral emitting spurious :1 for two-argument ranges#18

Open
tbjers wants to merge 2 commits into
BelfrySCAD:mainfrom
NascentMaker:fix/range-literal-range-order-swapped-end-step
Open

Fix RangeLiteral emitting spurious :1 for two-argument ranges#18
tbjers wants to merge 2 commits into
BelfrySCAD:mainfrom
NascentMaker:fix/range-literal-range-order-swapped-end-step

Conversation

@tbjers

@tbjers tbjers commented Jun 5, 2026

Copy link
Copy Markdown

The builder synthesises a default step (NumberLiteral 1.0) tagged with the range node's own position when the source form is [start:end]. RangeLiteral.__str__ previously always emitted [start:end:step], turning [0:5] into [0:5:1]. OpenSCAD's three-argument syntax is [start:step:end], so [0:5:1] is read as start=0, step=5, end=1 — semantically opposite.

Fix __str__ to compare step.position against the range node's position: same position means synthesised default → emit [start:end]; distinct position means an explicit third argument from source → emit [start:end:step].

Updates test_nodes.py to cover both the 2-arg and 3-arg cases with appropriate positions, and adds round-trip regression tests in test_vectors.py. Corrects two test_pretty_print assertions that were hard-coded to the old broken output (:1]).

The builder synthesises a default step (NumberLiteral 1.0) tagged with
the range node's own position when the source form is [start:end].
RangeLiteral.__str__ previously always emitted [start:end:step], turning
[0:5] into [0:5:1].  OpenSCAD's three-argument syntax is [start:step:end],
so [0:5:1] is read as start=0, step=5, end=1 — semantically opposite.

Fix __str__ to compare step.position against the range node's position:
same position means synthesised default → emit [start:end]; distinct
position means an explicit third argument from source → emit [start:end:step].

Updates test_nodes.py to cover both the 2-arg and 3-arg cases with
appropriate positions, and adds round-trip regression tests in
test_vectors.py.  Corrects two test_pretty_print assertions that were
hard-coded to the old broken output (:1]).

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Comment thread tests/test_nodes.py Outdated
step_pos = Position(origin="<test>", line=1, column=5)
step_explicit = NumberLiteral(val=2.0, position=step_pos)
range_3arg = RangeLiteral(start=_num(0.0), end=_num(5.0), step=step_explicit, position=_pos())
assert str(range_3arg) == "[0:5:2]"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be [0:2:5]?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only if we are to fix it across the whole library, but that would be a breaking change, and would most likely warrant a major version bump. What do you think?

Comment thread src/openscad_parser/ast/nodes.py Outdated
# source position.
if self.step.position == self.position:
return f"[{self.start}:{self.end}]"
return f"[{self.start}:{self.end}:{self.step}]"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be f"[{self.start}:{self.step}:{self.end}]"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would require a much larger change across the library. I can do that if you would like, but it would become a breaking change in that case so you'd most likely need a major version bump?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants