summaryrefslogtreecommitdiff
path: root/modules/m2sci/macros/percent/%i2sci.sci
blob: 27a69605a38af206465bc7d9662306c0ac540a2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) 2002-2004 - INRIA - Vincent COUVERT
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

function [tree]=%i2sci(tree)
    // M2SCI function
    // Conversion function for Matlab insertion
    // Input: tree = Matlab operation tree
    // Output: tree = Scilab equivalent for tree
    // Emulation functions: mtlb_i() and mtlb_is()

    // Global variable for M2SCI
    global("varslist")

    from=tree.operands($)
    to=tree.operands(1)

    // Special case for varargin/varargout
    if or(to.name==["varargin","varargout"]) then
        ind=tree.operands(2)
        if type(ind)<>15 then
            tree=Variable(to.name,Infer())
        else
            if type(ind(1))==15 | ind(1).vtype<>String then
                tree.operands(2)(2)=null() // Del 'entries'
            elseif typeof(ind(1))=="cste" then
                tree.operands(2)(2)=null() // Del 'entries'
            else
                tree.operands(2)(1)=null() // Del 'entries'
            end
        end
        return
    end


    // Ajust rhs value (variable and value to insert are part of rhs)
    rhs=rhs-2

    // Insertion with cells
    if to.vtype==Cell | from.vtype==Cell then
        tree=%i_ce2sci(tree)
        return
    end

    // Insertion with structs
    if to.vtype==Struct | from.vtype==Struct then
        tree=%i_st2sci(tree)
        return
    end

    // Just one index value
    if rhs==1 then
        ind=tree.operands(2)
        // --- Insertion of strings ---
        if to.vtype==String & from.vtype==String then
            tree=list()
            to.dims=allunknown(to.dims)
            [bval,index]=isdefinedvar(to)
            if bval then
                varslist(index).infer.dims=allunknown(to.dims)
            end
            insert(Equal(list(to),Funcall("mtlb_is",1,Rhs_tlist(to,from,ind),list(to))),1)
            // --- Insertion with just one index ---
        elseif type(ind)<>15 then
            if ind.vtype==String then
                if typeof(ind)=="cste" then
                    if ind.value<>":" then
                        tree=%i_st2sci(tree)
                        return
                    end
                end
            end

            iscste=typeof(ind)=="cste"
            iscolon=%F
            if iscste then
                iscolon=ind.value==":"
            end

            // --- Insertion with just one index which is not : ---
            if ~iscolon then
                if is_empty(to) then // Insertion in an empty matrix
                    if typeof(ind)=="cste" then
                        tree.out(1).dims=list(1,ind.value)
                    else
                        tree.out(1).dims=list(1,Unknown)
                    end
                    tree.out(1).type=to.type
                    tree=%i2sci_s(tree)
                else
                    if is_a_scalar(to) then
                        tree=%i2sci_s(tree)
                    elseif is_a_vector(to) & to.dims(2)<>1 then
                        tree=%i2sci_r(tree)
                    elseif is_a_vector(to) & to.dims(1)<>1 then
                        tree=%i2sci_c(tree)
                    else
                        tree=%i2sci_g(tree)
                        return
                    end
                end
                // --- Insertion with just one index which is : ---
            else
                if is_empty(to) then // Insertion in an empty matrix
                    if from.dims(2)<>1 then
                        if typeof(from)=="variable" then
                            if isdefinedvar(from) then
                                break
                            end
                        end
                        insert(Equal(list(to),list(from)))
                    end
                end
                tree.out(1).dims=list(Unknown,1)
                tree.out(1).type=to.type
            end
            // --- Insertion with more than one index value (index is a list) ---
        else
            indexisstr=%F
            iscell=%F
            for k=1:lstsize(ind)
                if type(ind(k))<>15 then
                    if ind(k).vtype==String then
                        if typeof(ind(k))=="cste" & ind(k).value=="entries" then
                            iscell=%T
                        else
                            indexisstr=%T
                        end
                    end
                end
            end
            if iscell then
                tree=%i_ce2sci(tree)
                return
            end
            if indexisstr then
                tree=%i_st2sci(tree)
                return
            end
            error(msprintf(gettext("recursive insertion in a variable which is not a Cell nor a Struct: %s."),to.name))
        end
        // Two indexes: to(ind1,ind2,...)=from or more
    else
        if to.vtype==String & from.vtype==String then
            tree=list()
            to.dims=allunknown(to.dims)
            [bval,index]=isdefinedvar(to)
            if bval then
                varslist(index).infer.dims=allunknown(to.dims)
            end
            insert(Equal(list(to),Funcall("mtlb_is",1,Rhs_tlist(to,from,tree.operands(2),tree.operands(3)),list(to))),1)
        else
            tree.out(1).dims=list()
            for k=1:lstsize(tree.operands)-2
                tree.out(1).dims(k)=Unknown
            end
            // dim can be infered when index is a constant and when index value is greater than older dim and this dim is not unknown
            for kdim=1:size(tree.operands)-2
                if typeof(tree.operands(kdim+1))=="cste" & tree.operands(kdim+1).vtype<>String then
                    if to.dims(kdim)<>Unknown then
                        if to.dims(kdim)<=tree.operands(kdim+1).value then
                            tree.out(1).dims(kdim)=tree.operands(kdim+1).value;
                        else
                            tree.out(1).dims(kdim)=to.dims(kdim)
                        end
                    end
                end
            end
            if is_empty(to) then
                // a(k,:)=b with a==[] is converted by a(1,1:length(b))=b
                if lstsize(tree.operands)-2 == 2 & typeof(tree.operands($-1))=="cste" & tree.operands($-1).value==":" then
                    length_funcall=Funcall("length",1,Rhs_tlist(tree.operands($)),list())
                    tree.operands($-1)=Operation(":",list(Cste(1),length_funcall),list())
                end
                // a(:,k)=b with a==[] is converted by a(1:length(b),1)=b
                if lstsize(tree.operands)-2 == 2 & typeof(tree.operands($-2))=="cste" & tree.operands($-2).value==":" then
                    length_funcall=Funcall("length",1,Rhs_tlist(tree.operands($)),list())
                    tree.operands($-2)=Operation(":",list(Cste(1),length_funcall),list())
                end
            end
            tree.out(1).type=from.type
        end
    end
endfunction