Complementary devs for floor, ceil, min, and max operations#186
Merged
Complementary devs for floor, ceil, min, and max operations#186
Conversation
These operators are valid in a linear expression when all operands are constant. Previously they raised unconditionally; now they evaluate to a constant result, and only raise ValueError when a non-constant (variable-containing) operand is present. https://claude.ai/code/session_01DL3tB7dnWvHsB8dNYBBNxZ
…itor Adds support for parenthesized-expression time-shift/index syntax such as `(ceil(p/q))[t-1]`, which previously failed to parse because the ANTLR visitor lacked handlers for the timeShiftExpr and timeIndexExpr grammar rules. The new methods mirror the existing visitTimeShift and visitTimeIndex logic, using ctx.expr().accept(self) instead of _convert_identifier() to obtain the base expression. https://claude.ai/code/session_01So7yDQ3KiPj8oDa4xYnhUK
tbittar
approved these changes
Mar 24, 2026
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.
Summary
This PR extends expression support in two areas:
TimeShiftExprandTimeIndexExpr) that were previously unhandled, enabling syntax such as(expr)[t-1]and(expr)[1].floor,ceil,min, andmaxoperators usable inside linear expressions when all their operands are constant, instead of always raising an error.Changes
src/gems/expression/parsing/parse_expression.pyvisitTimeShiftExpr: handles(expr)[t±N]— applies a time shift to an arbitrary expression node; simplifies to the expression itself when the shift is 0.visitTimeIndexExpr: handles(expr)[k]— evaluates an expression at an absolute time index.src/gems/simulation/linearize.pyfloor/ceil: visit the operand; if it is purely constant, returnmath.floor/math.ceilof its value; raiseValueErrorotherwise.maximum/minimum: visit all operands; if none contain variable terms, return the constantmax/min; raiseValueErrorotherwise.import math.Test coverage
tests/unittests/expressions/parsing/test_expression_parsing.py: four new parametrized cases covering[t-1],[t],[t-1]withceil, and[1].tests/unittests/expressions/visitor/test_linearization.py: eight new tests — constant floor/ceil/max/min succeed, variable floor/ceil/max/min raise with message matching"non-constant".Test plan
pytest tests/unittests/expressions/parsing/test_expression_parsing.pypytest tests/unittests/expressions/visitor/test_linearization.py