visual_script_expression.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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;
  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. error_str = p_err;
  98. error_set = true;
  99. }
  100. Error _get_token(Token &r_token);
  101. String error_str;
  102. bool error_set;
  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;
  118. Type type;
  119. ENode() { next = NULL; }
  120. virtual ~ENode() {
  121. if (next) {
  122. memdelete(next);
  123. }
  124. }
  125. };
  126. struct Expression {
  127. bool is_op;
  128. union {
  129. Variant::Operator op;
  130. ENode *node;
  131. };
  132. };
  133. ENode *_parse_expression();
  134. struct InputNode : public ENode {
  135. int index;
  136. InputNode() {
  137. type = TYPE_INPUT;
  138. }
  139. };
  140. struct ConstantNode : public ENode {
  141. Variant value;
  142. ConstantNode() {
  143. type = TYPE_CONSTANT;
  144. }
  145. };
  146. struct OperatorNode : public ENode {
  147. Variant::Operator op;
  148. ENode *nodes[2];
  149. OperatorNode() {
  150. type = TYPE_OPERATOR;
  151. }
  152. };
  153. struct SelfNode : public ENode {
  154. SelfNode() {
  155. type = TYPE_SELF;
  156. }
  157. };
  158. struct IndexNode : public ENode {
  159. ENode *base;
  160. ENode *index;
  161. IndexNode() {
  162. type = TYPE_INDEX;
  163. }
  164. };
  165. struct NamedIndexNode : public ENode {
  166. ENode *base;
  167. StringName name;
  168. NamedIndexNode() {
  169. type = TYPE_NAMED_INDEX;
  170. }
  171. };
  172. struct ConstructorNode : public ENode {
  173. Variant::Type data_type;
  174. Vector<ENode *> arguments;
  175. ConstructorNode() {
  176. type = TYPE_CONSTRUCTOR;
  177. }
  178. };
  179. struct CallNode : public ENode {
  180. ENode *base;
  181. StringName method;
  182. Vector<ENode *> arguments;
  183. CallNode() {
  184. type = TYPE_CALL;
  185. }
  186. };
  187. struct ArrayNode : public ENode {
  188. Vector<ENode *> array;
  189. ArrayNode() {
  190. type = TYPE_ARRAY;
  191. }
  192. };
  193. struct DictionaryNode : public ENode {
  194. Vector<ENode *> dict;
  195. DictionaryNode() {
  196. type = TYPE_DICTIONARY;
  197. }
  198. };
  199. struct BuiltinFuncNode : public ENode {
  200. VisualScriptBuiltinFunc::BuiltinFunc func;
  201. Vector<ENode *> arguments;
  202. BuiltinFuncNode() {
  203. type = TYPE_BUILTIN_FUNC;
  204. }
  205. };
  206. template <class T>
  207. T *alloc_node() {
  208. T *node = memnew(T);
  209. node->next = nodes;
  210. nodes = node;
  211. return node;
  212. }
  213. ENode *root;
  214. ENode *nodes;
  215. protected:
  216. bool _set(const StringName &p_name, const Variant &p_value);
  217. bool _get(const StringName &p_name, Variant &r_ret) const;
  218. void _get_property_list(List<PropertyInfo> *p_list) const;
  219. public:
  220. virtual int get_output_sequence_port_count() const;
  221. virtual bool has_input_sequence_port() const;
  222. virtual String get_output_sequence_port_text(int p_port) const;
  223. virtual int get_input_value_port_count() const;
  224. virtual int get_output_value_port_count() const;
  225. virtual PropertyInfo get_input_value_port_info(int p_idx) const;
  226. virtual PropertyInfo get_output_value_port_info(int p_idx) const;
  227. virtual String get_caption() const;
  228. virtual String get_text() const;
  229. virtual String get_category() const { return "operators"; }
  230. virtual VisualScriptNodeInstance *instance(VisualScriptInstance *p_instance);
  231. VisualScriptExpression();
  232. ~VisualScriptExpression();
  233. };
  234. void register_visual_script_expression_node();
  235. #endif // VISUALSCRIPTEXPRESSION_H