Call a procedure by it's name stored in the string variable?

General FreeBASIC programming questions.
Post Reply
FotonCat
Posts: 37
Joined: Nov 26, 2009 11:47
Location: Russia
Contact:

Call a procedure by it's name stored in the string variable?

Post by FotonCat »

Hello!

I need to call the subprogram or function in case when name of this sub/func is being stored in the variable. Something like this:

Code: Select all

declare sub a()
dim as string variable="a"

sub a()
end sub

call variable
the "SELECT CASE" and other constructions are not preferred. I need to directly call sub/func by it's name. How can be this realised in FreeBasic?

Sorry but I hadn't said one important note. In my case the name of procedure or function should be stored in separate file. Pointers in this situation are not helpful.

Thanks.
Last edited by FotonCat on Apr 19, 2010 8:35, edited 2 times in total.
smagin
Posts: 136
Joined: Jun 09, 2005 16:46
Location: Russia, Belgorod

Post by smagin »

Pointers, of course!

Code: Select all

Declare Sub a()
Declare Sub b()

Dim As Sub() variable

Sub a()
End Sub

Sub b()
End Sub

variable = @a

variable() ' parentheses are obligatory!

variable = @b

Call variable ' alternative synthax
In similar way you can use function pointers.
FotonCat
Posts: 37
Joined: Nov 26, 2009 11:47
Location: Russia
Contact:

Post by FotonCat »

Ok, thank you for your answer.

Sorry but I hadn't said one important note. In my case the name of procedure or function should be stored in separate file. Pointers in this situation are not helpful.

For example, I have a text file which contains an abstract instruction set:

LOAD
CALCULATE
SAVE

all these are names of real procedures defined in my program.
Program will read the contents of this text file and should call procedures with names given in this file (without select case, directly by name given in the file).

I think it is important feature and this could be in FB.
Richard
Posts: 3096
Joined: Jan 15, 2007 20:44
Location: Australia

Post by Richard »

You are building an interpreter. FB is compiled so it can't be merged with an interpreter. The key to implementing interpreters is either function pointers, decision trees or Select Case. I see no advantage in fighting a battle with one arm tied behind my back. Get the job done using Select Case, even if it is not your preference. “Preference” is relative to non-existent alternatives, better the devil you know than the devil you don't.
smagin
Posts: 136
Joined: Jun 09, 2005 16:46
Location: Russia, Belgorod

Post by smagin »

Procedure names are for human readability. When compiled, they are "lost", and the program being run has no idea of its procedure names. As I can see you want some kind of script being processed and executed. in this case the keywords LOAD, CALCULATE etc could not coincide with the names in your program.

Thus, the basic idea (pseudocode)

SELECT CASE ReadNextToken()
CASE "LOAD": LOAD()
CASE "CALCULATE": CALCULATE()
CASE "SAVE": MY_SAVE()
END SELECT CASE

I see no point to overcomplicate things. It's up to you to decide.
FotonCat
Posts: 37
Joined: Nov 26, 2009 11:47
Location: Russia
Contact:

Post by FotonCat »

Thank you very much for your answers!
I understood why this feature can't be realised.
I've decided to use SELECT CASE =))
agamemnus
Posts: 1842
Joined: Jun 02, 2005 4:48

Post by agamemnus »

Some languages, like LISP, PHP, and Javascript, do have ways to access and even modify a function using a string for its name.

If you ever wanted to do program where you need to constantly use strings as function names, think about utilizing those kind of languages.

They have some somewhat sophisticated algorithms for efficiently accessing the function the string name belongs to (ie: it's more than just a huge select case), but even with that they are slower than languages (like C++ or Freebasic) that do not have this feature built in.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Post by srvaldez »

just an idea, use function pointers and associate array, I think there's code in tips section on associate arrays.
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Post by anonymous1337 »

A hash table could handle basic string->function ptr relations for you.
Post Reply