Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / tools / importer / main_lustre_importer.ml @ a33a345a

History | View | Annotate | Download (3.46 KB)

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_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 Mini_vhdl_check
22
open Printf
23
open Printers
24
open Format
25

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

    
30
let input_mode = ref None
31
let output_mode = ref Stdout
32
let mode = ref GenLus
33
let output_file_name = ref ""
34

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

    
45
let options = [
46
  "-vhdl", Arg.Unit (fun _ -> set_input_mode VHDL), " parse VHDL Json as input";
47
  "-print", Arg.Unit (fun _ -> set_output_mode Stdout), " print the output to stdout";
48
  "-o", Arg.String set_output_file_name, "<file>  prints the output to file";
49
  "-check", Arg.Unit (fun _ -> set_mode Check), " checks VHDL model correction";
50
  "-gen_vhdl", Arg.Unit (fun _ -> set_mode GenVHDL), " generate VHDL model";
51
  "-gen_minivhdl", Arg.Unit (fun _ -> set_mode GenMiniVHDL), " generate MiniVHDL model";
52
  "-gen_lustre", Arg.Unit (fun _ -> set_mode GenLus), " generate lustre model (does nothing)"
53
]
54

    
55
let usage =
56
  "lustrei -vhdl [OPTIONS] [JSON file] takes as input a VHDL model in the JSON format.\n"^
57
    "Backends are either the VHDL code generator, the MiniVHDL code generator or the lustre code generator."
58

    
59
let output_result s =
60
  match !output_mode with
61
  | Stdout -> (
62
    Format.printf "\n%s\n" s;
63
  )
64
  | File -> (
65
    let oc = open_out !output_file_name in
66
    Printf.fprintf oc "%s" s;
67
    close_out oc;
68
  )
69

    
70
let _ =
71
  Arg.parse options (fun _ -> ()) usage;
72
  match !input_mode with
73
  | VHDL -> (
74
    (* Load model with Yojson *)
75
    (* let vhdl_json = from_file Sys.argv.(1) in *)
76
    let vhdl_json = from_file Sys.argv.(Array.length (Sys.argv) -1) in
77
    (* Create VHDL values *)
78
    let vhdl = vhdl_file_t_of_yojson vhdl_json in
79

    
80
    match vhdl with
81
      Ok x -> (
82
        (* Fold Op vhdl_expr_t values *)
83
        let folded = replace_op_expr#vhdl_file_t x in
84
        match !mode with
85
        | GenVHDL -> (
86
          let vhdl_value = show_vhdl_file_t folded in
87
          output_result vhdl_value;
88
          ()
89
         )
90
        | GenMiniVHDL -> (
91
          let mini_vhdl = to_mini_vhdl#vhdl_design_file_t folded.design_file in
92
          let mini_vhdl_value = show_mini_vhdl_design_file_t mini_vhdl in
93
          output_result mini_vhdl_value;
94
          ()
95
        )
96
        | Check -> (
97
          let mini_vhdl = to_mini_vhdl#vhdl_design_file_t folded.design_file in
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 -> ()