Remove Ast.PVar.
[goals.git] / src / ast.ml
1 (* Goalfile Abstract Syntax Tree
2  * Copyright (C) 2019 Richard W.M. Jones
3  * Copyright (C) 2019 Red Hat Inc.
4  *
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.
9  *
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.
14  *
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.
18  *)
19
20 open Lexing
21 open Printf
22
23 open Utils
24
25 module Env = Map.Make (String)
26
27 type loc = position * position
28 let noloc = dummy_pos, dummy_pos
29
30 let string_loc () loc =
31   let pos = fst loc in
32   sprintf "%s:%d:%d" pos.pos_fname pos.pos_lnum (pos.pos_cnum - pos.pos_bol)
33 let print_loc fp loc =
34   fprintf fp "%s" (string_loc () loc)
35
36 type env = expr Env.t
37 and pattern =
38   | PTactic of loc * id * substs list
39 and expr =
40   | EGoal of loc * goal
41   | ECall of loc * id * expr list
42   | ETactic of loc * id * expr list
43   | EVar of loc * id
44   | EList of loc * expr list
45   | ESubsts of loc * substs
46   | EConstant of loc * constant
47 and constant =
48   | CString of string
49 and goal = param_decl list * pattern list * expr list * code option
50 and param_decl = id
51 and id = string
52 and code = substs
53 and substs = subst list
54 and subst =
55   | SString of string
56   | SVar of id
57
58 let getvar env loc name =
59   try Env.find name env
60   with Not_found ->
61     failwithf "%a: variable ‘%s’ not found" string_loc loc name
62
63 let getgoal env loc name =
64   let expr =
65     try Env.find name env
66     with Not_found ->
67       failwithf "%a: goal ‘%s’ not found" string_loc loc name in
68   let goal =
69     match expr with
70     | EGoal (loc, goal) -> goal
71     | _ ->
72        failwithf "%a: tried to call ‘%s’ which is not a goal"
73          string_loc loc name in
74   goal
75
76 let rec to_constant env = function
77   | EConstant (loc, c) -> c
78
79   | EVar (loc, name) ->
80      let expr = getvar env loc name in
81      to_constant env expr
82
83   | ESubsts (loc, str) ->
84      CString (substitute env loc str)
85
86   | EList (loc, _) ->
87      failwithf "%a: list found where constant expression expected"
88        string_loc loc
89
90   | ECall (loc, name, _) ->
91      failwithf "%a: cannot use goal ‘%s’ in constant expression"
92        string_loc loc name
93
94   | ETactic (loc, name, _) ->
95      failwithf "%a: cannot use tactic ‘*%s’ in constant expression"
96        string_loc loc name
97
98   | EGoal (loc, _) ->
99      failwithf "%a: cannot use goal in constant expression"
100        string_loc loc
101
102 and substitute env loc substs =
103   let b = Buffer.create 13 in
104   List.iter (
105     function
106     | SString s -> Buffer.add_string b s
107     | SVar name ->
108        let expr = getvar env loc name in
109        match to_constant env expr with
110        | CString s -> Buffer.add_string b s
111   ) substs;
112   Buffer.contents b
113
114 let rec to_shell_script env loc substs =
115   let b = Buffer.create 13 in
116   List.iter (
117     function
118     | SString s -> Buffer.add_string b s
119     | SVar name ->
120        let expr = getvar env loc name in
121        let s = expr_to_shell_string env expr in
122        Buffer.add_string b s
123   ) substs;
124   Buffer.contents b
125
126 and expr_to_shell_string env = function
127   | EConstant (loc, CString s) -> Filename.quote s
128
129   | EVar (loc, name) ->
130      let expr = getvar env loc name in
131      expr_to_shell_string env expr
132
133   | ESubsts (loc, str) ->
134      Filename.quote (substitute env loc str)
135
136   | EList (loc, exprs) ->
137      let strs = List.map (expr_to_shell_string env) exprs in
138      (* These are shell quoted so we can just concat them with space. *)
139      String.concat " " strs
140
141   | ECall (loc, name, _) ->
142      failwithf "%a: cannot use goal ‘%s’ in shell expansion"
143        string_loc loc name
144
145   (* Tactics expand to the first parameter. *)
146   | ETactic (loc, _, []) -> Filename.quote ""
147   | ETactic (loc, _, (arg :: _)) -> expr_to_shell_string env arg
148
149   | EGoal (loc, _) ->
150      failwithf "%a: cannot use goal in shell expansion"
151        string_loc loc
152
153 module Substs = struct
154   type t = {
155       mutable elems : subst list; (* built in reverse order *)
156       curr : Buffer.t;            (* current string *)
157     }
158
159   let create () = { elems = []; curr = Buffer.create 13 }
160
161   let finalize t =
162     if Buffer.length t.curr > 0 then
163       t.elems <- SString (Buffer.contents t.curr) :: t.elems;
164     Buffer.clear t.curr
165
166   let get t = finalize t; List.rev t.elems
167
168   let add_char { curr } = Buffer.add_char curr
169   let add_string { curr} = Buffer.add_string curr
170   let add_var t id = finalize t; t.elems <- SVar id :: t.elems
171 end
172
173 let iter_with_commas
174     : out_channel -> (out_channel -> 'a -> unit) -> 'a list -> unit =
175   fun fp f xs ->
176   let comma = ref false in
177   List.iter (
178     fun x ->
179       if !comma then fprintf fp ", ";
180       comma := true;
181       f fp x
182   ) xs
183
184 let rec string_env () env =
185   let env = Env.bindings env in
186   String.concat "" (List.map (string_def ()) env)
187
188 and print_env fp env = output_string fp (string_env () env)
189
190 and string_def () (name, expr) =
191   match expr with
192   | EGoal (loc, goal) -> string_goal () (Some name, goal) ^ "\n"
193   | expr -> sprintf "let %s = %a\n" name string_expr expr;
194
195 and print_def fp name expr = output_string fp (string_def () (name, expr))
196
197 and string_goal () (name, (param_decls, patterns, exprs, code)) =
198   sprintf "goal%s (%s) = %s : %s%s"
199     (match name with None -> "" | Some name -> " " ^ name)
200     (String.concat ", " (List.map (string_param_decl ()) param_decls))
201     (String.concat ", " (List.map (string_pattern ()) patterns))
202     (String.concat ", " (List.map (string_expr ()) exprs))
203     (match code with None -> "" | Some code -> " = { ... }")
204
205 and string_param_decl () name = name
206
207 and string_pattern () = function
208   | PTactic (loc, name, params) ->
209      sprintf "*%s (%s)" name (String.concat ", "
210                                 (List.map (string_substs ()) params))
211
212 and print_pattern fp p = output_string fp (string_pattern () p)
213
214 and string_expr () = function
215   | EGoal (loc, goal) -> string_goal () (None, goal)
216   | ECall (loc, name, params) ->
217      sprintf "%s (%s)"
218        name (String.concat ", " (List.map (string_expr ()) params))
219   | ETactic (loc, name, params) ->
220      sprintf "*%s (%s)"
221        name (String.concat ", " (List.map (string_expr ()) params))
222   | EVar (loc, var) -> var
223   | EList (loc, xs) ->
224      sprintf "[%s]" (String.concat ", " (List.map (string_expr ()) xs))
225   | ESubsts (loc, s) -> string_substs () s
226   | EConstant (loc, c) -> string_constant () c
227
228 and print_expr fp expr = output_string fp (string_expr () expr)
229
230 and string_constant () = function
231   | CString s -> sprintf "%S" s
232
233 and print_constant fp c = output_string fp (string_constant () c)
234
235 and print_id = output_string
236
237 and string_substs () ss =
238   let ss =
239     List.map (
240       function
241       | SString s -> sprintf "%S" s
242       | SVar id -> id
243     ) ss in
244   (String.concat "+" ss)
245
246 and print_substs fp ss = output_string fp (string_substs () ss)
247
248 and print_code fp xs =
249   List.iter (
250     function
251     | SString s -> fprintf fp "%s" s
252     | SVar id -> fprintf fp "%%%s" id
253   ) xs