[fpc-pascal] specify variable name with string variable
james at productionautomation.net
Mon Jul 8 22:43:48 CEST 2019
Well what it’s for, is not just debugging, I’m trying to manage outputs to ports that are defined by port numbers and pins in an ini file, the ports and pins can really be anything but I need to keep track of the output states of all the pins by specific port value because I can only output all 8 bits of the port at one time. I can just read back the port and then use AND or OR with what I want and write it back out, but that’s slow, and it also is not necessarily accurate because some ports will give what’s on the pin itself, not what output it is trying to send out… you can be outputting a 1 and the user can force it to 0 and it will read in a 0 now when you write to it, it’s outputting a 0 and if the user removes the external condition forcing it to 0 it will not go back to 1.. the ports are 1/O ports and are designed to be operated in this manner. Anyway I made an array of ports and the data each should contain, and then I also have an array of each I/O pin (defined with a string of the name of the variable that holds the pin number) and the port it is looking for… and then I have procedures and functions that use these arrays to set the stored value of each port correctly, then I just output the entire 8 bits from the storage array. It’s working well, but I’m tired of doing things like this:
SetStoragePinByName('Ini_Opto_Enable_Positive_Pin', Ini_Opto_Enable_Positive_Pin,True); Which calls a procedure like this:
Procedure SetStoragePinbyname(SSPBN_Var_Name:String; setpinnum:Byte; Pin_State:Boolean);
So in quotes I have the name of the variable that contains the name of the variable in the array, so it can figure out the position in the port array, then the actual contents of the actual variable being used… so I know what pin number I am outputting to, and then the state I want to set… So I have a TON of exactly what you describe, calls to functions and procedures with the name of the variable in quotes AND the variable itself. Variable names are not assigned at runtime, they are pre-defined. I did not need to use the name of the variable in my pin-list array, but it makes it less confusing if the variable name is what I search for in the array than some kind of cryptic code that would be difficult to maintain.
So yes, the only reason to want to do this is so I could hopefully shorted the call to just
SetStoragePinByName('Ini_Opto_Enable_Positive_Pin', True) ;
And it would somehow get the value of 'Ini_Opto_Enable_Positive_Pin'
Hmmmm now that I’m typing this all out, I think I can just put the value of the pin in another array also referenced by the same string and then I could make my calls like that.
From: fpc-pascal <fpc-pascal-bounces at lists.freepascal.org> On Behalf Of Santiago A.
Sent: Monday, July 8, 2019 3:40 PM
To: fpc-pascal at lists.freepascal.org
Subject: Re: [fpc-pascal] specify variable name with string variable
El 07/07/2019 a las 21:58, James Richters escribió:
This might sound silly, but is it possible to somehow specify a variable with a string containing the name of the variable?
MyVariable1 : Word;
MyVariableName : String;
Writeln(Variablename,' = $', inttohex( (Somehow get the value of the variable specified by the name here ) ,4));
Is such a thing possible?
What is it for?
procedure ShowVariable(const Variablename:string; const value:word);
Writeln(Variablename,' = $'+inttohex(value ,4));
And as far as I understand, you want to call ShowVariable('myVar',myVar), but not wasting time writing the name enclosed in quotes, let the program get the identifier name in runtime, or the other way around, let the program get the value from a name passed in runtime.
Bad luck, Pascal is not a script language, it is not aware of identifier names in runtime there is no "eval". Thanks to that it gets rids of all the burden of having to keep in runtime structures to know the name, type and value of variable. It generate fast native programs.
Nevertheless, there are ways, you can program that in Freepascal using classes, properties, RTII etc. It is more flexible and powerful. But obviously it has a price in complexity. Instead or using the standard operators and reference a simple variable, you must use a more convoluted syntax.
As far as I see, you don't intend to let the program create new vars in runtime, you just want to write the identifiers of the vars you have hardcoded. Is it worth?
(I hope it is not just for debugging)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the fpc-pascal