Skip to content

Simulator: string comparisons, parentheses, ! and stop on error - #9

Merged
code4teaching merged 1 commit into
mainfrom
simulator-conditions
Oct 18, 2026
Merged

code4teaching merged 1 commit into
mainfrom
simulator-conditions

Conversation

@code4teaching

Copy link
Copy Markdown
Owner

Requested by Holger · project thread

Before: in the simulator, name == "Anna" failed with "Invalid expression", (a < 3 || a > 10) && b and !(a > 3) were evaluated wrong or failed, an && inside a string literal split the condition, and if (a > 3) && (a < 10) lost its first and last parenthesis. After any error the simulation skipped the failing block and kept running.

After: conditions behave like Java: || binds weaker than &&, which binds weaker than !; parentheses group; comparisons with a string literal or string variable compare text (==, !=, <, …), and name.equals("Anna") works. Comparing a string with a number is reported as an error. After an error the simulation stops on the failing block (it stays highlighted, Play/Step are disabled) until Reset.

How: evaluateBoolExpression now splits only outside brackets and string literals (topLevelMask), strips a pair of outer parentheses only when they actually match, and routes comparisons through evaluateComparison, which picks text or numeric comparison. VisuStructSimulationState gains isHalted (defaults to false), and canStep respects it. The app needs no changes: it already disables the controls via canStep and shows state.message.

Stacked on #8 (tests + CI); GitHub retargets this PR to main once #8 is merged. New tests are in SimulationConditionTests.swift; CHANGELOG has an Unreleased entry.


Generated by Claude Code

Conditions are now split only outside brackets and string literals, so
'(a || b) && c', '!(a > 3)' and '"a && b"' evaluate as in Java.
Comparisons with a string literal or string variable compare text,
and 'x.equals(y)' is accepted. 'if (a) && (b)' no longer has its
first and last parenthesis stripped.

After an error the engine sets state.isHalted and stays on the failing
step instead of advancing past it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Cp4EQgFQyGbM8MVLiWbJKQ
@code4teaching code4teaching self-assigned this Oct 18, 2026
@code4teaching
code4teaching marked this pull request as ready for review October 18, 2026 21:55
@code4teaching
code4teaching changed the base branch from core-tests-and-ci to main October 18, 2026 21:58
@code4teaching
code4teaching merged commit e0e8812 into main Oct 18, 2026
1 check passed
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