1
function []=sci_m2scideclare(def)
3
// This function translate calls to m2scideclare
4
// which can be used by the user to influence translation
5
// by adding a comment which begins by m2scideclare
7
// Global variable for M2SCI
10
//variable_name|dimensions|datatype|property
12
//dimensions: variable dimensions separated by blanks (Unknown dimension is ?)
15
// - double: scalar/vector/matrix/hypermatrix containing double values
16
// - string: scalar/vector/matrix/hypermatrix containing character strings
17
// - boolean: scalar/vector/matrix/hypermatrix containing boolean values
18
// - int: scalar/vector/matrix/hypermatrix containing integer values
19
// - handle: matrix of graphics handles or graphics handle
20
// - sparse: sparse scalar/vector/matrix/hypermatrix
22
// - struct: struct array
26
// - real/complex/? for double and int datatype
27
// - real for string and boolean datatype (ignored if not)
28
// - NOT USED for struct/cell/handle datatype
30
// def is the comment added by the user
31
userdata=def.rhs(1).value
32
// Remove all multiple blanks
33
while strindex(userdata," ")<>[]
34
userdata=strsubst(userdat," "," ")
37
seppos=strindex(userdata,"|")
39
seppos=[seppos,length(userdata)+1]
41
nbsep=size(seppos,"*")
44
error("m2scideclare: not enough data, you should give at least variable_name|dimensions|datatype");
46
error("m2scideclare: too much data");
49
name=stripblanks(part(userdata,1:seppos(1)-1))
51
dimstxt=part(userdata,seppos(1)+1:seppos(2)-1)
53
datatypetxt=part(userdata,seppos(2)+1:seppos(3)-1)
56
proptxt=part(userdata,seppos(3)+1:seppos(4)-1)
62
dimstxt=strsubst(dimstxt,"?","-1")
63
blpos=strindex(dimstxt," ")
65
blpos=[1,blpos,length(dimstxt)+1]
68
for kbl=1:size(blpos,"*")-1
69
dims($+1)=evstr(part(dimstxt,blpos(kbl):blpos(kbl+1)))
73
datatypetxt=strsubst(datatypetxt,"?","Unknown")
74
datatype=convstr(datatypetxt,"l")
75
if or(datatype==["double","boolean","string","int","handle","sparse","cell","struct","unknown"]) then
76
datatype=convstr(part(datatype,1),"u")+convstr(part(datatype,2:length(datatype)),"l")
79
error("m2scideclare: Unknown datatype "+datatypetxt);
83
proptxt=strsubst(proptxt,"?","Unknown")
84
prop=convstr(proptxt,"l")
85
if or(prop==["real","complex","homogen","unknown"]) then
86
prop=convstr(part(prop,1),"u")+part(prop,2:length(prop))
89
error("m2scideclare: Unknown property "+proptxt);
91
// Property correction
92
if or(vtype==[Boolean,String]) then
96
if strindex(name,".")<>[] then // Cell or Struct m2scideclare
98
endofname=min([strindex(name,[".","("])])-1
99
varname=part(name,1:endofname)
101
// First field name (if is 'entries' then a cell else a struct)
102
firstpoint=min(strindex(name,"."))
103
secpoint=min(strindex(part(name,firstpoint+1:length(name)),"."))
104
par=min(strindex(part(name,firstpoint+1:length(name)),"("))
105
if isempty(secpoint) & isempty(par) then //x.fieldname
106
firstfield=part(name,firstpoint:length(name))
107
elseif isempty(secpoint) then //x.fieldname(p...)
108
firstfield=part(name,firstpoint:par-1)
109
elseif isempty(par) then //x.fieldname.fieldname2
110
firstfield=part(name,firstpoint:secpoint-1)
111
else //x.fieldname(p...).fieldname2
112
firstfield=part(name,firstpoint:min([secpoint par])-1)
115
if firstfield=="entries" then // Cell
121
// Indexes for varname ? myvar(1,2).field....
122
if or(strindex(name,"(")<strindex(name,".")) then
123
vardims=evstr("list"+part(name,min(strindex(name,"(")):min(strindex(name,")"))))
128
[isvar,index]=isdefinedvar(Variable(varname,Infer()))
130
if ~isvar then // If variable does not exist it is added to varslist
132
evstr("contents"+part(name,length(name))+"=1"); // should be removed is no more Scilab bug for it
133
evstr("contents"+part(name,length(name))+"=Infer(dims,Type(vtype,property))");
134
varslist($+1)=M2scivar(varname,varname,Infer(vardims,Type(vartype,Unknown,contents)))
136
inferreddims=varslist(index).dims
139
for kd=1:min([lstsize(vardims) lstsize(infereddims)])
140
if infereddims(kd)~=vardims(kd) & infereddims(kd)<>Unknown then
148
set_infos(["Dimensions current value and m2scideclare statements conflict for: "+varname
149
" m2scideclare given dimension: "+dims2str(vardims)
150
" Current dimension: "+dims2str(infereddims)
151
" m2scideclare IGNORED"],2)
153
varslist(index).dims=dims
157
if varslist(index).type.vtype==Unknown then
158
varslist(index).type.vtype=vartype
159
elseif varslist(index).type.vtype~=vartype then
160
set_infos(["Type current value and m2scideclare statements conflict for: "+varname
161
" m2scideclare given type: "+tp2str(vartype)
162
" current type: "+tp2str(varslist(index).type.vtype)
163
" m2scideclare IGNORED"],2)
167
if varslist(index).type.property==Unknown then
168
varslist(index).type.property=Unknown
170
set_infos(["Property current value and m2scideclare statements conflict for: "+name
171
" m2scideclare given type: "+prop2str(Unknown)
172
" current type: "+prop2str(varslist(index).type.property)
173
" m2scideclare IGNORED"],2)
176
// Update contents (no verification made...too complex)
177
contents=varslist(index).contents
178
if vartype==Cell & typeof(contents)<>"ce" then
180
evstr("contents"+part(name,length(name))+"=1"); // should be removed is no more Scilab bug for it
181
elseif vartype==Struct & typeof(contents)<>"st" then
183
evstr("contents"+part(name,length(name))+"=1"); // should be removed is no more Scilab bug for it
185
evstr("contents"+part(name,length(name))+"=Infer(dims,Type(vtype,property))");
186
varslist(index).contents=M2scivar(varname,varname,Infer(vardims,Type(vartype,Unknown,contents)))
190
else // Variable m2scideclare
192
// Special part for %graphicswindow
193
if name=="%graphicswindow" then
194
global %graphicswindow
195
if and(vtype<>[Handle,Double]) then
196
set_infos("%graphicswindow set to default value Handle",2);
198
%graphicswindow=vtype
203
[isvar,index]=isdefinedvar(Variable(name,Infer()))
205
if ~isvar then // If variable does not exist it is added to varslist
206
varslist($+1)=M2scivar(name,name,Infer(dims,Type(vtype,property)))
207
else // Try to compare with already infered data
209
infereddims=varslist(index).dims
212
for kd=1:min([lstsize(dims) lstsize(infereddims)])
213
if infereddims(kd)~=dims(kd) & infereddims(kd)<>Unknown then
220
set_infos(["Dimensions current value and m2scideclare statements conflict for: "+name
221
" m2scideclare given dimension: "+dims2str(dims)
222
" Current dimension: "+dims2str(infereddims)
223
" m2scideclare IGNORED"],2)
225
varslist(index)=M2scivar(varslist(index).matname,varslist(index).sciname,Infer(dims,varslist(index).type))
229
if varslist(index).type.vtype==Unknown then
230
varslist(index)=M2scivar(varslist(index).matname,varslist(index).sciname,Infer(varslist(index).dims,Type(vtype,varslist(index).type.property)))
231
elseif varslist(index).type.vtype~=vtype then
232
set_infos(["Type current value and m2scideclare statements conflict for: "+name
233
" m2scideclare given type: "+tp2str(vtype)
234
" current type: "+tp2str(varslist(index).type.vtype)
235
" m2scideclare IGNORED"],2)
239
if varslist(index).type.property==Unknown then
240
varslist(index)=M2scivar(varslist(index).matname,varslist(index).sciname,Infer(varslist(index).dims,Type(varslist(index).type.vtype,property)))
241
elseif varslist(index).type.property~=property then
242
set_infos(["Property current value and m2scideclare statements conflict for: "+name
243
" m2scideclare given type: "+prop2str(property)
244
" current type: "+prop2str(varslist(index).type.property)
245
" m2scideclare IGNORED"],2)
252
function str=tp2str(tp)
254
// Scilab Project - V. Couvert
255
// Returns equivalent of typeof() from data coming from type()
275
error("m2scideclare: type "+string(tp)+" is not implemented")
279
function str=prop2str(prop)
280
if type(prop)==10 then
285
error("m2scideclare: property "+string(prop)+" is not implemented")
289
function str=dims2str(dims)
291
for kd=1:lstsize(dims)
292
str=str+string(dims(kd))+" "