expression.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*************************************************************************/
  2. /* expression.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  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/reference.h"
  33. class Expression : public Reference {
  34. GDCLASS(Expression, Reference)
  35. public:
  36. enum BuiltinFunc {
  37. MATH_SIN,
  38. MATH_COS,
  39. MATH_TAN,
  40. MATH_SINH,
  41. MATH_COSH,
  42. MATH_TANH,
  43. MATH_ASIN,
  44. MATH_ACOS,
  45. MATH_ATAN,
  46. MATH_ATAN2,
  47. MATH_SQRT,
  48. MATH_FMOD,
  49. MATH_FPOSMOD,
  50. MATH_FLOOR,
  51. MATH_CEIL,
  52. MATH_ROUND,
  53. MATH_ABS,
  54. MATH_SIGN,
  55. MATH_POW,
  56. MATH_LOG,
  57. MATH_EXP,
  58. MATH_ISNAN,
  59. MATH_ISINF,
  60. MATH_EASE,
  61. MATH_DECIMALS,
  62. MATH_STEPIFY,
  63. MATH_LERP,
  64. MATH_INVERSE_LERP,
  65. MATH_RANGE_LERP,
  66. MATH_DECTIME,
  67. MATH_RANDOMIZE,
  68. MATH_RAND,
  69. MATH_RANDF,
  70. MATH_RANDOM,
  71. MATH_SEED,
  72. MATH_RANDSEED,
  73. MATH_DEG2RAD,
  74. MATH_RAD2DEG,
  75. MATH_LINEAR2DB,
  76. MATH_DB2LINEAR,
  77. MATH_POLAR2CARTESIAN,
  78. MATH_CARTESIAN2POLAR,
  79. MATH_WRAP,
  80. MATH_WRAPF,
  81. LOGIC_MAX,
  82. LOGIC_MIN,
  83. LOGIC_CLAMP,
  84. LOGIC_NEAREST_PO2,
  85. OBJ_WEAKREF,
  86. FUNC_FUNCREF,
  87. TYPE_CONVERT,
  88. TYPE_OF,
  89. TYPE_EXISTS,
  90. TEXT_CHAR,
  91. TEXT_STR,
  92. TEXT_PRINT,
  93. TEXT_PRINTERR,
  94. TEXT_PRINTRAW,
  95. VAR_TO_STR,
  96. STR_TO_VAR,
  97. VAR_TO_BYTES,
  98. BYTES_TO_VAR,
  99. COLORN,
  100. FUNC_MAX
  101. };
  102. static int get_func_argument_count(BuiltinFunc p_func);
  103. static String get_func_name(BuiltinFunc p_func);
  104. static void exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant *r_return, Variant::CallError &r_error, String &r_error_str);
  105. static BuiltinFunc find_function(const String &p_string);
  106. private:
  107. static const char *func_name[FUNC_MAX];
  108. struct Input {
  109. Variant::Type type;
  110. String name;
  111. Input() { type = Variant::NIL; }
  112. };
  113. Vector<Input> inputs;
  114. Variant::Type output_type;
  115. String expression;
  116. bool sequenced;
  117. int str_ofs;
  118. bool expression_dirty;
  119. bool _compile_expression();
  120. enum TokenType {
  121. TK_CURLY_BRACKET_OPEN,
  122. TK_CURLY_BRACKET_CLOSE,
  123. TK_BRACKET_OPEN,
  124. TK_BRACKET_CLOSE,
  125. TK_PARENTHESIS_OPEN,
  126. TK_PARENTHESIS_CLOSE,
  127. TK_IDENTIFIER,
  128. TK_BUILTIN_FUNC,
  129. TK_SELF,
  130. TK_CONSTANT,
  131. TK_BASIC_TYPE,
  132. TK_COLON,
  133. TK_COMMA,
  134. TK_PERIOD,
  135. TK_OP_IN,
  136. TK_OP_EQUAL,
  137. TK_OP_NOT_EQUAL,
  138. TK_OP_LESS,
  139. TK_OP_LESS_EQUAL,
  140. TK_OP_GREATER,
  141. TK_OP_GREATER_EQUAL,
  142. TK_OP_AND,
  143. TK_OP_OR,
  144. TK_OP_NOT,
  145. TK_OP_ADD,
  146. TK_OP_SUB,
  147. TK_OP_MUL,
  148. TK_OP_DIV,
  149. TK_OP_MOD,
  150. TK_OP_SHIFT_LEFT,
  151. TK_OP_SHIFT_RIGHT,
  152. TK_OP_BIT_AND,
  153. TK_OP_BIT_OR,
  154. TK_OP_BIT_XOR,
  155. TK_OP_BIT_INVERT,
  156. TK_INPUT,
  157. TK_EOF,
  158. TK_ERROR,
  159. TK_MAX
  160. };
  161. static const char *token_name[TK_MAX];
  162. struct Token {
  163. TokenType type;
  164. Variant value;
  165. };
  166. void _set_error(const String &p_err) {
  167. if (error_set)
  168. return;
  169. error_str = p_err;
  170. error_set = true;
  171. }
  172. Error _get_token(Token &r_token);
  173. String error_str;
  174. bool error_set;
  175. struct ENode {
  176. enum Type {
  177. TYPE_INPUT,
  178. TYPE_CONSTANT,
  179. TYPE_SELF,
  180. TYPE_OPERATOR,
  181. TYPE_INDEX,
  182. TYPE_NAMED_INDEX,
  183. TYPE_ARRAY,
  184. TYPE_DICTIONARY,
  185. TYPE_CONSTRUCTOR,
  186. TYPE_BUILTIN_FUNC,
  187. TYPE_CALL
  188. };
  189. ENode *next;
  190. Type type;
  191. ENode() { next = NULL; }
  192. virtual ~ENode() {
  193. if (next) {
  194. memdelete(next);
  195. }
  196. }
  197. };
  198. struct ExpressionNode {
  199. bool is_op;
  200. union {
  201. Variant::Operator op;
  202. ENode *node;
  203. };
  204. };
  205. ENode *_parse_expression();
  206. struct InputNode : public ENode {
  207. int index;
  208. InputNode() {
  209. type = TYPE_INPUT;
  210. }
  211. };
  212. struct ConstantNode : public ENode {
  213. Variant value;
  214. ConstantNode() {
  215. type = TYPE_CONSTANT;
  216. }
  217. };
  218. struct OperatorNode : public ENode {
  219. Variant::Operator op;
  220. ENode *nodes[2];
  221. OperatorNode() {
  222. type = TYPE_OPERATOR;
  223. }
  224. };
  225. struct SelfNode : public ENode {
  226. SelfNode() {
  227. type = TYPE_SELF;
  228. }
  229. };
  230. struct IndexNode : public ENode {
  231. ENode *base;
  232. ENode *index;
  233. IndexNode() {
  234. type = TYPE_INDEX;
  235. }
  236. };
  237. struct NamedIndexNode : public ENode {
  238. ENode *base;
  239. StringName name;
  240. NamedIndexNode() {
  241. type = TYPE_NAMED_INDEX;
  242. }
  243. };
  244. struct ConstructorNode : public ENode {
  245. Variant::Type data_type;
  246. Vector<ENode *> arguments;
  247. ConstructorNode() {
  248. type = TYPE_CONSTRUCTOR;
  249. }
  250. };
  251. struct CallNode : public ENode {
  252. ENode *base;
  253. StringName method;
  254. Vector<ENode *> arguments;
  255. CallNode() {
  256. type = TYPE_CALL;
  257. }
  258. };
  259. struct ArrayNode : public ENode {
  260. Vector<ENode *> array;
  261. ArrayNode() {
  262. type = TYPE_ARRAY;
  263. }
  264. };
  265. struct DictionaryNode : public ENode {
  266. Vector<ENode *> dict;
  267. DictionaryNode() {
  268. type = TYPE_DICTIONARY;
  269. }
  270. };
  271. struct BuiltinFuncNode : public ENode {
  272. BuiltinFunc func;
  273. Vector<ENode *> arguments;
  274. BuiltinFuncNode() {
  275. type = TYPE_BUILTIN_FUNC;
  276. }
  277. };
  278. template <class T>
  279. T *alloc_node() {
  280. T *node = memnew(T);
  281. node->next = nodes;
  282. nodes = node;
  283. return node;
  284. }
  285. ENode *root;
  286. ENode *nodes;
  287. Vector<String> input_names;
  288. bool execution_error;
  289. bool _execute(const Array &p_inputs, Object *p_instance, Expression::ENode *p_node, Variant &r_ret, String &r_error_str);
  290. protected:
  291. static void _bind_methods();
  292. public:
  293. Error parse(const String &p_expression, const Vector<String> &p_input_names = Vector<String>());
  294. Variant execute(Array p_inputs, Object *p_base = NULL, bool p_show_error = true);
  295. bool has_execute_failed() const;
  296. String get_error_text() const;
  297. Expression();
  298. ~Expression();
  299. };
  300. #endif // EXPRESSION_H