comp.lang.ada
 help / color / mirror / Atom feed
* Re: Ada 83 Style Query
  1996-05-06  0:00 Ada 83 Style Query W. Wesley Groleau (Wes)
@ 1996-05-06  0:00 ` John Herro
  1996-05-06  0:00 ` Theodore E. Dennison
  1996-05-07  0:00 ` Laurent Guerby
  2 siblings, 0 replies; 4+ messages in thread
From: John Herro @ 1996-05-06  0:00 UTC (permalink / raw)



"W. Wesley Groleau (Wes)" <wwgrol@PSESERV3.FW.HAC.COM> writes:
> I'm working on some old code that has a
> long string of elsif checking strings that
> come in from outside against known
> commands.  Seeking opinions on the
> desirability of using an enumerated type
> with 'VAL and a case statement.
     One thing to consider is that none of the commands can match Ada
reserved words, such as BEGIN, REVERSE, ACCEPT, RECORD, END, USE, etc.  If
you write the program with an enumeration type, and later someone wants to
add an END command, you'll have to redesign the program!  However, if none
of the commands match reserved words and you're sure you won't later be
adding such commands, then using an enumeration type is certainly a viable
way of doing it.

> The sore spot is the need for an exception handler ...
     If you do choose to use an enumeration type, adding an exception
handler for the case of an incorrect incoming string should be simple.  I
suggest localizing the scope of the exception handler as much as possible
with a block, like this:
type Command_Type is (Go, Stop, ..., Unrecognized);
Command   : Command_Type;
Input     : String(1 .. 80);
Input_Len : Integer;
...
Get_Line(Input, Input_Len);
begin
   Command := Command_Type'Value(Input(1 .. Input_Len));
exception
   when Constraint_Error => Command := Unrecognized;
end;
case Command is ...

- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ada 83 Style Query
  1996-05-06  0:00 Ada 83 Style Query W. Wesley Groleau (Wes)
  1996-05-06  0:00 ` John Herro
@ 1996-05-06  0:00 ` Theodore E. Dennison
  1996-05-07  0:00 ` Laurent Guerby
  2 siblings, 0 replies; 4+ messages in thread
From: Theodore E. Dennison @ 1996-05-06  0:00 UTC (permalink / raw)



W. Wesley Groleau (Wes) wrote:
> 
> I'm working on some old code that has a long string of elsif checking
> strings that come in from outside against known commands.
> 
> Seeking opinions on the desirability of using an enumerated type with 'VAL
> and a case statement.  The sore spot is the need for an exception handler
> when the incoming string matches none of the literals in the command list.

This is one of my most common text-processing techniques.

The major drawback to doing this is that the text must be a vaild
Ada identifier. That means no commands called "exit" or "end" or "abort" 
(this comes up more often than you'd think), and nothing case-sensitive
or with spaces in the middle. If you don't have control over the command 
names, you may be in trouble here.

I don't see the exception handler being too much of a drawback. It will
only be called when the command isn't in your enumeration. The only 
way you'd even notice the difference is if:
   o  Invalid commands are very common AND
   o  Your compiler has a VERY large exception overhead AND
   o  Your enumeration is very small (only a few values with short
      names).

In many cases, error processing throughput isn't considered that vital.
In most other cases, the time taken to discover that the string isn't
a command will dwarf the time taken in "exception overhead". 

If these commands are comming from a human user, the time taken waiting
for him/her to respond will dwarf both. So why not code it in the 
easiest, most maintainable way?

I don't see any reason other than speed for an exception handler to be
a "sore-spot". It should be way easier to read, understand, and 
maintain than a huge "if..elseif..." statement. Plus you can do nifty
stuff like loop through the list of commands.

-- 
T.E.D.          
                |  Work - mailto:dennison@escmail.orl.mmc.com  |
                |  Home - mailto:dennison@iag.net              |
                |  URL  - http://www.iag.net/~dennison         |




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Ada 83 Style Query
@ 1996-05-06  0:00 W. Wesley Groleau (Wes)
  1996-05-06  0:00 ` John Herro
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: W. Wesley Groleau (Wes) @ 1996-05-06  0:00 UTC (permalink / raw)



I'm working on some old code that has a long string of elsif checking
strings that come in from outside against known commands.

Seeking opinions on the desirability of using an enumerated type with 'VAL
and a case statement.  The sore spot is the need for an exception handler
when the incoming string matches none of the literals in the command list.

What would YOU do?  And yes, "look the other way" is a viable answer. :-)
--
---------------------------------------------------------------------------
W. Wesley Groleau (Wes)                                Office: 219-429-4923
Magnavox - Mail Stop 10-40                               Home: 219-471-7206
Fort Wayne,  IN   46808              elm (Unix): wwgrol@pseserv3.fw.hac.com
---------------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Ada 83 Style Query
  1996-05-06  0:00 Ada 83 Style Query W. Wesley Groleau (Wes)
  1996-05-06  0:00 ` John Herro
  1996-05-06  0:00 ` Theodore E. Dennison
@ 1996-05-07  0:00 ` Laurent Guerby
  2 siblings, 0 replies; 4+ messages in thread
From: Laurent Guerby @ 1996-05-07  0:00 UTC (permalink / raw)



Wes writes
: 
: I'm working on some old code that has a long string of elsif checking
: strings that come in from outside against known commands.
: 
: Seeking opinions on the desirability of using an enumerated type with 'VAL
: and a case statement.  The sore spot is the need for an exception handler
: when the incoming string matches none of the literals in the command list.
: 
: What would YOU do?  And yes, "look the other way" is a viable answer. :-)

   Depending of what is on your priority list, other ways :

- hcode table (you can try some automatic "best" hcode generators).
- long list of imbricated "if", look at the GNAt lexical analysis routine. 

   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).




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1996-05-07  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-05-06  0:00 Ada 83 Style Query W. Wesley Groleau (Wes)
1996-05-06  0:00 ` John Herro
1996-05-06  0:00 ` Theodore E. Dennison
1996-05-07  0:00 ` Laurent Guerby

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