Conversation
…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 '('.
Contributor
Author
|
Closing this PR — I'm wrapping up my open-source contribution effort. Thanks for your time and review. |
Collaborator
|
wow, that was quick... |
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
Extends the scripting language with callable math functions, as suggested in #1141.
The tokenizer now emits a
Commatoken, 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, soValidateScriptreports them early rather than at evaluation time.Supported functions
abs,sqrt,sin,cos,tan,asin,acos,atan,exp,log,log10,floor,ceil,roundpow,atan2,min,maxclampDetails
Ast::ExprFunctionnode coerces each argument todoubleand dispatches to<cmath>.((e.g.abs:=7still works).sqrt(9) + 1,-sqrt(9), andpow(abs(-2), 3)compose naturally.Tests
Added
ParserTest.MathFunctionscovering all functions, composition with operators/variables, and parse-time rejection of unknown functions and wrong arity.Full test suite: 531 passed.