comp.lang.ada
 help / color / mirror / Atom feed
From: johndoe@dsd.camb (k. beitz)
Subject: Re: Looking for calc. package in Ada
Date: 1996/06/28
Date: 1996-06-28T00:00:00+00:00	[thread overview]
Message-ID: <kd688bbybn.fsf@dsd.camb> (raw)
In-Reply-To: 4qp7vu$c9f$1@mhafn.production.compuserve.com

nasser@apldbio.com (Nasser Abbasi) writes:

> 
> 
>    From: Strategies SA <100747.2001@CompuServe.COM>
>    Newsgroups: comp.lang.ada
>    Date: 25 Jun 1996 17:34:54 GMT
> 
>    Hello
>    I'm looking for a package looking like :
> 
>    package CALCULATOR is 
>       ...
>       procedure STORE_VARIABLE is (NAME : STRING;
> 				   VALUE : FLOAT);
> 
>       function CALCULATE (EXPRESSION : STRING) 
> 	   return FLOAT;
>       ...
>    end CALCULATOR;
> 
>    ..snip..
> 
> 
> you might want to look at the Beitz calculator package, one of the
> ARA Ada contest programs, I dont know if it does what you want, but
> you might be abe to steal some code or idea from it. this is the
> main line of it: (it is a Xwindows Ada application)
> ps. the programs are located in  ftp://ocsystems.com/pub/ara-contest/2.
> 

being the author of this calculator, i can tell you that the intention of the
calculator was to fit as an engine into a scheme like you've described.

i can tell you that there is a variation in the contest directory that will
run on a normal tty type terminal.  the value.input.x_gui is the child of
input that supports an X GUI, and the value.input.text child is the one that
supports text only input.  it is from value.input.text that you should be
able to find the best source to start from

by having calculate call something akin to the procedure Value.Input.Dispatch
(which currently only appears in Value.Input.Text.Process, but could be
presumably cut-and-paste into the package at the bottom) -- on each character
of the string, and having the display refresh update the string that gets
returned by Calculate, you could achieve what you want.  (in the example
below, i've made calculate return a string instead of a float, but
presumably, you could just call float_io.Get on the string to get a float
value for other purposes).

internally, the way it does calculations is via an abstract tagged type
expression, extensions of that tagged type type for every kind of evaluatable
expression, and parsing based on the current input value.

-------------------------------------------------------------------------------

package calculator is

    procedure Refresh( S : String );

    function Calculate( Expression : String ) return String;
	
end calculator;

with Calculator;
procedure Display_Refresh( S : String ) is	-- with'ed by Value package
begin
    Refresh( S );
end Display_Refresh;

package Value.Input.Whole_String is

    procedure Dispatch( C : Character );

end Value.Input.Whole_String;

package body Value.Input.Whole_String is

    -- cut-and-paste here from Value.Input.Text.Dispatch as necessary
    -- it includes some things for text style popup menus and stuff you can
    -- probably ignore 

end Value.Input.Whole_String;

with Ada.Strings.Unbounded;
with Value.Input.Whole_String;
package body calculator is

    Display : Ada.Strings.Unbounded.Unbounded_String;

    procedure Refresh( S : String ) is
    begin
	Display := Ada.Strings.Unbounded.To_Unbounded_String( S );
    end Refresh;

    function Calculate( Expression : String ) return String is
    begin
	for E in Expression'RANGE loop
	    Value.Input.String_At_A_Time.Dispatch( Expression(E) );
	end loop;
	return Ada.Strings.Unbounded.To_String( Display );
    end Calculate;

end Calculator;

-- now, compile all of the value tree, create your main that calls
-- Calculator.Calculate, and you're good to go.

--kirk beitz





      parent reply	other threads:[~1996-06-28  0:00 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-06-25  0:00 Looking for calc. package in Ada Strategies SA
1996-06-27  0:00 ` Gary McKee
1996-06-27  0:00 ` Nasser Abbasi
1996-06-28  0:00 ` k. beitz [this message]
replies disabled

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