You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
4.5 KiB

  1. /*
  2. This file is part of libeval, a simple math expression evaluator
  3. Copyright (C) 2017 Michael Geselbracht, mgeselbracht3@gmail.com
  4. Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors.
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. %token_type { LIBEVAL::T_TOKEN }
  17. %extra_argument { LIBEVAL::COMPILER* pEval }
  18. %type nt {LIBEVAL::TREE_NODE*}
  19. %nonassoc G_IDENTIFIER G_ASSIGN G_SEMCOL.
  20. %left G_COMMA.
  21. %left G_BOOL_AND.
  22. %left G_BOOL_OR.
  23. %left G_BOOL_XOR.
  24. %nonassoc G_LESS_THAN G_GREATER_THAN G_LESS_EQUAL_THAN G_GREATER_EQUAL_THAN G_EQUAL G_NOT_EQUAL.
  25. %right G_BOOL_NOT.
  26. %left G_PLUS G_MINUS.
  27. %left G_DIVIDE G_MULT.
  28. %nonassoc G_STRUCT_REF.
  29. %nonassoc G_UNIT.
  30. %include {
  31. #include <assert.h>
  32. #include <libeval_compiler/libeval_compiler.h>
  33. }
  34. %syntax_error {
  35. pEval->parseError("Syntax error");
  36. }
  37. %parse_accept {
  38. pEval->parseOk();
  39. }
  40. main ::= in.
  41. /* Allow multiple statements in input string: x=1; y=2 */
  42. in ::= stmt.
  43. in ::= in stmt.
  44. /* A statement can be empty, an expr or an expr followed by ';' */
  45. stmt ::= G_ENDS.
  46. stmt ::= nt(A) G_ENDS. { pEval->setRoot(A); }
  47. nt(A) ::= G_VALUE(B).
  48. {
  49. A = newNode( pEval, TR_NUMBER, B.value );
  50. }
  51. nt(A) ::= G_VALUE(B) G_UNIT(C).
  52. {
  53. A = newNode( pEval, TR_NUMBER, B.value );
  54. A->leaf[0] = newNode( pEval, TR_UNIT, C.value );
  55. }
  56. nt(A) ::= G_STRING(B).
  57. {
  58. A = newNode( pEval, TR_STRING, B.value );
  59. }
  60. nt(A) ::= G_IDENTIFIER(B).
  61. {
  62. A = newNode( pEval, TR_IDENTIFIER, B.value );
  63. }
  64. nt(A) ::= nt(B) G_COMMA nt(C).
  65. {
  66. A = newNode( pEval, TR_ARG_LIST );
  67. A->leaf[0] = B;
  68. A->leaf[1] = C;
  69. }
  70. nt(A) ::= nt(B) G_LESS_THAN nt(C).
  71. {
  72. A = newNode( pEval, TR_OP_LESS );
  73. A->leaf[0] = B;
  74. A->leaf[1] = C;
  75. }
  76. nt(A) ::= nt(B) G_GREATER_THAN nt(C).
  77. {
  78. A = newNode( pEval, TR_OP_GREATER );
  79. A->leaf[0] = B;
  80. A->leaf[1] = C;
  81. }
  82. nt(A) ::= nt(B) G_LESS_EQUAL_THAN nt(C).
  83. {
  84. A = newNode( pEval, TR_OP_LESS_EQUAL );
  85. A->leaf[0] = B;
  86. A->leaf[1] = C;
  87. }
  88. nt(A) ::= nt(B) G_GREATER_EQUAL_THAN nt(C).
  89. {
  90. A = newNode( pEval, TR_OP_GREATER_EQUAL );
  91. A->leaf[0] = B;
  92. A->leaf[1] = C;
  93. }
  94. nt(A) ::= nt(B) G_NOT_EQUAL nt(C).
  95. {
  96. A = newNode( pEval, TR_OP_NOT_EQUAL );
  97. A->leaf[0] = B;
  98. A->leaf[1] = C;
  99. }
  100. nt(A) ::= nt(B) G_BOOL_AND nt(C).
  101. {
  102. A = newNode( pEval, TR_OP_BOOL_AND );
  103. A->leaf[0] = B;
  104. A->leaf[1] = C;
  105. }
  106. nt(A) ::= nt(B) G_BOOL_OR nt(C).
  107. {
  108. A = newNode( pEval, TR_OP_BOOL_OR );
  109. A->leaf[0] = B;
  110. A->leaf[1] = C;
  111. }
  112. nt(A) ::= G_BOOL_NOT nt(B).
  113. {
  114. A = newNode( pEval, TR_OP_BOOL_NOT );
  115. A->leaf[0] = B;
  116. }
  117. nt(A) ::= nt(B) G_PLUS nt(C).
  118. {
  119. A = newNode( pEval, TR_OP_ADD );
  120. A->leaf[0] = B;
  121. A->leaf[1] = C;
  122. }
  123. nt(A) ::= G_PLUS nt(B).
  124. {
  125. A = newNode( pEval, B->op, B->value );
  126. A->leaf[0] = B->leaf[0];
  127. A->leaf[1] = B->leaf[1];
  128. }
  129. nt(A) ::= G_MINUS nt(B).
  130. {
  131. A = newNode( pEval, TR_OP_SUB );
  132. A->leaf[0] = newNode( pEval, TR_NUMBER );
  133. A->leaf[1] = B;
  134. }
  135. nt(A) ::= nt(B) G_MINUS nt(C).
  136. {
  137. A = newNode( pEval, TR_OP_SUB );
  138. A->leaf[0] = B;
  139. A->leaf[1] = C;
  140. }
  141. nt(A) ::= nt(B) G_MULT nt(C).
  142. {
  143. A = newNode( pEval, TR_OP_MUL );
  144. A->leaf[0] = B;
  145. A->leaf[1] = C;
  146. }
  147. nt(A) ::= nt(B) G_DIVIDE nt(C).
  148. {
  149. A = newNode( pEval, TR_OP_DIV );
  150. A->leaf[0] = B;
  151. A->leaf[1] = C;
  152. }
  153. nt(A) ::= nt(B) G_EQUAL nt(C).
  154. {
  155. A = newNode( pEval, TR_OP_EQUAL );
  156. A->leaf[0] = B;
  157. A->leaf[1] = C;
  158. }
  159. nt(A) ::= nt(B) G_STRUCT_REF nt(C).
  160. {
  161. A = newNode( pEval, TR_STRUCT_REF );
  162. A->leaf[0] = B;
  163. A->leaf[1] = C;
  164. }
  165. nt(A) ::= G_PARENL nt(B) G_PARENR.
  166. {
  167. A = newNode( pEval, B->op, B->value );
  168. A->leaf[0] = B->leaf[0];
  169. A->leaf[1] = B->leaf[1];
  170. }
  171. nt(A) ::= G_IDENTIFIER(F) G_PARENL nt(B) G_PARENR.
  172. {
  173. A = newNode( pEval, TR_OP_FUNC_CALL );
  174. A->leaf[0] = newNode( pEval, TR_IDENTIFIER, F.value);
  175. A->leaf[1] = B;
  176. }
  177. nt(A) ::= G_IDENTIFIER(F) G_PARENL G_PARENR.
  178. {
  179. A = newNode( pEval, TR_OP_FUNC_CALL );
  180. A->leaf[0] = newNode( pEval, TR_IDENTIFIER, F.value);
  181. A->leaf[1] = newNode( pEval, TR_NULL );
  182. }