Browse thread
Where are the AST specs?
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Nicolas Pouillard <nicolas.pouillard@g...> |
| Subject: | Re: [Caml-list] Where are the AST specs? |
On 3/19/07, Loup Vaillant <loup.vaillant@gmail.com> wrote:
> > BTW camlp4 provides a simple macro system through a dynamic syntax
> > extension. It features DEFINE, IFDEF, INDEF, and INCLUDE.
>
> Actually, I didn't meant C macros.
Yes, but these are somewhat better than C ones.
> > However camlp4 is itself a powerful way to manage the OCaml syntax as
> > AST through it's quotation system.
>
> That is what I meant. However, I want it as simple as possible.
>
> > Have you a simple specification of what you want for OCaml (or some examples).
>
> For instance, I woud have wanted
> (defun f x y z (+ x (* y z)))
DEFINE F(x, y, z) = x + y * z;;
F(F(1, 2, 3), 4, 5);;
[...]
> another example:
> (+ x y z)
>
> (plus (plus x y) z)
Yes the macro extension is to weak for that.
Here is a camlp4 snippet todo it:
$ cat macros.ml
open Camlp4.PreCast;;
AstFilters.register_str_item_filter begin
Ast.map_expr begin function
| <:expr@loc< \!+ ($tup:e$) >> ->
begin match Ast.list_of_expr e [] with
| [] | [_] -> assert false
| x :: xs -> List.fold_left (fun acc x -> <:expr@loc< $acc$ + $x$ >>) x xs
end
| <:expr< \!+ $e$ >> -> e
| e -> e
end
end#str_item;;
Can be used like that:
$ cat test_macros.ml
!+ (1, 2, 3, 4);;
$ camlp4o ./macros.cmo test_macros.ml
let _ = ((1 + 2) + 3) + 4
> I havent specified the macro system yet. I think I shoud wait untill i
> have written a reader. (And therefore specified an AST)
IMHO starting from the macro extension of camlp4 and write a new one
more powerful is a good way to go.
--
Nicolas Pouillard