Source file soc2yaml.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
(* Time-stamp: <modified the 28/07/2022 (at 14:28) by Erwan Jahier> *)

(* Author: Antony Zahran *)

open Printf
(*open Soc2cIdent*)
open SocNameC

let (f : Soc.key -> Soc.tbl -> string -> unit) = fun sk stbl basename ->

  let main_soc = SocUtils.find (Lxm.dummy "dfsdf") sk stbl in
  (*let str = SocUtils.string_of_soc main_soc in*)

  printf "%s.yaml file created\n%!" basename;

  let filename = SocNameC.get_filename main_soc in
  let ctx_name = SocNameC.get_ctx_name main_soc in
  let ctx_new = SocNameC.get_ctx_new main_soc in
  let inputs = SocNameC.get_inputs main_soc in
  let outputs = SocNameC.get_outputs main_soc in
  let variables = SocNameC.get_variables main_soc in
  let nodes = SocNameC.get_nodes main_soc in
  let instances = SocNameC.get_instances main_soc in
  let yaml_file = open_out (filename ^ ".yml") in
  let inst_of_var = List.init ((List.length inputs) + (List.length outputs)
                               + (List.length variables)) (fun _ -> ref 0) in
  let set_inst_of_var = fun i ->
    List.iter (fun v -> (List.nth inst_of_var v) := i.id) i.var_out
  in
  List.iter set_inst_of_var instances;
  let inst_to_inst = (List.init (List.length instances + 1) (fun _ -> ref [])) in
  let add_inst = fun id v ->
    if not (List.mem !(List.nth inst_of_var v) !(List.nth inst_to_inst id)) then
      (List.nth inst_to_inst id) := List.append !(List.nth inst_to_inst id) [!(List.nth inst_of_var v)]
  in
  let set_inst_to_inst = fun i ->
    List.iter (add_inst i.id) i.var_in
  in
  List.iter set_inst_to_inst instances;
  let (add_inst : variable_type -> unit) = fun v -> add_inst 0 v.id in 
 
  List.iter add_inst outputs;

  let channel_id = ref 0 in
  let channels = List.init (List.length instances + 1) (fun _ -> (ref [], ref [])) in
  let add_channel = fun x -> fun y ->
    fst (List.nth channels x) := List.append !(fst (List.nth channels x)) [!channel_id];
    snd (List.nth channels y) := List.append !(snd (List.nth channels y)) [!channel_id];
    channel_id := !channel_id + 1
  in
  List.iteri (fun x -> fun yl -> (List.iter (add_channel x) !yl)) inst_to_inst;

  let (var2yaml : variable_type -> unit) = fun i ->
    let s = "  - id: " ^ (string_of_int i.id)
            ^ "\n    name: " ^ i.name
            ^ "\n    type: " ^ i.var_type in
    fprintf yaml_file "%s\n" s
  in

  let (channel2yaml : unit -> unit) = fun () ->
    for i = 0 to (!channel_id - 1) do
      fprintf yaml_file "  - %i\n" i;
    done
  in

  let (main2yaml : unit -> unit) = fun () ->
    let f1 : variable_type -> string = fun x -> "    - " ^ (string_of_int x.id) ^ "\n" in
    let s_vi = String.concat "" (List.map f1 inputs) in 
    let s_vo = String.concat "" (List.map f1 outputs) in 
    let f2 : int -> string = fun x -> "    - " ^ (string_of_int x) ^ "\n" in
    let s_ci = String.concat "" (List.map f2 !(snd (List.nth channels 0))) in 
    let s_co = String.concat "" (List.map f2 !(fst (List.nth channels 0))) in
    let s = "  name: " ^ filename
            ^ "\n  ctx_type: " ^ ctx_name
            ^ "\n  ctx_new: " ^ ctx_new
            ^ "\n  var_in:\n" ^ s_vi
            ^ "  var_out:\n" ^ s_vo
            ^ "  ch_in:\n" ^ s_ci
            ^ "  ch_out:\n" ^ s_co in
    fprintf yaml_file "%s\n" s
  in

  let node_id = ref 0
  in
  let (node2yaml : node_type -> unit) = fun n ->
    let s = "  - id: " ^ (string_of_int !node_id)
            ^ "\n    file_name: " ^ n.file_name
            ^ "\n    fct_name: " ^ n.fct_name
            ^ "\n    ctx: " ^ (string_of_bool n.ctx) in
    let s_ctx = if n.ctx then "\n    ctx_tab: " ^ n.ctx_tab else "" in
    node_id := !node_id + 1;
    fprintf yaml_file "%s\n\n" (s ^ s_ctx)
  in

  let inst_id = (List.init (List.length nodes) (fun _ -> ref 0)) in
  let (instance2yaml : instance_type -> unit) = fun i ->
    let f1 : int -> string = fun x -> "    - " ^ (string_of_int x) ^ "\n" in
    let s_vi = String.concat "" (List.map f1 i.var_in) in 
    let s_vo = String.concat "" (List.map f1 i.var_out) in 
    let f2 : int -> string = fun x -> "    - " ^ (string_of_int x) ^ "\n" in
    let s_ci = String.concat "" (List.map f2 !(fst (List.nth channels i.id))) in 
    let s_co = String.concat "" (List.map f2 !(snd (List.nth channels i.id))) in
    let s = "  - id: " ^ (string_of_int !(List.nth inst_id i.node))
            ^ "\n    node: " ^ (string_of_int i.node)
            ^ "\n    var_in:\n" ^ s_vi
            ^ "    var_out:\n" ^ s_vo
            ^ "    ch_in:\n" ^ s_ci
            ^ "    ch_out:\n" ^ s_co in
    (List.nth inst_id i.node) := !(List.nth inst_id i.node) + 1;
    fprintf yaml_file "%s\n" s
  in
  fprintf yaml_file "variables:\n";
  List.iter var2yaml (inputs);
  List.iter var2yaml (outputs);
  List.iter var2yaml (variables);

  fprintf yaml_file "\nchannels:\n";
  channel2yaml ();

  fprintf yaml_file "\nmain:\n";
  main2yaml ();

  fprintf yaml_file "nodes:\n";
  List.iter node2yaml nodes;

  fprintf yaml_file "instances:\n";
  List.iter instance2yaml instances;

  close_out yaml_file