Add support for functions with string and variable numeric arguments#163
Closed
beltoforion wants to merge 4 commits intomasterfrom
Closed
Add support for functions with string and variable numeric arguments#163beltoforion wants to merge 4 commits intomasterfrom
beltoforion wants to merge 4 commits intomasterfrom
Conversation
Introduces a new callback type for parser functions that take a string constant as their first parameter followed by a variable number of numeric arguments: typedef value_type(*multstrfun_type)(const char_type*, const value_type*, int); This combines the behaviour of strfun_type (string parameter) with multfun_type (variable-length numeric argument list). Changes across the codebase: - muParserDef.h: add multstrfun_type typedef; add cmFUNC_STR_VARARG enum value to ECmdCode - muParserToken.h: add call_multstrfun() to generic_callable_type - muParserCallback.h/.cpp: add ParserCallback constructor for the new type (m_iCode=cmFUNC_STR_VARARG, m_iType=tpSTR, m_iArgc=CALLBACK_INTERNAL_VAR_ARGS) - muParserBytecode.h/.cpp: add AddMultStrFun(); add AsciiDump case - muParserBase.h/.cpp: add ApplyMultStrFunc(); handle cmFUNC_STR_VARARG in CreateRPN (operator push and closing-bracket arg-count checks) and in the bytecode evaluation loop https://claude.ai/code/session_013oDu6iDDEJKVxpvNrQKQ4p
Adds MultStrFun as a test callback (multstrfun_type signature) that
sums all numeric arguments, ignoring the string parameter. Registers
it as "multstrfun" in both the ThrowTest and EqnTest parser setups,
and adds a test case:
multstrfun("hello", 1, 2, 3) == 6
https://claude.ai/code/session_013oDu6iDDEJKVxpvNrQKQ4p
Adds a Printf callback (multstrfun_type) to the example1 interactive
parser. The format string uses C#-style {n} placeholders that are
replaced by the nth numeric argument at evaluation time.
printf("x={0}, y={1}, sum={2}", a, b, a+b)
The function prints the formatted string to the console and returns
the number of substitutions made.
https://claude.ai/code/session_013oDu6iDDEJKVxpvNrQKQ4p
cmFUNC_STR_VARARG was unnecessary. The existing convention for variable- argument functions — storing the argument count as a negative value in the bytecode token — applies equally to string+vararg functions (multstrfun_type). A negative argc in a cmFUNC_STR token now signals the vararg path, mirroring exactly how cmFUNC handles vararg via AddFun(). Changes: - muParserDef.h: remove cmFUNC_STR_VARARG from ECmdCode - muParserCallback.cpp: multstrfun_type callback uses cmFUNC_STR code - muParserBytecode.cpp: AddMultStrFun stores tok.Cmd=cmFUNC_STR, tok.Fun.argc=-N; AsciiDump checks sign to print STRFUNC vs MULTSTRFUNC - muParserBase.cpp: cmFUNC_STR evaluator checks argc<0 for vararg path; ApplyFunc routes to ApplyMultStrFunc when GetArgCount()==-1; stale cmFUNC_STR_VARARG references in argument-count checks removed https://claude.ai/code/session_013oDu6iDDEJKVxpvNrQKQ4p
Owner
Author
|
closed; ai generated code will not be merged |
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 adds support for a new function type that combines string constants with a variable number of numeric arguments. This enables more flexible function signatures in the parser while maintaining type safety and proper argument validation.
Key Changes
cmFUNC_STR_VARARG: Introduced a new command code to represent functions that take a string constant followed by a variable number of numeric argumentsmultstrfun_type: Added function pointer typedef for the new signature:value_type(*multstrfun_type)(const char_type*, const value_type*, int)ApplyMultStrFunc()method inParserBaseto handle application of string+vararg functionsApplyFunc()to processcmFUNC_STR_VARARGtokensAddMultStrFun()method toParserByteCodefor bytecode generationcmFUNC_STR_VARARGin the evaluation loopParserCallbackconstructor overload formultstrfun_typecall_multstrfun()method togeneric_callable_typefor proper function invocationImplementation Details
The new function type follows the existing pattern for specialized function handling (similar to
cmFUNC_STRfor string-only functions). Arguments are validated at parse time, with numeric arguments processed first and the string argument stored separately in the string buffer. The bytecode execution properly manages the stack by accounting for the variable number of numeric arguments while maintaining a single result value.https://claude.ai/code/session_013oDu6iDDEJKVxpvNrQKQ4p