Project

General

Profile

Download (3.54 KB) Statistics
| Branch: | Tag: | Revision:
1
(* An application that loads json provided input and produces Lustre
2

    
3
Usage:
4
lustrei -vhdl myvhdl.json -gen_lustre
5
lustrei -scade myscademodel.json -gen_lustre
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
16
open Mini_vhdl_ast
17
open Mini_vhdl_ast_pp
18
open Vhdl_ast_pp
19
open Mini_vhdl_check
20
open Printf
21
open Printers
22
open Format
23

    
24
type input = VHDL | None
25
type output = Stdout | File
26
type mode = GenVHDL | GenMiniVHDL | GenLus | Check
27

    
28
let input_mode = ref None
29
let output_mode = ref Stdout
30
let mode = ref GenLus
31
let output_file_name = ref ""
32

    
33
let set_input_mode m =
34
  input_mode := m
35
let set_output_mode m =
36
  output_mode := m
37
let set_output_file_name s =
38
  set_output_mode File;
39
  output_file_name := s
40
let set_mode m =
41
  mode := m
42

    
43
let options = [
44
  "-vhdl", Arg.Unit (fun _ -> set_input_mode VHDL), " parse VHDL Json as input";
45
  "-print", Arg.Unit (fun _ -> set_output_mode Stdout), " print the output to stdout";
46
  "-o", Arg.String set_output_file_name, "<file>  prints the output to file";
47
  "-check", Arg.Unit (fun _ -> set_mode Check), " checks VHDL model correction";
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#visit_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
        | Check -> (
95
          let mini_vhdl = to_mini_vhdl#vhdl_design_file_t folded.design_file in
96
          let mini_vhdl_value = show_mini_vhdl_design_file_t mini_vhdl in
97
          output_result mini_vhdl_value;
98
          check_mini_vhdl to_mini_vhdl#get_db to_mini_vhdl#to_string_vhdl_name_t
99
        )
100
        | GenLus -> (
101
          let mini_vhdl = to_mini_vhdl#vhdl_design_file_t folded.design_file in
102
          let program = to_lustre#mini_vhdl_design_file_t mini_vhdl in
103
          (* Pretty print lustre value *)
104
          Printers.pp_prog str_formatter program;
105
          output_result (Format.flush_str_formatter ());
106
          ()
107
        )
108
      )
109
      | Error e -> failwith (Format.sprintf "Error: %s\n" e)
110
  )
111
  | None -> ()
(1-1/3)