S.ml 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. (**
  2. This module describe the type an analyzer must implement in order to be
  3. used with the parser.
  4. The module is divided in three modules :
  5. - Expression : the finest part of the QSP syntax.
  6. - Instruction : if/act block,
  7. - Location
  8. *)
  9. (** {1 Generic types used in the module } *)
  10. type pos = Lexing.position * Lexing.position
  11. (** The type pos is used to track the starting and ending position for the
  12. given location. *)
  13. type ('a, 'b) variable = { pos : 'a; name : string; index : 'b option }
  14. (** Describe a variable, using the name in capitalized text, and an optionnal
  15. index.
  16. If missing, the index should be considered as [0].*)
  17. type ('a, 'b) clause = pos * 'a * 'b list
  18. (** {1 Checker Signature} *)
  19. (** Represent the evaluation over an expression *)
  20. module type Expression = sig
  21. type t
  22. (** Internal type used in the evaluation *)
  23. type t'
  24. (** External type used outside of the module *)
  25. val v : t -> t'
  26. val ident : (pos, t) variable -> t
  27. (*
  28. Basic values, text, number…
  29. *)
  30. val integer : pos -> string -> t
  31. val literal : pos -> t T.literal list -> t
  32. val function_ : pos -> T.function_ -> t list -> t
  33. (** Call a function. The functions list is hardcoded in lib/lexer.mll *)
  34. val uoperator : pos -> T.uoperator -> t -> t
  35. (** Unary operator like [-123] or [+'Text']*)
  36. val boperator : pos -> T.boperator -> t -> t -> t
  37. (** Binary operator, for a comparaison, or an operation *)
  38. end
  39. module type Instruction = sig
  40. type t
  41. (** Internal type used in the evaluation *)
  42. type t'
  43. (** External type used outside of the module *)
  44. val v : t -> t'
  45. type expression
  46. val call : pos -> T.keywords -> expression list -> t
  47. (** Call for an instruction like [GT] or [*CLR] *)
  48. val location : pos -> string -> t
  49. (** Label for a loop *)
  50. val comment : pos -> t
  51. (** Comment *)
  52. val expression : expression -> t
  53. (** Raw expression *)
  54. val if_ :
  55. pos ->
  56. (expression, t) clause ->
  57. elifs:(expression, t) clause list ->
  58. else_:(pos * t list) option ->
  59. t
  60. val act : pos -> label:expression -> t list -> t
  61. val assign :
  62. pos ->
  63. (pos, expression) variable ->
  64. T.assignation_operator ->
  65. expression ->
  66. t
  67. end
  68. module type Location = sig
  69. type t
  70. type instruction
  71. val v : t -> Report.t list
  72. val location : pos -> instruction list -> t
  73. end
  74. (** {1 Unified module used by the parser } *)
  75. module type Analyzer = sig
  76. val identifier : string
  77. (** Identifier for the module *)
  78. val description : string
  79. (** Short description*)
  80. val active : bool ref
  81. (** Is the test active or not *)
  82. module Expression : Expression
  83. module Instruction : Instruction with type expression = Expression.t'
  84. module Location : Location with type instruction = Instruction.t'
  85. end
  86. (** Helper module used in order to convert elements from the differents
  87. representation levels.
  88. Thoses functions are intended to be used in the menhir parser, in order to
  89. limit the code in the mly file.
  90. *)
  91. module Helper (E : sig
  92. type t
  93. (** Internal type used in the evaluation *)
  94. type t'
  95. (** External type used outside of the module *)
  96. val v : t -> t'
  97. end) : sig
  98. val variable : (pos, E.t) variable -> (pos, E.t') variable
  99. (** Convert a variable from the [Expression.t] into [Expression.t'] *)
  100. end = struct
  101. let variable : (pos, E.t) variable -> (pos, E.t') variable =
  102. fun variable -> { variable with index = Option.map E.v variable.index }
  103. end