Source file lv6parserUtils.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
(** *)
open Lxm
open AstCore
open AstPredef
type sx_VarDeclList = (((Lxm.t list) * type_exp) list * AstCore.clock_exp) list
type sx_Params = sx_VarDeclList
type sx_LocalVars = sx_VarDeclList
let (build_node_var : var_info srcflagged list -> var_info srcflagged list ->
var_info srcflagged list option -> node_vars) =
fun invars outvars locvars_opt ->
let get_var_name vif = vif.it.var_name in
{
inlist = List.map get_var_name invars;
outlist = List.map get_var_name outvars;
loclist = (
match locvars_opt with
| None -> None
| Some locvars -> Some (List.map get_var_name locvars)
);
vartable =
let tbl = Hashtbl.create 0 in
let add_var_in_tbl vif = Hashtbl.add tbl vif.it.var_name vif in
List.iter add_var_in_tbl invars;
List.iter add_var_in_tbl outvars;
(match locvars_opt with
| None -> ()
| Some locvars -> List.iter add_var_in_tbl locvars
);
tbl;
}
let flat_flagged_list
(inlist: ('a list * 'b) list)
(makeitem: 'a -> 'b -> 'c)
= (
let g (cl: 'c list) ((al: 'a list) , (b: 'b)) = (
let f (a: 'a) = makeitem a b in
List.append cl (List.map f al)
) in
List.fold_left g [] inlist
)
let _ = assert (
(flat_flagged_list
[ (["a1";"a2";"a3"], "b1") ; (["a4";"a5"], "b2") ]
(fun a b -> a ^ "-" ^ b))
=
["a1-b1"; "a2-b1"; "a3-b1"; "a4-b2"; "a5-b2"]
)
let flat_twice_flagged_list
(inlist: (('a list * 'b) list * 'c) list )
(makeitem: 'a -> 'b -> 'c -> 'd )
= (
let g (dl: 'd list) ((albl: ('a list * 'b) list), (c: 'c)) = (
let h (dl: 'd list) ((al: 'a list), (b: 'b)) = (
let f (a: 'a) = makeitem a b c in
List.append dl (List.map f al)
) in
List.fold_left h dl albl
) in
List.fold_left g [] inlist
)
let (name_table : (string, unit) Hashtbl.t) =
Hashtbl.create 0
let idref_of_lxm lxm =
let name = (Lxm.str lxm) in
if name.[0] = '_' then (
Hashtbl.add name_table name ());
try Lxm.flagit (Lv6Id.idref_of_string name) lxm
with _ ->
print_string ("Lv6parser.idref_of_lxm" ^(Lxm.str lxm));
assert false
(** Traitement des listes d'idents avec valeur éventuelle
(constantes, champs de struct etc...)
*)
let (lexeme_to_ident_flagged: Lxm.t -> Lv6Id.t Lxm.srcflagged) =
fun x -> {it = (Lxm.id x); src = x }
let (lexeme_to_val_exp_flagged: Lxm.t -> val_exp Lxm.srcflagged) =
fun x ->
let idref = idref_of_lxm x in
let ve = CallByPos({ it = IDENT_n idref.it ; src=x },Oper []) in
{it = ve; src = x }
let (lexeme_to_pack_name_flagged:Lxm.t -> Lv6Id.pack_name Lxm.srcflagged) =
fun x -> {it = (Lv6Id.pack_name_of_string (Lxm.str x)); src = x }
let (make_merge_bool_op : Lxm.t -> val_exp -> val_exp -> val_exp) =
fun enum_clk vet vef ->
Merge_bool_n(lexeme_to_val_exp_flagged enum_clk, vet, vef)
type bool_or_idref = Bool of bool | Idref of Lv6Id.idref
(** Utilitaries to build [val_exp] *)
let make_merge_op (enum_clk:Lxm.t) (l:(bool_or_idref * Lxm.t * val_exp) list) =
match l with
| [(Bool true ,_,vet); (Bool false,_,vef)]
| [(Bool false,_,vef); (Bool true ,_,vet)] ->
make_merge_bool_op enum_clk vet vef
| _ ->
let l = List.map
(fun (b_or_idref,lxm,ve) ->
match b_or_idref with
| Idref idref -> flagit idref lxm, ve
| Bool true
| Bool false ->
raise (
Lv6errors.Compile_error (enum_clk, "The merge mixes booleans and enums"))
)
l
in
Merge_n(lexeme_to_val_exp_flagged enum_clk, l)
let save_make_merge_op (enum_clk:Lxm.t) (l:(Lv6Id.idref srcflagged * val_exp) list) =
let l = List.map (fun (idref,ve) -> idref,ve) l in
Merge_n(lexeme_to_val_exp_flagged enum_clk, l)
let make_predef_posop lxm op =
let op = flagit op lxm in
{src = lxm ; it = Predef_n (op) }
let leafexp lxm op =
CallByPos({src = lxm ; it = op }, Oper [])
let leafexp_predef lxm op =
let op = flagit op lxm in
CallByPos({src = lxm ; it = Predef_n (op) }, Oper [])
let unexp lxm op e1 =
CallByPos( {src = lxm ; it = op }, Oper [e1] )
let unexp_predef lxm op e1 =
let op = flagit op lxm in
CallByPos( {src = lxm ; it = Predef_n (op) }, Oper [e1] )
let binexp lxm op e1 e2 =
CallByPos( {src = lxm ; it = op }, Oper [e1 ; e2] )
let binexp_predef lxm op e1 e2 =
let op = flagit op lxm in
CallByPos( {src = lxm ; it = Predef_n (op) }, Oper [e1 ; e2] )
let ternexp lxm op e1 e2 e3 = CallByPos( {src = lxm ; it = op }, Oper [e1 ; e2; e3] )
let ternexp_predef lxm op e1 e2 e3 =
let op = flagit op lxm in
CallByPos( {src = lxm ; it = Predef_n (op) }, Oper [e1 ; e2; e3] )
let naryexp lxm op elst =
CallByPos( {src = lxm ; it = op }, Oper elst )
let naryexp_predef lxm op elst =
let op = flagit op lxm in
CallByPos( {src = lxm ; it = Predef_n (op) }, Oper elst )
let bynameexp lxm op nelst = CallByName( {src = lxm ; it = op } , nelst )
(** add_info
-----------------------------------------------------------------------
Rôle :
proc générique pour mettre une info 'a dans
une table (Lv6Id.t, 'a srcflagged).
Effets de bord :
erreur de compil si déjà utilisé
*)
let (add_info : (Lv6Id.t, 'a srcflagged) Hashtbl.t ->
string ->
Lxm.t ->
'a ->
unit) =
fun htbl kindof lxm info ->
try
let x = Hashtbl.find htbl (Lxm.id lxm) in
raise (
Lv6errors.Compile_error (
lxm,
Printf.sprintf "bad %s declaration, ident already linked at %s" kindof
(Lxm.position x.src)
)
)
with Not_found ->
Hashtbl.add htbl (Lxm.id lxm) { src = lxm ; it = info }
let (const_table:(Lv6Id.t, const_info srcflagged) Hashtbl.t) = Hashtbl.create 50
let (type_table :(Lv6Id.t, type_info srcflagged) Hashtbl.t) = Hashtbl.create 50
let (node_table :(Lv6Id.t, node_info srcflagged) Hashtbl.t) = Hashtbl.create 50
let (def_list : item_ident list ref) = ref []
type id_valopt = (Lxm.t * type_exp * val_exp option)
let id_valopt_list_of_id_list (idlist : Lxm.t list) (texp : type_exp) =
let treat_id (id : Lxm.t) = (id, texp, None) in
List.map treat_id idlist
let id_valopt_of_id_val (id : Lxm.t) (texp : type_exp) (vexp : val_exp) =
(id, texp, Some vexp)
let make_external_const_list lst typ =
let f = function lxm -> (lxm, (ExternalConst ((Lxm.id lxm), typ, None)))
in List.map f lst
let make_defined_const lxm typ exp =
(lxm, (DefinedConst ((Lxm.id lxm) , typ, exp)))
let treat_const_decl_list clst =
let f = function (lxm, cinfo) ->
add_info const_table "constant" lxm cinfo;
def_list := (ConstItem (Lxm.id lxm)) :: !def_list
in
List.iter f clst
let (make_struct_type_info : Lxm.t -> id_valopt list ->
struct_type_info) =
fun typlxm flexlist ->
let ftab = Hashtbl.create 50 in
let (put_in_ftab : (Lxm.t * type_exp * val_exp option) -> Lv6Id.t) =
fun (lx, ty, va) ->
let lxstr = Lxm.id lx in
let fi = { fd_name = lxstr ; fd_type = ty ; fd_value = va } in
add_info ftab "field" lx fi;
lxstr
in
let flst = List.map put_in_ftab flexlist in
{ st_name = Lxm.id typlxm ; st_flist = flst ; st_ftable = ftab }
let treat_type_decl (typlxm, typinfo) = (
let typstr = Lxm.id typlxm in
add_info type_table "type" typlxm typinfo ;
def_list := (TypeItem typstr) :: !def_list
)
type typed_ids = (Lxm.t list * type_exp)
type clocked_ids = (typed_ids list * clock_exp)
let (clocked_ids_to_var_infos : var_nature ->
(((Lxm.t list) * type_exp) list * AstCore.clock_exp) list ->
var_info srcflagged list) =
fun vnat vdefs ->
let i = ref 0 in
let makevar lxm te ce =
let res =
Lxm.flagit
{
var_nature = vnat;
var_name = (Lxm.id lxm);
var_number = !i;
var_type = te;
var_clock = ce;
}
lxm
in
incr i;
res
in
flat_twice_flagged_list vdefs makevar
let treat_node_decl
(is_unsafe: bool)
(has_memory: bool)
(nlxm: Lxm.t)
(statics: static_param srcflagged list)
(indefs: clocked_ids list)
(outdefs: clocked_ids list)
(locdecls: clocked_ids list * (Lxm.t * const_info) list)
(asserts: (val_exp srcflagged) list)
(eqs: (eq_info srcflagged) list)
: unit =
let (locdefs, locconsts) = locdecls in
let vtable = Hashtbl.create 50 in
let rec (treat_vars : clocked_ids list -> var_nature -> var_info srcflagged list) =
fun vdefs nat ->
let i = ref 0 in
match vdefs with
| [] -> []
| (tids, ck)::reste ->
let put_var_in_table (lxm: Lxm.t) (ty: type_exp) =
let vinfo = {
var_nature = nat; var_name = (Lxm.id lxm);
var_type = ty; var_clock = ck; var_number = !i
}
in
incr i;
add_info vtable "variable" lxm vinfo;
Lxm.flagit vinfo lxm
in
(flat_flagged_list tids put_var_in_table)
@ (treat_vars reste nat)
in
let invars = treat_vars indefs VarInput
and outvars = treat_vars outdefs VarOutput
and locvars = treat_vars locdefs VarLocal
in
let vars = build_node_var invars outvars (Some locvars) in
let nstr = Lxm.id nlxm in
let ninfo = {
name = nstr;
static_params = statics;
vars = Some vars;
loc_consts = locconsts;
def = Body { asserts = asserts ; eqs = eqs };
has_mem = has_memory;
is_safe = not is_unsafe;
}
in
add_info node_table "node" nlxm ninfo;
def_list := (NodeItem (nstr,statics)) :: !def_list
let (treat_node_alias : bool -> bool -> Lxm.t -> static_param srcflagged list ->
(var_info srcflagged list * var_info srcflagged list) option ->
node_exp srcflagged -> unit) =
fun is_unsafe has_memory nlxm statics node_profile value ->
let nstr = Lxm.id nlxm in
let vars =
match node_profile with
| None -> None
| Some (invars,outvars) -> Some (build_node_var invars outvars None)
in
let ninfo = {
name = nstr;
static_params = statics;
vars = vars;
loc_consts = [];
def = Alias (flagit (CALL_n value) value.src);
has_mem = has_memory;
is_safe = not is_unsafe ;
}
in
add_info node_table "(alias) node" nlxm ninfo;
def_list := (NodeItem (nstr,statics)) :: !def_list
let treat_abstract_or_extern_node_do
(is_unsafe: bool)
(has_memory: bool)
(lxm: Lxm.t)
(statics: static_param srcflagged list)
(inpars: sx_Params)
(outpars: sx_Params)
(is_abstract: bool)
: node_info =
let (invars, outvars : var_info srcflagged list * var_info srcflagged list) =
clocked_ids_to_var_infos VarInput inpars,
clocked_ids_to_var_infos VarOutput outpars
in
let vars = build_node_var invars outvars None in
let xn = {
name = Lxm.id lxm;
static_params = statics;
vars = Some vars;
loc_consts = [];
def = if is_abstract then Abstract else Extern;
has_mem = has_memory;
is_safe = not is_unsafe;
}
in
xn
let treat_abstract_node
(is_unsafe: bool)
(has_memory: bool)
(lxm: Lxm.t)
(statics: static_param srcflagged list)
(inpars: sx_Params)
(outpars: sx_Params)
: item_info Lxm.srcflagged =
Lxm.flagit
(NodeInfo (treat_abstract_or_extern_node_do is_unsafe has_memory lxm statics inpars outpars true))
lxm
let treat_external_node
(is_unsafe: bool)
(has_memory: bool)
(ext_nodelxm: Lxm.t)
(inpars: sx_Params)
(outpars: sx_Params)
: unit =
let statics = [] in
let ninfo =
treat_abstract_or_extern_node_do
is_unsafe has_memory ext_nodelxm statics inpars outpars false
in
add_info node_table "(extern) node" ext_nodelxm ninfo ;
def_list := (NodeItem (Lxm.id (ext_nodelxm),statics)) :: !def_list
let (threat_slice_start : Lxm.t -> val_exp -> val_exp option -> slice_info srcflagged) =
fun lxm last step ->
let str = Lxm.str lxm in
let int_to_val_exp istr =
try
ignore (int_of_string istr);
let ic = flagit (ICONST_n (Lv6Id.of_string(istr))) lxm in
CallByPos(flagit (Predef_n (ic)) lxm, Oper [])
with _ ->
CallByPos(flagit (IDENT_n (Lv6Id.idref_of_string(istr))) lxm, Oper [])
in
match Str.split (Str.regexp (Str.quote "..")) str with
| [first] ->
let slice_info =
{
si_first = int_to_val_exp first;
si_last = last;
si_step = step
}
in
flagit slice_info lxm
| _ -> assert false
let (make_ident : Lxm.t -> pragma list -> Lxm.t) =
fun lxm pl ->
if pl = [] then lxm else Lxm.add_pragma lxm pl
let (make_clock_exp : Lv6Id.idref -> Lxm.t -> clock_exp) =
fun str v_lxm ->
NamedClock( Lxm.flagit (Lv6Id.long_of_idref str, (Lxm.id v_lxm)) v_lxm)