File: m2sci.sci

package info (click to toggle)
scilab 2.4-1
  • links: PTS
  • area: non-free
  • in suites: potato, slink
  • size: 55,196 kB
  • ctags: 38,019
  • sloc: ansic: 231,970; fortran: 148,976; tcl: 7,099; makefile: 4,585; sh: 2,978; csh: 154; cpp: 101; asm: 39; sed: 5
file content (159 lines) | stat: -rw-r--r-- 4,001 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
function [txt,trad]=m2sci(lst,nam,Imode,Recmode)
// translate matlab syntax to scilab
//%Parameters
// lst   : list, represents the interpreted code of the matlab function given by macr2lst
// nam   : nam of the scilab function to generate
// txt   : character string column vector: the text of resulting scilab function
//!
//%Main variables
// vnms  : nx2 matrix of strings, each row contains the matlab and scilab name of 
//         a refered matlab variable 
// vtps  : a list, each entry is a list(?,?,?,?)
// Copyright INRIA
[lhs,rhs]=argn(0)
if rhs==1 then nam=lst(1),end
if rhs<=2 then Imode=%t,end
if rhs<=3 then Recmode=%f,end
//sci_min=sci_mini
//sci_max=sci_maxi
lst=mmodlst(lst)
lcount=1;level=[0,0];
quote=''''
//
sciparam()

//stack of named  variables

// add input variable in the defined variables
inputs=lst(3)
macrhs=size(inputs,2) 
vnms=[],
vtps=list()
for k=1:macrhs
  if or(inputs(k)==killed(1)) then
    vnms=[vnms;['%'+inputs(k),inputs(k)]],
  else
    if funptr(inputs(k))<>0 then inputs(k)='%'+inputs(k),end
    vnms=[vnms;[inputs(k),inputs(k)]],
  end
  if Imode then
    r=askfortype(inputs(k))
    if r<>[] then
      vtps($+1)=r
    else
      vtps($+1)=list('?','?','?',0)
    end
  else
    vtps($+1)=list('?','?','?',0)
  end
end,

// add predefined variable in the defined variables
vnms=[vnms;['%i','i']],
vtps($+1)=list('1','1','1',0)
vnms=[vnms;['%i','j']],
vtps($+1)=list('1','1','1',0)
vnms=[vnms;['%nan','NaN']],
vtps($+1)=list('1','1','1',0)
vnms=[vnms;['%nan','nan']],
vtps($+1)=list('1','1','1',0)
vnms=[vnms;['%inf','Inf']],
vtps($+1)=list('1','1','1',0)
vnms=[vnms;['%inf','inf']],
vtps($+1)=list('1','1','1',0)
vnms=[vnms;['%pi','pi']],
vtps($+1)=list('1','1','1',0)
vnms=[vnms;['%eps','eps']],
vtps($+1)=list('1','1','1',0)


outputs=lst(2)
maclhs=size(outputs,2)
bot=size(vtps)


// translate
sciexp=0
[crp,vnms,vtps]=ins2sci(lst,4,vnms,vtps)
dcl=[]

//add special code
// nargin & nargout
if find(vnms(:,1)=='nargin'|vnms(:,1)=='nargout') then
  dcl='[nargout,nargin] = argn(0)'
end
//Initial value of lhs arguments
ini=emptystr()
for k=outputs
  if find(inputs==k)==[] then ini=ini+k+'=[];',end
end
if ini==emptystr() then ini=[],end


//info on macros variables
n=size(vtps)
info=[]
for k=1:n
  m=string(vtps(k)(2))
  n=string(vtps(k)(3))
  tp=string(vtps(k)(1))
  info=[info;
      '// '+part(vnms(k,2),1:24)+' size :'+part(m+' x '+n,1:12)+' type: '+tp];
end
//write(%io(2),info,'(a)')
  


//add the function header
hdr='function '+lhsargs(outputs)+'='+nam+rhsargs(vnms(1:macrhs,1));
txt=[hdr;ini;dcl;crp(1:$-1)]


// generate associated translation function
if nam=='script' then
  f=fnam+'.sce'
  trad=[
      'function [stk,txt,top]=sci_'+fnam+'()'
      'stk=list(''exec('''+sci2exp(f)+''')'',''0'',''?'',''?'',''?'')']
else
  trad=[
      'function [stk,txt,top]=sci_'+nam+'()'
      'RHS=[]'
      'for k=1:rhs'
      '  RHS=[stk(top)(1),RHS]'
      '  top=top-1'
      'end'
      'top=top+1']
  if maclhs==0 then
    trad=[trad;
        'stk=list('+sci2exp(nam)+'+rhsargs(RHS),''0'',''?'',''?'',''?'')']
  elseif maclhs==1 then
    k1=find(outputs(1)==vnms(:,2));k1=k1(1);
    w=strcat([sci2exp(vtps(k1)(2)),sci2exp(vtps(k1)(3)),sci2exp(vtps(k1)(1))],',')
    trad=[trad;
        'stk=list('+sci2exp(nam)+'+rhsargs(RHS),''0'','+w+')']
  else  
    w=[]
    for k=1:maclhs
      k1=find(outputs(k)==vnms(:,2));k1=k1(1);
      w=[w;[sci2exp(vtps(k1)(2)),sci2exp(vtps(k1)(3)),sci2exp(vtps(k1)(1))]];
    end  
    trad=[trad;
        '//  w(i,1) is the ith output argument type'
        '//  w(i,2) is the ith output argument row dimension'
        '//  w(i,2) is the ith output argument column dimension'
        sci2exp(w,'w')]
    trad=[trad;
        'if lhs==1 then'
        '  stk=list('+sci2exp(nam)+'+rhsargs(RHS),''0'',w(1,1),w(1,2),w(1,3))'
        'else'
        '  stk=list()'
        '  for k=1:lhs'
        '    stk(k)=list('+sci2exp(nam)+'+rhsargs(RHS),''-1'',w(k,1),w(k,2),w(k,3))'
        '  end'
        'end']
  end
end