expression.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /**************************************************************************/
  2. /* expression.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef EXPRESSION_H
  31. #define EXPRESSION_H
  32. #include "core/object/ref_counted.h"
  33. class Expression : public RefCounted {
  34. GDCLASS(Expression, RefCounted);
  35. private:
  36. struct Input {
  37. Variant::Type type = Variant::NIL;
  38. String name;
  39. Input() {}
  40. };
  41. Vector<Input> inputs;
  42. Variant::Type output_type = Variant::NIL;
  43. String expression;
  44. bool sequenced = false;
  45. int str_ofs = 0;
  46. bool expression_dirty = false;
  47. bool _compile_expression();
  48. enum TokenType {
  49. TK_CURLY_BRACKET_OPEN,
  50. TK_CURLY_BRACKET_CLOSE,
  51. TK_BRACKET_OPEN,
  52. TK_BRACKET_CLOSE,
  53. TK_PARENTHESIS_OPEN,
  54. TK_PARENTHESIS_CLOSE,
  55. TK_IDENTIFIER,
  56. TK_BUILTIN_FUNC,
  57. TK_SELF,
  58. TK_CONSTANT,
  59. TK_BASIC_TYPE,
  60. TK_COLON,
  61. TK_COMMA,
  62. TK_PERIOD,
  63. TK_OP_IN,
  64. TK_OP_EQUAL,
  65. TK_OP_NOT_EQUAL,
  66. TK_OP_LESS,
  67. TK_OP_LESS_EQUAL,
  68. TK_OP_GREATER,
  69. TK_OP_GREATER_EQUAL,
  70. TK_OP_AND,
  71. TK_OP_OR,
  72. TK_OP_NOT,
  73. TK_OP_ADD,
  74. TK_OP_SUB,
  75. TK_OP_MUL,
  76. TK_OP_DIV,
  77. TK_OP_MOD,
  78. TK_OP_POW,
  79. TK_OP_SHIFT_LEFT,
  80. TK_OP_SHIFT_RIGHT,
  81. TK_OP_BIT_AND,
  82. TK_OP_BIT_OR,
  83. TK_OP_BIT_XOR,
  84. TK_OP_BIT_INVERT,
  85. TK_INPUT,
  86. TK_EOF,
  87. TK_ERROR,
  88. TK_MAX
  89. };
  90. static const char *token_name[TK_MAX];
  91. struct Token {
  92. TokenType type;
  93. Variant value;
  94. };
  95. void _set_error(const String &p_err) {
  96. if (error_set) {
  97. return;
  98. }
  99. error_str = p_err;
  100. error_set = true;
  101. }
  102. Error _get_token(Token &r_token);
  103. String error_str;
  104. bool error_set = true;
  105. struct ENode {
  106. enum Type {
  107. TYPE_INPUT,
  108. TYPE_CONSTANT,
  109. TYPE_SELF,
  110. TYPE_OPERATOR,
  111. TYPE_INDEX,
  112. TYPE_NAMED_INDEX,
  113. TYPE_ARRAY,
  114. TYPE_DICTIONARY,
  115. TYPE_CONSTRUCTOR,
  116. TYPE_BUILTIN_FUNC,
  117. TYPE_CALL
  118. };
  119. ENode *next = nullptr;
  120. Type type = TYPE_INPUT;
  121. ENode() {}
  122. virtual ~ENode() {
  123. if (next) {
  124. memdelete(next);
  125. }
  126. }
  127. };
  128. struct ExpressionNode {
  129. bool is_op = false;
  130. union {
  131. Variant::Operator op;
  132. ENode *node = nullptr;
  133. };
  134. };
  135. ENode *_parse_expression();
  136. struct InputNode : public ENode {
  137. int index = 0;
  138. InputNode() {
  139. type = TYPE_INPUT;
  140. }
  141. };
  142. struct ConstantNode : public ENode {
  143. Variant value = Variant::NIL;
  144. ConstantNode() {
  145. type = TYPE_CONSTANT;
  146. }
  147. };
  148. struct OperatorNode : public ENode {
  149. Variant::Operator op = Variant::Operator::OP_ADD;
  150. ENode *nodes[2] = { nullptr, nullptr };
  151. OperatorNode() {
  152. type = TYPE_OPERATOR;
  153. }
  154. };
  155. struct SelfNode : public ENode {
  156. SelfNode() {
  157. type = TYPE_SELF;
  158. }
  159. };
  160. struct IndexNode : public ENode {
  161. ENode *base = nullptr;
  162. ENode *index = nullptr;
  163. IndexNode() {
  164. type = TYPE_INDEX;
  165. }
  166. };
  167. struct NamedIndexNode : public ENode {
  168. ENode *base = nullptr;
  169. StringName name;
  170. NamedIndexNode() {
  171. type = TYPE_NAMED_INDEX;
  172. }
  173. };
  174. struct ConstructorNode : public ENode {
  175. Variant::Type data_type = Variant::Type::NIL;
  176. Vector<ENode *> arguments;
  177. ConstructorNode() {
  178. type = TYPE_CONSTRUCTOR;
  179. }
  180. };
  181. struct CallNode : public ENode {
  182. ENode *base = nullptr;
  183. StringName method;
  184. Vector<ENode *> arguments;
  185. CallNode() {
  186. type = TYPE_CALL;
  187. }
  188. };
  189. struct ArrayNode : public ENode {
  190. Vector<ENode *> array;
  191. ArrayNode() {
  192. type = TYPE_ARRAY;
  193. }
  194. };
  195. struct DictionaryNode : public ENode {
  196. Vector<ENode *> dict;
  197. DictionaryNode() {
  198. type = TYPE_DICTIONARY;
  199. }
  200. };
  201. struct BuiltinFuncNode : public ENode {
  202. StringName func;
  203. Vector<ENode *> arguments;
  204. BuiltinFuncNode() {
  205. type = TYPE_BUILTIN_FUNC;
  206. }
  207. };
  208. template <class T>
  209. T *alloc_node() {
  210. T *node = memnew(T);
  211. node->next = nodes;
  212. nodes = node;
  213. return node;
  214. }
  215. ENode *root = nullptr;
  216. ENode *nodes = nullptr;
  217. Vector<String> input_names;
  218. bool execution_error = false;
  219. bool _execute(const Array &p_inputs, Object *p_instance, Expression::ENode *p_node, Variant &r_ret, bool p_const_calls_only, String &r_error_str);
  220. protected:
  221. static void _bind_methods();
  222. public:
  223. Error parse(const String &p_expression, const Vector<String> &p_input_names = Vector<String>());
  224. Variant execute(Array p_inputs = Array(), Object *p_base = nullptr, bool p_show_error = true, bool p_const_calls_only = false);
  225. bool has_execute_failed() const;
  226. String get_error_text() const;
  227. Expression() {}
  228. ~Expression();
  229. };
  230. #endif // EXPRESSION_H