summaryrefslogtreecommitdiff
path: root/modules/m2sci/macros/kernel/sci_m2scideclare.sci
blob: c1c41bcc07a205fc51aa1a876d8fb535daa1ddfa (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) ???? - INRIA - 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 []=sci_m2scideclare(def)
    // This function translate calls to m2scideclare
    // which can be used by the user to influence translation
    // by adding a comment which begins by m2scideclare

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

    //variable_name|dimensions|datatype|property

    //dimensions: variable dimensions separated by blanks (Unknown dimension is ?)

    //datatype:
    // - double: scalar/vector/matrix/hypermatrix containing double values
    // - string: scalar/vector/matrix/hypermatrix containing character strings
    // - boolean: scalar/vector/matrix/hypermatrix containing boolean values
    // - int: scalar/vector/matrix/hypermatrix containing integer values
    // - handle: matrix of graphics handles or graphics handle
    // - sparse: sparse scalar/vector/matrix/hypermatrix
    // - cell: cell array
    // - struct: struct array
    // - ? if unknown

    //property:
    // - real/complex/? for double and int datatype
    // - real for string and boolean datatype (ignored if not)
    // - NOT USED for struct/cell/handle datatype

    // def is the comment added by the user
    userdata=def.rhs(1).value
    // Remove all multiple blanks
    while strindex(userdata,"  ")<>[]
        userdata=strsubst(userdat,"  "," ")
    end

    seppos=strindex(userdata,"|")

    seppos=[seppos,length(userdata)+1]

    nbsep=size(seppos,"*")

    if nbsep<3 then
        error(gettext("not enough data, you should give at least variable_name|dimensions|datatype."));
    elseif nbsep>4 then
        error(gettext("too much data."));
    end

    name=stripblanks(part(userdata,1:seppos(1)-1))

    dimstxt=part(userdata,seppos(1)+1:seppos(2)-1)

    datatypetxt=part(userdata,seppos(2)+1:seppos(3)-1)

    if nbsep==4 then
        proptxt=part(userdata,seppos(3)+1:seppos(4)-1)
    else
        proptxt="?"
    end

    // Dimensions
    dimstxt=strsubst(dimstxt,"?","-1")
    dimstxt=strsubst(dimstxt,"*","-2")
    dimstxt=strsubst(dimstxt,"#","-3")
    blpos=strindex(dimstxt," ")
    nbblanks=size(blpos)
    blpos=[1,blpos,length(dimstxt)+1]
    fmt="%d"
    dims=list()
    for kbl=1:size(blpos,"*")-1
        dims($+1)=evstr(part(dimstxt,blpos(kbl):blpos(kbl+1)))
    end

    // Datatype
    datatypetxt=strsubst(datatypetxt,"?","Unknown")
    datatype=convstr(datatypetxt,"l")
    if or(datatype==["double","boolean","string","int","handle","sparse","cell","struct","unknown"]) then
        datatype=convstr(part(datatype,1),"u")+convstr(part(datatype,2:length(datatype)),"l")
        vtype=evstr(datatype)
    else
        error(msprintf(gettext("Unknown datatype %s."),datatypetxt));
    end

    // Property
    proptxt=strsubst(proptxt,"?","Unknown")
    prop=convstr(proptxt,"l")
    if or(prop==["real","complex","homogen","unknown"]) then
        prop=convstr(part(prop,1),"u")+part(prop,2:length(prop))
        property=evstr(prop)
    else
        error(msprintf(gettext("Unknown property %s."),proptxt));
    end
    // Property correction
    if or(vtype==[Boolean,String]) then
        property=Real
    end

    if strindex(name,".")<>[] then // Cell or Struct m2scideclare
        // Get varname
        endofname=min([strindex(name,[".","("])])-1
        varname=part(name,1:endofname)

        // First field name (if is 'entries' then a cell else a struct)
        firstpoint=min(strindex(name,"."))
        secpoint=min(strindex(part(name,firstpoint+1:length(name)),"."))
        par=min(strindex(part(name,firstpoint+1:length(name)),"("))
        if isempty(secpoint) & isempty(par) then //x.fieldname
            firstfield=part(name,firstpoint:length(name))
        elseif isempty(secpoint) then //x.fieldname(p...)
            firstfield=part(name,firstpoint:par-1)
        elseif isempty(par) then //x.fieldname.fieldname2
            firstfield=part(name,firstpoint:secpoint-1)
        else //x.fieldname(p...).fieldname2
            firstfield=part(name,firstpoint:min([secpoint par])-1)
        end
        if firstfield==".entries" then // Cell
            vartype=Cell
        else // Struct
            vartype=Struct
        end

        // Indexes for varname ? myvar(1,2).field....
        if or(strindex(name,"(")<strindex(name,".")) | (~isempty(strindex(name,"("))&isempty(strindex(name,"."))) then
            ierr=execstr("vardims=list"+part(name,min(strindex(name,"(")):min(strindex(name,")"))),"errcatch")
            if ierr then
                if ~isempty(strindex(part(name,min(strindex(name,"(")):min(strindex(name,")"))),"*")) then // Generic command *
                    vardims="generic"
                else
                    error(gettext("Wrong dimensions user data."));
                end
            end
        else
            vardims=list(1,1)
        end

        [isvar,index]=isdefinedvar(Variable(varname,Infer()))

        if ~isvar then // If variable does not exist it is added to varslist
            if vardims=="generic" then
                vardims=list(Unknown,Unknown)
            end
            contents=Contents()
            name=strsubst(name,"*","%genericm2sciindex")
            deff("m2scitmp",name)
            t=macr2tree(m2scitmp);
            if isempty(firstpoint) then
                contents.index($+1)=t.statements(2).expression.rhs;
            else
                contents.index($+1)=t.statements(2).expression.rhs(1);
            end
            clear m2scitmp
            for k=1:lstsize(contents.index($))
                if typeof(contents.index($)(k))=="variable" & contents.index($)(k).name=="%genericm2sciindex" then
                    contents.index($)(k)=Cste("*")
                elseif typeof(contents.index($)(k))=="cste" then
                    contents.index($)(k)=Cste(contents.index($)(k).value)
                elseif typeof(contents.index($)(k))=="list" then
                    for kk=1:lstsize(contents.index($)(k))
                        if typeof(contents.index($)(k)(kk))=="variable" & contents.index($)(k)(kk).name=="%genericm2sciindex" then
                            contents.index($)(k)(kk)=Cste("*")
                        elseif typeof(contents.index($)(k)(kk))=="cste" then
                            contents.index($)(k)(kk)=Cste(contents.index($)(k)(kk).value)
                        end
                    end
                end
            end
            contents.data($+1)=Infer(dims,Type(vtype,property))
            varslist($+1)=M2scivar(varname,varname,Infer(vardims,Type(vartype,Unknown),contents))
        else
            if vardims=="generic" then
                vardims=varslist(index).dims
            else
                vardims=dims
            end

            infereddims=varslist(index).dims

            err=%F
            for kd=1:min([lstsize(vardims) lstsize(infereddims)])
                if infereddims(kd)~=vardims(kd) & infereddims(kd)<>Unknown then
                    err=%T
                    break
                end
            end

            // Update dimensions
            if err then
                set_infos(msprintf(gettext("Dimensions current value and m2scideclare statements conflict for: %s\n   m2scideclare given dimension: %s\n   Current dimension: %s\n   m2scideclare IGNORED"),varname,dims2str(vardims),dims2str(infereddims)),2)
            else
                varslist(index)=M2scivar(varslist(index).matname,varslist(index).matname,Infer(vardims,Type(varslist(index).type.vtype,property)))
            end

            // Update vtype
            if varslist(index).type.vtype==Unknown then
                varslist(index)=M2scivar(varslist(index).matname,varslist(index).matname,Infer(vardims,Type(vartype,varslist(index).property)))
            elseif varslist(index).type.vtype~=vartype then
                set_infos(msprintf(gettext("Type current value and m2scideclare statements conflict for: %s\n   m2scideclare given type: %s\n   current type: %s\n   m2scideclare IGNORED"),varname,tp2str(vartype),tp2str(varslist(index).type.vtype)),2)
            end

            // Update property
            if varslist(index).property==Unknown then
                varslist(index).infer.type.property=property
            elseif property==Unknown then
                varslist(index).type.property=Unknown
            elseif varslist(index).type.property~=property then
                set_infos(msprintf(gettext("Property current value and m2scideclare statements conflict for: %s\n   m2scideclare given type: %s\n   current type: %s\n   m2scideclare IGNORED"),name,prop2str(Unknown),prop2str(varslist(index).type.property)),2)
            end

            // Update contents (no verification made...too complex)
            contents=varslist(index).contents
            name=strsubst(name,"*","%genericm2sciindex")
            deff("m2scitmp",name)
            t=macr2tree(m2scitmp);
            if isempty(firstpoint) then
                contents.index($+1)=t.statements(2).expression.rhs;
            else
                contents.index($+1)=t.statements(2).expression.rhs(1);
            end
            clear m2scitmp
            for k=1:lstsize(contents.index($))
                if typeof(contents.index($)(k))=="variable" & contents.index($)(k).name=="%genericm2sciindex" then
                    contents.index($)(k)=Cste("*")
                elseif typeof(contents.index($)(k))=="cste" then
                    contents.index($)(k)=Cste(contents.index($)(k).value)
                elseif typeof(contents.index($)(k))=="list" then
                    for kk=1:lstsize(contents.index($)(k))
                        if typeof(contents.index($)(k)(kk))=="variable" & contents.index($)(k)(kk).name=="%genericm2sciindex" then
                            contents.index($)(k)(kk)=Cste("*")
                        elseif typeof(contents.index($)(k)(kk))=="cste" then
                            contents.index($)(k)(kk)=Cste(contents.index($)(k)(kk).value)
                        end
                    end
                end
            end
            contents.data($+1)=Infer(dims,Type(vtype,property))
            varslist(index)=M2scivar(varname,varname,Infer(vardims,Type(vartype,Unknown),contents))

        end
    else // Variable m2scideclare

        // Special part for %graphicswindow
        if name=="%graphicswindow" then
            global %graphicswindow
            if and(vtype<>[Handle,Double]) then
                set_infos(gettext("%graphicswindow set to default value Handle."),2);
            else
                %graphicswindow=vtype
            end
            return
        end

        [isvar,index]=isdefinedvar(Variable(name,Infer()))

        if ~isvar then // If variable does not exist it is added to varslist
            varslist($+1)=M2scivar(name,name,Infer(dims,Type(vtype,property)))
        else // Try to compare with already infered data
            // Update dims
            infereddims=varslist(index).dims

            err=%F
            for kd=1:min([lstsize(dims) lstsize(infereddims)])
                if infereddims(kd)~=dims(kd) & infereddims(kd)<>Unknown then
                    err=%T
                    break
                end
            end

            if err then
                set_infos(msprintf(gettext("Dimensions current value and m2scideclare statements conflict for: %s\n   m2scideclare given dimension: %s\n   Current dimension: %s\n   m2scideclare IGNORED"),name,dims2str(dims),dims2str(infereddims)),2)
            else
                varslist(index)=M2scivar(varslist(index).matname,varslist(index).sciname,Infer(dims,varslist(index).type))
            end

            // Update vtype
            if varslist(index).type.vtype==Unknown then
                varslist(index)=M2scivar(varslist(index).matname,varslist(index).sciname,Infer(varslist(index).dims,Type(vtype,varslist(index).type.property)))
            elseif varslist(index).type.vtype~=vtype then
                set_infos(msprintf(gettext("Type current value and m2scideclare statements conflict for: %s\n   m2scideclare given type: %s\n   current type: %s\n   m2scideclare IGNORED"),name,tp2str(vtype),tp2str(varslist(index).type.vtype)),2)
            end

            // Update property
            if varslist(index).type.property==Unknown then
                varslist(index)=M2scivar(varslist(index).matname,varslist(index).sciname,Infer(varslist(index).dims,Type(varslist(index).type.vtype,property)))
            elseif varslist(index).type.property~=property then
                set_infos(msprintf(gettext("Property current value and m2scideclare statements conflict for: %s\n   m2scideclare given type: %s\n   current type: %s\n   m2scideclare IGNORED"),name,prop2str(property),prop2str(varslist(index).type.property)),2)
            end
        end
    end
endfunction


function str=tp2str(tp)
    // Returns equivalent of typeof() from data coming from type()

    if tp==1 then
        str="Double"
    elseif tp==4 then
        str="Boolean"
    elseif tp==6 then
        str="Sparse"
    elseif tp==8 then
        str="Int"
    elseif tp==9 then
        str="Handle"
    elseif tp==10 then
        str="String"
    elseif tp==16 then
        str="Struct"
    elseif tp==17 then
        str="Cell"
    elseif tp==-1 then
        str="Unknown"
    else
        error(msprintf(gettext("type %d is not implemented."),tp))
    end
endfunction

function str=prop2str(prop)
    if type(prop)==10 then
        str=prop
    elseif prop==-1 then
        str="Unknown"
    else
        error(msprintf(gettext("type %d is not implemented."),prop))
    end
endfunction

function str=dims2str(dims)
    str=""
    for kd=1:lstsize(dims)
        str=str+string(dims(kd))+" "
    end
endfunction