visual_script_expression.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #ifndef VISUALSCRIPTEXPRESSION_H
  2. #define VISUALSCRIPTEXPRESSION_H
  3. #include "visual_script.h"
  4. #include "visual_script_builtin_funcs.h"
  5. class VisualScriptExpression : public VisualScriptNode {
  6. GDCLASS(VisualScriptExpression,VisualScriptNode)
  7. friend class VisualScriptNodeInstanceExpression;
  8. struct Input {
  9. Variant::Type type;
  10. String name;
  11. Input() { type=Variant::NIL; }
  12. };
  13. Vector<Input> inputs;
  14. Variant::Type output_type;
  15. String expression;
  16. bool sequenced;
  17. int str_ofs;
  18. bool expression_dirty;
  19. bool _compile_expression();
  20. enum TokenType {
  21. TK_CURLY_BRACKET_OPEN,
  22. TK_CURLY_BRACKET_CLOSE,
  23. TK_BRACKET_OPEN,
  24. TK_BRACKET_CLOSE,
  25. TK_PARENTHESIS_OPEN,
  26. TK_PARENTHESIS_CLOSE,
  27. TK_IDENTIFIER,
  28. TK_BUILTIN_FUNC,
  29. TK_SELF,
  30. TK_CONSTANT,
  31. TK_BASIC_TYPE,
  32. TK_COLON,
  33. TK_COMMA,
  34. TK_PERIOD,
  35. TK_OP_IN,
  36. TK_OP_EQUAL,
  37. TK_OP_NOT_EQUAL,
  38. TK_OP_LESS,
  39. TK_OP_LESS_EQUAL,
  40. TK_OP_GREATER,
  41. TK_OP_GREATER_EQUAL,
  42. TK_OP_AND,
  43. TK_OP_OR,
  44. TK_OP_NOT,
  45. TK_OP_ADD,
  46. TK_OP_SUB,
  47. TK_OP_MUL,
  48. TK_OP_DIV,
  49. TK_OP_MOD,
  50. TK_OP_SHIFT_LEFT,
  51. TK_OP_SHIFT_RIGHT,
  52. TK_OP_BIT_AND,
  53. TK_OP_BIT_OR,
  54. TK_OP_BIT_XOR,
  55. TK_OP_BIT_INVERT,
  56. TK_EOF,
  57. TK_ERROR,
  58. TK_MAX
  59. };
  60. static const char* token_name[TK_MAX];
  61. struct Token {
  62. TokenType type;
  63. Variant value;
  64. };
  65. void _set_error(const String& p_err) {
  66. if (error_set)
  67. return;
  68. error_str=p_err;
  69. error_set=true;
  70. }
  71. Error _get_token(Token& r_token);
  72. String error_str;
  73. bool error_set;
  74. struct ENode {
  75. enum Type {
  76. TYPE_INPUT,
  77. TYPE_CONSTANT,
  78. TYPE_SELF,
  79. TYPE_OPERATOR,
  80. TYPE_INDEX,
  81. TYPE_NAMED_INDEX,
  82. TYPE_ARRAY,
  83. TYPE_DICTIONARY,
  84. TYPE_CONSTRUCTOR,
  85. TYPE_BUILTIN_FUNC,
  86. TYPE_CALL
  87. };
  88. ENode *next;
  89. Type type;
  90. ENode() { next=NULL; }
  91. virtual ~ENode() { if (next) { memdelete(next); } }
  92. };
  93. struct Expression {
  94. bool is_op;
  95. union {
  96. Variant::Operator op;
  97. ENode *node;
  98. };
  99. };
  100. ENode* _parse_expression();
  101. struct InputNode : public ENode {
  102. int index;
  103. InputNode() {
  104. type=TYPE_INPUT;
  105. }
  106. };
  107. struct ConstantNode : public ENode {
  108. Variant value;
  109. ConstantNode() {
  110. type=TYPE_CONSTANT;
  111. }
  112. };
  113. struct OperatorNode : public ENode {
  114. Variant::Operator op;
  115. ENode* nodes[2];
  116. OperatorNode() {
  117. type=TYPE_OPERATOR;
  118. }
  119. };
  120. struct SelfNode : public ENode {
  121. SelfNode() {
  122. type=TYPE_SELF;
  123. }
  124. };
  125. struct IndexNode : public ENode {
  126. ENode*base;
  127. ENode*index;
  128. IndexNode() {
  129. type=TYPE_INDEX;
  130. }
  131. };
  132. struct NamedIndexNode : public ENode {
  133. ENode*base;
  134. StringName name;
  135. NamedIndexNode() {
  136. type=TYPE_NAMED_INDEX;
  137. }
  138. };
  139. struct ConstructorNode : public ENode {
  140. Variant::Type data_type;
  141. Vector<ENode*> arguments;
  142. ConstructorNode() {
  143. type=TYPE_CONSTRUCTOR;
  144. }
  145. };
  146. struct CallNode : public ENode {
  147. ENode*base;
  148. StringName method;
  149. Vector<ENode*> arguments;
  150. CallNode() {
  151. type=TYPE_CALL;
  152. }
  153. };
  154. struct ArrayNode : public ENode {
  155. Vector<ENode*> array;
  156. ArrayNode() {
  157. type=TYPE_ARRAY;
  158. }
  159. };
  160. struct DictionaryNode : public ENode {
  161. Vector<ENode*> dict;
  162. DictionaryNode() {
  163. type=TYPE_DICTIONARY;
  164. }
  165. };
  166. struct BuiltinFuncNode : public ENode {
  167. VisualScriptBuiltinFunc::BuiltinFunc func;
  168. Vector<ENode*> arguments;
  169. BuiltinFuncNode() {
  170. type=TYPE_BUILTIN_FUNC;
  171. }
  172. };
  173. template<class T>
  174. T* alloc_node() {
  175. T* node = memnew(T);
  176. node->next=nodes;
  177. nodes=node;
  178. return node;
  179. }
  180. ENode *root;
  181. ENode *nodes;
  182. protected:
  183. bool _set(const StringName& p_name, const Variant& p_value);
  184. bool _get(const StringName& p_name,Variant &r_ret) const;
  185. void _get_property_list( List<PropertyInfo> *p_list) const;
  186. public:
  187. virtual int get_output_sequence_port_count() const;
  188. virtual bool has_input_sequence_port() const;
  189. virtual String get_output_sequence_port_text(int p_port) const;
  190. virtual int get_input_value_port_count() const;
  191. virtual int get_output_value_port_count() const;
  192. virtual PropertyInfo get_input_value_port_info(int p_idx) const;
  193. virtual PropertyInfo get_output_value_port_info(int p_idx) const;
  194. virtual String get_caption() const;
  195. virtual String get_text() const;
  196. virtual String get_category() const { return "operators"; }
  197. virtual VisualScriptNodeInstance* instance(VisualScriptInstance* p_instance);
  198. VisualScriptExpression();
  199. ~VisualScriptExpression();
  200. };
  201. void register_visual_script_expression_node();
  202. #endif // VISUALSCRIPTEXPRESSION_H