-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.ml
170 lines (152 loc) · 8.08 KB
/
main.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(****************************************************************************)
(* BSD 2-Clause License *)
(* *)
(* Copyright (c) 2020-2021 Alasdair Armstrong *)
(* Copyright (c) 2020-2021 Brian Campbell *)
(* Copyright (c) 2020-2021 Thibaut Pérami *)
(* Copyright (c) 2020-2021 Peter Sewell *)
(* Copyright (c) 2020-2021 Dhruv Makwana *)
(* Copyright (c) 2021 Michael Sammler *)
(* Copyright (c) 2021 Rodolphe Lepigre *)
(* *)
(* All rights reserved. *)
(* *)
(* This software was developed by the University of Cambridge Computer *)
(* Laboratory (Department of Computer Science and Technology) and *)
(* contributors, in part under DARPA/AFRL contract FA8650-18-C-7809 *)
(* ("CIFV"), in part funded by EPSRC Programme Grant EP/K008528/1 "REMS: *)
(* Rigorous Engineering for Mainstream Systems", in part funded from the *)
(* European Research Council (ERC) under the European Union’s Horizon *)
(* 2020 research and innovation programme (grant agreement No 789108, *)
(* "ELVER"), in part supported by the UK Government Industrial Strategy *)
(* Challenge Fund (ISCF) under the Digital Security by Design (DSbD) *)
(* Programme, to deliver a DSbDtech enabled digital platform (grant *)
(* 105694), and in part funded by Google. *)
(* *)
(* *)
(* Redistribution and use in source and binary forms, with or without *)
(* modification, are permitted provided that the following conditions are *)
(* met: *)
(* *)
(* 1. Redistributions of source code must retain the above copyright *)
(* notice, this list of conditions and the following disclaimer. *)
(* *)
(* 2. Redistributions in binary form must reproduce the above copyright *)
(* notice, this list of conditions and the following disclaimer in the *)
(* documentation and/or other materials provided with the distribution. *)
(* *)
(* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *)
(* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *)
(* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *)
(* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *)
(* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *)
(* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *)
(* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *)
(* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *)
(* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *)
(* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *)
(* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *)
(* *)
(****************************************************************************)
open Isla_lang
type ast = AST.lrng AST.trcs
type tree_ast = AST.lrng AST.whole_tree
let pp_pretty : out_channel -> ast -> unit = fun oc ast ->
PPrint.ToChannel.compact oc (PP.pp_trcs ast)
let pp_pretty_tree : out_channel -> tree_ast -> unit = fun oc ast ->
PPrint.ToChannel.compact oc (PP.pp_whole_tree ast)
let pp_raw : out_channel -> ast -> unit = fun oc ast ->
PPrint.ToChannel.compact oc (PP.pp_raw_trcs ast)
let pp_raw_tree : out_channel -> tree_ast -> unit = fun oc ast ->
PPrint.ToChannel.compact oc (PP.pp_raw_whole_tree ast)
let pp_position : out_channel -> Lexing.position -> unit = fun oc pos ->
PPrint.ToChannel.compact oc (AST.pp_lpos pos)
let with_parse_errors f lexbuf =
try
f lexbuf
with
| Lexer.Error(s) -> Printf.printf "%s" s; exit 1
| Parser.Error ->
pp_position stdout (Lexing.lexeme_start_p lexbuf);
print_newline ();
exit 1
let parse : Lexing.lexbuf -> ast = fun lexbuf ->
with_parse_errors (Parser.trcs_start Lexer.token) lexbuf
let parse_tree : Lexing.lexbuf -> tree_ast = fun lexbuf ->
with_parse_errors (Parser.whole_tree_start Lexer.token) lexbuf
let run_batch : in_channel -> unit = fun ic ->
let lexbuf = Lexing.from_channel ic in
try
let ast = parse lexbuf in
Printf.printf "Raw version:\n %a\n\n" pp_raw ast;
Printf.printf "Pretty version:\n %a\n" pp_pretty ast;
with
| Lexer.Error(s) -> Printf.printf "%s" s; exit 1
| Parser.Error -> Printf.printf "Parse error.\n"; exit 1
let run_batch_tree : in_channel -> unit = fun ic ->
let lexbuf = Lexing.from_channel ic in
try
let ast = parse_tree lexbuf in
Printf.printf "Raw version:\n %a\n\n" pp_raw_tree ast;
Printf.printf "Pretty version:\n %a\n" pp_pretty_tree ast;
with
| Lexer.Error(s) -> Printf.printf "%s" s; exit 1
| Parser.Error -> Printf.printf "Parse error.\n"; exit 1
let run_interactive : unit -> unit = fun _ ->
let prompt = "isla-lang> " in
let process_line line =
if String.length (String.trim line) = 0 then () else
let lexbuf = Lexing.from_string line in
try
let ast = parse lexbuf in
Printf.printf "Raw: %a\n" pp_raw ast;
Printf.printf "Pretty: %a\n" pp_pretty ast;
with
| Lexer.Error(s) -> Printf.printf "%s" s; exit 1
| Parser.Error ->
let padding = String.length prompt + Lexing.lexeme_start lexbuf in
print_string (String.make padding ' ');
pp_position stdout (Lexing.lexeme_start_p lexbuf);
exit 1
in
try while true do
Printf.printf "%s%!" prompt;
process_line (read_line ())
done with End_of_file -> ()
let run_interactive_tree : unit -> unit = fun _ ->
let prompt = "isla-lang> " in
let process_line line =
if String.length (String.trim line) = 0 then () else
let lexbuf = Lexing.from_string line in
let ast = parse_tree lexbuf in
Printf.printf "Raw: %a\n" pp_raw_tree ast;
Printf.printf "Pretty: %a\n" pp_pretty_tree ast;
in
try while true do
Printf.printf "%s%!" prompt;
process_line (read_line ())
done with End_of_file -> ()
let with_file : string -> (in_channel -> 'a) -> 'a = fun file fn ->
let ic = open_in file in
let res = fn ic in
close_in ic; res
let print_help : string -> unit = fun prog ->
Printf.printf "Usage: %s [-t] [-i|-h|-|FILE]\n" prog;
Printf.printf "Options:\n";
Printf.printf "\t-t\tParse traces as trees.\n";
Printf.printf "\t-i\tRun in interactive mode.\n";
Printf.printf "\t-h\tDisplay this help message.\n";
Printf.printf "\t-\tProcess standard input as an isla trace file.\n";
Printf.printf "\tFILE\tProcess FILE as an isla trace file.\n"
let _ =
let not_arg s = String.length s = 0 || s.[0] <> '-' in
match Sys.argv with
| [| _ ; "-i" |] -> run_interactive ()
| [| prog ; "-h" |] -> print_help prog
| [| _ ; "-" |] -> run_batch stdin
| [| _ ; f |] when not_arg f -> with_file f run_batch
| [| _ ; "-t" ; "-i" |] -> run_interactive_tree ()
| [| prog ; "-t" ; "-h" |] -> print_help prog
| [| _ ; "-t" ; "-" |] -> run_batch_tree stdin
| [| _ ; "-t" ; f |] when not_arg f -> with_file f run_batch_tree
| _ -> print_help Sys.argv.(0); exit 1