Skip to content

Add support for functions with string and variable numeric arguments#163

Closed
beltoforion wants to merge 4 commits intomasterfrom
claude/add-string-callback-type-phUj9
Closed

Add support for functions with string and variable numeric arguments#163
beltoforion wants to merge 4 commits intomasterfrom
claude/add-string-callback-type-phUj9

Conversation

@beltoforion
Copy link
Copy Markdown
Owner

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

  • New function type cmFUNC_STR_VARARG: Introduced a new command code to represent functions that take a string constant followed by a variable number of numeric arguments
  • New callback type multstrfun_type: Added function pointer typedef for the new signature: value_type(*multstrfun_type)(const char_type*, const value_type*, int)
  • Parser implementation:
    • Added ApplyMultStrFunc() method in ParserBase to handle application of string+vararg functions
    • Added handling in ApplyFunc() to process cmFUNC_STR_VARARG tokens
    • Updated expression parsing logic to recognize the new function type alongside existing function types
  • Bytecode support:
    • Added AddMultStrFun() method to ParserByteCode for bytecode generation
    • Added bytecode execution logic for cmFUNC_STR_VARARG in the evaluation loop
    • Added debug output support for the new function type
  • Callback infrastructure:
    • Added ParserCallback constructor overload for multstrfun_type
    • Added call_multstrfun() method to generic_callable_type for proper function invocation
  • Validation: Implemented proper error checking to ensure the last argument is a string and all preceding arguments are numeric

Implementation Details

The new function type follows the existing pattern for specialized function handling (similar to cmFUNC_STR for 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

claude added 4 commits March 9, 2026 20:38
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
@beltoforion
Copy link
Copy Markdown
Owner Author

closed; ai generated code will not be merged

@beltoforion beltoforion deleted the claude/add-string-callback-type-phUj9 branch April 10, 2026 21:07
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