feat(lang): add for loop — range and list forms (V2 Tier 2c)#13
Merged
Conversation
Enables iterative geometry in SCAD scripts:
for (i = [0:4]) translate([i*10, 0, 0]) sphere(r=3);
for (i = [0:2:8]) translate([i*5, 0, 0]) cube([4,4,4]);
for (v = [1, 3, 7]) cylinder(h=v, r=2);
difference() {
cube([60, 12, 12], center=true);
for (i = [0:4]) translate([-24+i*12, 0, 0]) cylinder(h=20, r=3);
}
Changes:
- Token.h: For, Colon token kinds
- Lexer.cpp: "for" keyword, ':' operator
- AST.h: ForRange (isRange/start/step/end/list), ForNode;
added to AstNode variant with makeFor() helper
- Parser.h/cpp: parseFor() — parses [start:end], [start:step:end],
and [v0,v1,...] forms; disambiguates on first ':' vs ','
- Interpreter.h/cpp: getVar()/setVar() for loop variable binding
- CsgEvaluator.h/cpp: evalFor() — builds value sequence, binds
loop var in interpreter, collects geometry from each iteration,
unions all results; saves/restores outer variable binding;
safety cap of 10000 iterations; empty range → nullptr
- Tests: 13 new tests across lexer, parser, and CSG evaluator
(121 total, 688 assertions)
- tests/for_test.scad: 6 visual scenes including nested for loops
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Enables iterative geometry in SCAD scripts:
for (i = [0:4]) translate([i10, 0, 0]) sphere(r=3);
for (i = [0:2:8]) translate([i5, 0, 0]) cube([4,4,4]);
for (v = [1, 3, 7]) cylinder(h=v, r=2);
difference() {
cube([60, 12, 12], center=true);
for (i = [0:4]) translate([-24+i*12, 0, 0]) cylinder(h=20, r=3);
}
Changes: