ocaml-community / cppo

C-style preprocessor for OCaml
BSD 3-Clause "New" or "Revised" License
107 stars 26 forks source link

Higher-order macros and macro definitions with explicit delimiters #90

Closed fpottier closed 1 month ago

fpottier commented 2 months ago

Hello,

This PR proposes:

The new features are documented in README.md.

Some examples of the use of higher-order macros appear in the example file test/higher_order_macros.cppo. Similar examples, which also take advantage of #def ... #enddef, appear in test/def.cppo.

Here are some more ideas, about which I would welcome the opinion of the cppo maintainers. I believe that each of these ideas would be easy to implement:

pmetzger commented 2 months ago

A minor comment:

(minor) #define on the last line of a file, without a newline, is now accepted. (It used to be an error. Accepting it allows me to produce a reliable error message when #def does not have a matching #enddef.)

Not that C has much to do with this, but I'll note that in C, a translation unit (aka a file) without a terminating newline is documented as UB.

pmetzger commented 2 months ago

Also, not that Lisp is a precedent we need to care about...

In order to improve hygiene in higher-order macros, I would like to introduce a new directive #fresh x y, which behaves like #define x . A globally fresh name would be obtained by combining the prefix y with a numeric suffix obtained by reading and incrementing a global counter.

In Lisp, this would be a gensym.

pmetzger commented 2 months ago

So a lot of this looks neat. I am obligated to ask, of course, what the anticipated use case is. I've traditionally viewed cppo as a sometimes needed hack that one generally wants to avoid. Also, since I started doing a lot of work on refactoring C code I've come to hate cpp in that context because it makes it harder to automate refactoring. However, some of this seems like a very cool experiment.

fpottier commented 2 months ago

So a lot of this looks neat. I am obligated to ask, of course, what the anticipated use case is.

Good question! I don't really have a clear use case at this point. Lately, for performance reasons, I have been playing with unrolling loops and specializing higher-order functions (which the OCaml compiler does not do), so cppo came in handy. This led me to think about the potential uses and current limitations of cppo, and I thought that it could be useful to remove some of these limitations.

fpottier commented 2 months ago

In Lisp, this would be a gensym.

Sure. I would be happy to use whatever syntax you think is most convenient or elegant.

pmetzger commented 2 months ago

Lately, for performance reasons, I have been playing with unrolling loops and specializing higher-order functions (which the OCaml compiler does not do), so cppo came in handy. This led me to think about the potential uses and current limitations of cppo, and I thought that it could be useful to remove some of these limitations.

I have never used metaocaml, but I understand it is also useful for such things. Would this serve a distinct need?

Also, I have no particular brief for gensym, I just am noting it's an analogous piece of functionality.

pmetzger commented 1 month ago

What do we need to do next to move this pull request forward?

pmetzger commented 1 month ago

Seems fine by me.

fpottier commented 1 month ago

Is there something I can/should do?

In particular, I haven't written an entry in Changes.md.

liyishuai commented 1 month ago

Given the time length for re-running the CI, maybe worth editting the changelog in a separate commit.

fpottier commented 1 month ago

I have pushed two commits to fix a couple typos and add an entry in Changes.md. If the CI passes then I believe it is ready to release as 1.7.0.

fpottier commented 1 month ago

I believe that the CI has succeeded. Is it OK if I release by running make publish1.7.0? Or should I wait for a maintainer to do it?

liyishuai commented 1 month ago

Please fire at will. You might need to create a tag for v1.7.0 before running the Makefile script.

pmetzger commented 1 month ago

@fpottier Just FYI, you're as much a maintainer now as the rest of us. :)

fpottier commented 1 month ago

It's an honor :-)