Nested Functions. How to implement?

Just starting out? Need help? Post your questions and find answers here.
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Nested Functions. How to implement?

Post by Inf0Byt3 »

I need a little idea about how to handle nested functions in my little interpreter. For example, how to evaluate this one? I've made the variable support, but I can't get this to work. Any input is highly appreciated.

a = Str(Val("1"))
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

I came accross this problem myself. Have a search for 'Compound Basic' on the forums and you should find my simple PB clone - if the source isn't included I'll post it on my FTP account tonight.

Anyway, the basic premise is this:

The simplest way I can think of right now (although it may not be the best) would be to find the first ")" character then work backwards from there. So you'd check Str( Val("1") ) for the first ")" and you'd come accross the closing bracket for Val.

Then work backwards through the string until you find the secod "(" character (as the first will be for 'Val'). You'll then have the most nested command, which is Val() in this case.

Split the original string up into two parts. The first being all the characters upto the start of Val(), which will be "Str(" (call this 'First'), and all the characters after val, which will be ")" (call this 'Second').

Work out the result of the command, and save the result in another string. Then you add the three pieces you have together:

First+Result+Second

Which will result in the string "Str(1)". You can then repeat the process until there's no more ")" characters, at which point you'll have the result of the whole lot.
~I see one problem with your reasoning: the fact is thats not a chicken~
Inf0Byt3
PureBasic Fanatic
PureBasic Fanatic
Posts: 2236
Joined: Fri Dec 09, 2005 12:15 pm
Location: Elbonia

Post by Inf0Byt3 »

Thank you very much KillSwitch. You saved my day (again) :D.
None are more hopelessly enslaved than those who falsely believe they are free. (Goethe)
User avatar
aston
User
User
Posts: 64
Joined: Wed Nov 18, 2009 11:18 pm

Re: Nested Functions. How to implement?

Post by aston »

I know that this one is old topic but i make search trough all possible topics and also
i have read whole blog of TronD about user defined functions but i simply don't understand it
because whole his reference goes with assembly code which i don't understand.

my question is about UDF in script interpreter like this classic basic-ish code

func myFunc( $ variable ,..)
,...
...
end func

1. do i must use scope ...like :
if scope = 1 then is local ..else global

2.do i must store parameters or function arguments in separate array or list
as funcArgs() or can be just local to current function ?

3.is there any example written in PB how to do that properly ?

thanks in advance!
:)
Post Reply