1
|
(* An application that loads json provided input and produces Lustre
|
2
|
|
3
|
Usage:
|
4
|
lustrei -vhdl myvhdl.json
|
5
|
lustrei -scade myscademodel.json
|
6
|
will produce a lustre file that can be compiled and analyzed
|
7
|
|
8
|
VHDL is handled in a double way: as a backend and as an import language
|
9
|
In a first step, lustrei -vhdl -print myvhdl.json shall print the VHDL model in stdout
|
10
|
|
11
|
*)
|
12
|
open Yojson.Safe
|
13
|
open Mini_vhdl_to_lustre
|
14
|
open Vhdl_ast_utils
|
15
|
open Vhdl_ast_map
|
16
|
open Vhdl_ast
|
17
|
open Mini_vhdl_ast
|
18
|
open Mini_vhdl_ast_pp
|
19
|
open Vhdl_ast_pp
|
20
|
open Vhdl_ast_yojson
|
21
|
open Printf
|
22
|
open Printers
|
23
|
open Format
|
24
|
|
25
|
type input = VHDL | None
|
26
|
type output = Stdout | File
|
27
|
type mode = GenVHDL | GenMiniVHDL | GenLus
|
28
|
|
29
|
let input_mode = ref None
|
30
|
let output_mode = ref Stdout
|
31
|
let mode = ref GenLus
|
32
|
let output_file_name = ref ""
|
33
|
|
34
|
let set_input_mode m =
|
35
|
input_mode := m
|
36
|
let set_output_mode m =
|
37
|
output_mode := m
|
38
|
let set_output_file_name s =
|
39
|
set_output_mode File;
|
40
|
output_file_name := s
|
41
|
let set_mode m =
|
42
|
mode := m
|
43
|
|
44
|
let options = [
|
45
|
"-vhdl", Arg.Unit (fun _ -> set_input_mode VHDL), " parse VHDL Json as input";
|
46
|
"-print", Arg.Unit (fun _ -> set_output_mode Stdout), " print the output to stdout";
|
47
|
"-o", Arg.String set_output_file_name, "<file> prints the output to file";
|
48
|
"-gen_vhdl", Arg.Unit (fun _ -> set_mode GenVHDL), " generate VHDL model";
|
49
|
"-gen_minivhdl", Arg.Unit (fun _ -> set_mode GenMiniVHDL), " generate MiniVHDL model";
|
50
|
"-gen_lustre", Arg.Unit (fun _ -> set_mode GenLus), " generate lustre model (does nothing)"
|
51
|
]
|
52
|
|
53
|
let usage =
|
54
|
"lustrei -vhdl [OPTIONS] [JSON file] takes as input a VHDL model in the JSON format.\n"^
|
55
|
"Backends are either the VHDL code generator, the MiniVHDL code generator or the lustre code generator."
|
56
|
|
57
|
let output_result s =
|
58
|
match !output_mode with
|
59
|
| Stdout -> (
|
60
|
Format.printf "\n%s\n" s;
|
61
|
)
|
62
|
| File -> (
|
63
|
let oc = open_out !output_file_name in
|
64
|
Printf.fprintf oc "%s" s;
|
65
|
close_out oc;
|
66
|
)
|
67
|
|
68
|
let _ =
|
69
|
Arg.parse options (fun _ -> ()) usage;
|
70
|
match !input_mode with
|
71
|
| VHDL -> (
|
72
|
(* Load model with Yojson *)
|
73
|
(* let vhdl_json = from_file Sys.argv.(1) in *)
|
74
|
let vhdl_json = from_file Sys.argv.(Array.length (Sys.argv) -1) in
|
75
|
(* Create VHDL values *)
|
76
|
let vhdl = vhdl_file_t_of_yojson vhdl_json in
|
77
|
|
78
|
match vhdl with
|
79
|
Ok x -> (
|
80
|
(* Fold Op vhdl_expr_t values *)
|
81
|
let folded = replace_op_expr#vhdl_file_t x in
|
82
|
match !mode with
|
83
|
| GenVHDL -> (
|
84
|
let vhdl_value = show_vhdl_file_t folded in
|
85
|
output_result vhdl_value;
|
86
|
()
|
87
|
)
|
88
|
| GenMiniVHDL -> (
|
89
|
let mini_vhdl = to_mini_vhdl#vhdl_design_file_t folded.design_file in
|
90
|
let mini_vhdl_value = show_mini_vhdl_design_file_t mini_vhdl in
|
91
|
output_result mini_vhdl_value;
|
92
|
()
|
93
|
)
|
94
|
| GenLus -> (
|
95
|
let mini_vhdl = to_mini_vhdl#vhdl_design_file_t folded.design_file in
|
96
|
let program = to_lustre#mini_vhdl_design_file_t mini_vhdl in
|
97
|
(* Pretty print lustre value *)
|
98
|
Printers.pp_prog str_formatter program;
|
99
|
output_result (Format.flush_str_formatter ());
|
100
|
()
|
101
|
)
|
102
|
)
|
103
|
| Error e -> failwith (Format.sprintf "Error: %s\n" e)
|
104
|
)
|
105
|
| None -> ()
|