From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 315EEBCAE for ; Sun, 26 Jun 2005 17:03:21 +0200 (CEST) Received: from furbychan.cocan.org (furbychan.cocan.org [80.68.91.176]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j5QF3K98027732 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Sun, 26 Jun 2005 17:03:20 +0200 Received: from rich by furbychan.cocan.org with local (Exim 3.35 #1 (Debian)) id 1DmYm0-00073G-00; Sun, 26 Jun 2005 16:10:16 +0100 Date: Sun, 26 Jun 2005 16:10:16 +0100 To: Jonathan Roewen Cc: caml-list@yquem.inria.fr Subject: Re: [Caml-list] Making something similar to format[4] Message-ID: <20050626151016.GA25445@furbychan.cocan.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.3.28i From: Richard Jones X-Miltered: at nez-perce with ID 42BEC3B8.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 printf:01 scanf:01 bigarray:01 struct:01 bool:01 char:01 specifier:01 char:01 ocaml:01 printf:01 scanf:01 compiler:01 compiler:01 c-like:01 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: On Sun, Jun 26, 2005 at 11:07:35PM +1200, Jonathan Roewen wrote: > I'd like to create something that works like the format type for > format strings with Printf/Scanf. What I want to do is take a > one-dimensional bigarray, and a format string of all the sizes of the > fields corresponding to a struct, and return the typed results. > > So sizes of 1 = bool, <= 8 char/int (format specifier to choose char), > <= 31 int/int32, = 32 int32, > 32, int64. The OCaml printf/scanf functions are basically hacks done in the compiler. Whereas it is possible to write a new function which takes precisely the same string format as printf/scanf, it's not possible to modify the format without changing the compiler. However, see below ... > The idea is to make reading/writing C-like structs painless & > automated. I'm just not sure where to begin in making this type-safe > format string. Does it require modding the ocaml parser & libraries? > Cause I'm really confused how ocaml can statically parse the string > and enforce type safety without some hack in the compiler at some > stage. It's actually a really good idea. I can't see any immediate reason why it couldn't be done trivially with camlp4. For a good start, read Martin Jambon's camlp4 tutorial (http://martin.jambon.free.fr/extend-ocaml-syntax.html) and then have a look at the code for tywith (http://www.seedwiki.com/wiki/shifting_focus/tywith) and my old simplesoap library (http://merjis.com/developers/simplesoap) which both manipulate types from camlp4. Rich. -- Richard Jones, CTO Merjis Ltd. Merjis - web marketing and technology - http://merjis.com Team Notepad - intranets and extranets for business - http://team-notepad.com