1 (* Goalfile Abstract Syntax Tree
2 * Copyright (C) 2019 Richard W.M. Jones
3 * Copyright (C) 2019 Red Hat Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program 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.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 | Goal of id * id list * pattern list * expr list * code option
27 | PTactic of id * substs list
30 | ECall of id * expr list
36 and substs = subst list
41 module Substs = struct
43 mutable elems : subst list; (* built in reverse order *)
44 curr : Buffer.t; (* current string *)
47 let create () = { elems = []; curr = Buffer.create 13 }
50 if Buffer.length t.curr > 0 then
51 t.elems <- SString (Buffer.contents t.curr) :: t.elems;
54 let get t = finalize t; List.rev t.elems
56 let add_char { curr } = Buffer.add_char curr
57 let add_string { curr} = Buffer.add_string curr
58 let add_var t id = finalize t; t.elems <- SVar id :: t.elems
62 : out_channel -> (out_channel -> 'a -> unit) -> 'a list -> unit =
64 let comma = ref false in
67 if !comma then fprintf fp ", ";
72 let rec print_file fp file =
73 List.iter (print_stmt fp) file
75 and print_stmt fp = function
77 fprintf fp "let %s = " name;
80 | Goal (name, params, patterns, exprs, code) ->
81 fprintf fp "goal %s (%s) =\n" name (String.concat ", " params);
83 iter_with_commas fp print_pattern patterns;
85 iter_with_commas fp print_expr exprs;
95 and print_pattern fp = function
96 | PTactic (name, params) ->
97 fprintf fp "%s (" name;
98 iter_with_commas fp print_substs params;
100 | PVarSubst id -> print_id fp id
102 and print_expr fp = function
103 | ECall (name, params) ->
104 fprintf fp "%s (" name;
105 iter_with_commas fp print_expr params;
107 | EVar var -> print_id fp var
108 | EString s -> print_substs fp s;
111 iter_with_commas fp print_expr xs;
114 and print_id = output_string
116 and print_substs fp xs =
120 | SString s -> sprintf "%S" s
123 fprintf fp "%s" (String.concat "+" xs)
125 and print_code fp xs =
128 | SString s -> fprintf fp "%s" s
129 | SVar id -> fprintf fp "%%%s" id