caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Making something similar to format[4]
@ 2005-06-26 11:07 Jonathan Roewen
  2005-06-26 15:10 ` Richard Jones
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Roewen @ 2005-06-26 11:07 UTC (permalink / raw)
  To: caml-list

Hi,

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 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.

Jonathan


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Making something similar to format[4]
  2005-06-26 11:07 [Caml-list] Making something similar to format[4] Jonathan Roewen
@ 2005-06-26 15:10 ` Richard Jones
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Jones @ 2005-06-26 15:10 UTC (permalink / raw)
  To: Jonathan Roewen; +Cc: caml-list

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-06-26 15:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-06-26 11:07 [Caml-list] Making something similar to format[4] Jonathan Roewen
2005-06-26 15:10 ` Richard Jones

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).