summaryrefslogtreecommitdiff
path: root/modules/m2sci/macros/percent/%e_ce2sci.sci
blob: 28b00873ec215c8289f13d2d3652ac8d449b907f (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
// 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]=%e_ce2sci(tree)
    // M2SCI function
    // Conversion function for Matlab extraction from cells
    // Input: tree = Matlab operation tree
    // Output: tree = Scilab equivalent for tree
    // Emulation function: mtlb_e()

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

    var=tree.operands(1)

    // Do not consider variable as a rhs...
    rhs=rhs-1

    // One index value
    if rhs==1 then
        ind=tree.operands(2)

        // --- Recursive extraction ---
        if type(ind)==15 then
            // If last index value is not a fieldname, it is ignored
            // it will be considered after all other indexes
            // Avoid some extraction problems from character strings
            lastisnotfield=typeof(ind($))=="list"
            if ~lastisnotfield then
                lastisnotfield=ind($).vtype<>String
            end

            // Inference
            infertree=tree.operands(2)
            if lastisnotfield then
                infertree($)=null() // Last index is not a fieldname, ignored here
            end

            // Change index value if just one double
            for k=1:lstsize(infertree)
                if typeof(infertree(k))=="cste" | (typeof(infertree(k))<>"list" & is_a_scalar(infertree(k))) then
                    if infertree(k).vtype<>String then
                        infertree(k)=list(Cste(1),infertree(k))
                    end
                end
            end

            [bval,index]=isdefinedvar(var)
            if ~bval then
                error(msprintf(gettext("M2SCI bug: extraction from unknown variable %s in varslist."),var.name))
            else
                tmp=get_contents_infer(var,infertree);
                tree.out(1).dims=tmp.dims
                tree.out(1).type=tmp.type
                tree.out(1).contents=tmp.contents
            end

            // Convert last extraction operation is not already done
            if lastisnotfield then
                [inftlist,pos]=get_contents_infer(var,infertree)
                tmp=gettempvar()
                tmp.infer=inftlist
                tmp.name=var.name+expression2code(infertree)

                oplist=list()
                oplist(1)=tmp
                infertree=tree.operands($)($)
                if typeof(infertree)<>"list" then
                    infertree=list(infertree);
                end
                for k=1:lstsize(infertree)
                    oplist($+1)=infertree(k)
                end
                newop=Operation("ext",oplist,tree.out)
                rhs=size(newop.operands)
                newop=%e2sci(newop)
                if typeof(newop)=="operation" then
                    tree.out(1).dims=newop.out(1).dims
                    tree.out(1).type=newop.out(1).type
                    tree.out(1).contents=newop.out(1).contents
                else
                    tree.out(1).dims=newop.lhs(1).dims
                    tree.out(1).type=newop.lhs(1).type
                    tree.out(1).contents=newop.lhs(1).contents
                end
            end
        else // Just one index value
            tree.out(1).vtype=Cell

            iscste=typeof(ind)=="cste"
            iscolon=%F
            if iscste then
                iscolon=ind.value==":"
            end
            if ~iscolon & iscste then
                tree.out(1).dims=list(1,1)
                tree.out(1).contents=Contents()
                if tree.operands(1).dims(1)==1 then // row vector
                    tree.out(1).contents.index(1)=list(list(Cste(1),Cste(1)),Cste("entries"))
                    tree.out(1).contents.data(1)=get_contents_infer(tree.operands(1),list(list(Cste(1),ind),Cste("entries")))
                elseif tree.operands(1).dims(2)==1 then // column vector
                    tree.out(1).contents.index(1)=list(list(Cste(1),Cste(1)),Cste("entries"))
                    tree.out(1).contents.data(1)=get_contents_infer(tree.operands(1),list(list(ind,Cste(1)),Cste("entries")))
                end
            else
                tree.out(1).contents=Contents()
                if ~iscolon then
                    tree=Operation("''",list(tree),tree.out)
                    tree.out(1).dims=ind.dims
                else
                    dprod=1
                    for kd=1:lstsize(var.dims)
                        dprod=dprod*var.dims(kd)
                        if dprod<0 then
                            break
                        end
                    end
                    if dprod>0 then
                        tree.out(1).dims=list(dprod,1)
                    else
                        tree.out(1).dims=list(Unknown,1)
                    end
                end
            end

        end
        // More than one index value
    else
        dims=list()
        for k=2:rhs+1
            dimsum=0
            for l=1:size(tree.operands(k).dims)
                dimsum=dimsum+tree.operands(k).dims(l)
            end
            if dimsum==size(tree.operands(k).dims) // All dims are 1
                dims(k-1)=1
                if typeof(tree.operands(k))=="cste" then
                    if tree.operands(k).value==":" then
                        dims(k-1)=var.dims(k-1)
                    end
                end
            else
                dims(k-1)=Unknown
            end
        end

        infertree=tree.operands
        infertree(1)=null()

        tree.out(1).type=var.type
        tree.out(1).dims=dims

        [bval,index]=isdefinedvar(var)
        if ~bval then
            error(msprintf(gettext("M2SCI bug: extraction from unknown variable %s in varslist."),var.name))
        else
            tmp=get_contents_infer(var,list(infertree,Cste("entries")));
            if is_a_scalar(tree.out(1)) then
                tree.out(1).contents.index($+1)=list(list(Cste(1),Cste(1)),Cste("entries"))
                tree.out(1).contents.data($+1)=tmp
            elseif not_a_scalar(tree.out(1)) then
                tree.out(1).contents.index($+1)=list(infertree,Cste("entries"))
                tree.out(1).contents.data($+1)=tmp
            end
        end
    end
endfunction