Page 1 of 1

Nested Functions. How to implement?

Posted: Fri Mar 17, 2006 5:58 pm
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"))

Posted: Fri Mar 17, 2006 6:19 pm
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.

Posted: Fri Mar 17, 2006 6:29 pm
by Inf0Byt3
Thank you very much KillSwitch. You saved my day (again) :D.

Re: Nested Functions. How to implement?

Posted: Sun Mar 10, 2024 5:15 pm
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!
:)