tree.ml 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. open StdLabels
  2. let identifier = "tree"
  3. let description = "Build the AST"
  4. let active = ref true
  5. module Ast = struct
  6. type 'a literal = 'a T.literal = Text of string | Expression of 'a
  7. [@@deriving eq, show]
  8. type 'a variable = { pos : 'a; name : string; index : 'a expression option }
  9. [@@deriving eq, show]
  10. and 'a expression =
  11. | Integer of 'a * string
  12. | Literal of 'a * 'a expression literal list
  13. | Ident of 'a variable
  14. | BinaryOp of 'a * T.boperator * 'a expression * 'a expression
  15. | Op of 'a * T.uoperator * 'a expression
  16. | Function of 'a * T.function_ * 'a expression list
  17. [@@deriving eq, show]
  18. and 'a condition = 'a * 'a expression * 'a statement list
  19. and 'a statement =
  20. | If of {
  21. loc : 'a;
  22. then_ : 'a condition;
  23. elifs : 'a condition list;
  24. else_ : 'a statement list;
  25. }
  26. | Act of { loc : 'a; label : 'a expression; statements : 'a statement list }
  27. | Declaration of ('a * 'a variable * T.assignation_operator * 'a expression)
  28. | Expression of 'a expression
  29. | Comment of 'a
  30. | Call of 'a * T.keywords * 'a expression list
  31. | Location of 'a * string
  32. [@@deriving eq, show]
  33. end
  34. (** Default implementation for the expression *)
  35. module Expression : S.Expression with type t' = S.pos Ast.expression = struct
  36. type t = S.pos Ast.expression
  37. type t' = t
  38. let v : t -> t' = fun t -> t
  39. let integer : S.pos -> string -> t = fun pos i -> Ast.Integer (pos, i)
  40. let literal : S.pos -> t T.literal list -> t =
  41. fun pos l -> Ast.Literal (pos, l)
  42. let function_ : S.pos -> T.function_ -> t list -> t =
  43. fun pos name args -> Ast.Function (pos, name, args)
  44. let uoperator : S.pos -> T.uoperator -> t -> t =
  45. fun pos op expression -> Ast.Op (pos, op, expression)
  46. let boperator : S.pos -> T.boperator -> t -> t -> t =
  47. fun pos op op1 op2 ->
  48. let op1 = op1 and op2 = op2 in
  49. Ast.BinaryOp (pos, op, op1, op2)
  50. let ident : (S.pos, t) S.variable -> t =
  51. fun { pos; name; index } ->
  52. let index = Option.map (fun i -> i) index in
  53. Ast.Ident { pos; name; index }
  54. end
  55. module Instruction :
  56. S.Instruction
  57. with type expression = Expression.t'
  58. and type t' = S.pos Ast.statement = struct
  59. type t = S.pos Ast.statement
  60. type t' = t
  61. let v : t -> t' = fun t -> t
  62. type expression = Expression.t'
  63. let call : S.pos -> T.keywords -> expression list -> t =
  64. fun pos name args -> Ast.Call (pos, name, args)
  65. let location : S.pos -> string -> t =
  66. fun loc label -> Ast.Location (loc, label)
  67. let comment : S.pos -> t = fun pos -> Ast.Comment pos
  68. let expression : expression -> t = fun expr -> Ast.Expression expr
  69. let if_ :
  70. S.pos ->
  71. (expression, t) S.clause ->
  72. elifs:(expression, t) S.clause list ->
  73. else_:(S.pos * t list) option ->
  74. t =
  75. fun pos predicate ~elifs ~else_ ->
  76. let clause (pos, expr, repr) = (pos, expr, repr) in
  77. let elifs = List.map ~f:clause elifs
  78. and else_ =
  79. match else_ with None -> [] | Some (_, instructions) -> instructions
  80. in
  81. Ast.If { loc = pos; then_ = clause predicate; elifs; else_ }
  82. let act : S.pos -> label:expression -> t list -> t =
  83. fun pos ~label statements -> Ast.Act { loc = pos; label; statements }
  84. let assign :
  85. S.pos ->
  86. (S.pos, expression) S.variable ->
  87. T.assignation_operator ->
  88. expression ->
  89. t =
  90. fun pos_loc { pos; name; index } op expr ->
  91. (*let index = Option.map (fun i -> fst @@ Expression.observe (i [])) index*)
  92. Ast.Declaration (pos_loc, { pos; name; index }, op, expr)
  93. end
  94. module Location = struct
  95. type instruction = Instruction.t'
  96. type t = S.pos * S.pos Ast.statement list
  97. let v _ = []
  98. let location : S.pos -> instruction list -> t = fun pos block -> (pos, block)
  99. end