Release coccinelle-0.2.0rc1
[bpt/coccinelle.git] / popl / asttopopl.ml
1 module Ast = Ast_cocci
2 module Past = Ast_popl
3
4 (* --------------------------------------------------------------------- *)
5
6 let rec stm s =
7 match Ast.unwrap s with
8 Ast.Atomic(ast) ->
9 (match Ast.unwrap ast with
10 Ast.ExprStatement(_,_) -> Past.Term ast
11 | Ast.Exp(_) -> Past.Term ast
12 | Ast.Decl(_,_,_) -> Past.Term ast
13 | _ -> failwith "complex statements not supported")
14 | Ast.Disj(stm1::stm2::stmts) ->
15 List.fold_left
16 (function prev ->
17 function cur ->
18 Past.Or(Past.Seq(prev,Past.Empty),stm_list cur))
19 (Past.Or(stm_list stm1,stm_list stm2)) stmts
20 | Ast.Dots(dots,whencodes,_,_) ->
21 (match whencodes with
22 [Ast.WhenNot(a)] -> Past.DInfo(Past.When(Past.Dots,stm_list a),[],[])
23 | _ -> failwith "only one when != supported")
24 | Ast.Nest(stmt_dots,whencodes,false,_,_) ->
25 let nest = Past.Nest(stm_list stmt_dots) in
26 (match whencodes with
27 [Ast.WhenNot(a)] -> Past.DInfo(Past.When(nest,stm_list a),[],[])
28 | _ -> failwith "only when != supported")
29 | Ast.While(header,body,(_,_,_,aft)) | Ast.For(header,body,(_,_,_,aft)) ->
30 (* only allowed if only the header is significant *)
31 (match (Ast.unwrap body,aft) with
32 (Ast.Atomic(re),Ast.CONTEXT(_,Ast.NOTHING)) ->
33 (match Ast.unwrap re with
34 Ast.MetaStmt(_,Type_cocci.Unitary,_,false) -> Past.Term header
35 | _ -> failwith "unsupported statement1")
36 | _ -> failwith "unsupported statement2")
37 | _ ->
38 Pretty_print_cocci.statement "" s;
39 failwith "unsupported statement3"
40
41 and stm_list s =
42 match Ast.unwrap s with
43 Ast.DOTS(d) ->
44 List.fold_right
45 (function cur -> function rest -> Past.Seq(stm cur, rest))
46 d Past.Empty
47 | _ -> failwith "only DOTS handled"
48
49 let top s =
50 match Ast.unwrap s with
51 Ast.CODE(stmt_dots) -> stm_list stmt_dots
52 | _ -> failwith "only CODE handled"