visual_script_expression.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*************************************************************************/
  2. /* visual_script_expression.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 VISUALSCRIPTEXPRESSION_H
  31. #define VISUALSCRIPTEXPRESSION_H
  32. #include "visual_script.h"
  33. #include "visual_script_builtin_funcs.h"
  34. class VisualScriptExpression : public VisualScriptNode {
  35. GDCLASS(VisualScriptExpression, VisualScriptNode);
  36. friend class VisualScriptNodeInstanceExpression;
  37. struct Input {
  38. Variant::Type type = Variant::NIL;
  39. String name;
  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 = true;
  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_SHIFT_LEFT,
  79. TK_OP_SHIFT_RIGHT,
  80. TK_OP_BIT_AND,
  81. TK_OP_BIT_OR,
  82. TK_OP_BIT_XOR,
  83. TK_OP_BIT_INVERT,
  84. TK_EOF,
  85. TK_ERROR,
  86. TK_MAX
  87. };
  88. static const char *token_name[TK_MAX];
  89. struct Token {
  90. TokenType type;
  91. Variant value;
  92. };
  93. void _set_error(const String &p_err) {
  94. if (error_set) {
  95. return;
  96. }
  97. error_str = p_err;
  98. error_set = true;
  99. }
  100. Error _get_token(Token &r_token);
  101. String error_str;
  102. bool error_set = true;
  103. struct ENode {
  104. enum Type {
  105. TYPE_INPUT,
  106. TYPE_CONSTANT,
  107. TYPE_SELF,
  108. TYPE_OPERATOR,
  109. TYPE_INDEX,
  110. TYPE_NAMED_INDEX,
  111. TYPE_ARRAY,
  112. TYPE_DICTIONARY,
  113. TYPE_CONSTRUCTOR,
  114. TYPE_BUILTIN_FUNC,
  115. TYPE_CALL
  116. };
  117. ENode *next = nullptr;
  118. Type type = Type::TYPE_SELF;
  119. virtual ~ENode() {
  120. if (next) {
  121. memdelete(next);
  122. }
  123. }
  124. };
  125. struct Expression {
  126. bool is_op = false;
  127. union {
  128. Variant::Operator op;
  129. ENode *node = nullptr;
  130. };
  131. };
  132. ENode *_parse_expression();
  133. struct InputNode : public ENode {
  134. int index = 0;
  135. InputNode() {
  136. type = TYPE_INPUT;
  137. }
  138. };
  139. struct ConstantNode : public ENode {
  140. Variant value;
  141. ConstantNode() {
  142. type = TYPE_CONSTANT;
  143. }
  144. };
  145. struct OperatorNode : public ENode {
  146. Variant::Operator op = Variant::Operator::OP_ADD;
  147. ENode *nodes[2] = { nullptr, nullptr };
  148. OperatorNode() {
  149. type = TYPE_OPERATOR;
  150. }
  151. };
  152. struct SelfNode : public ENode {
  153. SelfNode() {
  154. type = TYPE_SELF;
  155. }
  156. };
  157. struct IndexNode : public ENode {
  158. ENode *base = nullptr;
  159. ENode *index = nullptr;
  160. IndexNode() {
  161. type = TYPE_INDEX;
  162. }
  163. };
  164. struct NamedIndexNode : public ENode {
  165. ENode *base = nullptr;
  166. StringName name;
  167. NamedIndexNode() {
  168. type = TYPE_NAMED_INDEX;
  169. }
  170. };
  171. struct ConstructorNode : public ENode {
  172. Variant::Type data_type = Variant::Type::NIL;
  173. Vector<ENode *> arguments;
  174. ConstructorNode() {
  175. type = TYPE_CONSTRUCTOR;
  176. }
  177. };
  178. struct CallNode : public ENode {
  179. ENode *base = nullptr;
  180. StringName method;
  181. Vector<ENode *> arguments;
  182. CallNode() {
  183. type = TYPE_CALL;
  184. }
  185. };
  186. struct ArrayNode : public ENode {
  187. Vector<ENode *> array;
  188. ArrayNode() {
  189. type = TYPE_ARRAY;
  190. }
  191. };
  192. struct DictionaryNode : public ENode {
  193. Vector<ENode *> dict;
  194. DictionaryNode() {
  195. type = TYPE_DICTIONARY;
  196. }
  197. };
  198. struct BuiltinFuncNode : public ENode {
  199. VisualScriptBuiltinFunc::BuiltinFunc func = VisualScriptBuiltinFunc::BuiltinFunc::BYTES_TO_VAR;
  200. Vector<ENode *> arguments;
  201. BuiltinFuncNode() {
  202. type = TYPE_BUILTIN_FUNC;
  203. }
  204. };
  205. template <class T>
  206. T *alloc_node() {
  207. T *node = memnew(T);
  208. node->next = nodes;
  209. nodes = node;
  210. return node;
  211. }
  212. ENode *root = nullptr;
  213. ENode *nodes = nullptr;
  214. protected:
  215. bool _set(const StringName &p_name, const Variant &p_value);
  216. bool _get(const StringName &p_name, Variant &r_ret) const;
  217. void _get_property_list(List<PropertyInfo> *p_list) const;
  218. public:
  219. virtual void reset_state() override;
  220. virtual int get_output_sequence_port_count() const override;
  221. virtual bool has_input_sequence_port() const override;
  222. virtual String get_output_sequence_port_text(int p_port) const override;
  223. virtual int get_input_value_port_count() const override;
  224. virtual int get_output_value_port_count() const override;
  225. virtual PropertyInfo get_input_value_port_info(int p_idx) const override;
  226. virtual PropertyInfo get_output_value_port_info(int p_idx) const override;
  227. virtual String get_caption() const override;
  228. virtual String get_text() const override;
  229. virtual String get_category() const override { return "operators"; }
  230. virtual VisualScriptNodeInstance *instance(VisualScriptInstance *p_instance) override;
  231. VisualScriptExpression();
  232. ~VisualScriptExpression();
  233. };
  234. void register_visual_script_expression_node();
  235. #endif // VISUALSCRIPTEXPRESSION_H