Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: itz@rahul.net (Ian T Zimmerman)
Cc: caml-list@inria.fr
Subject: Re: lexing__get_next_char ?
Date: Mon, 21 Oct 1996 16:30:50 +0200 (MET DST)	[thread overview]
Message-ID: <199610211430.QAA11843@pauillac.inria.fr> (raw)
In-Reply-To: <199610180340.UAA12614@kronstadt.rahul.net> from "Ian T Zimmerman" at Oct 17, 96 08:40:55 pm


> In the caml-light sources, in src/runtime/lexing.c, the primitive
> get_next_char is defined as follows:
> 
> struct lexer_buffer {
>   value refill_buff;
>   value lex_buffer;
>   value lex_abs_pos;
>   value lex_start_pos;
>   value lex_curr_pos;
>   value lex_last_pos;
>   value lex_last_action;
> };
> 
> value get_next_char(lexbuf)     /* ML */
>      struct lexer_buffer * lexbuf;
> {
>   mlsize_t buffer_len, curr_pos;
>   
>   buffer_len = string_length(lexbuf->lex_buffer);
>      ...
> 
> How can this work, when lexer buffers are ML records on the heap, as
> the following piece of src/lib/lexing.ml seems to show

Viewed from C, Caml records are arrays of elements of type "value".
So, we're basically casting a pointer to a "value" array to a pointer 
to a struct with all fields having type "value". 

This is probably not guaranteed to work by the ANSI C standard, but I
doubt there's any C compiler around that does not represent both types
identically.

(There are several other assumptions not guaranteed by ANSI C in the
Caml runtime, in particular that any pointer type can be cast to and
from the type "long". I don't think it is even possible to write a
memory manager and runtime system such as Caml's in strictly
conformant ANSI C.)

The function could be rewritten to use Field(lexbuf, ...) as you
suggested, but having a "struct" declaration in the C code that
reflects the Caml record declaration makes it easier to keep both C
and Caml code in sync.

- Xavier Leroy





  reply	other threads:[~1996-10-22 14:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-10-18  3:40 Ian T Zimmerman
1996-10-21 14:30 ` Xavier Leroy [this message]
1996-10-21 16:55   ` Ian T Zimmerman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=199610211430.QAA11843@pauillac.inria.fr \
    --to=xavier.leroy@inria.fr \
    --cc=caml-list@inria.fr \
    --cc=itz@rahul.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox