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.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,24d7acf9b853aac8 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!news.glorb.com!news.netfront.net!not-for-mail From: Jeffrey Carter Newsgroups: comp.lang.ada Subject: Re: S-expression I/O in Ada Date: Tue, 17 Aug 2010 12:00:08 -0700 Organization: Netfront http://www.netfront.net/ Message-ID: References: <547afa6b-731e-475f-a7f2-eaefefb25861@k8g2000prh.googlegroups.com> NNTP-Posting-Host: 75.211.233.16 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: adenine.netfront.net 1282071611 58782 75.211.233.16 (17 Aug 2010 19:00:11 GMT) X-Complaints-To: news@netfront.net NNTP-Posting-Date: Tue, 17 Aug 2010 19:00:11 +0000 (UTC) User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.11) Gecko/20100713 Thunderbird/3.0.6 In-Reply-To: Xref: g2news1.google.com comp.lang.ada:13464 Date: 2010-08-17T12:00:08-07:00 List-Id: On 08/17/2010 10:01 AM, Natasha Kerensikova wrote: > > Even though I'm much more used to C than to Ada, I have the feeling it's > horribly ugly and that using access types all over the place like I did > is extremely poor. Yet I just can't find out exactly how it's wrong, nor > how to get it right. I would agree. Access types and values should not appear in the visible part of a package specification if at all possible. > Would any other you be kind enough to have a look at it, and point me > where I did wrong and explain me how wrong it is, be it on high-level > package design to low-level implementation choice and anything in > between including code style. It seemed to me that you could implement this without any access types or values, so I gave it a shot. The following has been compiled: private with Ada.Containers.Indefinite_Vectors; private with Ada.Containers.Vectors; package S_Expressions is type Atom is private; function To_Atom (Value : in String) return Atom; function To_String (Value : in Atom) return String; generic -- Object_Conversions type Element (<>) is limited private; package Object_Conversions is function To_Atom (Value : in Element) return Atom; function To_Object (Value : in Atom) return Element; end Object_Conversions; type S_Expression (<>) is tagged private; -- An Atom or a list of S_Expression. function Make (Item : in Atom) return S_Expression; function Is_Atom (Expression : in S_Expression) return Boolean; Not_An_Atom : exception; function To_Atom (Expression : in S_Expression) return Atom; -- Raises Not_An_Atom if not Is_Atom (Expression); Empty_List : constant S_Expression; Not_A_List : exception; function Append (Onto : in S_Expression; Value : in S_Expression) return S_Expression; -- Returns a list consisting of the exisiting list Onto with Value appended to it. -- Raises Not_A_List if Is_Atom (Onto); procedure Iterate (Over : in S_Expression; Process : not null access procedure (Expression : in S_Expression; Continue : in out Boolean) ); -- Passes each element of Over to Process in turn, with Continue => True. -- Returns immediately if Process sets Continue to False; remaining elements will not be processed. -- Raises Not_A_List if Is_Atom (Over). private -- S_Expressions Byte_Size : constant := 8; type Byte_Value is mod 2 ** Byte_Size; package Byte_Lists is new Ada.Containers.Vectors (Index_Type => Positive, Element_Type => Byte_Value); type Atom is record Value : Byte_Lists.Vector; end record; type Root is tagged null record; package Lists is new Ada.Containers.Indefinite_Vectors (Index_Type => Positive, Element_Type => Root'Class); type S_Expression (Is_Atom : Boolean) is new Root with record case Is_Atom is when False => List : Lists.Vector; when True => Value : Atom; end case; end record; Empty_List : constant S_Expression := (Is_Atom => False, List => Lists.Empty_Vector); end S_Expressions; with Ada.Unchecked_Conversion; package body S_Expressions is function To_Atom (Value : in String) return Atom is Result : Atom; begin -- To_Atom All_Characters : for I in Value'range loop Result.Value.Append (New_Item => Character'Pos (Value (I) ) ); end loop All_Characters; return Result; end To_Atom; function To_String (Value : in Atom) return String is Result : String (Value.Value.First_Index .. Value.Value.Last_Index); begin -- To_String All_Bytes : for I in Result'range loop Result (I) := Character'Val (Value.Value.Element (I) ); end loop All_Bytes; return Result; end To_String; package body Object_Conversions is type Byte_List is array (Positive range <>) of Byte_Value; function To_Atom (Value : in Element) return Atom is Num_Bytes : constant Positive := (Value'Size + Byte_Size - 1) / Byte_Size; subtype Element_List is Byte_List (1 .. Num_Bytes); function Convert is new Ada.Unchecked_Conversion (Source => Element, Target => Element_List); Byte : Element_List renames Convert (Value); Result : Atom; begin -- To_Atom All_Bytes : for I in Byte'range loop Result.Value.Append (New_Item => Byte (I) ); end loop All_Bytes; return Result; end To_Atom; function To_Object (Value : in Atom) return Element is subtype Element_List is Byte_List (Value.Value.First_Index .. Value.Value.Last_Index); function Convert is new Ada.Unchecked_Conversion (Source => Element_List, Target => Element); Byte : Element_List; begin -- To_Object All_Bytes : for I in Byte'range loop Byte (I) := Value.Value.Element (I); end loop All_Bytes; return Convert (Byte); end To_Object; end Object_Conversions; function Make (Item : in Atom) return S_Expression is -- null; begin -- Make return S_Expression'(Is_Atom => True, Value => Item); end Make; function Is_Atom (Expression : in S_Expression) return Boolean is -- null; begin -- Is_Atom return Expression.Is_Atom; end Is_Atom; function To_Atom (Expression : in S_Expression) return Atom is -- null; begin -- To_Atom if not Expression.Is_Atom then raise Not_An_Atom; end if; return Expression.Value; end To_Atom; function Append (Onto : in S_Expression; Value : in S_Expression) return S_Expression is Result : S_Expression (Is_Atom => False); begin -- Append if Onto.Is_Atom then raise Not_A_List; end if; Result.List := Onto.List; Result.List.Append (New_Item => Value); return Result; end Append; procedure Iterate (Over : in S_Expression; Process : not null access procedure (Expression : in S_Expression; Continue : in out Boolean) ) is Continue : Boolean := True; begin -- Iterate if Over.Is_Atom then raise Not_A_List; end if; All_Expressions : for I in Over.List.First_Index .. Over.List.Last_Index loop Process (Expression => S_Expression (Over.List.Element (I) ), Continue => Continue); exit All_Expressions when not Continue; end loop All_Expressions; end Iterate; end S_Expressions; I think this includes all necessary functionality to build and process S-expressions, though you might have some additional operations you might like to add. Reading it might prove instructional, whether you like this approach or not. -- Jeff Carter "I'm a lumberjack and I'm OK." Monty Python's Flying Circus 54 --- news://freenews.netfront.net/ - complaints: news@netfront.net ---