Release coccinelle-0.2.0
[bpt/coccinelle.git] / popl09 / asttopopl.ml
1 (*
2 * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen
3 * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix
4 * This file is part of Coccinelle.
5 *
6 * Coccinelle is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, according to version 2 of the License.
9 *
10 * Coccinelle is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * The authors reserve the right to distribute this or future versions of
19 * Coccinelle under other licenses.
20 *)
21
22
23 module Ast = Ast_cocci
24 module Past = Ast_popl
25
26 (* --------------------------------------------------------------------- *)
27
28 let term s inif =
29 let fail _ =
30 Pretty_print_cocci.statement "" s;
31 Format.print_newline();
32 failwith "complex statements not supported" in
33 match Ast.unwrap s with
34 Ast.Atomic(ast) ->
35 (match Ast.unwrap ast with
36 Ast.ExprStatement(_,_) -> Past.Atomic ast
37 | Ast.Exp(_) -> Past.Atomic ast
38 | Ast.Decl(_,_,_) -> Past.Atomic ast
39 | Ast.ReturnExpr(_,_,_) -> Past.Atomic ast
40 | Ast.MetaStmt(_,_,_,_) when inif -> Past.Atomic ast
41 | Ast.DisjRuleElem(_) -> Past.Atomic ast
42 | _ -> fail())
43 | _ -> fail()
44
45 let rec stm s =
46 match Ast.unwrap s with
47 Ast.Atomic(ast) -> Past.Term(term s false,dots_bef_aft s false)
48 | Ast.IfThen(header,body,aft) ->
49 Past.Term(
50 Past.IfThen(Past.Atomic header,term body true,aft),
51 dots_bef_aft s true)
52 | Ast.Disj(stm1::stm2::stmts) ->
53 List.fold_left
54 (function prev ->
55 function cur ->
56 Past.Or(Past.Seq(prev,Past.Empty),stm_list cur))
57 (Past.Or(stm_list stm1,stm_list stm2)) stmts
58 | Ast.Dots(dots,whencodes,_,_) ->
59 Past.DInfo
60 (List.fold_left
61 (function prev ->
62 function
63 Ast.WhenNot(a) -> Past.When(prev,stm_list a)
64 | _ -> failwith "only when != supported")
65 Past.Dots whencodes)
66 | Ast.Nest(stmt_dots,whencodes,false,_,_) ->
67 let nest = Past.Nest(stm_list stmt_dots) in
68 Past.DInfo
69 (List.fold_left
70 (function prev ->
71 function
72 Ast.WhenNot(a) -> Past.When(prev,stm_list a)
73 | _ -> failwith "only when != supported")
74 nest whencodes)
75 | _ ->
76 Pretty_print_cocci.statement "" s;
77 failwith "unsupported statement3"
78
79 and dots_bef_aft s inif =
80 match Ast.get_dots_bef_aft s with
81 Ast.AddingBetweenDots (brace_term,n) ->
82 Past.AddingBetweenDots (term brace_term inif,n)
83 | Ast.DroppingBetweenDots (brace_term,n) ->
84 Past.DroppingBetweenDots (term brace_term inif,n)
85 | Ast.NoDots -> Past.NoDots
86
87 and stm_list s =
88 match Ast.unwrap s with
89 Ast.DOTS(d) ->
90 List.fold_right
91 (function cur -> function rest -> Past.Seq(stm cur, rest))
92 d Past.Empty
93 | _ -> failwith "only DOTS handled"
94
95 let top s =
96 match Ast.unwrap s with
97 Ast.CODE(stmt_dots) -> stm_list stmt_dots
98 | _ -> failwith "only CODE handled"