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.
22 let rec evaluate_targets env exprs =
23 List.iter (evaluate_target env) exprs
25 and evaluate_target env = function
26 | Ast.EGoal _ -> assert false
29 | Ast.ECall (loc, name, args) ->
30 let goal = Ast.getgoal env loc name in
31 run_goal env loc name args goal
33 | Ast.ETactic (loc, name, args) ->
34 (* All parameters of tactics must be simple constant expressions
35 * (strings, in future booleans, numbers, etc).
37 let args = List.map (Ast.to_constant env) args in
38 run_tactic env loc name args
40 (* Look up the variable and substitute it. *)
41 | Ast.EVar (loc, name) ->
42 let expr = Ast.getvar env loc name in
43 evaluate_target env expr
45 (* Lists are inlined when found as a target. *)
46 | Ast.EList (loc, exprs) ->
47 evaluate_targets env exprs
49 (* A string (with or without substitutions) implies *file(filename). *)
50 | Ast.ESubsts (loc, str) ->
51 let str = Ast.substitute env loc str in
52 run_tactic env loc "file" [Ast.CString str]
54 | Ast.EConstant (loc, c) ->
55 run_tactic env loc "file" [c]
57 (* Run a goal by name. *)
58 and run_goal env loc name args (params, patterns, deps, code) =
59 (* Create a new environment which maps the parameter names to
64 try List.combine params args
65 with Invalid_argument _ ->
66 failwithf "%a: calling goal ā%sā with wrong number of arguments"
67 Ast.string_loc loc name in
68 List.fold_left (fun env (k, v) -> Ast.Env.add k v env) env params in
70 (* Check all dependencies have been updated. *)
71 evaluate_targets env deps;
73 (* Check if any target (ie. pattern) needs to be rebuilt. *)
75 List.exists (needs_rebuild env loc name deps) patterns in
78 (* Run the code (if any). *)
82 (* Add some standard variables to the environment. *)
83 let expr_of_substs s = Ast.ESubsts (Ast.noloc, s) in
84 let expr_of_pattern = function
85 | Ast.PTactic (loc, tactic, targs) ->
86 Ast.ETactic (loc, tactic, List.map expr_of_substs targs)
87 | Ast.PVar (loc, name) ->
90 let pexprs = List.map expr_of_pattern patterns in
91 let env = Ast.Env.add "@" (Ast.EList (Ast.noloc, pexprs)) env in
92 let env = Ast.Env.add "<" (Ast.EList (Ast.noloc, deps)) env in
96 | d :: _ -> Ast.Env.add "^" d env in
97 let code = Ast.to_shell_script env loc code in
98 Printf.printf "running : %s\n" code
101 (* Check all targets were updated (else it's an error). *)
102 let pattern_still_needs_rebuild =
103 try Some (List.find (needs_rebuild env loc name deps) patterns)
104 with Not_found -> None in
105 match pattern_still_needs_rebuild with
108 failwithf "%a: goal ā%sā ran successfully but it did not rebuild %a"
111 Ast.string_pattern pattern
114 (* Return whether the target (pattern) needs to be rebuilt. *)
115 and needs_rebuild env loc name deps pattern =
117 | Ast.PTactic (loc, tactic, targs) ->
118 (* Resolve the targs down to constants. *)
119 let targs = List.map ((* XXX Ast.to_shell_script *)
120 Ast.substitute env loc) targs in
121 (* XXX Look up the tactic.
122 * We would do that, but for now hard code the *file tactic. XXX
124 assert (tactic = "file");
125 assert (List.length targs = 1);
126 let targ = List.hd targs in
127 not (Sys.file_exists targ)
128 | Ast.PVar _ -> assert false (* XXX not implemented *)
130 (* Find the goal which matches the given tactic and run it.
131 * cargs is a list of parameters (all constants).
133 and run_tactic env loc tactic cargs =
134 (* Find all goals in the environment. Returns a list of (name, goal). *)
136 let env = Ast.Env.bindings env in
139 | name, Ast.EGoal (loc, goal) -> Some (name, goal)
142 (* Find all patterns. Returns a list of (pattern, name, goal). *)
143 let patterns : (Ast.pattern * Ast.id * Ast.goal) list =
145 (List.map (fun (name, ((_, patterns, _, _) as goal)) ->
146 List.map (fun pattern -> (pattern, name, goal)) patterns) goals) in
148 (* Find any patterns (ie. tactics) which match the one we
149 * are searching for. This returns a binding for the goal args,
150 * so we end up with a list of (pattern, name, goal, args).
152 let patterns : (Ast.pattern * Ast.id * Ast.goal * Ast.expr list) list =
154 fun (pattern, name, ((params, _, _, _) as goal)) ->
155 match matching_pattern env loc tactic cargs pattern params with
157 | Some args -> Some (pattern, name, goal, args)
160 let _, name, goal, args =
164 let t = Ast.ETactic (loc, tactic,
165 List.map (fun c -> Ast.EConstant (loc, c))
167 failwithf "%a: don't know how to build %a"
168 Ast.string_loc loc Ast.string_expr t
170 (* If there are multiple matching goals, then assuming the goals
171 * are different we must pick the one which was defined last in
172 * the file. However we don't do that right now. XXX
174 assert false (* TODO! *) in
176 run_goal env loc name args goal
178 (* Test if pattern matches *tactic(cargs). If it does
179 * then we return Some args where args is the arguments that must
180 * be passed to the matching goal. The params parameter is
181 * the names of the parameters of that goal.
183 and matching_pattern env loc tactic cargs pattern params =
185 | Ast.PVar (loc, name) -> assert false (* TODO! *)
186 | Ast.PTactic (loc, ttactic, targs)
187 when tactic <> ttactic ||
188 List.length cargs <> List.length targs ->
189 None (* Can't possibly match if tactic name or #args is different. *)
190 | Ast.PTactic (loc, ttactic, targs) ->
191 (* Do the args match with a possible params binding? *)
192 try Some (matching_params env loc params targs cargs)
193 with Not_found -> None
195 (* Return a possible binding. For example the goal is:
196 * goal compile (name) = "%name.o": "%name.c" {}
197 * which means that params = ["name"] and targs = ["%name.o"].
199 * If we are called with cargs = ["file1.o"], we would
202 * On non-matching this raises Not_found.
204 and matching_params env loc params targs cargs =
205 (* This is going to record the resulting binding. *)
206 let res = ref Ast.Env.empty in
207 List.iter2 (matching_param env loc params res) targs cargs;
209 (* Rearrange the result into goal parameter order. Also this
210 * checks that every parameter got a binding.
214 (* Allow the Not_found exception to escape if no binding for this param. *)
215 fun param -> Ast.Env.find param res
218 (* If targ = "%name.o" and carg = "file.o" then this would set
219 * name => "file" in !res. If they don't match, raises Not_found.
221 and matching_param env loc params res targ carg =
223 | Ast.CString carg ->
224 (* Substitute any non parameters in targ from the environment. *)
228 | Ast.SString _ as s -> s
230 if not (List.mem name params) then (
232 let expr = Ast.getvar env loc name in
233 match Ast.to_constant env expr with
234 | Ast.CString s -> Ast.SString s
235 with Failure _ -> raise Not_found
241 (* Do the actual pattern matching. Any remaining SVar elements
242 * must refer to goal parameters.
244 let carg = ref carg in
245 let rec loop = function
247 (* End of targ, we must have matched all of carg. *)
248 if !carg <> "" then raise Not_found
249 | Ast.SString s :: rest ->
250 (* Does this match the first part of !carg? *)
251 let clen = String.length !carg in
252 let slen = String.length s in
253 if slen > clen || s <> String.sub !carg 0 slen then
255 (* Yes, so continue after the matching prefix. *)
256 carg := String.sub !carg slen (clen-slen);
258 | Ast.SVar name :: Ast.SString s :: rest ->
259 (* This is a goal parameter. Find s later in !carg. *)
260 let i = string_find !carg s in
261 if i = -1 then raise Not_found;
262 (* Set the binding in !res. *)
263 let r = Ast.EConstant (Ast.noloc,
264 Ast.CString (String.sub !carg 0 i)) in
265 res := Ast.Env.add name r !res;
266 (* Continue after the match. *)
267 let skip = i + String.length s in
268 carg := String.sub !carg skip (String.length !carg - skip);
270 | Ast.SVar name :: [] ->
271 (* Matches the whole remainder of the string. *)
272 let r = Ast.EConstant (Ast.noloc, Ast.CString !carg) in
273 res := Ast.Env.add name r !res
274 | Ast.SVar x :: Ast.SVar y :: _ ->
275 (* TODO! We cannot match a target like "%x%y". *)