as has been suggested some type of "select case" block to catch the callable functions will get you there.
One extension to the such an idea would be to write a program that skims through your source code looking for functions that fit a particular pattern. I generally use a group name for such functions, you can just load the file into a string/array and do a bunch of searches on the strings looking for the ket, when you find it, find the next open bracket and then grab than name ans paste it into a select block.
Mock up library code
// Library of function I want to call dynamically
// Call user callable functions are named UCF (User Callable Function followed by an _) in this example
function UCF_RedTEXT()
EndFunction
function UCF_GreenTEXT()
EndFunction
function UCF_BlueTEXT()
EndFunction
function UCF_CLS()
EndFunction
Here's an example (in PlayBASIC, but it's basically just some string operations.. following the concept should be easy enough)
This example loads itself, searches for the functions with
function UCF_ if it finds any, it grabs the name and makes a simple case statement with function call. When all of the functions have been found the newly create code is inserted back into a template function. From here you would save this to disc for inclusion within your actual program. But in this example I've dump the text to the debug console in PB.
/*
RUN IN DEBUG MODE = F7 To Run
You need to have saved this example for it run
In this example the generated code is output to the debug
console, in a real world use case you would save it to disk
and use the include directive to import the code into your
project
*/
Build_Select_Statement("main.pba")
Sync
waitkey
end
Function Build_Select_Statement(File$)
// You'd most like have a hand written template
// but
fh=readnewfile(file$)
if fh
RawCode$=readchr$(fh,Filesize(File$))
closefile fh
// End of line in TEXT file / Source code
cr$=chr$(10)+Chr$(13)
// Find the functions that match out pattern
Key$="function"+" UCF_"
// Insert the string that will hold our generated source code
CaseCode$=cr$
// Find the functions that match our key within this source code
LastMatch=0
do
MatchPos = instring(RAwCode$,key$,LastMatch,true)
if MatchPos =0 then exitdo
// Function the brackets after this
Open_Bracket_POs = instring(RawCode$,"(",MatchPos)
if Open_Bracket_POs > MatchPOs
StartPos =MatchPos+Len(Key$)
Name$= mid$(RAwCode$,StartPOS,Open_Bracket_POs-StartPOS)
// Make the SELECT CASE code to catch and call this function
CaseCode$ +=" case "+chr$(34)+upper$(Name$)+chr$(34)+cr$
// Add the prefix to the name
Name$=replace$(Key$,"function ","")+Name$
CaseCode$ +=" "+Name$+"()"+cr$
CaseCode$ +=cr$
else
#print "Errror Missing after function name dude"
endif
LastMatch = MatchPOs+Len(Key$)
loop
// -----------------------------------------------------------------
// Create the caller function from the template example
// -----------------------------------------------------------------
Pos =instring(RAwCode$,"Function"+" TEMPLATE_CALL_UCF_FUNCTION",0,1)
if Pos>0
EndPos =instring(RAwCode$,"//END OF TEMPLATE",pos,true)
if EndPos>pos
CustomFunction$ = mid$(RawCode$,Pos,EndPOS-pos)
// Remove the TEMPLATE from the function name
CustomFunction$ = replace$(CustomFunction$,"Function TEMPLATE_","Function ")
// insert the CASES
CustomFunction$ = replace$(CustomFunction$,"//[INSERT CASES HERE]",CaseCode$)
// DUMP the Generate CODE TO THE DEBUG CONSOLE
#print CustomFunction$
endif
endif
endif
EndFunction
// -------------------------------------------------------------------
// -------------------------------------------------------------------
// -------------------------------------------------------------------
// -------------------------------------------------------------------
Function TEMPLATE_CALL_UCF_FUNCTION(Opcode$)
Select upper$(OPcode$)
//[INSERT CASES HERE]
EndSelect
EndFunction
//END OF TEMPLATE
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// ----------------------->> FUNCTION LIBRARY <<-------------------------
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
function UCF_RedTEXT()
EndFunction
function UCF_GreenTEXT()
EndFunction
function UCF_BlueTEXT()
EndFunction
function UCF_CLS()
EndFunction
Example of the create caller code:
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// EXAMPLE OF THE CREATED CALLER FUNCTION
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
Function CALL_UCF_FUNCTION(Opcode$)
Select upper$(OPcode$)
case "REDTEXT"
UCF_RedTEXT()
case "GREENTEXT"
UCF_GreenTEXT()
case "BLUETEXT"
UCF_BlueTEXT()
case "CLS"
UCF_CLS()
EndSelect
EndFunction