check.ml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. (** This module provide a way to create new Id dynamically in the runtime,
  2. and some fonctions for comparing them. *)
  3. module Id : sig
  4. type 'a typeid
  5. (** The type created on-the-fly. *)
  6. val newtype : unit -> 'a typeid
  7. (** Create a new instance of a dynamic type *)
  8. type ('a, 'b) eq = Eq : ('a, 'a) eq
  9. val try_cast : 'a typeid -> 'b typeid -> ('a, 'b) eq option
  10. (** Compare two types using the Eq pattern *)
  11. end = struct
  12. type 'a witness = ..
  13. module type Witness = sig
  14. type t
  15. type _ witness += Id : t witness
  16. end
  17. type 'a typeid = (module Witness with type t = 'a)
  18. type ('a, 'b) eq = Eq : ('a, 'a) eq
  19. let try_cast : type a b. a typeid -> b typeid -> (a, b) eq option =
  20. fun x y ->
  21. let module X : Witness with type t = a = (val x) in
  22. let module Y : Witness with type t = b = (val y) in
  23. match X.Id with Y.Id -> Some Eq | _ -> None
  24. let newtype (type u) () =
  25. (* The extensible type need to be extended in a module, it is not possible
  26. to declare a type in a function. That’s why we need to pack a module
  27. here *)
  28. let module Witness = struct
  29. type t = u
  30. type _ witness += Id : t witness
  31. end in
  32. (module Witness : Witness with type t = u)
  33. end
  34. (** The the Id module, wrap a value in an existencial type with a witness
  35. associate with. *)
  36. type result = R : { value : 'a; witness : 'a Id.typeid } -> result
  37. let get : type a. a Id.typeid -> result -> a option =
  38. fun typeid (R { value; witness }) ->
  39. match Id.try_cast typeid witness with Some Eq -> Some value | None -> None
  40. type t =
  41. | E : {
  42. module_ :
  43. (module S.Analyzer
  44. with type Expression.t = 'a
  45. and type Expression.t' = 'b
  46. and type Instruction.t = 'c
  47. and type Instruction.t' = 'd
  48. and type Location.t = 'e
  49. and type context = 'f);
  50. expr_witness : 'a Id.typeid;
  51. expr' : 'b Id.typeid;
  52. instr_witness : 'c Id.typeid;
  53. instr' : 'd Id.typeid;
  54. location_witness : 'e Id.typeid;
  55. context : 'f Id.typeid;
  56. }
  57. -> t
  58. let build :
  59. (module S.Analyzer
  60. with type Expression.t = _
  61. and type Expression.t' = _
  62. and type Instruction.t = _
  63. and type Instruction.t' = _
  64. and type Location.t = 'a
  65. and type context = _) ->
  66. 'a Id.typeid * t =
  67. fun module_ ->
  68. let expr_witness = Id.newtype ()
  69. and expr' = Id.newtype ()
  70. and instr_witness = Id.newtype ()
  71. and instr' = Id.newtype ()
  72. and location_witness = Id.newtype ()
  73. and context = Id.newtype () in
  74. let t =
  75. E
  76. {
  77. module_;
  78. expr_witness;
  79. expr';
  80. instr_witness;
  81. instr';
  82. location_witness;
  83. context;
  84. }
  85. in
  86. (location_witness, t)
  87. let get_module : t -> (module S.Analyzer) =
  88. fun (E { module_; _ }) -> (module_ :> (module S.Analyzer))
  89. module type App = sig
  90. val t : t array
  91. end
  92. open StdLabels
  93. module Helper = struct
  94. type 'a expr_list = { witness : 'a Id.typeid; values : 'a list }
  95. let expr_i : result array list -> 'a Id.typeid -> int -> 'a expr_list =
  96. fun args witness i ->
  97. let result =
  98. List.fold_left args ~init:{ values = []; witness }
  99. ~f:(fun (type a) ({ values; witness } : a expr_list) t : a expr_list ->
  100. match get witness (Array.get t i) with
  101. | None -> failwith "Does not match"
  102. | Some value_1 -> { values = value_1 :: values; witness })
  103. in
  104. { result with values = result.values }
  105. end
  106. module Make (A : App) = struct
  107. let identifier = "main_checker"
  108. let description = "Internal module"
  109. let is_global = false
  110. let active = ref false
  111. type context = result Array.t
  112. (** We associate each context from the differents test in an array. The
  113. context for this module is a sort of context of contexts *)
  114. (** Initialize each test, and keep the result in the context. *)
  115. let initialize : unit -> context =
  116. fun () ->
  117. Array.map A.t ~f:(fun (E { module_ = (module S); context; _ }) ->
  118. let value = S.initialize () in
  119. R { value; witness = context })
  120. let finalize : result Array.t -> (string * Report.t) list =
  121. fun context_array ->
  122. let _, report =
  123. Array.fold_left A.t ~init:(0, [])
  124. ~f:(fun (i, acc) (E { module_ = (module S); context; _ }) ->
  125. let result = Array.get context_array i in
  126. let local_context = Option.get (get context result) in
  127. let reports = S.finalize local_context in
  128. (i + 1, List.rev_append reports acc))
  129. in
  130. report
  131. (* Global variable for the whole module *)
  132. let len = Array.length A.t
  133. module Expression : S.Expression with type t' = result array = struct
  134. type t = result array
  135. type t' = result array
  136. let literal : S.pos -> t T.literal list -> t =
  137. fun pos values ->
  138. Array.mapi A.t ~f:(fun i (E { module_ = (module S); expr_witness; _ }) ->
  139. (* Map every values to the Checker *)
  140. let values' =
  141. List.map values
  142. ~f:
  143. (T.map_litteral ~f:(fun expr ->
  144. Option.get (get expr_witness (Array.get expr i))))
  145. in
  146. let value = S.Expression.literal pos values' in
  147. R { value; witness = expr_witness })
  148. let integer : S.pos -> string -> t =
  149. fun pos value ->
  150. Array.map A.t ~f:(fun (E { module_ = (module S); expr_witness; _ }) ->
  151. let value = S.Expression.integer pos value in
  152. R { value; witness = expr_witness })
  153. (** Unary operator like [-123] or [+'Text']*)
  154. let uoperator : S.pos -> T.uoperator -> t -> t =
  155. fun pos op values ->
  156. (* Evaluate the nested expression *)
  157. let results = values in
  158. (* Now evaluate the remaining expression.
  159. Traverse both the module the apply, and the matching expression already
  160. evaluated.
  161. It’s easer to use [map] and declare [report] as reference instead of
  162. [fold_left2] and accumulate the report inside the closure, because I
  163. don’t manage the order of the results.
  164. *)
  165. let results =
  166. Array.map2 A.t results
  167. ~f:(fun (E { module_ = (module S); expr_witness; _ }) value ->
  168. match get expr_witness value with
  169. | None -> failwith "Does not match"
  170. | Some value ->
  171. (* Evaluate the single expression *)
  172. let value = S.Expression.uoperator pos op value in
  173. R { witness = expr_witness; value })
  174. in
  175. results
  176. (** Basically the same as uoperator, but operate over two operands instead
  177. of a single one. *)
  178. let boperator : S.pos -> T.boperator -> t -> t -> t =
  179. fun pos op expr1 expr2 ->
  180. Array.init len ~f:(fun i ->
  181. let (E { module_ = (module S); expr_witness; _ }) = Array.get A.t i in
  182. match
  183. ( get expr_witness (Array.get expr1 i),
  184. get expr_witness (Array.get expr2 i) )
  185. with
  186. | Some value_1, Some value_2 ->
  187. let value = S.Expression.boperator pos op value_1 value_2 in
  188. R { witness = expr_witness; value }
  189. | _ -> failwith "Does not match")
  190. (** Call a function. The functions list is hardcoded in lib/lexer.mll *)
  191. let function_ : S.pos -> T.function_ -> t list -> t =
  192. fun pos func args ->
  193. Array.init len ~f:(fun i ->
  194. let (E { module_ = (module S); expr_witness; _ }) = Array.get A.t i in
  195. (* Extract the arguments for each module *)
  196. let args_i = List.rev (Helper.expr_i args expr_witness i).values in
  197. let value = S.Expression.function_ pos func args_i in
  198. R { witness = expr_witness; value })
  199. let ident : (S.pos, t) S.variable -> t =
  200. fun { pos : S.pos; name : string; index : t option } ->
  201. Array.init len ~f:(fun i ->
  202. let (E { module_ = (module S); expr_witness; _ }) = Array.get A.t i in
  203. match index with
  204. | None ->
  205. (* Easest case, just return the plain ident *)
  206. let value = S.Expression.ident { pos; name; index = None } in
  207. R { witness = expr_witness; value }
  208. | Some t -> (
  209. match get expr_witness (Array.get t i) with
  210. | None -> failwith "Does not match"
  211. | Some value_1 ->
  212. let value =
  213. S.Expression.ident { pos; name; index = Some value_1 }
  214. in
  215. R { witness = expr_witness; value }))
  216. (** Convert each internal represention for the expression into its external
  217. representation *)
  218. let v : t -> t' =
  219. fun t ->
  220. let result =
  221. Array.map2 A.t t
  222. ~f:(fun (E { module_ = (module S); expr_witness; expr'; _ }) result ->
  223. match get expr_witness result with
  224. | None -> failwith "Does not match"
  225. | Some value ->
  226. let value = S.Expression.v value in
  227. R { witness = expr'; value })
  228. in
  229. result
  230. end
  231. module Instruction :
  232. S.Instruction
  233. with type expression = Expression.t'
  234. and type t' = result array = struct
  235. type expression = Expression.t'
  236. type t = result array
  237. type t' = result array
  238. let location : S.pos -> string -> t =
  239. fun pos label ->
  240. Array.map A.t ~f:(fun (E { module_ = (module S); instr_witness; _ }) ->
  241. let value = S.Instruction.location pos label in
  242. R { value; witness = instr_witness })
  243. let comment : S.pos -> t =
  244. fun pos ->
  245. Array.map A.t ~f:(fun (E { module_ = (module S); instr_witness; _ }) ->
  246. let value = S.Instruction.comment pos in
  247. R { value; witness = instr_witness })
  248. let expression : expression -> t =
  249. fun expr ->
  250. Array.map2 A.t expr
  251. ~f:(fun (E { module_ = (module S); instr_witness; expr'; _ }) result ->
  252. match get expr' result with
  253. | None -> failwith "Does not match"
  254. | Some value ->
  255. (* The evaluate the instruction *)
  256. let value = S.Instruction.expression value in
  257. R { value; witness = instr_witness })
  258. let call : S.pos -> T.keywords -> expression list -> t =
  259. fun pos keyword args ->
  260. (* The arguments are given like an array of array. Each expression is
  261. actually the list of each expression in the differents modules. *)
  262. Array.init len ~f:(fun i ->
  263. let (E { module_ = (module S); expr'; instr_witness; _ }) =
  264. Array.get A.t i
  265. in
  266. let values = List.rev (Helper.expr_i args expr' i).values in
  267. let value = S.Instruction.call pos keyword values in
  268. R { witness = instr_witness; value })
  269. let act : S.pos -> label:expression -> t list -> t =
  270. fun pos ~label instructions ->
  271. Array.init len ~f:(fun i ->
  272. let (E { module_ = (module S); instr_witness; expr'; _ }) =
  273. Array.get A.t i
  274. in
  275. let values =
  276. List.rev (Helper.expr_i instructions instr_witness i).values
  277. in
  278. match get expr' (Array.get label i) with
  279. | None -> failwith "Does not match"
  280. | Some label_i ->
  281. let value = S.Instruction.act pos ~label:label_i values in
  282. R { witness = instr_witness; value })
  283. (* I think it’s one of the longest module I’ve ever written in OCaml… *)
  284. let assign :
  285. S.pos ->
  286. (S.pos, expression) S.variable ->
  287. T.assignation_operator ->
  288. expression ->
  289. t =
  290. fun pos { pos = var_pos; name; index } op expression ->
  291. Array.init len ~f:(fun i ->
  292. let (E { module_ = (module A); instr_witness; expr'; _ }) =
  293. Array.get A.t i
  294. in
  295. let index_i =
  296. Option.map
  297. (fun expression ->
  298. Option.get (get expr' (Array.get expression i)))
  299. index
  300. in
  301. let variable = S.{ pos = var_pos; name; index = index_i } in
  302. match get expr' (Array.get expression i) with
  303. | None -> failwith "Does not match"
  304. | Some value ->
  305. let value = A.Instruction.assign pos variable op value in
  306. R { value; witness = instr_witness })
  307. let rebuild_clause :
  308. type a b.
  309. int ->
  310. a Id.typeid ->
  311. b Id.typeid ->
  312. S.pos * result array * result array list ->
  313. (b, a) S.clause =
  314. fun i instr_witness expr' clause ->
  315. let pos_clause, expr_clause, ts = clause in
  316. match get expr' (Array.get expr_clause i) with
  317. | None -> failwith "Does not match"
  318. | Some value ->
  319. let ts = Helper.expr_i ts instr_witness i in
  320. let ts = List.rev ts.values in
  321. let clause = (pos_clause, value, ts) in
  322. clause
  323. let if_ :
  324. S.pos ->
  325. (expression, t) S.clause ->
  326. elifs:(expression, t) S.clause list ->
  327. else_:(S.pos * t list) option ->
  328. t =
  329. fun pos clause ~elifs ~else_ ->
  330. (* First, apply the report for all the instructions *)
  331. let else_ =
  332. match else_ with
  333. | None -> None
  334. | Some (pos, instructions) -> Some (pos, instructions)
  335. in
  336. Array.init len ~f:(fun i ->
  337. let (E { module_ = (module A); instr_witness; expr'; _ }) =
  338. Array.get A.t i
  339. in
  340. let clause = rebuild_clause i instr_witness expr' clause
  341. and elifs = List.map elifs ~f:(rebuild_clause i instr_witness expr')
  342. and else_ =
  343. match else_ with
  344. | None -> None
  345. | Some (pos, instructions) ->
  346. let elses = Helper.expr_i instructions instr_witness i in
  347. Some (pos, List.rev elses.values)
  348. in
  349. let value = A.Instruction.if_ pos clause ~elifs ~else_ in
  350. R { value; witness = instr_witness })
  351. (** This code is almost a copy/paste from Expression.v but I did not found
  352. a way to factorize it. *)
  353. let v : t -> t' =
  354. fun t ->
  355. let result =
  356. Array.map2 A.t t
  357. ~f:(fun
  358. (E { module_ = (module S); instr_witness; instr'; _ }) result ->
  359. match get instr_witness result with
  360. | None -> failwith "Does not match"
  361. | Some value ->
  362. let value = S.Instruction.v value in
  363. R { witness = instr'; value })
  364. in
  365. result
  366. end
  367. module Location :
  368. S.Location
  369. with type t = result array
  370. and type instruction = Instruction.t'
  371. and type context := context = struct
  372. type instruction = Instruction.t'
  373. type t = result array
  374. let location : context -> S.pos -> instruction list -> t =
  375. fun local_context pos args ->
  376. ignore pos;
  377. let result =
  378. Array.init len ~f:(fun i ->
  379. let (E
  380. { module_ = (module A); instr'; location_witness; context; _ })
  381. =
  382. Array.get A.t i
  383. in
  384. let local_context =
  385. Option.get (get context (Array.get local_context i))
  386. in
  387. let instructions = List.rev (Helper.expr_i args instr' i).values in
  388. let value = A.Location.location local_context pos instructions in
  389. R { value; witness = location_witness })
  390. in
  391. result
  392. let v : t -> Report.t list =
  393. fun args ->
  394. let report = ref [] in
  395. let () =
  396. Array.iteri args ~f:(fun i result ->
  397. let (E { module_ = (module A); location_witness; _ }) =
  398. Array.get A.t i
  399. in
  400. match get location_witness result with
  401. | None -> failwith "Does not match"
  402. | Some value ->
  403. let re = A.Location.v value in
  404. report := List.rev_append re !report)
  405. in
  406. !report
  407. end
  408. end