Pocket Plane Group
Friends and Neighbors => Weimer Republic (WeiDU.org) => WeiDU => Topic started by: Argent77 on January 28, 2018, 05:47:38 AM
-
It's currently not possible to return more than a fixed list of variables defined by the RET keyword from WeiDU functions. This limitation makes it impossible to return dynamic data structures, such as arrays, where the final number and/or name may not be known in advance.
Would it be feasible to add a feature that allows us to return dynamically created variables, e.g. by using a new keyword that adds them to the RET list at runtime?
Example:
DEFINE_PATCH_FUNCTION array_generator
INT_VAR
array_size = 10
RET
array
BEGIN
FOR (i = 0; i < array_size; ++i) BEGIN
SET EVAL ~array_%i%~ = i
EXPORT ~array_%i%~ // mark variable "array_%i%" as a return value
END
SET array = array_size
END
Currently only "array" will be defined outside the function. Evaluating "%array_0%" will only result in the string "%array_0%".
-
Or maybe a new command like LAUNCH_ACTION_FUNCTION_OUTER/LAFO, which forcing the functions run in the global scope instead off a closed one?
-
I can probably add a RET_ARRAY keyword that essentially does a PHP_EACH over the specified array name and returns those variables.
-
Done.
DEFINE_ACTION_FUNCTION foobar
RET_ARRAY
foo
bar
BEGIN
ACTION_DEFINE_ARRAY foo BEGIN 1 2 3 END
ACTION_DEFINE_ARRAY bar BEGIN a b c END
END
LAF foobar RET_ARRAY foo baz = bar END
// After the function has returned, the arrays foo and baz will exist
// The array baz will be derived from the array bar
ACTION_PHP_EACH foo AS _ => v BEGIN
PRINT "%v%" // will print 1, 2 and 3
END
ACTION_PHP_EACH baz AS _ => v BEGIN
PRINT "%v%" // will print a, b and c
END
-
Great work!
After a bit of trial and error I was able to manage even arrays of dynamic size.
With the new feature, the example from my first post would look like this:
DEFINE_PATCH_FUNCTION array_generator
INT_VAR
array_size = 10
RET
array
RET_ARRAY
array
BEGIN
FOR (i = 0; i < array_size; ++i) BEGIN
SET $array(~%i%~) = i // handled by RET_ARRAY
SET EVAL ~array_%i%~ = i // !!! Does not work. Use $array(index) notation instead. !!!
END
SET array = array_size // handled by RET
END
-
SET EVAL ~array_%i%~ = i // !!! Does not work. Use $array(index) notation instead. !!!
The reason it does not work is that while $array(x y z) is syntactical sugar for array_x_y_z, it also registers the variable as being part of the array as a side-effect. If you define array_x_y_z with normal assignment, you can retrieve its value with $array(x y z), but the variable is not considered to be part of the array, which means it's also not considered when PHP_EACH does its thing, and possibly in other situations.