Skip to content

Commit 403c6bb

Browse files
committed
Fix issue with nested branching (an else was attached to the wrong if)
1 parent f24e69d commit 403c6bb

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/strands/strands_transpiler.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,10 @@ const ASTCallbacks = {
363363
if (!localVars.has(left.object.name)) {
364364
assignedVars.add(left.object.name);
365365
}
366-
} else if (stmt.type === 'BlockStatement') {
367-
// Recursively analyze nested block statements
368-
analyzeBlock(stmt);
369366
}
367+
} else if (stmt.type === 'BlockStatement') {
368+
// Recursively analyze nested block statements
369+
analyzeBlock(stmt);
370370
}
371371
}
372372
};
@@ -629,10 +629,6 @@ const ASTCallbacks = {
629629
updateExpr = this.replaceIdentifierReferences(updateExpr, loopVarName, 'loopVar');
630630
}
631631
const updateAst = { type: 'Program', body: [{ type: 'ExpressionStatement', expression: updateExpr }] };
632-
// const nonControlFlowCallbacks = { ...ASTCallbacks };
633-
// delete nonControlFlowCallbacks.IfStatement;
634-
// delete nonControlFlowCallbacks.ForStatement;
635-
// ancestor(updateAst, nonControlFlowCallbacks, undefined, _state);
636632
updateExpr = updateAst.body[0].expression;
637633

638634
updateFunction = {

test/unit/webgl/p5.Shader.js

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -616,11 +616,45 @@ suite('p5.Shader', function() {
616616
myp5.noStroke();
617617
myp5.shader(testShader);
618618
myp5.plane(myp5.width, myp5.height);
619-
// Check that the center pixel is gray (medium condition was true)
619+
// Check that the center pixel is black (else condition was true)
620+
const pixelColor = myp5.get(25, 25);
621+
assert.approximately(pixelColor[0], 0, 5);
622+
assert.approximately(pixelColor[1], 0, 5);
623+
assert.approximately(pixelColor[2], 0, 5);
624+
});
625+
test('handle conditional assignment in if-else-if chains', () => {
626+
myp5.createCanvas(50, 50, myp5.WEBGL);
627+
const testShader = myp5.baseMaterialShader().modify(() => {
628+
const val = myp5.uniformFloat(() => Math.PI * 8);
629+
myp5.getPixelInputs(inputs => {
630+
let shininess = 0
631+
let color = 0
632+
if (val > 5) {
633+
const elevation = myp5.sin(val)
634+
if (elevation > 0.4) {
635+
shininess = 0;
636+
} else if (elevation > 0.25) {
637+
shininess = 30;
638+
} else {
639+
color = 1;
640+
shininess = 100;
641+
}
642+
} else {
643+
shininess += 25;
644+
}
645+
inputs.shininess = shininess;
646+
inputs.color = [color, color, color, 1];
647+
return inputs;
648+
});
649+
}, { myp5 });
650+
myp5.noStroke();
651+
myp5.shader(testShader);
652+
myp5.plane(myp5.width, myp5.height);
653+
// Check that the center pixel is 255 (hit nested else statement)
620654
const pixelColor = myp5.get(25, 25);
621-
assert.approximately(pixelColor[0], 0, 5); // Red channel should be ~127 (gray)
622-
assert.approximately(pixelColor[1], 0, 5); // Green channel should be ~127
623-
assert.approximately(pixelColor[2], 0, 5); // Blue channel should be ~127
655+
assert.approximately(pixelColor[0], 255, 5);
656+
assert.approximately(pixelColor[1], 255, 5);
657+
assert.approximately(pixelColor[2], 255, 5);
624658
});
625659
test('handle nested if statements', () => {
626660
myp5.createCanvas(50, 50, myp5.WEBGL);
@@ -1192,7 +1226,7 @@ suite('p5.Shader', function() {
11921226
// Normal of plane facing camera should be [0, 0, 1], so color should be [0, 0, 255]
11931227
const centerColor = myp5.get(25, 25);
11941228
assert.approximately(centerColor[0], 0, 5); // Red component
1195-
assert.approximately(centerColor[1], 0, 5); // Green component
1229+
assert.approximately(centerColor[1], 0, 5); // Green component
11961230
assert.approximately(centerColor[2], 255, 5); // Blue component
11971231
});
11981232

0 commit comments

Comments
 (0)