From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,e5d23ac8a9173493 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!40g2000prx.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Using a string as a binary operator Date: Wed, 15 Oct 2008 08:22:36 -0700 (PDT) Organization: http://groups.google.com Message-ID: <8d9b43fa-05a1-4024-bbd3-ddf276478748@40g2000prx.googlegroups.com> References: <8cbb04c3-e789-4b67-897a-fd6f83486bbc@x16g2000prn.googlegroups.com> NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1224084156 32583 127.0.0.1 (15 Oct 2008 15:22:36 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 15 Oct 2008 15:22:36 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: 40g2000prx.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:8126 Date: 2008-10-15T08:22:36-07:00 List-Id: On Oct 14, 11:51 am, "Jeffrey R. Carter" wrote: > Joe wrote: > > > I'm trying to build a simple stack the evaluates expressions in > > postfix notation (i.e. "1 2 +"). I can't find a way to use the > > operator directly when I get to it. When I get to the "+", but how > > do I apply this string to 1 and 2? The best I can do is make a case > > statement that has a case for each binary operator, but this seems > > very klunky. I know you can write "+"(1,2) to return 3 but how do I > > get Ada to recognize the string as an operator? Here's a short > > example of what I would like to do: > > The short answer is that you can't. You have to evaluate "1" and "2" to get > values of some numeric type and push them on a stack, then find a branch in an > if-elsif-else chain If-elsif-elsif-elsif-elsif-elsif-else chain? That's not very imaginative! Why not: type Operator_Descriptor is record Name : access String; Operation : access function (Left, Right : Integer) return Integer; end record; type Operator_Desc_Array is array (natural range <>) of Operator_Descriptor; Available_Operators : constant Operator_Desc_Array := ((new string' ("+"), Plus'access), (new string' ("-"), Minus'access), .......); Then you can search the array for an element whose Name.all matches the operator string, and then call the Operation function of the element. You'll have to write Plus, Minus, etc., since you can't take the 'access of an intrinsic function. If you want to mix some unary operators in the array, you might use a variant record, or with a tagged type that has "Unary_Operation" or "Binary_Operation" type extensions, or something along those lines. Although it isn't always possible, I definitely prefer to set things up in an array or some sort of data structure when possible, rather than to rely on a 67-branch IF statement or even a CASE statement. -- Adam