qsp_instruction.mly 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. %%
  2. optionnal_delimited(opening, X, closing):
  3. | v = delimited(opening, X, closing) { v }
  4. | v = X { v }
  5. (* Redefine the arguments from expression here because we accept
  6. * values without parens. *)
  7. argument(X):
  8. | a = optionnal_delimited(L_PAREN, arguments(X), R_PAREN) { a }
  9. | a = X { [ a ] }
  10. (** At the opposite of an expression, an instruction does not return anything. *)
  11. %public instruction:
  12. | s = single_instruction { s }
  13. (** Action like act or if in a single line *)
  14. %public inline_action:
  15. | a = onliner(ACT)
  16. { let loc, label, statements, _, _ = a in
  17. let label = Analyzer.Expression.v label in
  18. Analyzer.Instruction.act loc ~label statements
  19. }
  20. | a = onliner(IF)
  21. else_opt = preceded(ELSE, instruction)?
  22. { let loc, expr, statements, loc_s, _body = a in
  23. let elifs = []
  24. and else_ = match else_opt with
  25. | None -> None
  26. | Some instructions -> Some ($loc(else_opt), [ instructions ]) in
  27. Analyzer.Instruction.if_
  28. loc
  29. (loc_s, Analyzer.Expression.v expr, statements)
  30. ~elifs
  31. ~else_
  32. }
  33. | a = onliner(IF)
  34. else_ = preceded(ELSE, inline_action)
  35. { let loc, expr, statements, loc_s, _body = a in
  36. let elifs = []
  37. and else_ = Some ($loc(else_), [ else_ ]) in
  38. Analyzer.Instruction.if_
  39. loc
  40. (loc_s, Analyzer.Expression.v expr, statements)
  41. ~elifs
  42. ~else_
  43. }
  44. single_instruction:
  45. | expr = expression
  46. {
  47. let expr = Analyzer.Expression.v expr in
  48. Analyzer.Instruction.expression expr
  49. }
  50. | e = let_assignation { e }
  51. | k = keyword
  52. args = argument(expression)
  53. {
  54. let args = List.map args ~f:(Analyzer.Expression.v) in
  55. Analyzer.Instruction.call $loc k args
  56. }
  57. keyword:
  58. | k = KEYWORD { k }
  59. let_assignation:
  60. | assignation
  61. variable = variable
  62. op = assignation_operator
  63. value = expression
  64. {
  65. let variable = Helper.variable variable
  66. and value = Analyzer.Expression.v value in
  67. Analyzer.Instruction.assign $loc variable op value
  68. }
  69. %inline assignation:
  70. |
  71. | LET
  72. | SET {}
  73. assignation_operator:
  74. | EQUAL { T.Eq' }
  75. | INCR { T.Inc }
  76. | DECR { T.Decr }
  77. | MULT_EQUAL { T.Mult }
  78. | DIV_EQUAL { T.Div_assign }
  79. inline_instruction:
  80. | hd = inline_instruction
  81. tl = single_instruction
  82. AMPERSAND+
  83. { tl :: hd }
  84. |
  85. { [] }
  86. final_inline_instruction:
  87. | hd = inline_instruction
  88. tl = instruction
  89. | hd = inline_instruction
  90. tl = inline_action
  91. { tl :: hd }
  92. | hd = inline_instruction
  93. COMMENT
  94. { (Analyzer.Instruction.comment $loc) :: hd }
  95. | hd = inline_instruction
  96. { hd }
  97. onliner(TOKEN):
  98. | TOKEN
  99. e = expression
  100. COLUMN
  101. s = rev (final_inline_instruction)
  102. { $loc, e, s, $loc(s), None }