Closed shsajjadi closed 1 year ago
Is seems that mastering nested functions is as complicated as correctly implementing them. A variable in Coder intermediate C++ representation is implemented as Symbol class that has a two members : 1- (value)a COW pointer to octave data type and 2- (reference) a pointer to other Symbol. The (reference) data member solves to some extent implementing global and persistent variable and also variable in nested functions. Currently nested functions are implemented as nested c++ lambdas. Each function captures its own variables by value. Variables in the nesting function are captured by reference in the nested function. Also the nested function holds a COW pointer of the nesting function as one of its captures to keep it alive so its variables that are captured by reference can be used later from the handle to the nested functions. But this design has limitations and a more complex data structure is needed for holding the Octave objects. The new design should have theses specifications:
Using Octave test cases was helpful. I pushed the following commits: e3c7d38409b50860c97ccc104928f2352a92bce8 f6522aee6cc263fd8f13f1cc7ee4a3645dedff86 a14dfac0221ac12dd097e696c4f5fc9a389f3c1c
Nested functions specially in more complex nesting relations don't work correctly, have memory leak and are incompatible to some extent with global variables