Skip to content

feat(scripting): add built-in math functions (abs, sqrt, pow, trig, min/max, clamp) - #1213

Closed
webzuweb wants to merge 1 commit into
BehaviorTree:masterfrom
webzuweb:feat/script-math-functions
Closed

webzuweb wants to merge 1 commit into
BehaviorTree:masterfrom
webzuweb:feat/script-math-functions

Conversation

@webzuweb

Copy link
Copy Markdown
Contributor

Summary

Extends the scripting language with callable math functions, as suggested in #1141.

The tokenizer now emits a Comma token, and the Pratt parser treats an identifier followed by a parenthesized argument list as a function call. Unknown-function and wrong-arity errors are raised at parse time, so ValidateScript reports them early rather than at evaluation time.

Supported functions

  • 1 arg: abs, sqrt, sin, cos, tan, asin, acos, atan, exp, log, log10, floor, ceil, round
  • 2 args: pow, atan2, min, max
  • 3 args: clamp

Details

  • New Ast::ExprFunction node coerces each argument to double and dispatches to <cmath>.
  • A plain identifier still resolves as a variable/enum reference when not followed by ( (e.g. abs:=7 still works).
  • Functions bind tighter than any binary operator, so sqrt(9) + 1, -sqrt(9), and pow(abs(-2), 3) compose naturally.

Tests

Added ParserTest.MathFunctions covering all functions, composition with operators/variables, and parse-time rejection of unknown functions and wrong arity.

Full test suite: 531 passed.

…in/max, clamp)

Extend the scripting language with callable math functions, addressing BehaviorTree#1141.

The tokenizer now emits a Comma token and the Pratt parser recognizes an
identifier followed by a parenthesized argument list as a function call.
Arity and unknown-function checks happen at parse time so ValidateScript
reports them early. Evaluation goes through a new Ast::ExprFunction node
that coerces each argument to double and dispatches to <cmath>.

Supported: abs, sqrt, pow, sin, cos, tan, asin, acos, atan, atan2, exp,
log, log10, floor, ceil, round, min, max, clamp.

A variable/enum still wins when the name is not followed by '('.
@webzuweb

webzuweb commented Oct 1, 2026

Copy link
Copy Markdown
Contributor Author

Closing this PR — I'm wrapping up my open-source contribution effort. Thanks for your time and review.

@webzuweb webzuweb closed this Oct 1, 2026
@facontidavide

Copy link
Copy Markdown
Collaborator

wow, that was quick...

@facontidavide facontidavide self-assigned this Oct 2, 2026
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