* Bugs 16567 16586 fixed: mfile2sci() supports block %{..%}. Appended comments improved"
[scilab.git] / scilab / modules / m2sci / macros / kernel / variablesearch.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) ???? - INRIA - Scilab
3 //
4 // Copyright (C) 2012 - 2016 - Scilab Enterprises
5 //
6 // This file is hereby licensed under the terms of the GNU GPL v2.0,
7 // pursuant to article 5.3.4 of the CeCILL v.2.1.
8 // This file was originally licensed under the terms of the CeCILL v2.1,
9 // and continues to be available under such terms.
10 // For more information, see the COPYING file which you should have received
11 // along with this program.
12
13 function  variablename=variablesearch(instr,variablename)
14     //  VARIABLESEARCH recursive function (used by "translatepaths" function)
15     //  Searches names of declared variables for each instruction of mtlbtree
16     //  Output
17     //  -variablename : a vector which contains the names of declared variables
18     //  -instr : mtlbtree instruction
19
20     // case : ifthenelse instruction
21     if typeof(instr) == "ifthenelse" then
22         for i=1:size(instr.then)
23             [variablename]=variablesearch((instr.then(i)),variablename)
24         end
25         for i=1:size(instr.elseifs)
26             for k=1:size(instr.elseifs(i).then)
27                 [variablename]=variablesearch((instr.elseifs(i).then(k)),variablename)
28             end
29         end
30         for i=1:size(instr.else)
31         [variablename]=variablesearch((instr.else(i)),variablename)
32         end
33         // case : selectcase instruction
34     elseif typeof(instr) == "selectcase" then
35         for i=1:size(instr.cases)
36             [variablename]=variablesearch(instr.cases(i).expression,variablename)
37             for j=1:size(instr.cases(i).then)
38                 [variablename]=variablesearch((instr.cases(i).then(j)),variablename)
39             end
40         end
41         for i=1:size(instr.else)
42         [variablename]=variablesearch(instr.else(i),variablename)
43         end
44         // case : while instruction
45     elseif typeof(instr) == "while" then
46         for i=1:size(instr.statements)
47             [variablename]=variablesearch(instr.statements(i),variablename)
48         end
49         // case : for instruction
50     elseif typeof(instr) == "for" then
51         [variablename]=variablesearch(instr.expression,variablename)
52         for i=1:size(instr.statements)
53             [variablename]=variablesearch(instr.statements(i),variablename)
54         end
55         // case : equal instruction
56     elseif typeof(instr) == "equal" then
57         for i=1:size(instr.lhs)
58             [variablename]=variablesearch(instr.lhs(i),variablename)
59         end
60         // case : operation instruction
61     elseif typeof(instr) == "operation" then
62         if instr.operator=="ins" then
63             if find(instr.operands(1).name==variablename)==[] then
64                 variablename($+1)=instr.operands(1).name
65             end
66         end
67         // case : variable instruction
68     elseif typeof(instr) == "variable" then
69         if find(instr.name==variablename)==[] & instr.name<>"ans" then
70             variablename($+1)=instr.name
71         end
72     end
73
74 endfunction