~ubuntu-branches/ubuntu/hoary/scilab/hoary

« back to all changes in this revision

Viewing changes to macros/m2sci/kernel/sci_m2scideclare.sci

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2005-01-09 22:58:21 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050109225821-473xr8vhgugxxx5j
Tags: 3.0-12
changed configure.in to build scilab's own malloc.o, closes: #255869

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function []=sci_m2scideclare(def)
 
2
// Copyright INRIA
 
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
 
6
 
 
7
// Global variable for M2SCI
 
8
global("varslist")
 
9
 
 
10
//variable_name|dimensions|datatype|property
 
11
 
 
12
//dimensions: variable dimensions separated by blanks (Unknown dimension is ?)
 
13
 
 
14
//datatype:
 
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
 
21
// - cell: cell array
 
22
// - struct: struct array
 
23
// - ? if unknown
 
24
 
 
25
//property:
 
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
 
29
 
 
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,"  "," ")
 
35
end
 
36
 
 
37
seppos=strindex(userdata,"|")
 
38
 
 
39
seppos=[seppos,length(userdata)+1]
 
40
 
 
41
nbsep=size(seppos,"*")
 
42
 
 
43
if nbsep<3 then
 
44
  error("m2scideclare: not enough data, you should give at least variable_name|dimensions|datatype");
 
45
elseif nbsep>4 then
 
46
  error("m2scideclare: too much data");
 
47
end
 
48
 
 
49
name=stripblanks(part(userdata,1:seppos(1)-1))
 
50
 
 
51
dimstxt=part(userdata,seppos(1)+1:seppos(2)-1)
 
52
 
 
53
datatypetxt=part(userdata,seppos(2)+1:seppos(3)-1)
 
54
 
 
55
if nbsep==4 then
 
56
  proptxt=part(userdata,seppos(3)+1:seppos(4)-1)
 
57
else
 
58
  proptxt="?"
 
59
end
 
60
 
 
61
// Dimensions
 
62
dimstxt=strsubst(dimstxt,"?","-1")
 
63
blpos=strindex(dimstxt," ")
 
64
nbblanks=size(blpos)
 
65
blpos=[1,blpos,length(dimstxt)+1]
 
66
fmt="%d"
 
67
dims=list()
 
68
for kbl=1:size(blpos,"*")-1
 
69
  dims($+1)=evstr(part(dimstxt,blpos(kbl):blpos(kbl+1)))
 
70
end
 
71
 
 
72
// Datatype
 
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")
 
77
  vtype=evstr(datatype)
 
78
else
 
79
  error("m2scideclare: Unknown datatype "+datatypetxt);
 
80
end
 
81
 
 
82
// Property
 
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))
 
87
  property=evstr(prop)
 
88
else
 
89
  error("m2scideclare: Unknown property "+proptxt);
 
90
end
 
91
// Property correction
 
92
if or(vtype==[Boolean,String]) then
 
93
  property="Real"
 
94
end
 
95
 
 
96
if strindex(name,".")<>[] then // Cell or Struct m2scideclare
 
97
  // Get varname
 
98
  endofname=min([strindex(name,[".","("])])-1
 
99
  varname=part(name,1:endofname)
 
100
  
 
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)
 
113
  end
 
114
    
 
115
  if firstfield=="entries" then // Cell
 
116
    vartype=Cell
 
117
  else // Struct
 
118
    vartype=Struct
 
119
  end
 
120
  
 
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,")"))))
 
124
  else
 
125
    vardims=list(1,1)
 
126
  end
 
127
  
 
128
  [isvar,index]=isdefinedvar(Variable(varname,Infer()))
 
129
    
 
130
  if ~isvar then // If variable does not exist it is added to varslist
 
131
    contents=struct()
 
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)))
 
135
  else
 
136
    inferreddims=varslist(index).dims
 
137
    
 
138
    err=%F
 
139
    for kd=1:min([lstsize(vardims) lstsize(infereddims)])
 
140
      if infereddims(kd)~=vardims(kd) & infereddims(kd)<>Unknown then
 
141
        err=%T
 
142
        break
 
143
      end
 
144
    end
 
145
    
 
146
    // Update dimensions
 
147
    if err 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)
 
152
    else
 
153
      varslist(index).dims=dims
 
154
    end
 
155
    
 
156
    // Update vtype
 
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)
 
164
    end
 
165
 
 
166
    // Update property
 
167
    if varslist(index).type.property==Unknown then
 
168
      varslist(index).type.property=Unknown
 
169
    else
 
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)
 
174
    end
 
175
    
 
176
    // Update contents (no verification made...too complex)
 
177
    contents=varslist(index).contents
 
178
    if vartype==Cell & typeof(contents)<>"ce" then
 
179
      contents=makecell()
 
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
 
182
      contents=struct()
 
183
      evstr("contents"+part(name,length(name))+"=1"); // should be removed is no more Scilab bug for it
 
184
    end
 
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)))
 
187
    
 
188
    
 
189
  end
 
190
else // Variable m2scideclare 
 
191
  
 
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);
 
197
    else
 
198
      %graphicswindow=vtype
 
199
    end
 
200
    return
 
201
  end
 
202
  
 
203
  [isvar,index]=isdefinedvar(Variable(name,Infer()))
 
204
 
 
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
 
208
    // Update dims
 
209
    infereddims=varslist(index).dims
 
210
    
 
211
    err=%F
 
212
    for kd=1:min([lstsize(dims) lstsize(infereddims)])
 
213
      if infereddims(kd)~=dims(kd) & infereddims(kd)<>Unknown then
 
214
        err=%T
 
215
        break
 
216
      end
 
217
    end
 
218
 
 
219
    if err 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)
 
224
    else
 
225
      varslist(index)=M2scivar(varslist(index).matname,varslist(index).sciname,Infer(dims,varslist(index).type))
 
226
    end
 
227
    
 
228
    // Update vtype
 
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)
 
236
    end
 
237
    
 
238
    // Update property
 
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)
 
246
    end
 
247
  end
 
248
end
 
249
endfunction
 
250
 
 
251
 
 
252
function str=tp2str(tp)
 
253
// Copyright INRIA
 
254
// Scilab Project - V. Couvert
 
255
// Returns equivalent of typeof() from data coming from type()
 
256
if tp==1 then
 
257
  str="Double"
 
258
elseif tp==4 then
 
259
  str="Boolean"
 
260
elseif tp==6 then
 
261
  str="Sparse"
 
262
elseif tp==8 then
 
263
  str="Int"
 
264
elseif tp==9 then
 
265
  str="Handle"
 
266
elseif tp==10 then
 
267
  str="String"
 
268
elseif tp==16 then
 
269
  str="Struct"
 
270
elseif tp==17 then
 
271
  str="Cell"
 
272
elseif tp==-1 then
 
273
  str="Unknown"
 
274
else
 
275
  error("m2scideclare: type "+string(tp)+" is not implemented")
 
276
end
 
277
endfunction
 
278
 
 
279
function str=prop2str(prop)
 
280
if type(prop)==10 then
 
281
  str=prop
 
282
elseif prop==-1 then
 
283
  str="Unknown"
 
284
else
 
285
  error("m2scideclare: property "+string(prop)+" is not implemented")
 
286
end
 
287
endfunction
 
288
 
 
289
function str=dims2str(dims)
 
290
str=""
 
291
for kd=1:lstsize(dims)
 
292
  str=str+string(dims(kd))+" "
 
293
end
 
294
endfunction