OpenClaw Calculator
When to Use
- Add or change supported arithmetic behavior in the calculator.
- Tighten validation for unsupported AST syntax or unsafe expressions.
- Update command-line behavior for expression input or interactive mode.
- Add or adjust regression tests for parsing, precedence, unary operators, or error cases.
Project Facts
- Main implementation lives in
calculator.py. - CLI entry points live in
__main__.pyand package exports in__init__.py. - Tests live in
tests/test_calculator.pyand useunittest. - The calculator evaluates parsed Python AST nodes and only allows a small set of operators.
Procedure
- Read
calculator.py,__main__.py, and any affected tests before changing behavior. - Identify whether the request changes parsing rules, supported operators, error messages, or CLI flow.
- Prefer small, explicit AST validation changes instead of broad
eval-style execution. - Preserve the safety model: only accept numeric constants, allowed binary operators, and allowed unary operators unless the task explicitly expands support.
- Add or update
unittestcoverage intests/test_calculator.pyfor both success cases and rejected input. - Run
python -m unittest discover -s testsfrom the workspace root after changes.
Implementation Notes
- Raise
CalculationErrorfor invalid input and unsupported operations. - Keep public behavior simple and predictable; avoid adding implicit coercions or side effects.
- If a new operator or syntax form is added, update the allowed AST handling in one place and test at least one valid and one invalid case.
- If CLI behavior changes, verify both direct expression execution and interactive mode.
Done Criteria
- Behavior matches the requested calculator change.
- Unsafe or unsupported syntax is still rejected.
- Relevant unit tests are added or updated.
- The unittest command passes locally.
