* Re: [Caml-list] C-like macros in OCaml
[not found] <20070627044609.6B456BC77@yquem.inria.fr>
@ 2007-06-27 7:21 ` David Allsopp
0 siblings, 0 replies; 8+ messages in thread
From: David Allsopp @ 2007-06-27 7:21 UTC (permalink / raw)
To: caml-list
On Jun 26, 2007, at 6:06 PM, Jonathan Bryant wrote:
> On Jun 26, 2007, at 6:00 PM, Raj B wrote:
> > Similarly, is there any way of getting a C-like enumeration? e.g.
> >
> > enum days {Mon = 1, Tue, Wed...}
>
> You can use a variant and a pair of functions:
>
> type weekday =
> | Monday
> | Tuesday
> ...
> | Sunday
You can also use a variant and Obj.magic which turns the two functions into:
let weekday_to_int (w : weekday) = Obj.magic w + 1
let int_to_weekday i =
if i >= 1 && i <= 7
then (Obj.magic (i - 1) : weekday)
else raise ...
But this is really dirty and a potential source of bugs... but the result is
closer to the "#define" way.
David
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] C-like macros in OCaml
2007-06-27 12:53 ` Christopher L Conway
[not found] ` <6f9f8f4a0706280037h5f19af5flb764e1c2999b0337@mail.gmail.com>
@ 2007-06-28 7:44 ` Nicolas Pouillard
1 sibling, 0 replies; 8+ messages in thread
From: Nicolas Pouillard @ 2007-06-28 7:44 UTC (permalink / raw)
To: Christopher L Conway; +Cc: Loup Vaillant, caml-list
On 6/27/07, Christopher L Conway <cconway@cs.nyu.edu> wrote:
> On 6/27/07, Loup Vaillant <loup.vaillant@gmail.com> wrote:
> > By the way, does camlp4 handle cross modules C-like macros? If it does, how?
> > For instance, what if I write, in foo.ml :
> > DEFINE FOO = 1
> >
> > and then, in bar.ml :
> > let bar = FOO;;
> >
> > or else:
> > open foo;;
> > let bar = FOO;;
> >
> > or even :
> > let bar = Foo.FOO;;
> >
> > Will I get what I want?
>
> Probably not, but that depends on what you want. ;-) I believe that
> pa_macro DEFINEs have module scope. To get the same DEFINE in multiple
> files, you can use -D arguments on the command line, but pa_macro
> doesn't allow these arguments to have values (i.e., you can only test
> IFDEF/IFNDEF).* If you want arguments, see pa_macro_arg:
> http://caml.inria.fr/cgi-bin/hump.en.cgi?contrib=569
>
> Chris
>
> * I submitted a patch for this, but I don't think it's made its way
> into a release. I didn't realize the 3.10/new camlp4 push was on when
> I made it.
And your patch has been applied.
--
Nicolas Pouillard
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] C-like macros in OCaml
[not found] ` <6f9f8f4a0706280037h5f19af5flb764e1c2999b0337@mail.gmail.com>
@ 2007-06-28 7:38 ` Loup Vaillant
0 siblings, 0 replies; 8+ messages in thread
From: Loup Vaillant @ 2007-06-28 7:38 UTC (permalink / raw)
To: Caml List
2007/6/27, Christopher L Conway <cconway@cs.nyu.edu>:
> On 6/27/07, Loup Vaillant <loup.vaillant@gmail.com> wrote:
> > By the way, does camlp4 handle cross modules C-like macros? If it does, how?
> > For instance, what if I write, in foo.ml :
> > [...]
> > Will I get what I want?
>
> Probably not, but that depends on what you want. ;-) I believe that
> pa_macro DEFINEs have module scope.
Too bad. Actually, I want to implement a Lisp syntax for Ocaml, which
means I also want Lisp macros (regular and reader ones). Lisp macros
are often used extensively (Paul Graham talks about over 20% of the
code). That is why I want properly scoped macros : no global
namespace, and yet the possibility to use them between compilation
unit.
The only solution I came up with was producing at parsing type some
file for each .ml file containing a macro, so the preprocessor can
check these when parsing another file using this macro.
Hence my question : doesn't camlp4 provide a better solution to this
kind of problem? I know very little about the inner core of camlp4,
but I am sure I can reuse some ideas.
> To get the same DEFINE in multiple
> files, you can use -D arguments on the command line, but pa_macro
> doesn't allow these arguments to have values (i.e., you can only test
> IFDEF/IFNDEF).* If you want arguments, see pa_macro_arg:
> http://caml.inria.fr/cgi-bin/hump.en.cgi?contrib=569
I will take a look, thanks for the pointer.
Loup
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] C-like macros in OCaml
2007-06-27 8:08 ` Loup Vaillant
@ 2007-06-27 12:53 ` Christopher L Conway
[not found] ` <6f9f8f4a0706280037h5f19af5flb764e1c2999b0337@mail.gmail.com>
2007-06-28 7:44 ` Nicolas Pouillard
0 siblings, 2 replies; 8+ messages in thread
From: Christopher L Conway @ 2007-06-27 12:53 UTC (permalink / raw)
To: Loup Vaillant; +Cc: caml-list
On 6/27/07, Loup Vaillant <loup.vaillant@gmail.com> wrote:
> By the way, does camlp4 handle cross modules C-like macros? If it does, how?
> For instance, what if I write, in foo.ml :
> DEFINE FOO = 1
>
> and then, in bar.ml :
> let bar = FOO;;
>
> or else:
> open foo;;
> let bar = FOO;;
>
> or even :
> let bar = Foo.FOO;;
>
> Will I get what I want?
Probably not, but that depends on what you want. ;-) I believe that
pa_macro DEFINEs have module scope. To get the same DEFINE in multiple
files, you can use -D arguments on the command line, but pa_macro
doesn't allow these arguments to have values (i.e., you can only test
IFDEF/IFNDEF).* If you want arguments, see pa_macro_arg:
http://caml.inria.fr/cgi-bin/hump.en.cgi?contrib=569
Chris
* I submitted a patch for this, but I don't think it's made its way
into a release. I didn't realize the 3.10/new camlp4 push was on when
I made it.
> Now, what about regular quotations? And grammar rules? How can I
> specify the scope of particular grammar rule (or quotation)? For the
> little I know about camlp4, once defined, the scope of a new grammar
> rule is global. (Idem for quotations).
>
> Am I right?
> Any clue about the way camlp4 keep track of new grammar rules an
> quotations? (Or some pointer to the documentation involved)
>
> Thanks
> Loup Vaillant
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] C-like macros in OCaml
2007-06-27 7:43 ` Jeremy Yallop
@ 2007-06-27 8:08 ` Loup Vaillant
2007-06-27 12:53 ` Christopher L Conway
0 siblings, 1 reply; 8+ messages in thread
From: Loup Vaillant @ 2007-06-27 8:08 UTC (permalink / raw)
To: caml-list
2007/6/27, Jeremy Yallop <jeremy.yallop@ed.ac.uk>:
> Richard Jones wrote:
> > On Tue, Jun 26, 2007 at 05:00:40PM -0500, Raj B wrote:
> >> Hi there
> >>
> >> Is there any way I can achieve the use of C-like preprocessor macros
> >> in OCaml?
> >> i.e.
> >> [...]
> >[...]
> It appears to support them. If you have a file `foo.ml' containing the
> following:
> [...]
By the way, does camlp4 handle cross modules C-like macros? If it does, how?
For instance, what if I write, in foo.ml :
DEFINE FOO = 1
and then, in bar.ml :
let bar = FOO;;
or else:
open foo;;
let bar = FOO;;
or even :
let bar = Foo.FOO;;
Will I get what I want?
Now, what about regular quotations? And grammar rules? How can I
specify the scope of particular grammar rule (or quotation)? For the
little I know about camlp4, once defined, the scope of a new grammar
rule is global. (Idem for quotations).
Am I right?
Any clue about the way camlp4 keep track of new grammar rules an
quotations? (Or some pointer to the documentation involved)
Thanks
Loup Vaillant
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] C-like macros in OCaml
2007-06-27 4:10 ` Richard Jones
@ 2007-06-27 7:43 ` Jeremy Yallop
2007-06-27 8:08 ` Loup Vaillant
0 siblings, 1 reply; 8+ messages in thread
From: Jeremy Yallop @ 2007-06-27 7:43 UTC (permalink / raw)
To: Richard Jones; +Cc: Raj B, caml-list
Richard Jones wrote:
> On Tue, Jun 26, 2007 at 05:00:40PM -0500, Raj B wrote:
>> Hi there
>>
>> Is there any way I can achieve the use of C-like preprocessor macros
>> in OCaml?
>> i.e.
>>
>> #define FOO 1
>>
>> etc.
>
> What about ocamlc -pp /lib/cpp ? :-)
>
> There's also a camlp4 macro preprocessor, but I can't remember now
> whether it can do constant defines like that (IIRC it used not, but
> now it can, or something like that).
It appears to support them. If you have a file `foo.ml' containing the
following:
DEFINE FOO = 1
let x = FOO
then compiling it with pa_macro under 3.09 gives:
$ camlp4 pa_o.cmo pa_macro.cmo pr_o.cmo foo.ml
(* *)
let x = 1
or with the built-in Camlp4MacroParser under 3.10:
$ camlp4of foo.ml
let x = 1
Jeremy.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] C-like macros in OCaml
2007-06-26 22:00 Raj B
2007-06-26 22:06 ` [Caml-list] " Jonathan Bryant
@ 2007-06-27 4:10 ` Richard Jones
2007-06-27 7:43 ` Jeremy Yallop
1 sibling, 1 reply; 8+ messages in thread
From: Richard Jones @ 2007-06-27 4:10 UTC (permalink / raw)
To: Raj B; +Cc: caml-list
On Tue, Jun 26, 2007 at 05:00:40PM -0500, Raj B wrote:
> Hi there
>
> Is there any way I can achieve the use of C-like preprocessor macros
> in OCaml?
> i.e.
>
> #define FOO 1
>
> etc.
What about ocamlc -pp /lib/cpp ? :-)
There's also a camlp4 macro preprocessor, but I can't remember now
whether it can do constant defines like that (IIRC it used not, but
now it can, or something like that).
Rich.
--
Richard Jones
Red Hat
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: [Caml-list] C-like macros in OCaml
2007-06-26 22:00 Raj B
@ 2007-06-26 22:06 ` Jonathan Bryant
2007-06-27 4:10 ` Richard Jones
1 sibling, 0 replies; 8+ messages in thread
From: Jonathan Bryant @ 2007-06-26 22:06 UTC (permalink / raw)
To: Raj B; +Cc: caml-list
On Jun 26, 2007, at 6:00 PM, Raj B wrote:
> Similarly, is there any way of getting a C-like enumeration? e.g.
>
> enum days {Mon = 1, Tue, Wed...}
You can use a variant and a pair of functions:
type weekday =
| Monday
| Tuesday
...
| Sunday
let weekday_to_int w = match w with
| Monday -> 1
| Tuesday -> 2
...
| Sunday -> 7
let int_to_weekday i = match i with
| 1 -> Monday
| 2 -> Tuesday
...
| 7 -> Sunday
| _ -> raise (Invalid_argument "int_to_weekday")
--Jonathan
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2007-06-28 7:44 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <20070627044609.6B456BC77@yquem.inria.fr>
2007-06-27 7:21 ` [Caml-list] C-like macros in OCaml David Allsopp
2007-06-26 22:00 Raj B
2007-06-26 22:06 ` [Caml-list] " Jonathan Bryant
2007-06-27 4:10 ` Richard Jones
2007-06-27 7:43 ` Jeremy Yallop
2007-06-27 8:08 ` Loup Vaillant
2007-06-27 12:53 ` Christopher L Conway
[not found] ` <6f9f8f4a0706280037h5f19af5flb764e1c2999b0337@mail.gmail.com>
2007-06-28 7:38 ` Loup Vaillant
2007-06-28 7:44 ` Nicolas Pouillard
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox