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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no 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!news2.google.com!news.glorb.com!wn13feed!worldnet.att.net!bgtnsc04-news.ops.worldnet.att.net.POSTED!53ab2750!not-for-mail Newsgroups: comp.lang.ada From: anon@anon.org (anon) Subject: Re: Using a string as a binary operator Reply-To: no to spamers (No@email.given.org) References: <8cbb04c3-e789-4b67-897a-fd6f83486bbc@x16g2000prn.googlegroups.com> X-Newsreader: IBM NewsReader/2 2.0 Message-ID: Date: Wed, 15 Oct 2008 05:45:04 GMT NNTP-Posting-Host: 12.65.102.114 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc04-news.ops.worldnet.att.net 1224049504 12.65.102.114 (Wed, 15 Oct 2008 05:45:04 GMT) NNTP-Posting-Date: Wed, 15 Oct 2008 05:45:04 GMT Organization: AT&T Worldnet Xref: g2news2.google.com comp.lang.ada:8114 Date: 2008-10-15T05:45:04+00:00 List-Id: First, it seams that your talking about a version of "Polish notation" or "Lisp notation". There are 100s of articles and some sample program across the net. Just do a search, and these article should give more information than you have question for. For a starter in this simple example there will be one push/pop function one for "operations" and the other for numeric values. type operations is ( add, sub, mul, div, equal ) ; - sample list for operations use ( noop => 0, add => 1, sub => 2, mul => 3, div => 4, equal => 5 ) ; - sample list procedure do_notation ( buffer : string ) is operation_code : character ; begin -- decode operation_code and arguments from buffer -- push arguments push ( arg1 ) ; -- push numeric value of argument 1 and 2 push ( arg2 ) ; case operation_code is when '+' => push ( add ) ; when '-' => push ( sub ) ; ... when others => raise Operations_Error ; - illegal operation end case ; end ; ----- function process_notation return data_type is cmd : operations ; begin arg1 := pop ; arg2 := pop ; cmd := pop ; case cmd is when add => result := arg1 + arg2 ; when sub => result := arg1 - arg2 ; when div => if arg2 /= 0 then result := arg1 / arg2 ; else raise Div_Zero_Error ; end ; ... when others => raise Stack_Error ; end case ; -- Display or return result. end ; result : data_type ; -- data_type is user assigned numeric type begin push ( noop ) ; -- not needed. Used only for showing stack list -- stack should have ( 0, ... ) do_notation ( "5 + 10" ) ; -- stack should have ( 5, 10, 1, 0, ... ) result := process_notation ; -- If the then after the stack should have ( 0, ... ) and result := 15 ... end ; In <8cbb04c3-e789-4b67-897a-fd6f83486bbc@x16g2000prn.googlegroups.com>, Joe writes: >Hey all, > >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: > >with Ada.Integer_Text_IO; use Ada.Integer_Text_IO; >procedure Operators is > Plus : String := "+"; >begin > Put( Plus(1,2)); >end Operators; > >Thanks, >Joe