visual_script_expression.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**************************************************************************/
  2. /* visual_script_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 VISUAL_SCRIPT_EXPRESSION_H
  31. #define VISUAL_SCRIPT_EXPRESSION_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;
  39. String name;
  40. Input() { type = Variant::NIL; }
  41. };
  42. Vector<Input> inputs;
  43. Variant::Type output_type;
  44. String expression;
  45. bool sequenced;
  46. int str_ofs;
  47. bool expression_dirty;
  48. bool _compile_expression();
  49. enum TokenType {
  50. TK_CURLY_BRACKET_OPEN,
  51. TK_CURLY_BRACKET_CLOSE,
  52. TK_BRACKET_OPEN,
  53. TK_BRACKET_CLOSE,
  54. TK_PARENTHESIS_OPEN,
  55. TK_PARENTHESIS_CLOSE,
  56. TK_IDENTIFIER,
  57. TK_BUILTIN_FUNC,
  58. TK_SELF,
  59. TK_CONSTANT,
  60. TK_BASIC_TYPE,
  61. TK_COLON,
  62. TK_COMMA,
  63. TK_PERIOD,
  64. TK_OP_IN,
  65. TK_OP_EQUAL,
  66. TK_OP_NOT_EQUAL,
  67. TK_OP_LESS,
  68. TK_OP_LESS_EQUAL,
  69. TK_OP_GREATER,
  70. TK_OP_GREATER_EQUAL,
  71. TK_OP_AND,
  72. TK_OP_OR,
  73. TK_OP_NOT,
  74. TK_OP_ADD,
  75. TK_OP_SUB,
  76. TK_OP_MUL,
  77. TK_OP_DIV,
  78. TK_OP_MOD,
  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_EOF,
  86. TK_ERROR,
  87. TK_MAX
  88. };
  89. static const char *token_name[TK_MAX];
  90. struct Token {
  91. TokenType type;
  92. Variant value;
  93. };
  94. void _set_error(const String &p_err) {
  95. if (error_set) {
  96. return;
  97. }
  98. error_str = p_err;
  99. error_set = true;
  100. }
  101. Error _get_token(Token &r_token);
  102. String error_str;
  103. bool error_set;
  104. struct ENode {
  105. enum Type {
  106. TYPE_INPUT,
  107. TYPE_CONSTANT,
  108. TYPE_SELF,
  109. TYPE_OPERATOR,
  110. TYPE_INDEX,
  111. TYPE_NAMED_INDEX,
  112. TYPE_ARRAY,
  113. TYPE_DICTIONARY,
  114. TYPE_CONSTRUCTOR,
  115. TYPE_BUILTIN_FUNC,
  116. TYPE_CALL
  117. };
  118. ENode *next;
  119. Type type;
  120. ENode() { next = nullptr; }
  121. virtual ~ENode() {
  122. if (next) {
  123. memdelete(next);
  124. }
  125. }
  126. };
  127. struct Expression {
  128. bool is_op;
  129. union {
  130. Variant::Operator op;
  131. ENode *node;
  132. };
  133. };
  134. ENode *_parse_expression();
  135. struct InputNode : public ENode {
  136. int index;
  137. InputNode() {
  138. type = TYPE_INPUT;
  139. }
  140. };
  141. struct ConstantNode : public ENode {
  142. Variant value;
  143. ConstantNode() {
  144. type = TYPE_CONSTANT;
  145. }
  146. };
  147. struct OperatorNode : public ENode {
  148. Variant::Operator op;
  149. ENode *nodes[2];
  150. OperatorNode() {
  151. type = TYPE_OPERATOR;
  152. }
  153. };
  154. struct SelfNode : public ENode {
  155. SelfNode() {
  156. type = TYPE_SELF;
  157. }
  158. };
  159. struct IndexNode : public ENode {
  160. ENode *base;
  161. ENode *index;
  162. IndexNode() {
  163. type = TYPE_INDEX;
  164. }
  165. };
  166. struct NamedIndexNode : public ENode {
  167. ENode *base;
  168. StringName name;
  169. NamedIndexNode() {
  170. type = TYPE_NAMED_INDEX;
  171. }
  172. };
  173. struct ConstructorNode : public ENode {
  174. Variant::Type data_type;
  175. Vector<ENode *> arguments;
  176. ConstructorNode() {
  177. type = TYPE_CONSTRUCTOR;
  178. }
  179. };
  180. struct CallNode : public ENode {
  181. ENode *base;
  182. StringName method;
  183. Vector<ENode *> arguments;
  184. CallNode() {
  185. type = TYPE_CALL;
  186. }
  187. };
  188. struct ArrayNode : public ENode {
  189. Vector<ENode *> array;
  190. ArrayNode() {
  191. type = TYPE_ARRAY;
  192. }
  193. };
  194. struct DictionaryNode : public ENode {
  195. Vector<ENode *> dict;
  196. DictionaryNode() {
  197. type = TYPE_DICTIONARY;
  198. }
  199. };
  200. struct BuiltinFuncNode : public ENode {
  201. VisualScriptBuiltinFunc::BuiltinFunc func;
  202. Vector<ENode *> arguments;
  203. BuiltinFuncNode() {
  204. type = TYPE_BUILTIN_FUNC;
  205. }
  206. };
  207. template <class T>
  208. T *alloc_node() {
  209. T *node = memnew(T);
  210. node->next = nodes;
  211. nodes = node;
  212. return node;
  213. }
  214. ENode *root;
  215. ENode *nodes;
  216. protected:
  217. bool _set(const StringName &p_name, const Variant &p_value);
  218. bool _get(const StringName &p_name, Variant &r_ret) const;
  219. void _get_property_list(List<PropertyInfo> *p_list) const;
  220. public:
  221. virtual int get_output_sequence_port_count() const;
  222. virtual bool has_input_sequence_port() const;
  223. virtual String get_output_sequence_port_text(int p_port) const;
  224. virtual int get_input_value_port_count() const;
  225. virtual int get_output_value_port_count() const;
  226. virtual PropertyInfo get_input_value_port_info(int p_idx) const;
  227. virtual PropertyInfo get_output_value_port_info(int p_idx) const;
  228. virtual String get_caption() const;
  229. virtual String get_text() const;
  230. virtual String get_category() const { return "operators"; }
  231. virtual VisualScriptNodeInstance *instance(VisualScriptInstance *p_instance);
  232. VisualScriptExpression();
  233. ~VisualScriptExpression();
  234. };
  235. void register_visual_script_expression_node();
  236. #endif // VISUAL_SCRIPT_EXPRESSION_H