summaryrefslogtreecommitdiff
path: root/modules/m2sci/macros/percent/%e_st2sci.sci
blob: 92e55ecc4f5c04665fc78c481288d198ed916600 (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
// 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_st2sci(tree)
    // M2SCI function
    // Conversion function for Matlab extraction from structs
    // 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 // Last index is not a fieldname, ignored here
                infertree($)=null()
            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
                varslist($+1)=M2scivar(tmp.name,tmp.name,tmp.infer)

                oplist=list()
                oplist(1)=tmp
                infertree=tree.operands($)($)
                if typeof(infertree)<>"list" & infertree.vtype<>String then
                    infertree=list(infertree);
                end
                for k=1:size(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
            // extraction from struct
            // this case should happen only if var(ind.value) contains only one value
            [bval,index]=isdefinedvar(var)
            if ~bval then
                error(msprintf(gettext("M2SCI bug: extraction from unknown variable %s in varslist."),var.name))
            else
                if varslist(index).vtype<>Struct then
                    // variable not defined as a struct, modify inference
                    varslist(index).infer.vtype=Struct
                end
            end
            outdims=list(Unknown,Unknown)
            if ind.dims(1)==1 then // a scalar or a vector
                outdims=list(1,ind.dims(2))
            end
            if typeof(ind)=="cste" | (typeof(ind)<>"list" & is_a_scalar(ind)) then
                if ind.vtype<>String then
                    if var.dims(1)==1 then // row vector
                        ind=list(Cste(1),ind)
                    elseif var.dims(2)==1 then // column vector
                        ind=list(ind,Cste(1))
                    end
                end
            end
            if typeof(ind)<>"list" then
                ind=list(ind);
            end
            tree.out(1).infer=get_contents_infer(var,ind)
            if tree.out(1).dims==list(Unknown,Unknown) then
                tree.out(1).dims=outdims
            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

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

        IND=tree.operands
        IND(1)=null() // Variable is not an index

        tree.out(1).contents.index($+1)=IND
        tree.out(1).contents.data($+1)=get_contents_infer(tree.operands(1),IND)
    end
endfunction