Skip to content

Commit

Permalink
Ability to pass custom variables to expression.
Browse files Browse the repository at this point in the history
  • Loading branch information
reduz committed Aug 8, 2018
1 parent 934c641 commit a71a5fc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
30 changes: 23 additions & 7 deletions core/math/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion core/math/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,16 @@ class Expression : public Reference {
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);

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;
Expand Down

0 comments on commit a71a5fc

Please sign in to comment.