File: m2sci_syntax.sci

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (499 lines) | stat: -rw-r--r-- 13,856 bytes parent folder | download
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
function [helppart,txt,batch]=m2sci_syntax(txt)
// Copyright INRIA
// Scilab Project - V. Couvert
// Make minor changes on M-file data syntax to have it readable by Scilab
// Input arguments:
//  - txt: the contents of an M-file
// Output:
//  - helppart: Matlab help contained in M-file
//  - txt: input txt modified (If M-file contains only comments returned txt is[])
//  - batch: boolean flag indicates if it is a batch file

// m2sci kernel functions called :
//  - isacomment
//  - isinstring
//  - replace_brackets

sciparam();
quote=''''
dquote=""""
ctm='.'+'.'+'.' // Continuation mark
batch=%t

k=0
first_ncl=[]
while k<size(txt,'r')
  k=k+1
  tk=txt(k)
  if part(stripblanks(tk),1:9) == 'function ' | part(stripblanks(tk),1:9) == 'function[' then
    eolind=strindex(tk,";")
    if eolind<>[] then
      kc=isacomment(tk)
      if kc<>0 then // Current line has or is a comment
      // If function prototype immediately followed by a comment on same line
        if stripblanks(part(tk,eolind(1):kc))<>'' then
          txt=[txt(1:k-1);part(tk,1:eolind(1)-1);part(tk,eolind(1)+1:length(tk));txt(k+1:size(txt,"*"))]
          tk=part(tk,1:eolind(1)-1)
        end
      elseif stripblanks(part(tk,eolind(1)+1:length(tk)))<>'' then
        txt=[txt(1:k-1);part(tk,1:eolind(1)-1);part(tk,eolind(1)+1:length(tk));txt(k+1:size(txt,"*"))]
        tk=part(tk,1:eolind(1)-1) 
      end
    end      
  end
end

// Number of lines in txt (i.e. in M-file)
n=size(txt,'r')
eoltoinsert=0
firstctm=[]
k=0
while k<size(txt,'r')
  k=k+1
  kc=strindex(txt(k),ctm)
  isacontline=%f
  
  for kck=kc,
    kc1=isacomment(txt(k))
    if ~isinstring(txt(k),kck) then
      if kc1<>0 then // Line has a comment
	if kc1<kck then // Continuation mark in a comment
	else // Comment follow continuation mark
	  com=part(txt(k),kc1(1):length(txt(k)))
	  txt(k)=part(txt(k),1:kck-1)+txt(k+1)+com
	  txt(k+1)=[]
	  k=k-1
	  break
	end
      else // Not a comment line
	if isempty(firstctm) then firstctm=k;end
	txt(k)=part(txt(k),1:kck-1)+txt(k+1)
	txt(k+1)=[]
	eoltoinsert=eoltoinsert+1
	if isempty(strindex(txt(k),ctm)) then // If no ctm in txt(k), insert all necessary EOL to keep nblines unchanged
	  for l=0:eoltoinsert-1
	    txt=[txt(1:firstctm-1+l);"";txt(firstctm-1+l+1:$)]
	  end
	  eoltoinsert=0
	  firstctm=[]
	end
	k=k-1
	break
      end
    end
  end
  if k<>0 then 
    if part(stripblanks(txt(k)),1)=="}" then
      txt(k-1)=txt(k-1)+txt(k);
      txt(k)="";
    end
  end
end

// Change comments and get help part in input txt
n=size(txt,'r')
first=%t
helppart=[],endofhelp=%f

for k=1:n
  tk=txt(k)
  
  // ifthenelse expression like if (x==1)t=2 becomes if (x==1) t=2
  // Add a blank between parenthesize expression and the first instruction 
  kif=strindex(tk,"if")
  if kif<>[] then
    kcom=isacomment(tk)
    for i=1:size(kif,"*")
      if kif(i)>kcom & kcom<>0 then
        break
      else
        if (kif(i)>1 & or(stripblanks(part(tk,kif(i)-1:kif(i)+2))==["if(","if"])) | (kif(i)==1 & or(stripblanks(part(tk,kif(i):kif(i)+2))==["if(","if"]))
          m=min(strindex(tk,"("))
	  if m<>[] then 
            for l=1:size(m,"*")
	      if m(l)>=kif(i)+2
	        if stripblanks(part(tk,kif(i)+2:m(l)))=="(" then
	          openpar=1
	          mtemp=m(l)+1
                  while openpar<>0
	            if or(part(tk,mtemp)=="(") then
	              openpar=openpar+1
	            elseif or(part(tk,mtemp)==")") then
                      openpar=openpar-1
                    end
                    mtemp=mtemp+1 
                  end
                  tk=part(tk,1:mtemp-1)+" "+part(tk,mtemp:length(tk))
	          break
	        end
              end
            end
          end
        end
      end
    end
  end  
  

  // Parenthesize expressions like 1+-2, 1*-2... which becomes 1+(-2), 1*(-2)...
  // Parentheses are deleted by comp() and will be added one more time by %?2sci function
kop=strindex(tk,["+","-","*","/","\","^"])
kcom=isacomment(tk)
if kcom<>0 then
  kop=kop(kop<kcom)
end
offset=1
l=1

while l<=size(kop,"*")
  if ~isinstring(tk,kop(l)) then
    ksym=kop(l)+offset
    while part(tk,ksym)==" "
      ksym=ksym+1
      if ksym>length(tk) then
	break
      end
    end
    if part(tk,ksym)=="-" then
      l=l+1
    endoftk=part(tk,ksym+1:length(tk))
  m=min(strindex(endoftk,[ops(:,1)',";"]))
  p=min(strindex(endoftk,["(","["]))

    if p<>[] then
      if m==[] | (m<>[] & p<m) then
	openpar=1
	p=p+1
	while openpar<>0
	  if or(part(endoftk,p)==["(","["]) then
	      openpar=openpar+1
	      elseif or(part(endoftk,p)==[")","]"]) then
	  openpar=openpar-1
	end
	p=p+1 
      end
      tk=part(tk,1:ksym-1)+"("+part(tk,ksym:ksym+p-1)+")"+part(tk,ksym+p:length(tk))	    
      if strindex(part(tk,ksym+2:ksym+p-1),["+","-","*","/","\","^"])==[] then
	offset=offset+2
      else
	offset=offset+1
      end
    elseif m<p then 
      tk=part(tk,1:ksym-1)+"("+part(tk,ksym:ksym+m-1)+")"+part(tk,ksym+m:length(tk))
      offset=offset+2
    end
  elseif p==[] then
    if m<>[] then
      tk=part(tk,1:ksym-1)+"("+part(tk,ksym:ksym+m-1)+")"+part(tk,ksym+m:length(tk))
      offset=offset+2
    else
      tk=part(tk,1:ksym-1)+"("+part(tk,ksym:length(tk))+")"
      offset=offset+2 
    end
  end
end
end
l=l+1
end

// Modify expressions like 1++2, 1*+2... which become 1+2, 1*2...
kop=strindex(tk,["+","-","*","/","\","^"])
offset=0
for l=1:size(kop,"*")
  ksym=kop(l)+offset+1
  while part(tk,ksym)==" "
    ksym=ksym+1
    if ksym>length(tk) then
      break
    end
  end
  if part(tk,ksym)=="+" then
    tk=part(tk,1:ksym-1)+part(tk,ksym+1:length(tk))
    offset=offset-1
  end
end

// Insert a blank when a digit is followed by a dotted operator
// So that point is associated to operator and not to digit
// Because if it is associated to digit, dot is suppressed by comp()
kdot=strindex(tk,[".*","./",".\",".^",".''"])
if kdot<>[] then
  kdgt=kdot(find(abs(str2code(part(tk,kdot-1)))<9))
  for kk=size(kdgt,"*"):-1:1
    tk=part(tk,1:kdgt(kk)-1)+" "+part(tk,kdgt(kk):length(tk));
  end
end

// Modify struct like x.(fieldname) which become x(fieldname)
tk=strsubst(tk,".(","(")

// Parenthesize calls to pause when pause on or pause off
kpause=strindex(tk,"pause") 
kpsav=length(tk) // kpsave is kp value for l-1 index
for l=size(kpause,"*"):-1:1
  kp=kpause(l)
  kon=strindex(tk,"on")
  kon=kon(find((kon>kp)&(kon<kpsav)))
  if kon<>[] then
    for l=kp+5:kon-1
      if part(tk,l)<>" " then
	break
      end
    end
    tk=part(tk,1:kp+4)+"(''on'')"+part(tk,kon+2:length(tk))
  end
  koff=strindex(tk,"off")
  koff=koff(find((koff>kp)&(koff<kpsav)))
  if koff<>[] then
    for l=kp+5:koff-1
      if part(tk,l)<>" " then
	break
      end
    end
    tk=part(tk,1:kp+4)+"(''off'')"+part(tk,koff+3:length(tk))
  end
  kpsav=kp
end

// Convert @fhandle to 'fhandle' (cf feval)
symbs=[" ",",",";","=",")","]"]
kpunct=strindex(tk,"@")
kcom=isacomment(tk)
if kcom<>0 then
  kpunct=kpunct(kpunct<kcom)
end
if kpunct<>[] then
  for l=size(kpunct,"*"):-1:1
    if ~isinstring(tk,kpunct(l)) then
      kk=gsort(strindex(tk,symbs),"r","i")
      kk=kk(find(kk>kpunct(l)))
      if kk==[] then 
        kk=length(tk)
        tk=part(tk,1:kpunct(l)-1)+quote+part(tk,kpunct(l)+1:kk)+quote
      else
        kk=kk(1)
        tk=part(tk,1:kpunct(l)-1)+quote+part(tk,kpunct(l)+1:kk-1)+quote+part(tk,kk:length(tk))
      end
    end
  end
end

// Looking for comments
kc=isacomment(tk)
if kc<>0 then // Current line has or is a comment
  // If function prototype immediately followed by a comment on same line
  if part(stripblanks(tk),1:9) == 'function ' | part(stripblanks(tk),1:9) == 'function[' then
    first_ncl=k
  end      
  com=part(tk,kc+1:length(tk)) 
  if stripblanks(part(tk,1:kc-1))<>'' &  ~(stripblanks(part(tk,1:9))=='function ' | stripblanks(part(tk,1:9))=='function[')  then endofhelp=%t;end 
  if ~endofhelp & part(tk,1:9) ~= 'function ' then helppart=[helppart;com];end // Get help part placed at the beginning of the file
  if length(com)==0 then com=" ",end
  com=strsubst(com,quote,quote+quote)
  com=strsubst(com,dquote,dquote+dquote)
  if part(com,1:12)=="m2sciassume " | part(com,1:13)=="m2scideclare " then // User has given a clue to help translation
    if part(com,1:12)=="m2sciassume " then
      warning("m2sciassume is obsolete, used m2scideclare instead");
    end
    com=";m2scideclare("+quote+part(com,13:length(com))+quote+")"
  else
    com=";//"+com
  end
  tkbeg=part(tk,1:kc-1)
  
  // Short circuiting operators
  if ~isempty(strindex(tkbeg,"||")) then
    orexpr=tokens(tkbeg,"|")
    boolendsymbol=%f
    orexprtemp=orexpr($)
    indendsymbol=strindex(orexpr($),[";",","])
    if indendsymbol<>[] then 
      if stripblanks(part(orexpr($),indendsymbol($)+1:length(orexpr($))))=='' then
        boolendsymbol=%t
	endsymbol=part(orexprtemp,indendsymbol($))
	indendsymbol=indendsymbol($)
        orexpr($)=part(orexpr($),1:indendsymbol($)-1)
      end
    end   
    for i=2:size(orexpr,"*")
      notsymbol=strindex(stripblanks(orexpr(i)),"~")
      if notsymbol<>[]
        if notsymbol(1)==1 then
          orexpr(i)="("+  stripblanks(orexpr(i)) + ")" 
        end 
      end
    end    
    for kk=2:2:size(orexpr,"*")
      orexpr=[orexpr(1:kk);"%shortcircuit";orexpr(kk+1:size(orexpr,"*"))]
    end
    tkbeg=strcat(orexpr,"|")
    if boolendsymbol then
      tkbeg=tkbeg+endsymbol
    end
  end
  if ~isempty(strindex(tkbeg,"&&")) then
    andexpr=tokens(tkbeg,"&")
    boolendsymbol=%f
    andexprtemp=andexpr($)
    indendsymbol=strindex(andexpr($),[";",","])
    if indendsymbol<>[] then 
      if stripblanks(part(andexpr($),indendsymbol($)+1:length(andexpr($))))=='' then
        boolendsymbol=%t
	endsymbol=part(andexprtemp,indendsymbol($))
	indendsymbol=indendsymbol($)
        andexpr($)=part(andexpr($),1:indendsymbol($)-1)
      end
    end
    for i=2:size(andexpr,"*")
      notsymbol=strindex(stripblanks(andexpr(i)),"~")
      if notsymbol<>[]
        if notsymbol(1)==1 then
          andexpr(i)="("+  stripblanks(andexpr(i)) + ")" 
        end 
      end
    end    
    for kk=2:2:size(andexpr,"*")
      andexpr=[andexpr(1:kk);"%shortcircuit";andexpr(kk+1:size(andexpr,"*"))]
    end
    tkbeg=strcat(andexpr,"&")
    if boolendsymbol then
      tkbeg=tkbeg+endsymbol
    end
  end
  
  // varargout replaced by %varargout so that code can be compiled with varargout considered as a Cell
  if isempty(strindex(tkbeg,"function")) then
    tkbeg=strsubst(tkbeg,"varargout","%varargout")
  end

  txt(k)=tkbeg+com
else // Current line has not and is not a comment line 
  if first then // Function keyword not yet found
    tk=stripblanks(tk)
    if tk<>'' then // Current line is not a blank line
      if ~(part(tk,1:9) == 'function '| part(tk,1:9) == 'function[') then
        endofhelp=%t;
        txt(k)=tk; // VC 01/04/2003
      else
        first_ncl=k
        first=%f
    end
  else
    if ~endofhelp then helppart=[helppart;' '],end
    txt(k)="// "
  end
else // Current line is a line after function keyword
endofhelp=%t
txt(k)=tk
end

// Short circuiting operators
if ~isempty(strindex(tk,"||")) then
  orexpr=tokens(tk,"|")  
  indendsymbol=strindex(orexpr($),[";",","])
  boolendsymbol=%f
  if indendsymbol<>[] then 
    if stripblanks(part(orexpr($),indendsymbol($)+1:length(orexpr($))))=='' then
      boolendsymbol=%t
      indendsymbol=indendsymbol($)
      endsymbol=part(orexpr($),indendsymbol($))
      orexpr($)=part(orexpr($),1:indendsymbol($)-1)
    end
  end 
  for i=2:size(orexpr,"*")
    notsymbol=strindex(stripblanks(orexpr(i)),"~")
    if notsymbol<>[]
      if notsymbol(1)==1 then
        orexpr(i)="("+  stripblanks(orexpr(i)) + ")" 
      end 
    end
  end
  for kk=2:2:size(orexpr,"*")
    orexpr=[orexpr(1:kk);"%shortcircuit";orexpr(kk+1:size(orexpr,"*"))]
  end
  tk=strcat(orexpr,"|")
  if boolendsymbol then
    tk=tk+endsymbol
  end
end
if ~isempty(strindex(tk,"&&")) then
  andexpr=tokens(tk,"&")
  boolendsymbol=%f
    andexprtemp=andexpr($)
    indendsymbol=strindex(andexpr($),[";",","])
    if indendsymbol<>[] then 
      if stripblanks(part(andexpr($),indendsymbol($)+1:length(andexpr($))))=='' then
        boolendsymbol=%t
	endsymbol=part(andexprtemp,indendsymbol($))
	indendsymbol=indendsymbol($)
        andexpr($)=part(andexpr($),1:indendsymbol($)-1)
      end
    end
  for i=2:size(andexpr,"*")
    notsymbol=strindex(stripblanks(andexpr(i)),"~")
    if notsymbol<>[]
      if notsymbol(1)==1 then
        andexpr(i)="("+  stripblanks(andexpr(i)) + ")" 
      end 
    end
  end    
  for kk=2:2:size(andexpr,"*")
    andexpr=[andexpr(1:kk);"%shortcircuit";andexpr(kk+1:size(andexpr,"*"))]
  end
  tk=strcat(andexpr,"&")
  if boolendsymbol then
    tk=tk+endsymbol
  end
end

// varargout replaced by %varargout so that code can be compiled with varargout considered as a Cell
if isempty(strindex(tk,"function")) then
  tk=strsubst(tk,"varargout","%varargout")
end

txt(k)=tk
end
end

// When there is just help line in txt
if ~endofhelp then
  txt=[] 
  return
end

// Syntax modification

// Complex variable
txt=i_notation(txt)

// Replace double quotes
txt=strsubst(txt,dquote,dquote+dquote)

// Replace switch by select
txt=strsubst(txt,"switch ","select ")
txt=strsubst(txt,"switch(","select (")

// Replace otherwise by else
txt=strsubst(txt,"otherwise","else")

// Replace {..} by (..) or [..] : useful for cells translation
txt=replace_brackets(txt)

// Place function definition line at first line
kc=strindex(txt(first_ncl),"function")
if kc==[] then
  // Batch file
  txt=["function []="+fnam+"()";txt] // fnam is defined in mfile2sci() 
  batch=%t
else
  kc=kc(1)
  batch=%f
  if first_ncl<>1 then
    while strindex(txt(first_ncl($)+1),ctm)<>[] then
      first_ncl=[first_ncl,first_ncl($)+1]
    end
    txt=[txt(first_ncl);txt(1:first_ncl(1)-1);txt(first_ncl($)+1:$)]
  end
end

endfunction