comp.lang.ada
 help / color / mirror / Atom feed
From: Laurent.Guerby@enst-bretagne.fr (Laurent Guerby)
Subject: Re: String to Variable Name Mapping
Date: 1996/04/13
Date: 1996-04-13T00:00:00+00:00	[thread overview]
Message-ID: <4xybo0sb1s.fsf@leibniz.enst-bretagne.fr> (raw)
In-Reply-To: 4kjico$emb@newssvr.cacd.rockwell.com

"F. Britt Snodgrass" writes
: 
: Is there an attribute or some other way to use the contents of a string
: variable to point to another variable?
: 
: For example, if I have the following declarations,
: 
:   Int_Var : Integer;
:   Str_Var : String (1..32) := "Int_Var";
: 
: can I somehow use the contents of Str_Var to make an assignment to 
: Int_Var?
: 
: I'd like to do this as a way of initializing variables by reading from a 
: text file containing the variable name and the desired initial value.

   Ada is not an interpreted language that allow such tricks ;-). Here
is a reasonable way of doing something equivalent :

--  Laurent Guerby, 13 April 1996.
--  Tested and approved by GNAT ;-).

with Ada.Text_IO;

procedure Symbol is

   package Text_IO renames Ada.Text_IO;
   --  with vs use ... ;-).

   type Variable_Type is new Integer;
   --  Taken from your example.

   package Value_IO is new 
      Ada.Text_IO.Integer_IO (Variable_Type);
   --  Easy value IO.
  
   type Variable_Name is (Var_1, Var_2, Var_3); 
   --  Enumeration type.

   package Name_IO is new 
      Ada.Text_IO.Enumeration_IO (Variable_Name);
   --  You'll need it for easy IO operations on names.

   type Table is array (Variable_Name) of Variable_Type;
   --  If you want different types, it's doable but it'll need
   --  a bit more work.

   Symbol_Table : Table;
   --  The interesting variable ;-).

   Current_Name  : Variable_Name;
   Current_Value : Variable_Type;

begin
   Name_IO.Get  (Current_Name);
   Value_IO.Get (Current_Value);
   Symbol_Table (Current_Name) := Current_Value;

   Text_IO.New_Line (2);

   Name_IO.Put  (Current_Name); 
   Text_IO.Put  (" = ");
   Value_IO.Put (Symbol_table (Current_Name));
   Text_IO.New_line;
end Symbol;

   Here is one possible execution :

guerby@leibniz:/tmp$ ./symbol 
var_1
2
 
 
VAR_1 =           2
guerby@leibniz:/tmp$

   This   make (extensive)   use  of  Ada.Text_IO, if   you  want more
precisisons   about that,  just   follow the  Lovelace tutorial  (from
http://lglwww.epfl.ch/Ada/ by  David Wheeler) or  run to your bookshop
and buy an Ada book ;-).

   Note that we use  that in DART with an  array of access to  Mapping
function,  to make the link between  the scene description file (which
uses names) and predefined color mappings (the Symbol_Table is defined
at elaboration time with procedures access).

   If you got a name clash between your enum type and a predefined Ada
word, youi've to add a dummy postfix (or prefix) to each name and hack
a bit the IO routines.

: Thanks,
: Britt Snodgrass  (mailto:britt@acm.org)

   Hope this helps,

-- 
--  Laurent Guerby, student at Telecom Bretagne (France), Team Ada
--  "Use the Source, Luke. The Source will be with you, always (GPL)"
--  http://www-eleves.enst-bretagne.fr/~guerby/ (GATO Project)
--  Try GNAT, the GNU Ada 95 compiler (ftp://cs.nyu.edu/pub/gnat)




  parent reply	other threads:[~1996-04-13  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-04-11  0:00 String to Variable Name Mapping F. Britt Snodgrass
1996-04-13  0:00 ` Robert Dewar
1996-04-13  0:00 ` Laurent Guerby [this message]
1996-04-19  0:00 ` Todd Coniam
1996-04-25  0:00 ` Felaco
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox