[m2sci] kernel code reorganization: clarify dependencies & ease code browsing
[scilab.git] / scilab / modules / m2sci / macros / kernel / lst_funcall.sci
index 06c5bb5..b4e167f 100644 (file)
 // For more information, see the COPYING file which you should have received
 // along with this program.
 
-function   funcallname=lst_funcall(fil,fnamvect)
-    //  LST_FUNCALL function (used by "translatepaths" function) Creates a list of vectors. The first component of each vector is the name of a M-file (found in the Paths to translate), followed by the called functions by this file
+function   funcallname = lst_funcall(fil, fnamvect)
+    //  Internal function called only by translatepaths()
+    //
+    //  Creates a list of vectors.
+    //  The first component of each vector is the name of a M-file (found in the Paths to translate),
+    //  followed by the called functions by this file
     //  Output
     //  -funcallname : a list of vectors
     //  Input
@@ -238,3 +242,167 @@ function   funcallname=lst_funcall(fil,fnamvect)
     funcallname=[fnam;funcallname]
 
 endfunction
+
+// ---------------------------------------------------------------------------
+
+function  variablename = variablesearch(instr,variablename)
+    //  PRIVATE INTERNAL function called only by lst_funcall() hereabove and itself (recursive)
+    //
+    //  Searches names of declared variables for each instruction of mtlbtree
+    //  Output
+    //  -variablename : a vector which contains the names of declared variables
+    //  -instr : mtlbtree instruction
+
+    // case : ifthenelse instruction
+    if typeof(instr) == "ifthenelse" then
+        for i=1:size(instr.then)
+            [variablename]=variablesearch((instr.then(i)),variablename)
+        end
+        for i=1:size(instr.elseifs)
+            for k=1:size(instr.elseifs(i).then)
+                [variablename]=variablesearch((instr.elseifs(i).then(k)),variablename)
+            end
+        end
+        for i=1:size(instr.else)
+        [variablename]=variablesearch((instr.else(i)),variablename)
+        end
+        // case : selectcase instruction
+    elseif typeof(instr) == "selectcase" then
+        for i=1:size(instr.cases)
+            [variablename]=variablesearch(instr.cases(i).expression,variablename)
+            for j=1:size(instr.cases(i).then)
+                [variablename]=variablesearch((instr.cases(i).then(j)),variablename)
+            end
+        end
+        for i=1:size(instr.else)
+        [variablename]=variablesearch(instr.else(i),variablename)
+        end
+        // case : while instruction
+    elseif typeof(instr) == "while" then
+        for i=1:size(instr.statements)
+            [variablename]=variablesearch(instr.statements(i),variablename)
+        end
+        // case : for instruction
+    elseif typeof(instr) == "for" then
+        [variablename]=variablesearch(instr.expression,variablename)
+        for i=1:size(instr.statements)
+            [variablename]=variablesearch(instr.statements(i),variablename)
+        end
+        // case : equal instruction
+    elseif typeof(instr) == "equal" then
+        for i=1:size(instr.lhs)
+            [variablename]=variablesearch(instr.lhs(i),variablename)
+        end
+        // case : operation instruction
+    elseif typeof(instr) == "operation" then
+        if instr.operator=="ins" then
+            if find(instr.operands(1).name==variablename)==[] then
+                variablename($+1)=instr.operands(1).name
+            end
+        end
+        // case : variable instruction
+    elseif typeof(instr) == "variable" then
+        if find(instr.name==variablename)==[] & instr.name<>"ans" then
+            variablename($+1)=instr.name
+        end
+    end
+
+endfunction
+
+// ---------------------------------------------------------------------------
+
+function   [funcallname,variablename] = funcallsearch(instr,funcallname,fnamvect,variablename)
+    //  PRIVATE INTERNAL function called only by lst_funcall() hereabove and itself (recursive)
+    //
+    //  Searches the functions names in each instruction of mtlbtree
+    //  Input-Output
+    //  -funcallname : a vector which contains the names of called functions if it exists a M-file having the same name in the Paths
+    //  -variablename : a vector which contains the names of declared variables
+    //  Input
+    //  -instr : mtlbtree instruction
+    //  -fnamvect : vector which contains all M-files names found in the Paths
+
+    // case : ifthenelse instruction
+    if typeof(instr) == "ifthenelse" then
+        [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
+        for i=1:size(instr.then)
+            [funcallname,variablename]=funcallsearch((instr.then(i)),funcallname,fnamvect,variablename)
+        end
+        for i=1:size(instr.elseifs)
+            for k=1:size(instr.elseifs(i).then)
+                [funcallname,variablename]=funcallsearch((instr.elseifs(i).then(k)),funcallname,fnamvect,variablename)
+            end
+        end
+        for i=1:size(instr.else)
+        [funcallname,variablename]=funcallsearch((instr.else(i)),funcallname,fnamvect,variablename)
+        end
+        // case : selectcase instruction
+    elseif typeof(instr) == "selectcase" then
+        [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
+
+        for i=1:size(instr.cases)
+            [funcallname,variablename]=funcallsearch((instr.cases(i).expression),funcallname,fnamvect,variablename)
+            for j=1:size(instr.cases(i).then)
+                [funcallname,variablename]=funcallsearch((instr.cases(i).then(j)),funcallname,fnamvect,variablename)
+            end
+        end
+        for i=1:size(instr.else)
+        [funcallname,variablename]=funcallsearch(instr.else(i),funcallname,fnamvect,variablename)
+        end
+        // case : while instruction
+    elseif typeof(instr) == "while" then
+        [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
+        for i=1:size(instr.statements)
+            [funcallname,variablename]=funcallsearch(instr.statements(i),funcallname,fnamvect,variablename)
+        end
+        // case : for instruction
+    elseif typeof(instr) == "for" then
+        [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
+        for i=1:size(instr.statements)
+            [funcallname,variablename]=funcallsearch(instr.statements(i),funcallname,fnamvect,variablename)
+        end
+        // case : cste instruction
+    elseif  typeof(instr)== "cste" then
+        return
+        // case : variable instruction
+    elseif typeof(instr)=="variable"
+        if find(instr.name==variablename)==[] & find(instr.name==stripblanks(part(fnamvect,1:24)))<>[] & find(instr.name==funcallname)==[] then
+            funcallname=[funcallname;fnamvect(find(instr.name==stripblanks(part(fnamvect,1:24))))]
+        else
+            return
+        end
+        // case : equal instruction
+    elseif typeof(instr) == "equal" then
+        [funcallname,variablename]=funcallsearch(instr.expression,funcallname,fnamvect,variablename)
+        // case : expression is a funcall
+    elseif typeof(instr) == "funcall" then
+        if find(funcallname==instr.name) == [] & find(instr.name==stripblanks(part(fnamvect,1:24)))<>[]  then
+            if size(find(instr.name==stripblanks(part(fnamvect,1:24))),2)==1 then
+                funcallname=[funcallname;fnamvect(find(instr.name==stripblanks(part(fnamvect,1:24))))]
+            else
+                findvect=find(instr.name==stripblanks(part(fnamvect,1:24)))
+                funcallname=[funcallname;fnamvect(findvect(2))]
+                st = " " + mtlbtree.name + ": " + fnamvect(findvect(1))
+                for i=2:size(findvect,2)
+                    st = st+ " <-> " + fnamvect(findvect(i))
+                end
+                st = st + gettext(": The 24 first characters of the files names are equal: ");
+                warning(st)
+            end
+        end
+        // case : expression is cste
+        if typeof(instr.rhs)== "constant" then
+            return
+        else
+            for ind=1:size(instr.rhs)
+                [funcallname,variablename]=funcallsearch(instr.rhs(ind),funcallname,fnamvect,variablename)
+            end
+        end
+        // case : operation instruction
+    elseif typeof(instr) == "operation" then
+        for ind=1:size(instr.operands)
+            [funcallname,variablename]=funcallsearch(instr.operands(ind),funcallname,fnamvect,variablename)
+        end
+    end
+
+endfunction