Project

General

Profile

Download (5.88 KB) Statistics
| Branch: | Tag: | Revision:
1
open Yojson
2
open Datatype
3
(* open Simulink *)
4
(* open Transformer *)
5
open Basetypes
6
open Basic
7
open Corelang
8
open CPS
9
open LustreSpec
10
open Str
11

    
12
module type ParseExt =
13
sig
14
  val parse_condition : json -> Condition.t
15
  val parse_action : json -> Action.t
16
  val parse_event : json -> Basetypes.event_t
17
end
18

    
19
module Parser (Ext : ParseExt) =
20
struct
21
  let path_split = String.split_on_char '/'
22
  let path_concat = String.concat (String.make 1 '/')
23

    
24
  open Util
25

    
26
  let to_list json =
27
    try
28
      json |> to_list
29
    with
30
      Type_error _ -> [ json ]
31

    
32
  let rec parse_prog json : prog_t =
33
     (*Format.printf "parse_prog@.";*)
34
    Program (
35
      json |> member "name"        |> to_string,
36
     (json |> member "states"      |> to_list |> List.map parse_state) @
37
     (json |> member "junctions"   |> to_list |> List.map parse_junction)
38
     @
39
     (json |> member "sffunctions" |> to_list |> List.map
40
        (fun res -> SFFunction (parse_prog res))),
41
      json |> member "data"        |> to_list |> List.map parse_variable
42
    )
43
  (* and parse_variables json = *)
44
  (*    (\*Format.printf "parse_variables@.";*\) *)
45
  (*   json |> member "data"       |> to_list |> List.map parse_variable *)
46
  and parse_state json =
47
    (*Format.printf "parse_state@.";*)
48
    State (
49
      json |> member "path" |> parse_path,
50
      json |> parse_state_def
51
    )
52
  and parse_path json =
53
      (*Format.printf "parse_path@.";*)
54
      json |> to_string |> path_split
55
  and parse_state_def json =
56
    (*Format.printf "parse_state_def@.";*)
57
    {
58
      state_actions        = json |> member "state_actions"        |> parse_state_actions;
59
      outer_trans          = json |> member "outer_trans"          |> to_list |> List.map parse_transition;
60
      inner_trans          = json |> member "inner_trans"          |> to_list |> List.map parse_transition;
61
      internal_composition = json |> member "internal_composition" |> parse_internal_composition
62
    }
63
  and parse_state_actions json =
64
    (*Format.printf "parse_state_actions@.";*)
65
    {
66
      entry_act  = json |> member "entry_act"  |> Ext.parse_action;
67
      during_act = json |> member "during_act" |> Ext.parse_action;
68
      exit_act   = json |> member "exit_act"   |> Ext.parse_action;
69
    }
70
  and parse_transition json =
71
    (*Format.printf "parse_transition@.";*)
72
    {
73
      event          = json |> member "event"          |> Ext.parse_event;
74
      condition      = json |> member "condition"      |> Ext.parse_condition;
75
      condition_act  = json |> member "condition_act"  |> Ext.parse_action;
76
      transition_act = json |> member "transition_act" |> Ext.parse_action;
77
      dest           = json |> member "dest"           |> parse_dest
78
    }
79
  and parse_dest json =
80
    (*Format.printf "parse_dest@.";*)
81
    (json |> member "type" |> to_string |>
82
	(function
83
	| "State"    -> (fun p -> DPath p)
84
	| "Junction" -> (fun j -> DJunction (path_concat j))
85
	| _ -> assert false))
86
      (json |> member "name" |> parse_path)
87
  and parse_internal_composition json =
88
    (*Format.printf "parse_internal_composition@.";*)
89
    (json |> member "type" |> to_string |>
90
	(function
91
	| "EXCLUSIVE_OR" -> (fun tinit substates ->                      Or  (tinit, substates))
92
	| "PARALLEL_AND" -> (fun tinit substates -> assert (tinit = []); And (substates))
93
	| _ -> assert false))
94
      (json |> member "tinit"     |> parse_tinit)
95
      (json |> member "substates" |> to_list |> List.map to_string)
96
  and parse_tinit json =
97
    (*Format.printf "parse_tinit@.";*)
98
    json |> to_list |> List.map parse_transition
99
  and parse_junction json =
100
    (*Format.printf "parse_junction@.";*)
101
    Junction (
102
      json |> member "path"        |> to_string,
103
      json |> member "outer_trans" |> to_list |> List.map parse_transition
104
    )
105
  and scope_of_string s =
106
    match s with
107
    | "Constant"  -> Constant
108
    | "Input"     -> Input
109
    | "Local"     -> Local
110
    | "Output"    -> Output
111
    | "Parameter" -> Parameter
112
    | _           -> failwith ("Invalid scope for variable: " ^ s)
113
  and parse_real_value s =
114
    let real_regexp_simp = regexp "-?\\([0-9][0-9]*\\)\\.\\([0-9]*\\)" in
115
    let real_regexp_e    = regexp "-?\\([0-9][0-9]*\\)\\.\\([0-9]*\\)(E|e)\\((\\+|\\-)[0-9][0-9]*\\)" in
116
    if string_match real_regexp_e s 0 then
117
      let l = matched_group 1 s in
118
      let r = matched_group 2 s in
119
      let e = matched_group 3 s in
120
      Const_real (Num.num_of_string (l ^ r),
121
                  String.length r + -1 * int_of_string e,
122
                  s)
123
    else
124
    if string_match real_regexp_simp s 0 then
125
      let l = matched_group 1 s in
126
      let r = matched_group 2 s in
127
      Const_real (Num.num_of_string (l ^ r), String.length r, s)
128
    else
129
      failwith ("Invalid real constant " ^ s)
130
  and lustre_datatype_of_json json location =
131
    let datatype      = json |> member "datatype"      |> to_string in
132
    let initial_value = json |> member "initial_value" |> to_string in
133
    match datatype with
134
    | "bool" -> (Tydec_bool, mkexpr location
135
                   (Expr_const (Const_bool
136
                                  (bool_of_string initial_value))))
137
    | "int"  -> (Tydec_int, mkexpr location
138
                   (Expr_const (Const_int (int_of_string
139
                                             initial_value))))
140
    | "real" -> (Tydec_real, mkexpr location
141
                   (Expr_const (parse_real_value initial_value)))
142
    | _      -> failwith ("Invalid datatype " ^ datatype
143
                          ^ " for variable " ^ (json |> member "name"
144
                                                |> to_string))
145
  and parse_variable json =
146
    (*Format.printf "parse_variable@.";*)
147
    let location                  = Location.dummy_loc in
148
    let (datatype, initial_value) = lustre_datatype_of_json json location in
149
    mkvar_decl location ~orig:true
150
      ( json |> member "name" |> to_string,
151
        {ty_dec_desc = datatype;  ty_dec_loc = location},
152
        {ck_dec_desc = Ckdec_any; ck_dec_loc = location},
153
        false,
154
        Some initial_value
155
      )
156
end
    (1-1/1)