The Parallel Nightmare Saga: Expressions

Expressions are not as well documented as they should. But I was able to get some more stuff done on this matter.

First. A revision of the current documentation, using my findings.

Executes the expression that was previously parsed by parse and returns the result. Before you use the returned object, you should check if the method failed by calling has_execute_failed.

If you defined input variables in parse, you can specify their values in the inputs array, in the same order.

If you pass an object in base_instance the function will be looked up and exectued in that object. 

Parses the expression and returns an Error code.

You can optionally specify names of variables that may appear in the expression with input_names, so that you can bind them when it gets executed.

 

Trying with a simple funtion worked:

func _init():
    var expr = Expression.new()
    var inputs = ["output"]
    var try_with = [24, "peperonni"]
    expr.parse("print(misc(output))", inputs)
    expr.execute(try_with, self)
   
   
func misc(ind):
    print("Parsed!", ind)

Prints: Parsed!24
Now let's try with code. A modified version of Triv Decision:

func _init():
    var expr = Expression.new()
    var inputs = ["output"]
    var long_input = ["input_data", "weight_1", "weight_2", "weight_3"]
    var long_string = """
    if max(max(weight_1,weight_2),weight_3) == weight_1:
        print(1, input_data)
    elif max(max(weight_1,weight_2),weight_3) == weight_2:
        print(2, input_data)
    elif max(max(weight_1,weight_2),weight_3) == weight_3:
        print(3, input_data)
"""
    var try_with = [24, 11,2,3]
    
    expr.parse(long_string, long_input)
    expr.execute(try_with, self)

Unfortunately with this code, nothing happens, that tells me that there's no way for it to parse logic which is an importan part of the project.

So I can't define the whole function there, but I could of course execute it there.
That means that the function should be stored somewhere.