Archive: Simulating Local Variables


Simulating Local Variables
I've become a big NSIS fan in a short period of time. One of the features I wish it had is "local variables". That is, variables scoped within a section and within a function. This is becoming more important as I write more and more script code and start to build up a libary of reusable scripts (using !include), where use of globals have the potential for conflict since they are all in the same namespace. And I realize there are many predefined variables that I could use ($R0...$R9 and $0...$9), but that doesn't make for very readable code.

So, I've come up with a way to simulate local variables using macros and defines and would like feedback on this approach from the experts in this forum. (See attached.) I'd like feedback both on the implementation and the usage.

Using this approach, I can write code like below and be assured of having a locally scoped variable "count".


${LocalVAR} count
Section "one"
strcpy ${count} 1
SectionEnd
${LocalVAREnd} count

${LocalVAR} count
Function one
strcpy ${count} 2
FunctionEnd
${LocalVAREnd} count


See the attachment for more examples and the implementation.

So folks, what do you think? Ideally, this would eventually be added to the compiler itself ;) but for now, this does the trick, for me anyway.

Joe A.

You can also use defined symbols to make $R0...$R9 and $0...$9 more readable.


Yes, I started using defines to make the register variables more readable before using this new approach. There are two issues with that approach as I see it that my approach addresses:

1. To be a "good programming citizen", you should save/restore registers (i.e. push/pop), which is very easy to overlook potentially causing problems in nested function calls. My approach doesn't require you to worry about this issue.

2. You still have the namespace issue since defines share a common namespace. My approach takes care of that for you (the LocalVAREnd simply undef's the defines used against the uniquely generated global VARs).


It is indeed useful for large libraries :) Can you create an archive page for your code?


NSIS programming language :)
The way things are going (good !) NSIS will turn into a programming language of sorts !!!

As a Win32 programmer, I'm happy !!!


But did you intend to add local variable to NSIS?
It is is very difficult to create complex scripts like it is.


You only have to include the header file, there is no need to write complex scripts.


Originally posted by Joost Verburg
It is indeed useful for large libraries :) Can you create an archive page for your code?
Joost -- Yes, I will add it to the archive soon. As I've never added to this archive, I have a question. Can anyone add to it or do I need a password and/or to be authorized in some way?

However before I add it, I also have a scheme in mind that simulates parameter passing to functions and thought I should incorporate that into this same LocalVAR.nsh header file as well. Or would you prefer that be a separate header?

That is fine. You have to register an NSIS Archive account to create pages, but it only takes a few seconds.


what happened with this?
Just wondering what the status of this was. I couldn't find any listing for "JoeAcunzo" in the archive... Does anyone know if this local variable code made it into the archive under a different name?

thanks,

-marc


LocalVAR Code
Marc - Sorry, I got very busy putting out a new product called ComputerTime (see www.softwaretime.com for details), and never got around to posting this code. I've attached the code to this posting. Hope it helps.

Joe A.

PS. I never finished the ability to (simulate) passing variables to functions. Not forgotten, just on a back burner.


Wow, quick response, I was just curious about the status. It looks pretty slick:)

thanks Joe.

-marc


I tried to declare a local variable like this
${LocalVAR} count
Strcpy ${count} 1
${LocalVAREnd} count,but it throws the following error when compiling.
VAR expects 1-2 parameters, got 3.
Usage: Var [/GLOBAL] var_name
Error in macro mLocalVAR on macroline 12