Ability to pass custom variables to expression.

This commit is contained in:
Juan Linietsky 2018-08-08 17:47:51 -03:00
parent 934c641a15
commit a71a5fc0c3
2 changed files with 26 additions and 8 deletions

View File

@ -1350,11 +1350,26 @@ Expression::ENode *Expression::_parse_expression() {
//named indexing
str_ofs = cofs;
NamedIndexNode *index = alloc_node<NamedIndexNode>();
SelfNode *self_node = alloc_node<SelfNode>();
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<InputNode>();
input->index = input_index;
expr = input;
} else {
NamedIndexNode *index = alloc_node<NamedIndexNode>();
SelfNode *self_node = alloc_node<SelfNode>();
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<String> &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<String>()));
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);

View File

@ -304,6 +304,8 @@ private:
ENode *root;
ENode *nodes;
Vector<String> 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<String> &p_input_names = Vector<String>());
Variant execute(Array p_inputs, Object *p_base = NULL, bool p_show_error = true);
bool has_execute_failed() const;
String get_error_text() const;