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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e86b97ab50813172 X-Google-Attributes: gid103376,public From: johndoe@dsd.camb (k. beitz) Subject: Re: Looking for calc. package in Ada Date: 1996/06/28 Message-ID: #1/1 X-Deja-AN: 162617730 sender: news@inmet.camb.inmet.com (USENET news) x-nntp-posting-host: dsd.camb.inmet.com references: <4qp7vu$c9f$1@mhafn.production.compuserve.com> organization: Intermetrics, Inc., Cambridge, MA newsgroups: comp.lang.ada Date: 1996-06-28T00:00:00+00:00 List-Id: 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