Olle,
I got bored, so coded up something that might resemble what you’re looking for. It … took a few minutes, so, y’know, if it’s not useful, not much lost.
Link: GitHub - chetmurthy/pa_ppx_forth: a little Camlp5-based PPX rewriter for Forth syntax
To use this, you need to have camlp5
, pa_ppx
, and not-ocamlfind
installed. At that point, you should be able to just run “make” in the toplevel directory. I haven’t bothered to make an opam
file, b/c … well, read on.
-
test/ppx_forth.ml
has a test which should, I hope, look like what you’re asking for. The expansion of
[%forth 1 2 plus dot;]
is
(((start () |> num 1) |> num 2) |> plus) |> dot
which is, I believe, what you were looking for.
-
If one wanted to instead have a custom syntax, that is to say, to use {%forth| 1 2 + .|}
instead, it wouldn’t be much more code: just hack together a little parser. If one wanted the lexemes of this little language to be different from those of OCaml, it would need a little lexer, which isn’t much more work.
-
BUT BUT BUT all of this is based on Camlp5. And I’m sure not pushing it on anybody, b/c sure, I recognize that it’s not compatible with the main thrust of OCaml development, and anybody who uses Camlp5, is buying into incompatibility.
That said, hey, this is a demonstration that you can do what you want with PPX extensions, b/c everything I did with Camlp5, you can do with PPX extensions – there’s no magic here.
So: look at pa_forth.ml
, and you’ll see the rewriter that matches expression PPX-extensions and rewrites them in the manner (I think) you desire. This should be straightforward (albeit tedious and (IMHO) painful) to do with the standard PPX rewriter infrastructure.
P.S. If one wanted to hack together a little parser and/or little lexer, there are examples in the Camlp5 tutorials ( camlp5/tutorials at master · camlp5/camlp5 · GitHub ) of just that, albeit not combined with PPX rewriters – but that’s not very difficult to do. Again though, all that stuff is dependent on Camlp5, and I’m not suggesting that anybody use it, b/c “nonstandard”.