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.
24 let pure_cache = Hashtbl.create 13
26 let rec to_constant env = function
27 | Ast.EConstant (loc, c) -> c
30 let expr = Ast.getvar env loc name in
33 | ESubsts (loc, str) ->
34 CString (substitute env loc str)
37 failwithf "%a: list found where constant expression expected"
40 | ECall (loc, name, args) ->
41 let expr = Ast.getvar env loc name in
44 (* Goals don't return anything so they cannot be used in
45 * constant expressions. Use a function instead.
47 failwithf "%a: cannot use goal call ‘%s’ in shell expansion"
48 Ast.string_loc loc name
50 | EFuncDefn (loc, func) ->
51 to_constant env (call_function env loc name args func)
54 failwithf "%a: cannot use ‘%s’ in constant expression"
55 Ast.string_loc loc name
58 | ETacticCtor (loc, name, _) ->
59 failwithf "%a: cannot use tactic ‘%s’ in constant expression"
60 Ast.string_loc loc name
62 | EGoalDefn (loc, _) ->
63 failwithf "%a: cannot use goal in constant expression"
66 | EFuncDefn (loc, _) ->
67 failwithf "%a: cannot use function in constant expression"
70 | ETacticDefn (loc, _) ->
71 failwithf "%a: cannot use tactic in constant expression"
74 and substitute env loc substs =
75 let b = Buffer.create 13 in
78 | Ast.SString s -> Buffer.add_string b s
80 let expr = Ast.getvar env loc name in
81 match to_constant env expr with
82 | Ast.CString s -> Buffer.add_string b s
86 and to_shell_script env loc substs =
87 let b = Buffer.create 13 in
90 | Ast.SString s -> Buffer.add_string b s
92 let expr = Ast.getvar env loc name in
93 let s = expr_to_shell_string env expr in
98 and expr_to_shell_string env = function
99 | Ast.EConstant (loc, CString s) -> Filename.quote s
101 | EVar (loc, name) ->
102 let expr = Ast.getvar env loc name in
103 expr_to_shell_string env expr
105 | ESubsts (loc, str) ->
106 Filename.quote (substitute env loc str)
108 | EList (loc, exprs) ->
109 let strs = List.map (expr_to_shell_string env) exprs in
110 (* These are shell quoted so we can just concat them with space. *)
111 String.concat " " strs
113 | ECall (loc, name, args) ->
114 let expr = Ast.getvar env loc name in
117 (* Goals don't return anything so they cannot be used in
118 * shell script expansions. Use a function instead.
120 failwithf "%a: cannot use goal call ‘%s’ in shell expansion"
121 Ast.string_loc loc name
123 | EFuncDefn (loc, func) ->
124 expr_to_shell_string env (call_function env loc name args func)
127 failwithf "%a: cannot call ‘%s’ which is not a function"
128 Ast.string_loc loc name
131 (* Tactics expand to the first parameter. *)
132 | ETacticCtor (loc, _, []) -> Filename.quote ""
133 | ETacticCtor (loc, _, (arg :: _)) -> expr_to_shell_string env arg
135 | EGoalDefn (loc, _) ->
136 failwithf "%a: cannot use goal in shell expansion"
139 | EFuncDefn (loc, _) ->
140 failwithf "%a: cannot use function in shell expansion"
143 | ETacticDefn (loc, _) ->
144 failwithf "%a: cannot use tactic in shell expansion"
147 and run_code env loc code =
148 let code = prepare_code env loc code in
151 and run_code_to_string env loc code =
152 let code = prepare_code env loc code in
153 let chan = Unix.open_process_in code in
154 let b = Buffer.create 1024 in
157 Buffer.add_string b (input_line chan);
158 Buffer.add_char b '\n'
160 with End_of_file -> ());
161 let st = Unix.close_process_in chan in
164 | Unix.WEXITED i -> i
165 | Unix.WSIGNALED i ->
166 failwithf "%a: killed by signal %d" Ast.string_loc loc i
168 failwithf "%a: stopped by signal %d" Ast.string_loc loc i in
171 and run_code_to_string_list env loc code =
172 let code = prepare_code env loc code in
173 let chan = Unix.open_process_in code in
174 let lines = ref [] in
175 (try while true do lines := input_line chan :: !lines done
176 with End_of_file -> ());
177 let st = Unix.close_process_in chan in
180 | Unix.WEXITED i -> i
181 | Unix.WSIGNALED i ->
182 failwithf "%a: killed by signal %d" Ast.string_loc loc i
184 failwithf "%a: stopped by signal %d" Ast.string_loc loc i in
185 let lines = List.rev !lines in
188 and prepare_code env loc (code, quiet) =
189 let code = to_shell_script env loc code in
190 "source " ^ Filename.quote Cmdline.prelude_sh_file ^ "\n" ^
192 (if not quiet then "set -x\n" else "") ^
196 and evaluate_goal_arg env = function
197 | Ast.EVar (loc, name) ->
198 let expr = Ast.getvar env loc name in
199 evaluate_goal_arg env expr
201 | ESubsts (loc, str) ->
202 let str = substitute env loc str in
203 Ast.EConstant (loc, Ast.CString str)
205 | EList (loc, exprs) ->
206 Ast.EList (loc, List.map (evaluate_goal_arg env) exprs)
208 | ETacticCtor (loc, name, exprs) ->
209 Ast.ETacticCtor (loc, name, List.map (evaluate_goal_arg env) exprs)
211 | ECall (loc, name, args) ->
212 let expr = Ast.getvar env loc name in
215 (* Goals don't return anything so they cannot be used in
216 * goal args. Use a function instead.
218 failwithf "%a: cannot use goal call ‘%s’ in goal argument"
219 Ast.string_loc loc name
221 | EFuncDefn (loc, func) ->
222 call_function env loc name args func
225 failwithf "%a: cannot call ‘%s’ which is not a function"
226 Ast.string_loc loc name
232 | ETacticDefn _ as e -> e
234 (* Functions are only called from goal args or when substituting
235 * into a shell script or constant expression (this may change if we
236 * implement ‘:=’ assignment for variables). This evaluates a
237 * function by running the associated shell script and parsing
238 * the output as an expression, string or list of strings.
240 and call_function env loc name args (params, returning, pure, code) =
241 (* This is used to print the function in debug and error messages only. *)
243 sprintf "%s (%s) returning %s" name
244 (String.concat ", " (List.map (Ast.string_expr ()) args))
245 (match returning with RetExpr -> "expression"
246 | RetString -> "string"
247 | RetStrings -> "strings") in
248 Cmdline.debug "%a: running function %s" Ast.string_loc loc debug_func;
250 (* Evaluate function args. Must be done before updating the environment. *)
251 let args = List.map (evaluate_goal_arg env) args in
253 (* Create a new environment which maps the parameter names to
258 try List.combine params args
259 with Invalid_argument _ ->
260 failwithf "%a: calling function %s with wrong number of arguments, expecting %d args but got %d args"
261 Ast.string_loc loc debug_func
262 (List.length params) (List.length args) in
263 List.fold_left (fun env (k, v) -> Ast.Env.add k v env) env params in
265 if pure then call_function_pure env loc name returning code
266 else call_function_really env loc name returning code
268 and call_function_really env loc name returning code =
271 let r, b = run_code_to_string env loc code in
273 eprintf "*** function ‘%s’ failed with exit code %d\n" name r;
276 Parse.parse_expr (sprintf "function:%s" name) b
279 let r, b = run_code_to_string env loc code in
281 eprintf "*** function ‘%s’ failed with exit code %d\n" name r;
284 Ast.EConstant (loc, Ast.CString b)
287 let r, lines = run_code_to_string_list env loc code in
288 let strs = List.map (fun s -> Ast.EConstant (loc, Ast.CString s)) lines in
291 (* For pure functions, check if the function can be matched to
292 * a previously memoized result, but don't fail if this goes wrong.
294 and call_function_pure env loc name returning code =
296 try Some (to_shell_script env loc (fst code))
297 with Failure _ -> None in
299 | None -> call_function_really env loc name returning code
302 try Some (Hashtbl.find pure_cache code_key)
303 with Not_found -> None in
307 let expr = call_function_really env loc name returning code in
308 Hashtbl.add pure_cache code_key expr;