From a71a5fc0c3f5d487d3f18c3b9e9a768052795e86 Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Wed, 8 Aug 2018 17:47:51 -0300 Subject: [PATCH] Ability to pass custom variables to expression. --- core/math/expression.cpp | 30 +++++++++++++++++++++++------- core/math/expression.h | 4 +++- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/core/math/expression.cpp b/core/math/expression.cpp index c6276151fca..b92cde07ceb 100644 --- a/core/math/expression.cpp +++ b/core/math/expression.cpp @@ -1350,11 +1350,26 @@ Expression::ENode *Expression::_parse_expression() { //named indexing str_ofs = cofs; - NamedIndexNode *index = alloc_node(); - SelfNode *self_node = alloc_node(); - index->base = self_node; - index->name = identifier; - expr = index; + int input_index = -1; + for (int i = 0; i < input_names.size(); i++) { + if (input_names[i] == identifier) { + input_index = i; + break; + } + } + + if (input_index != -1) { + InputNode *input = alloc_node(); + input->index = input_index; + expr = input; + } else { + + NamedIndexNode *index = alloc_node(); + SelfNode *self_node = alloc_node(); + index->base = self_node; + index->name = identifier; + expr = index; + } } } break; case TK_INPUT: { @@ -2042,7 +2057,7 @@ bool Expression::_execute(const Array &p_inputs, Object *p_instance, Expression: return false; } -Error Expression::parse(const String &p_expression) { +Error Expression::parse(const String &p_expression, const Vector &p_input_names) { if (nodes) { memdelete(nodes); @@ -2053,6 +2068,7 @@ Error Expression::parse(const String &p_expression) { error_str = String(); error_set = false; str_ofs = 0; + input_names = p_input_names; expression = p_expression; root = _parse_expression(); @@ -2097,7 +2113,7 @@ String Expression::get_error_text() const { void Expression::_bind_methods() { - ClassDB::bind_method(D_METHOD("parse", "expression"), &Expression::parse); + ClassDB::bind_method(D_METHOD("parse", "expression", "input_names"), &Expression::parse, DEFVAL(Vector())); ClassDB::bind_method(D_METHOD("execute", "inputs", "base_instance", "show_error"), &Expression::execute, DEFVAL(Array()), DEFVAL(NULL), DEFVAL(true)); ClassDB::bind_method(D_METHOD("has_execute_failed"), &Expression::has_execute_failed); ClassDB::bind_method(D_METHOD("get_error_text"), &Expression::get_error_text); diff --git a/core/math/expression.h b/core/math/expression.h index b0de6ca6dbf..7a7639cf0bf 100644 --- a/core/math/expression.h +++ b/core/math/expression.h @@ -304,6 +304,8 @@ private: ENode *root; ENode *nodes; + Vector input_names; + bool execution_error; bool _execute(const Array &p_inputs, Object *p_instance, Expression::ENode *p_node, Variant &r_ret, String &r_error_str); @@ -311,7 +313,7 @@ protected: static void _bind_methods(); public: - Error parse(const String &p_expression); + Error parse(const String &p_expression, const Vector &p_input_names = Vector()); Variant execute(Array p_inputs, Object *p_base = NULL, bool p_show_error = true); bool has_execute_failed() const; String get_error_text() const;