* Bugs 16567 16586 fixed: mfile2sci() supports block %{..%}. Appended comments improved"
[scilab.git] / scilab / modules / m2sci / macros / kernel / equal2sci.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_instr]=equal2sci(mtlb_instr)
14     // Convertion of a Matlab instruction or expression to Scilab
15     // Input:
16     // - mtlb_instr: Matlab instr or expression to convert
17     // Output:
18     // - sci_instr: Scilab equivalent for mtlb_instr
19
20     // Trees to insert in converted function tree
21     global("m2sci_to_insert_b")
22     m2sci_to_insert_b=list()
23     global("m2sci_to_insert_a")
24     m2sci_to_insert_a=list()
25     global("varslist")
26
27     sci_instr=mtlb_instr
28
29     // Add inference data to lhs
30     lhslist=list()
31
32     // Inference field added to each lhs argument
33     // Get infos in varslist to init
34     for k=1:size(mtlb_instr.lhs)
35         if typeof(mtlb_instr.lhs(k))=="variable" then
36             [bval,index]=isdefinedvar(mtlb_instr.lhs(k))
37             if bval then
38                 INFER=varslist(index).infer
39             else
40                 INFER=Infer()
41             end
42             lhslist($+1)=Variable(mtlb_instr.lhs(k).name,INFER)
43         elseif typeof(mtlb_instr.lhs(k))=="operation" then
44             if mtlb_instr.lhs(k).operator<>"ins" then
45                 error(msprintf(gettext("lhs cannot be a %s operation."),mtlb_instr.lhs(k).operator))
46             end
47
48             [bval,index]=isdefinedvar(mtlb_instr.lhs(k).operands(1))
49             if bval then
50                 INFER=varslist(index).infer
51             else
52                 INFER=Infer()
53             end
54
55             lhslist($+1)=Operation(mtlb_instr.lhs(k).operator,..
56             mtlb_instr.lhs(k).operands,..
57             list(Variable(mtlb_instr.lhs(k).operands(1).name,INFER)))
58         else
59             error(msprintf(gettext("lhs cannot be a %s."),typeof(mtlb_instr.lhs(k))))
60         end
61     end
62
63     // Convert expression
64
65     [sci_expr]=expression2sci(mtlb_instr.expression,lhslist);
66
67     if sci_expr==list() then // Conversion made by inserted instructions or 'm2scideclare'
68         sci_instr=list()
69     else
70
71         sci_instr.expression=sci_expr;
72
73         // Update lhs of instruction
74         select typeof(sci_instr.expression)
75         case "operation" then
76             sci_instr.lhs=sci_expr.out;
77         case "funcall" then
78             sci_instr.lhs=sci_instr.expression.lhs
79             if typeof(mtlb_instr.expression)=="funcall" then
80                 sci_instr.lhs=sci_expr.lhs;
81             end
82         case "cste" then
83             sci_instr.lhs=lhslist;
84             sci_instr.lhs(1).dims=sci_expr.dims;
85             sci_instr.lhs(1).type=sci_expr.type;
86         case "variable" then
87             sci_instr.lhs=lhslist;
88             sci_instr.lhs(1).dims=sci_expr.dims;
89             sci_instr.lhs(1).type=sci_expr.type;
90         else
91             error(msprintf(gettext("%s is not yet implemented."),typeof(sci_instr.expression)));
92         end
93
94         // If lhs are insertion operation, they also have to be converted
95         for k=1:size(sci_instr.lhs)
96             if typeof(sci_instr.lhs(k))=="operation" then
97                 sci_instr.lhs(k).operands($+1)=sci_instr.expression
98                 // Keep just one inference field in sci_instr.expression (if is a funcall) so that inference can be made in operation2sci()
99                 if typeof(sci_instr.lhs(k).operands($))=="funcall" then
100                     for l=1:size(sci_instr.lhs(k).operands($).lhs)
101                         if l<>k then
102                             sci_instr.lhs(k).operands($).lhs(l)=list()
103                         end
104                     end
105                     l=1
106                     while l<=size(sci_instr.lhs(k).operands($).lhs)
107                         if sci_instr.lhs(k).operands($).lhs(l)==list() then
108                             sci_instr.lhs(k).operands($).lhs(l)=null()
109                         else
110                             l=l+1
111                         end
112                     end
113                     // Verify that there is just one lhs kept
114                     if size(sci_instr.lhs(k).operands($).lhs)<>1 then pause;end
115                 end
116                 // If insertion made in an unknown variable, I add it to varslist
117                 inservar=sci_instr.lhs(k).operands(1)
118                 [bval,index]=isdefinedvar(inservar)
119                 if ~bval then
120                     // Variable added to varslist before insertion
121                     if funptr(inservar.name)<>0 then
122                         matname="%"+inservar.name
123                     else
124                         matname=inservar.name
125                     end
126                     if sci_instr.expression.vtype==Struct then
127                         // Variable is initialized to struct() in converted script is does not already exist
128                         varslist($+1)=M2scivar(matname,inservar.name,Infer(list(0,0),Type(Struct,Unknown)))
129                         //m2sci_to_insert_b($+1)=Equal(list(inservar),Funcall("struct",1,list(),list()))
130                     elseif sci_instr.expression.vtype==Cell then
131                         // Variable is initialized to cell() in converted script is does not already exist
132                         varslist($+1)=M2scivar(matname,inservar.name,Infer(list(0,0),Type(Cell,Unknown)))
133                         //m2sci_to_insert_b($+1)=Equal(list(inservar),Funcall("cell",1,list(),list()))
134                     else
135                         // Variable is initialized to [] in converted script is does not already exist
136                         varslist($+1)=M2scivar(matname,inservar.name,Infer(list(0,0),Type(Double,Real)))
137                         //m2sci_to_insert_b($+1)=Equal(list(inservar),Cste([]))
138                     end
139                     sci_instr.lhs(k).out(1).infer=varslist($).infer
140                 else
141                     sci_instr.lhs(k).out(1).infer=varslist(index).infer
142                 end
143                 [sci_instr.lhs(k)]=operation2sci(sci_instr.lhs(k))
144                 if typeof(sci_instr.lhs(k))=="operation" then
145                     if or(sci_instr.lhs(k).operands($)<>sci_instr.expression) then // Update expression if has been modified while converting lhs
146                         sci_instr.expression=sci_instr.lhs(k).operands($)
147                     end
148
149                     sci_instr.lhs(k).operands($)=null()
150                     updatevarslist(sci_instr.lhs(k).out)
151                 else
152                     // Insertion done by inserted instruction
153                     sci_instr=list()
154                     return
155                 end
156             end
157         end
158         // Update varslist
159         updatevarslist(sci_instr.lhs);
160     end
161 endfunction