Improve m2sci tool
[scilab.git] / scilab / modules / m2sci / macros / sci_files / sci_reshape.sci
1 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2 // Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
3 //
4 // This file must be used under the terms of the CeCILL.
5 // This source file is licensed as described in the file COPYING, which
6 // you should have received as part of this distribution.  The terms
7 // are also available at
8 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
9
10 function [tree]=sci_reshape(tree)
11     // M2SCI function
12     // Conversion function for Matlab reshape()
13     // Input: tree = Matlab funcall tree
14     // Ouput: tree = Scilab equivalent for tree
15
16     // knowndims is a boolean, it's true if all the dimensions of the lhs are known else it's false
17     // isemptyrhs is a boolean, it's true if one (or more than one) of the lhs dimensions it's equal to 0 else it's false
18     knowndims=%t
19     isemptyrhs=%f
20     for k=1:rhs
21         if k==1 then
22             if tree.rhs(k).vtype==String then
23                 tree.rhs(k)=Funcall("mstr2sci",1,list(tree.rhs(k)),list(Variable("",tree.rhs(k).infer)))
24             elseif tree.rhs(k).vtype==Unknown then
25                 scitree=tree
26                 scitree.rhs(k)=Funcall("mtlb_double",1,list(tree.rhs(k)),list(Variable("",tree.rhs(k).infer)))
27                 repl_poss(scitree,tree,tree.rhs(k),gettext("is not a character string matrix."))
28                 tree=scitree
29             end
30         end
31         if k>=2 then
32             for i=1:size(tree.rhs(k).dims)
33                 if tree.rhs(k).dims(i)==-1
34                     knowndims=%f
35                 end
36             end
37         end
38         if typeof(tree.rhs(k))=="cste" then
39             if isempty(tree.rhs(k).value) then
40                 isemptyrhs=%t
41                 tree.rhs(k)=Cste(-1)
42             end
43         end
44     end
45
46     set_infos(gettext("WARNING: Matlab reshape() suppresses singleton higher dimension, it is not the case for matrix."),2)
47     tree.name="matrix"
48     tree.lhs(1).type=tree.rhs(1).type
49     if knowndims==%t
50         dims=list()
51         if size(tree.rhs)==2
52             dims=lhsdimsearch(tree.rhs(2))
53             tree.lhs(1).dims=dims
54         elseif size(tree.rhs)>=2
55             if ~isemptyrhs then
56                 for k=2:size(tree.rhs)
57                     if typeof(tree.rhs(k)) == "cste" then
58                         dims($+1)=tree.rhs(k).value
59                     else
60                         dims($+1) = Unknown;
61                     end
62                 end
63                 tree.lhs(1).dims=dims
64             end
65         else
66             tree.lhs(1).dims=allunknown(tree.rhs(1).dims)
67         end
68     else
69         tree.lhs(1).dims=allunknown(tree.rhs(1).dims)
70     end
71 endfunction
72
73 function dims=lhsdimsearch(rhs)
74     // LHSDIMSEARCH function searches dimensions size of the lhs
75     // dims : a list which contains the size of the lhs
76     if typeof(rhs)=="operation" then
77         dims=lstcat(rhs.operands(2).value,dims)
78         if typeof(rhs.operands(1))=="operation" then
79             dims=lhsdimsearch(rhs.operands(1))
80         else
81             dims=lstcat(rhs.operands(1).value,dims)
82         end
83     end
84 endfunction