Factor out param_decl and improvements to AST printing.
[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   | PVar of loc * id
40 and expr =
41   | EGoal of loc * goal
42   | ECall of loc * id * expr list
43   | ETactic of loc * id * expr list
44   | EVar of loc * id
45   | EList of loc * expr list
46   | ESubsts of loc * substs
47   | EConstant of loc * constant
48 and constant =
49   | CString of string
50 and goal = param_decl list * pattern list * expr list * code option
51 and param_decl = id
52 and id = string
53 and code = substs
54 and substs = subst list
55 and subst =
56   | SString of string
57   | SVar of id
58
59 let getvar env loc name =
60   try Env.find name env
61   with Not_found ->
62     failwithf "%a: variable ‘%s’ not found" string_loc loc name
63
64 let getgoal env loc name =
65   let expr =
66     try Env.find name env
67     with Not_found ->
68       failwithf "%a: goal ‘%s’ not found" string_loc loc name in
69   let goal =
70     match expr with
71     | EGoal (loc, goal) -> goal
72     | _ ->
73        failwithf "%a: tried to call ‘%s’ which is not a goal"
74          string_loc loc name in
75   goal
76
77 let rec to_constant env = function
78   | EConstant (loc, c) -> c
79
80   | EVar (loc, name) ->
81      let expr = getvar env loc name in
82      to_constant env expr
83
84   | ESubsts (loc, str) ->
85      CString (substitute env loc str)
86
87   | EList (loc, _) ->
88      failwithf "%a: list found where constant expression expected"
89        string_loc loc
90
91   | ECall (loc, name, _) ->
92      failwithf "%a: cannot use goal ‘%s’ in constant expression"
93        string_loc loc name
94
95   | ETactic (loc, name, _) ->
96      failwithf "%a: cannot use tactic ‘*%s’ in constant expression"
97        string_loc loc name
98
99   | EGoal (loc, _) ->
100      failwithf "%a: cannot use goal in constant expression"
101        string_loc loc
102
103 and substitute env loc substs =
104   let b = Buffer.create 13 in
105   List.iter (
106     function
107     | SString s -> Buffer.add_string b s
108     | SVar name ->
109        let expr = getvar env loc name in
110        match to_constant env expr with
111        | CString s -> Buffer.add_string b s
112   ) substs;
113   Buffer.contents b
114
115 module Substs = struct
116   type t = {
117       mutable elems : subst list; (* built in reverse order *)
118       curr : Buffer.t;            (* current string *)
119     }
120
121   let create () = { elems = []; curr = Buffer.create 13 }
122
123   let finalize t =
124     if Buffer.length t.curr > 0 then
125       t.elems <- SString (Buffer.contents t.curr) :: t.elems;
126     Buffer.clear t.curr
127
128   let get t = finalize t; List.rev t.elems
129
130   let add_char { curr } = Buffer.add_char curr
131   let add_string { curr} = Buffer.add_string curr
132   let add_var t id = finalize t; t.elems <- SVar id :: t.elems
133 end
134
135 let iter_with_commas
136     : out_channel -> (out_channel -> 'a -> unit) -> 'a list -> unit =
137   fun fp f xs ->
138   let comma = ref false in
139   List.iter (
140     fun x ->
141       if !comma then fprintf fp ", ";
142       comma := true;
143       f fp x
144   ) xs
145
146 let rec string_env () env =
147   let env = Env.bindings env in
148   String.concat "" (List.map (string_def ()) env)
149
150 and print_env fp env = output_string fp (string_env () env)
151
152 and string_def () (name, expr) =
153   match expr with
154   | EGoal (loc, goal) -> string_goal () (Some name, goal) ^ "\n"
155   | expr -> sprintf "let %s = %a\n" name string_expr expr;
156
157 and print_def fp name expr = output_string fp (string_def () (name, expr))
158
159 and string_goal () (name, (param_decls, patterns, exprs, code)) =
160   sprintf "goal%s (%s) = %s : %s%s"
161     (match name with None -> "" | Some name -> " " ^ name)
162     (String.concat ", " (List.map (string_param_decl ()) param_decls))
163     (String.concat ", " (List.map (string_pattern ()) patterns))
164     (String.concat ", " (List.map (string_expr ()) exprs))
165     (match code with None -> "" | Some code -> " = { ... }")
166
167 and string_param_decl () name = name
168
169 and string_pattern () = function
170   | PTactic (loc, name, params) ->
171      sprintf "*%s (%s)" name (String.concat ", "
172                                 (List.map (string_substs ()) params));
173   | PVar (loc, id) -> id
174
175 and print_pattern fp p = output_string fp (string_pattern () p)
176
177 and string_expr () = function
178   | EGoal (loc, goal) -> string_goal () (None, goal)
179   | ECall (loc, name, params) ->
180      sprintf "%s (%s)"
181        name (String.concat ", " (List.map (string_expr ()) params))
182   | ETactic (loc, name, params) ->
183      sprintf "*%s (%s)"
184        name (String.concat ", " (List.map (string_expr ()) params))
185   | EVar (loc, var) -> var
186   | EList (loc, xs) ->
187      sprintf "[%s]" (String.concat ", " (List.map (string_expr ()) xs))
188   | ESubsts (loc, s) -> string_substs () s
189   | EConstant (loc, c) -> string_constant () c
190
191 and print_expr fp expr = output_string fp (string_expr () expr)
192
193 and string_constant () = function
194   | CString s -> sprintf "%S" s
195
196 and print_constant fp c = output_string fp (string_constant () c)
197
198 and print_id = output_string
199
200 and string_substs () ss =
201   let ss =
202     List.map (
203       function
204       | SString s -> sprintf "%S" s
205       | SVar id -> id
206     ) ss in
207   (String.concat "+" ss)
208
209 and print_substs fp ss = output_string fp (string_substs () ss)
210
211 and print_code fp xs =
212   List.iter (
213     function
214     | SString s -> fprintf fp "%s" s
215     | SVar id -> fprintf fp "%%%s" id
216   ) xs