comp.lang.ada
 help / color / mirror / Atom feed
From: Gene <gene.ressler@gmail.com>
Subject: Re: Using a string as a binary operator
Date: Tue, 14 Oct 2008 20:51:27 -0700 (PDT)
Date: 2008-10-14T20:51:27-07:00	[thread overview]
Message-ID: <6a2b5846-4458-4836-a7e6-3110901df461@d10g2000pra.googlegroups.com> (raw)
In-Reply-To: 8cbb04c3-e789-4b67-897a-fd6f83486bbc@x16g2000prn.googlegroups.com

On Oct 14, 11:35 am, Joe <joesmo...@gmail.com> wrote:
> 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;
>

The "+" in Ada code such as "+"(1, 2) is a function call, just like
f(1, 2).  You can't cast a string "+" to a function in Ada, just as
you can't cast "f".  You have to convert the string yourself.  Here is
a toy code with many things go fix:

with Ada.Text_IO; use Ada.Text_IO;

procedure Postfix is

   subtype Number is Integer;

   Operands : array(1 .. 1000) of Number;
   SP : Natural := 0;

   procedure Push(X : in Number) is
   begin
      SP := SP + 1;
      Operands(SP) := X;
   end Push;

   procedure Pop(X : out Number) is
   begin
      X := Operands(SP);
      SP := SP - 1;
   end Pop;

   Buf : String(1 .. 1000);
   Last : Natural;
   X, Y : Number;
begin
   loop
      Get_Line(Buf, Last);
      exit when Last = 0;
      case Buf(1) is
         when '+' =>
            Pop(X); Pop(Y); Push(X + Y);
         when '-' =>
            Pop(X); Pop(Y); Push(X - Y);
         when '*' =>
            Pop(X); Pop(Y); Push(X * Y);
         when '/' =>
            Pop(X); Pop(Y); Push(X / Y);
         when others =>
            Push(Number'Value(Buf(1 .. Last)));
      end case;
      if SP = 1 then
         Put_Line(Number'Image(Operands(1)));
      end if;
   end loop;
end Postfix;



  parent reply	other threads:[~2008-10-15  3:51 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 [this message]
2008-10-15  5:45 ` anon
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