comp.lang.ada
 help / color / mirror / Atom feed
From: anon@anon.org (anon)
Subject: Re: Using a string as a binary operator
Date: Wed, 15 Oct 2008 05:45:04 GMT
Date: 2008-10-15T05:45:04+00:00	[thread overview]
Message-ID: <ArfJk.71285$Mh5.5494@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: 8cbb04c3-e789-4b67-897a-fd6f83486bbc@x16g2000prn.googlegroups.com

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 <joesmoe10@gmail.com> 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




  parent reply	other threads:[~2008-10-15  5:45 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-14 15:35 Using a string as a binary operator Joe
2008-10-14 17:05 ` Dmitry A. Kazakov
2008-10-14 18:51 ` Jeffrey R. Carter
2008-10-15 15:22   ` Adam Beneschan
2008-10-15 17:23     ` Jeffrey R. Carter
2008-10-15 19:32     ` sjw
2008-10-16  8:14     ` Dmitry A. Kazakov
2008-10-14 22:01 ` george.priv
2008-10-15  3:51 ` Gene
2008-10-15  5:45 ` anon [this message]
2008-10-15 12:18   ` Joe
2008-10-15 13:43     ` John McCormick
replies disabled

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