expression.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #pragma once
  31. #include "core/object/ref_counted.h"
  32. class Expression : public RefCounted {
  33. GDCLASS(Expression, RefCounted);
  34. private:
  35. String expression;
  36. int str_ofs = 0;
  37. bool expression_dirty = false;
  38. bool _compile_expression();
  39. enum TokenType {
  40. TK_CURLY_BRACKET_OPEN,
  41. TK_CURLY_BRACKET_CLOSE,
  42. TK_BRACKET_OPEN,
  43. TK_BRACKET_CLOSE,
  44. TK_PARENTHESIS_OPEN,
  45. TK_PARENTHESIS_CLOSE,
  46. TK_IDENTIFIER,
  47. TK_BUILTIN_FUNC,
  48. TK_SELF,
  49. TK_CONSTANT,
  50. TK_BASIC_TYPE,
  51. TK_COLON,
  52. TK_COMMA,
  53. TK_PERIOD,
  54. TK_OP_IN,
  55. TK_OP_EQUAL,
  56. TK_OP_NOT_EQUAL,
  57. TK_OP_LESS,
  58. TK_OP_LESS_EQUAL,
  59. TK_OP_GREATER,
  60. TK_OP_GREATER_EQUAL,
  61. TK_OP_AND,
  62. TK_OP_OR,
  63. TK_OP_NOT,
  64. TK_OP_ADD,
  65. TK_OP_SUB,
  66. TK_OP_MUL,
  67. TK_OP_DIV,
  68. TK_OP_MOD,
  69. TK_OP_POW,
  70. TK_OP_SHIFT_LEFT,
  71. TK_OP_SHIFT_RIGHT,
  72. TK_OP_BIT_AND,
  73. TK_OP_BIT_OR,
  74. TK_OP_BIT_XOR,
  75. TK_OP_BIT_INVERT,
  76. TK_INPUT,
  77. TK_EOF,
  78. TK_ERROR,
  79. TK_MAX
  80. };
  81. static const char *token_name[TK_MAX];
  82. struct Token {
  83. TokenType type;
  84. Variant value;
  85. };
  86. void _set_error(const String &p_err) {
  87. if (error_set) {
  88. return;
  89. }
  90. error_str = p_err;
  91. error_set = true;
  92. }
  93. Error _get_token(Token &r_token);
  94. String error_str;
  95. bool error_set = true;
  96. struct ENode {
  97. enum Type {
  98. TYPE_INPUT,
  99. TYPE_CONSTANT,
  100. TYPE_SELF,
  101. TYPE_OPERATOR,
  102. TYPE_INDEX,
  103. TYPE_NAMED_INDEX,
  104. TYPE_ARRAY,
  105. TYPE_DICTIONARY,
  106. TYPE_CONSTRUCTOR,
  107. TYPE_BUILTIN_FUNC,
  108. TYPE_CALL
  109. };
  110. ENode *next = nullptr;
  111. Type type = TYPE_INPUT;
  112. virtual ~ENode() {
  113. if (next) {
  114. memdelete(next);
  115. }
  116. }
  117. };
  118. struct ExpressionNode {
  119. bool is_op = false;
  120. union {
  121. Variant::Operator op;
  122. ENode *node = nullptr;
  123. };
  124. };
  125. ENode *_parse_expression();
  126. struct InputNode : public ENode {
  127. int index = 0;
  128. InputNode() {
  129. type = TYPE_INPUT;
  130. }
  131. };
  132. struct ConstantNode : public ENode {
  133. Variant value = Variant::NIL;
  134. ConstantNode() {
  135. type = TYPE_CONSTANT;
  136. }
  137. };
  138. struct OperatorNode : public ENode {
  139. Variant::Operator op = Variant::Operator::OP_ADD;
  140. ENode *nodes[2] = { nullptr, nullptr };
  141. OperatorNode() {
  142. type = TYPE_OPERATOR;
  143. }
  144. };
  145. struct SelfNode : public ENode {
  146. SelfNode() {
  147. type = TYPE_SELF;
  148. }
  149. };
  150. struct IndexNode : public ENode {
  151. ENode *base = nullptr;
  152. ENode *index = nullptr;
  153. IndexNode() {
  154. type = TYPE_INDEX;
  155. }
  156. };
  157. struct NamedIndexNode : public ENode {
  158. ENode *base = nullptr;
  159. StringName name;
  160. NamedIndexNode() {
  161. type = TYPE_NAMED_INDEX;
  162. }
  163. };
  164. struct ConstructorNode : public ENode {
  165. Variant::Type data_type = Variant::Type::NIL;
  166. Vector<ENode *> arguments;
  167. ConstructorNode() {
  168. type = TYPE_CONSTRUCTOR;
  169. }
  170. };
  171. struct CallNode : public ENode {
  172. ENode *base = nullptr;
  173. StringName method;
  174. Vector<ENode *> arguments;
  175. CallNode() {
  176. type = TYPE_CALL;
  177. }
  178. };
  179. struct ArrayNode : public ENode {
  180. Vector<ENode *> array;
  181. ArrayNode() {
  182. type = TYPE_ARRAY;
  183. }
  184. };
  185. struct DictionaryNode : public ENode {
  186. Vector<ENode *> dict;
  187. DictionaryNode() {
  188. type = TYPE_DICTIONARY;
  189. }
  190. };
  191. struct BuiltinFuncNode : public ENode {
  192. StringName func;
  193. Vector<ENode *> arguments;
  194. BuiltinFuncNode() {
  195. type = TYPE_BUILTIN_FUNC;
  196. }
  197. };
  198. template <typename T>
  199. T *alloc_node() {
  200. T *node = memnew(T);
  201. node->next = nodes;
  202. nodes = node;
  203. return node;
  204. }
  205. ENode *root = nullptr;
  206. ENode *nodes = nullptr;
  207. Vector<String> input_names;
  208. bool execution_error = false;
  209. 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);
  210. protected:
  211. static void _bind_methods();
  212. public:
  213. Error parse(const String &p_expression, const Vector<String> &p_input_names = Vector<String>());
  214. Variant execute(const Array &p_inputs = Array(), Object *p_base = nullptr, bool p_show_error = true, bool p_const_calls_only = false);
  215. bool has_execute_failed() const;
  216. String get_error_text() const;
  217. ~Expression();
  218. };