qsp_instruction.mly 3.0 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. | is_local = assignation
  61. variable = variable
  62. op = assignation_operator
  63. value = expression
  64. {
  65. let variable = { (Helper.variable variable) with local = is_local }
  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 { false }
  73. | LOCAL { true }
  74. assignation_operator:
  75. | EQUAL { T.Eq' }
  76. | INCR { T.Inc }
  77. | DECR { T.Decr }
  78. | MULT_EQUAL { T.Mult }
  79. | DIV_EQUAL { T.Div_assign }
  80. inline_instruction:
  81. | hd = inline_instruction
  82. tl = single_instruction
  83. AMPERSAND+
  84. { tl :: hd }
  85. |
  86. { [] }
  87. final_inline_instruction:
  88. | hd = inline_instruction
  89. tl = instruction
  90. | hd = inline_instruction
  91. tl = inline_action
  92. { tl :: hd }
  93. | hd = inline_instruction
  94. COMMENT
  95. { (Analyzer.Instruction.comment $loc) :: hd }
  96. | hd = inline_instruction
  97. { hd }
  98. onliner(TOKEN):
  99. | TOKEN
  100. e = expression
  101. COLUMN
  102. s = rev (final_inline_instruction)
  103. { $loc, e, s, $loc(s), None }