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-2020 Juan Linietsky, Ariel Manzur. */ 9 /* Copyright (c) 2014-2020 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 31 #ifndef VISUALSCRIPTEXPRESSION_H 32 #define VISUALSCRIPTEXPRESSION_H 33 34 #include "visual_script.h" 35 #include "visual_script_builtin_funcs.h" 36 37 class VisualScriptExpression : public VisualScriptNode { 38 39 GDCLASS(VisualScriptExpression, VisualScriptNode); 40 friend class VisualScriptNodeInstanceExpression; 41 42 struct Input { 43 44 Variant::Type type; 45 String name; 46 InputInput47 Input() { type = Variant::NIL; } 48 }; 49 50 Vector<Input> inputs; 51 Variant::Type output_type; 52 53 String expression; 54 55 bool sequenced; 56 int str_ofs; 57 bool expression_dirty; 58 59 bool _compile_expression(); 60 61 enum TokenType { 62 TK_CURLY_BRACKET_OPEN, 63 TK_CURLY_BRACKET_CLOSE, 64 TK_BRACKET_OPEN, 65 TK_BRACKET_CLOSE, 66 TK_PARENTHESIS_OPEN, 67 TK_PARENTHESIS_CLOSE, 68 TK_IDENTIFIER, 69 TK_BUILTIN_FUNC, 70 TK_SELF, 71 TK_CONSTANT, 72 TK_BASIC_TYPE, 73 TK_COLON, 74 TK_COMMA, 75 TK_PERIOD, 76 TK_OP_IN, 77 TK_OP_EQUAL, 78 TK_OP_NOT_EQUAL, 79 TK_OP_LESS, 80 TK_OP_LESS_EQUAL, 81 TK_OP_GREATER, 82 TK_OP_GREATER_EQUAL, 83 TK_OP_AND, 84 TK_OP_OR, 85 TK_OP_NOT, 86 TK_OP_ADD, 87 TK_OP_SUB, 88 TK_OP_MUL, 89 TK_OP_DIV, 90 TK_OP_MOD, 91 TK_OP_SHIFT_LEFT, 92 TK_OP_SHIFT_RIGHT, 93 TK_OP_BIT_AND, 94 TK_OP_BIT_OR, 95 TK_OP_BIT_XOR, 96 TK_OP_BIT_INVERT, 97 TK_EOF, 98 TK_ERROR, 99 TK_MAX 100 }; 101 102 static const char *token_name[TK_MAX]; 103 struct Token { 104 105 TokenType type; 106 Variant value; 107 }; 108 _set_error(const String & p_err)109 void _set_error(const String &p_err) { 110 if (error_set) 111 return; 112 error_str = p_err; 113 error_set = true; 114 } 115 116 Error _get_token(Token &r_token); 117 118 String error_str; 119 bool error_set; 120 121 struct ENode { 122 123 enum Type { 124 TYPE_INPUT, 125 TYPE_CONSTANT, 126 TYPE_SELF, 127 TYPE_OPERATOR, 128 TYPE_INDEX, 129 TYPE_NAMED_INDEX, 130 TYPE_ARRAY, 131 TYPE_DICTIONARY, 132 TYPE_CONSTRUCTOR, 133 TYPE_BUILTIN_FUNC, 134 TYPE_CALL 135 }; 136 137 ENode *next; 138 139 Type type; 140 ENodeENode141 ENode() { next = NULL; } ~ENodeENode142 virtual ~ENode() { 143 if (next) { 144 memdelete(next); 145 } 146 } 147 }; 148 149 struct Expression { 150 151 bool is_op; 152 union { 153 Variant::Operator op; 154 ENode *node; 155 }; 156 }; 157 158 ENode *_parse_expression(); 159 160 struct InputNode : public ENode { 161 162 int index; InputNodeInputNode163 InputNode() { 164 type = TYPE_INPUT; 165 } 166 }; 167 168 struct ConstantNode : public ENode { 169 170 Variant value; ConstantNodeConstantNode171 ConstantNode() { 172 type = TYPE_CONSTANT; 173 } 174 }; 175 176 struct OperatorNode : public ENode { 177 178 Variant::Operator op; 179 180 ENode *nodes[2]; 181 OperatorNodeOperatorNode182 OperatorNode() { 183 type = TYPE_OPERATOR; 184 } 185 }; 186 187 struct SelfNode : public ENode { 188 SelfNodeSelfNode189 SelfNode() { 190 type = TYPE_SELF; 191 } 192 }; 193 194 struct IndexNode : public ENode { 195 ENode *base; 196 ENode *index; 197 IndexNodeIndexNode198 IndexNode() { 199 type = TYPE_INDEX; 200 } 201 }; 202 203 struct NamedIndexNode : public ENode { 204 ENode *base; 205 StringName name; 206 NamedIndexNodeNamedIndexNode207 NamedIndexNode() { 208 type = TYPE_NAMED_INDEX; 209 } 210 }; 211 212 struct ConstructorNode : public ENode { 213 Variant::Type data_type; 214 Vector<ENode *> arguments; 215 ConstructorNodeConstructorNode216 ConstructorNode() { 217 type = TYPE_CONSTRUCTOR; 218 } 219 }; 220 221 struct CallNode : public ENode { 222 ENode *base; 223 StringName method; 224 Vector<ENode *> arguments; 225 CallNodeCallNode226 CallNode() { 227 type = TYPE_CALL; 228 } 229 }; 230 231 struct ArrayNode : public ENode { 232 Vector<ENode *> array; ArrayNodeArrayNode233 ArrayNode() { 234 type = TYPE_ARRAY; 235 } 236 }; 237 238 struct DictionaryNode : public ENode { 239 Vector<ENode *> dict; DictionaryNodeDictionaryNode240 DictionaryNode() { 241 type = TYPE_DICTIONARY; 242 } 243 }; 244 245 struct BuiltinFuncNode : public ENode { 246 VisualScriptBuiltinFunc::BuiltinFunc func; 247 Vector<ENode *> arguments; BuiltinFuncNodeBuiltinFuncNode248 BuiltinFuncNode() { 249 type = TYPE_BUILTIN_FUNC; 250 } 251 }; 252 253 template <class T> alloc_node()254 T *alloc_node() { 255 T *node = memnew(T); 256 node->next = nodes; 257 nodes = node; 258 return node; 259 } 260 261 ENode *root; 262 ENode *nodes; 263 264 protected: 265 bool _set(const StringName &p_name, const Variant &p_value); 266 bool _get(const StringName &p_name, Variant &r_ret) const; 267 void _get_property_list(List<PropertyInfo> *p_list) const; 268 269 public: 270 virtual int get_output_sequence_port_count() const; 271 virtual bool has_input_sequence_port() const; 272 273 virtual String get_output_sequence_port_text(int p_port) const; 274 275 virtual int get_input_value_port_count() const; 276 virtual int get_output_value_port_count() const; 277 278 virtual PropertyInfo get_input_value_port_info(int p_idx) const; 279 virtual PropertyInfo get_output_value_port_info(int p_idx) const; 280 281 virtual String get_caption() const; 282 virtual String get_text() const; get_category()283 virtual String get_category() const { return "operators"; } 284 285 virtual VisualScriptNodeInstance *instance(VisualScriptInstance *p_instance); 286 287 VisualScriptExpression(); 288 ~VisualScriptExpression(); 289 }; 290 291 void register_visual_script_expression_node(); 292 293 #endif // VISUALSCRIPTEXPRESSION_H 294