Version française
Home     About     Download     Resources     Contact us    
Browse thread
Where are the AST specs?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ 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:
> 2007/3/19, Nicolas Pouillard <nicolas.pouillard@gmail.com>:
> > On 3/19/07, Loup Vaillant <loup.vaillant@gmail.com> wrote:
> > > 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:
> > [...]
>
> Looks promising. Now, I have more:
> (* x y z)
> (:: x y z [])
> etc. You see the pattern? I need a "fold" macro that writes those macros.

open Camlp4.PreCast;;
AstFilters.register_str_item_filter begin
  Ast.map_expr begin function
  | <:expr@loc< foldl($lid:op$, $e$) >> ->
      let rec foldl =
        function
        | [] -> assert false
        | [x] -> x
        | x :: xs -> <:expr@loc< $lid:op$ $foldl xs$ $x$ >>
      in foldl (List.rev (Ast.list_of_expr e []))
  | <:expr@loc< foldr($lid:op$, $e$) >> ->
      let rec foldr =
        function
        | [] -> assert false
        | [x] -> x
        | x :: xs -> <:expr@loc< $lid:op$ $x$ $foldr xs$ >>
      in foldr (Ast.list_of_expr e [])
  | e -> e
  end
end#str_item;;

$ cat test_macros.ml
let cons x xs = x :: xs;;

foldl(( + ), 1, 2, 3, 4);;
foldr(cons, 1, 2, 3, []);;

$ camlp4o ./macros.cmo test_macros.ml
let cons x xs = x :: xs
let _ = ((1 + 2) + 3) + 4
let _ = cons 1 (cons 2 (cons 3 []))

> Besides, I think my biggest challenge will probably be the automatic
> handling of "loc", and a beautiful anti quotation system.

You're right loc is something hard.
What's wrong with the current anti quotation system?

-- 
Nicolas Pouillard