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!news4.google.com!feeder2.cambriumusenet.nl!feed.tweaknews.nl!87.79.20.105.MISMATCH!news.netcologne.de!ramfeed1.netcologne.de!newsfeed.freenet.ag!feeder.erje.net!eternal-september.org!.POSTED!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: S-expression I/O in Ada Date: Sun, 01 Aug 2010 18:01:14 +0200 Organization: A noiseless patient Spider Message-ID: <87aap6wcdx.fsf@ludovic-brenta.org> References: <547afa6b-731e-475f-a7f2-eaefefb25861@k8g2000prh.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Sun, 1 Aug 2010 16:01:24 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="vhtspQ5pQqftjY2c+x4BbQ"; logging-data="13679"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/axQF7bnq2suR45rBjwTph" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:renpB9DUQHdwOPYcrcXmi3opdYc= sha1:IRBYNTRQTzwMr7980JJCrKLcLDo= Xref: g2news1.google.com comp.lang.ada:12772 Date: 2010-08-01T18:01:14+02:00 List-Id: Natacha Kerensikova writes: > Hi, > > I'm trying to learn Ada, coming from a C background. The first thing I > planned to code is a S-expression parser, because it's quite easy (at > least in C, less than 1000 lines including comments and memory > handling (dynamic arrays reinvented)) and very useful considering > almost all my existing programs use S-expressions as a serialization > format. > > To describe briefly S-expressions, I consider them to be the simplest > existing data organization beyond raw sequences of bits. They are > basically lists of elements, each element being either a list or an > atom, and atoms being raw sequences of bits. > > While I'm still not deep enough into Ada to know how to represent the > lists, I guess there won't be major issues, I think I can handle it > myself (though pointers and hints would still be welcome). > > My question here is about how to represent the atoms in Ada. In C it > was merely a void pointer and a size, but it seems more difficult in > Ada because of the strong typing. Because it's up to the application > to make sense (i.e. type) out of the raw sequences of bits in atoms, > the S-expression library has to handle them as a sort of untyped > memory chunk. Do you know of a way to handle that? > > Please correct me if I'm wrong, but my guess would be that the S- > expression library would provide a Sexp_Atom type, which refers to the > untyped memory chunks, and the application would have procedures to > convert back and forth between Sexp_Atom and whatever types it > internally uses (i.e. serialization and deserialization procedures). > However the library would provide these procedures for the most common > types (e.g. strings and numeric types). > > Though it looks like a fine Ada API (at least to my eyes), I have > absolutely no idea about how to implement the library. How to define > the application-opaque Sexp_Atom type? How to read Sexp_Atom objects > from a file? How to build them from Ada types? How to write them back > to disk? In Ada, you normally model blobs with System.Storage_Elements.Storage_Array; since arrays are first-class citizens (as opposed to C's void pointers), you do not need to carry the length of such an array separately. Thus, a naive approach might be: type Sexp_Atom is access System.Storage_Elements.Storage_Array; type Sexp; type Sexp_Access is access Sexp; type Sexp is record Car : Sexp_Atom; Cdr : Sexp_Access; end record; However, the purpose of S-Expressions being to be read and written as text, a blob may not be the most appropriate; you might be better off with simply: type Sexp; type Sexp_Access is access Sexp; type Sexp is Car : Ada.Strings.Unbounded.Unbounded_String; Cdr : Sexp_Access; Is_List : Boolean; end record; To write a sexp to disk and read back, you would leverage the Ada streams as Dmitry pointed out. You could then provide a generic package that serializes an arbitrary type T back and forth to the unbounded_string. -- Ludovic Brenta.