visual_script_expression.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*************************************************************************/
  2. /* visual_script_expression.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef VISUALSCRIPTEXPRESSION_H
  30. #define VISUALSCRIPTEXPRESSION_H
  31. #include "visual_script.h"
  32. #include "visual_script_builtin_funcs.h"
  33. class VisualScriptExpression : public VisualScriptNode {
  34. GDCLASS(VisualScriptExpression,VisualScriptNode)
  35. friend class VisualScriptNodeInstanceExpression;
  36. struct Input {
  37. Variant::Type type;
  38. String name;
  39. Input() { type=Variant::NIL; }
  40. };
  41. Vector<Input> inputs;
  42. Variant::Type output_type;
  43. String expression;
  44. bool sequenced;
  45. int str_ofs;
  46. bool expression_dirty;
  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. error_str=p_err;
  97. error_set=true;
  98. }
  99. Error _get_token(Token& r_token);
  100. String error_str;
  101. bool error_set;
  102. struct ENode {
  103. enum Type {
  104. TYPE_INPUT,
  105. TYPE_CONSTANT,
  106. TYPE_SELF,
  107. TYPE_OPERATOR,
  108. TYPE_INDEX,
  109. TYPE_NAMED_INDEX,
  110. TYPE_ARRAY,
  111. TYPE_DICTIONARY,
  112. TYPE_CONSTRUCTOR,
  113. TYPE_BUILTIN_FUNC,
  114. TYPE_CALL
  115. };
  116. ENode *next;
  117. Type type;
  118. ENode() { next=NULL; }
  119. virtual ~ENode() { if (next) { memdelete(next); } }
  120. };
  121. struct Expression {
  122. bool is_op;
  123. union {
  124. Variant::Operator op;
  125. ENode *node;
  126. };
  127. };
  128. ENode* _parse_expression();
  129. struct InputNode : public ENode {
  130. int index;
  131. InputNode() {
  132. type=TYPE_INPUT;
  133. }
  134. };
  135. struct ConstantNode : public ENode {
  136. Variant value;
  137. ConstantNode() {
  138. type=TYPE_CONSTANT;
  139. }
  140. };
  141. struct OperatorNode : public ENode {
  142. Variant::Operator op;
  143. ENode* nodes[2];
  144. OperatorNode() {
  145. type=TYPE_OPERATOR;
  146. }
  147. };
  148. struct SelfNode : public ENode {
  149. SelfNode() {
  150. type=TYPE_SELF;
  151. }
  152. };
  153. struct IndexNode : public ENode {
  154. ENode*base;
  155. ENode*index;
  156. IndexNode() {
  157. type=TYPE_INDEX;
  158. }
  159. };
  160. struct NamedIndexNode : public ENode {
  161. ENode*base;
  162. StringName name;
  163. NamedIndexNode() {
  164. type=TYPE_NAMED_INDEX;
  165. }
  166. };
  167. struct ConstructorNode : public ENode {
  168. Variant::Type data_type;
  169. Vector<ENode*> arguments;
  170. ConstructorNode() {
  171. type=TYPE_CONSTRUCTOR;
  172. }
  173. };
  174. struct CallNode : public ENode {
  175. ENode*base;
  176. StringName method;
  177. Vector<ENode*> arguments;
  178. CallNode() {
  179. type=TYPE_CALL;
  180. }
  181. };
  182. struct ArrayNode : public ENode {
  183. Vector<ENode*> array;
  184. ArrayNode() {
  185. type=TYPE_ARRAY;
  186. }
  187. };
  188. struct DictionaryNode : public ENode {
  189. Vector<ENode*> dict;
  190. DictionaryNode() {
  191. type=TYPE_DICTIONARY;
  192. }
  193. };
  194. struct BuiltinFuncNode : public ENode {
  195. VisualScriptBuiltinFunc::BuiltinFunc func;
  196. Vector<ENode*> arguments;
  197. BuiltinFuncNode() {
  198. type=TYPE_BUILTIN_FUNC;
  199. }
  200. };
  201. template<class T>
  202. T* alloc_node() {
  203. T* node = memnew(T);
  204. node->next=nodes;
  205. nodes=node;
  206. return node;
  207. }
  208. ENode *root;
  209. ENode *nodes;
  210. protected:
  211. bool _set(const StringName& p_name, const Variant& p_value);
  212. bool _get(const StringName& p_name,Variant &r_ret) const;
  213. void _get_property_list( List<PropertyInfo> *p_list) const;
  214. public:
  215. virtual int get_output_sequence_port_count() const;
  216. virtual bool has_input_sequence_port() const;
  217. virtual String get_output_sequence_port_text(int p_port) const;
  218. virtual int get_input_value_port_count() const;
  219. virtual int get_output_value_port_count() const;
  220. virtual PropertyInfo get_input_value_port_info(int p_idx) const;
  221. virtual PropertyInfo get_output_value_port_info(int p_idx) const;
  222. virtual String get_caption() const;
  223. virtual String get_text() const;
  224. virtual String get_category() const { return "operators"; }
  225. virtual VisualScriptNodeInstance* instance(VisualScriptInstance* p_instance);
  226. VisualScriptExpression();
  227. ~VisualScriptExpression();
  228. };
  229. void register_visual_script_expression_node();
  230. #endif // VISUALSCRIPTEXPRESSION_H