[m2sci] kernel code reorganization: clarify dependencies & ease code browsing
[scilab.git] / scilab / modules / m2sci / macros / kernel / funcall2sci.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
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 sci_equiv = funcall2sci(mtlb_expr)
14     // INTERNAL function called only by expression2sci()
15     //
16     // Convert a function call in an instruction or in an expression from Matlab to Scilab
17     // Input:
18     // - mtlb_instr: Matlab instr or expression to convert
19     // Output:
20     // - sci_instr: Scilab equivalent for mtlb_instr
21
22     rhslist=mtlb_expr.rhs
23     if rhslist==[] then // Function called as a command
24         rhsnb=-1
25         rhslist=list()
26         mtlb_expr.rhs=list()
27     else
28         rhsnb=size(rhslist)
29     end
30     // Init returned value
31     sci_expr=mtlb_expr
32
33     rhsind=1
34     while rhsind<=rhsnb
35         [sci_equiv]=expression2sci(rhslist(rhsind));
36         mtlb_expr.rhs(rhsind)=sci_equiv;
37         rhsind=rhsind+1;
38     end
39
40     // Performs the conversion of function call
41     lhs=mtlb_expr.lhsnb
42     if rhsnb==-1 then
43         rhs=-1
44     else
45         rhs=size(mtlb_expr.rhs)
46     end
47     funname=mtlb_expr.name
48
49     // If a translation function exists
50     if exists("sci_"+funname)==1 then
51         execstr("[sci_equiv]=sci_"+funname+"(mtlb_expr)");
52         // If I don't know where I can search other M-files
53     elseif res_path==[] then
54         sci_equiv=default_trad(mtlb_expr)
55     else
56         sci_tmpfile =pathconvert(TMPDIR)+pathconvert(fnam)+"sci_"+funname+".sci"
57         tmpierr=execstr("exec(sci_tmpfile,-1)","errcatch");errclear();
58         sci_file=res_path+"sci_"+funname+".sci"
59         ierr=execstr("exec(sci_file,-1)","errcatch");errclear();
60         if tmpierr==0 then
61             execstr("[sci_equiv]=sci_"+mtlb_expr.name+"(mtlb_expr)");
62             // If a translation function exists
63         elseif ierr==0 then
64             execstr("[sci_equiv]=sci_"+mtlb_expr.name+"(mtlb_expr)");
65             // If no translation indication given
66         elseif Recmode then
67             // Check if the M-file exists in the given paths
68             path=mfile_path(funname)
69             if path==[] then
70                 sci_equiv=default_trad(mtlb_expr)
71             elseif or(funname==nametbl)
72                 sci_equiv=sci_generic(mtlb_expr)
73             else
74                 fnam=funname
75                 scipath=res_path+fnam+".sci"
76                 scepath=res_path+fnam+".sce"
77                 catpath=res_path+fnam+".cat"
78                 res=0
79                 if newest(path,scipath,scepath)==1 then
80                     res=mfile2sci(path,res_path,%F,%T)
81                 end
82                 if res==1 then
83                     exec(sci_file, -1)
84                     ierr=execstr("[sci_equiv]=sci_"+mtlb_expr.name+"(mtlb_expr)","errcatch");
85                     if ierr<>0 then
86                         error(msprintf(gettext("Error while executing : [sci_equiv]=sci_%s(mtlb_expr)."),mtlb_expr.name));
87                     end
88                 else
89                     sci_equiv=default_trad(mtlb_expr)
90                 end
91             end
92         else  // Default translation
93             sci_equiv=default_trad(mtlb_expr)
94         end
95     end
96
97     // If equivalent is a funcall, number of lhs can have changed
98     if typeof(sci_equiv)=="funcall" then
99         sci_equiv.lhsnb=size(sci_equiv.lhs)
100     end
101
102 endfunction
103
104 // ---------------------------------------------------------------------------
105
106 function tree = default_trad(tree)
107     // M2SCI private function called only within funcall2sci()
108     // Create a default translation function
109
110     global("mtlbref_fun") //contains the matlab reference functions which not yet converted
111     global("mtlbtool_fun")//contains the matlab  toolboxes functions
112     global("not_mtlb_fun") // contains the not matlab functions
113
114     if ~exists("mtlbref_fun") then
115         mtlb_fun=[]
116     end
117     if ~exists("mtlbtool_fun") then
118         mtlbtool_fun=[]
119     end
120     if ~exists("not_mtlb_fun") then
121         not_mtlb_fun=[]
122     end
123
124     name=tree.name
125
126     ispriminame=%f;
127     //true if the name function is the name of scilab function primitive
128     if funptr(tree.name)<>0 then
129         name1="%"+tree.name
130         tree.name=name1
131         ispriminame=%t;
132     end
133     //ismtlbfun is true if the function is in a matlab toolbox, mtlbpath is the path where is the function
134     [mtlbpath,ismtlbtoolfun]=mtlbtoolfun(name)
135     //Matlab reference functions
136     if or(name==not_yet_converted()) then
137         set_infos(msprintf(gettext("Matlab function %s not yet converted, original calling sequence used."),name),2)
138         if ~or(name==mtlbref_fun(:,1)) then
139             mtlbref_fun($+1,1)=name
140             if ispriminame then
141                 mtlbref_fun($,2)=msprintf(gettext("(Warning name conflict: function name changed from %s to %s)."),name,name1);
142             else
143                 mtlbref_fun($,2)=""
144             end
145         end
146         //Matlab toolboxes functions
147     elseif ismtlbtoolfun then
148         set_infos(msprintf(gettext("Matlab toolbox(es) function %s not converted, original calling sequence used"),name),2)
149         if ~or(name==mtlbtool_fun(:,1)) then
150             mtlbtool_fun($+1,1)=name
151             if ispriminame then
152                 mtlbtool_fun($,2)=msprintf(gettext("Matlab toolbox(es) function %s not converted, original calling sequence used."),name,name1,mtlbpath)
153             else
154                 mtlbtool_fun($,2)=msprintf(gettext("(Find this function in matlab/%s)."),mtlbpath)
155             end
156         end
157     elseif isdefinedvar(Variable(tree.name,Infer())) then
158         operands=list()
159         operands(1)=Variable(tree.name,Infer())
160         for krhs = 1:size(tree.rhs)
161             operands($+1)=tree.rhs(krhs)
162         end
163         tree=Operation("ext",operands,tree.lhs)
164         tree=operation2sci(tree)
165
166         //Not matlbb function
167     else
168         set_infos(msprintf(gettext("Unknown function %s not converted, original calling sequence used."),name),2)
169         if ~or(name==not_mtlb_fun(:,1)) then
170             not_mtlb_fun($+1,1)=name
171             if ispriminame then
172                 not_mtlb_fun($,2)=msprintf(gettext("(Warning name conflict: function name changed from %s to %s)."),name,name1);
173             else
174                 not_mtlb_fun($,2)=""
175             end
176         end
177     end
178     if ispriminame then
179         set_infos(msprintf(gettext("(Warning name conflict: function name changed from %s to %s)."),name,name1),0)
180     end
181     [tree]=sci_generic(tree)
182 endfunction
183
184 // ---------------------------------------------------------------------------
185
186 function path = mfile_path(nam)
187     fil = nam+".m";
188     nf = length(fil)
189     path = [];
190     for k=1:size(mfiles,"*")
191         pk=mfiles(k);
192         kk=strindex(pk,["/" "\"]);
193         if kk==[]
194             kk = 0
195         end
196         if fil==part(pk,kk($)+1:length(pk)) then
197             path=pk;
198             break
199         end
200     end
201 endfunction
202
203 // ---------------------------------------------------------------------------
204
205 function tree = sci_generic(tree)
206     // M2SCI function
207     // Generic conversion function for unknown Matlab functions
208     // Input: tree = Matlab funcall tree
209     // Output: tree = Scilab equivalent for tree
210
211     if typeof(tree)=="operation"
212         tree.out(1).dims=list(-1,-1)
213         tree.out(1).type=Type(-1,-1)
214     else
215         for i=1:size(tree.lhs)
216             tree.lhs(i).dims=list(-1,-1)
217             tree.lhs(i).type=Type(-1,-1)
218         end
219     end
220 endfunction