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
|
// In this script you can find some examples that show how to work with the
// SCI2C and USER libraries.
// Run the following code lines in the directory where the main.sci file is stored.
// cd <path_of_main.sci_file>
clear;
clc;
mode(0);
getf("InitializeLibraries.sci");
getf("GetCFunCall.sci");
[SCI2CLib, USER2CLib] = InitializeLibraries();
save('SCI2CLib.dat',SCI2CLib);
save('USER2CLib.dat',USER2CLib);
disp("Scalar Version:")
FunName = "cos";
InArg.Name="inscalar";
InArg.Type="z";
InArg.Size=[1,1];
OutArg.Name = "outscalar";
CFunCall = GetCFunCall(FunName,InArg,OutArg);
disp(CFunCall)
disp(" ")
disp(" ")
disp("Array Version:")
FunName = "sin";
InArg.Name="inmtx";
InArg.Type="s";
InArg.Size=[10,7];
OutArg.Name = "outmtx";
CFunCall = GetCFunCall(FunName,InArg,OutArg);
disp(CFunCall)
disp(" ")
disp(" ")
// Let's consider the following example
// **************************************
// The input function to be traslated is:
// --------------------------------------
// function y=foo(x)
// y = sin(cos(x));
// endfunction
// --------------------------------------
// **************************************
// The corresponding AST is:
// --------------------------------------
// Equal
// Expression:
// Funcall : sin
// #lhs : 1
// Rhs :
// Funcall : cos
// #lhs : 1
// Rhs :
// x
// EndFuncall
// EndFuncall
// Lhs :
// y
// EndEqual
// **************************************
// The AST reader generates the following User-readable Intermediate Representation:
// --------------------------------------
// __temp1 = cos(x);
// __temp2 = sin(__temp1);
// y = __temp2;
// **************************************
// The AST reader generates also the following internal Intermediate Representation:
// --------------------------------------
FunName = "cos";
InArg.Name="x";
InArg.Type="s"; // This parameter is retrieved from annotations.
InArg.Size=[10,7]; // This parameter is retrieved from annotations.
OutArg.Number = 1;
OutArg.Name = "__temp1";
CFunCall = GetCFunCall(FunName,InArg,OutArg);
disp(CFunCall)
disp(" ")
disp(" ")
FunName = "sin";
InArg.Name="__temp1";
InArg.Type="s"; // This parameter is retrieved from annotations.
InArg.Size=[10,7]; // This parameter is retrieved from annotations.
OutArg.Number = 1;
OutArg.Name = "__temp2";
CFunCall = GetCFunCall(FunName,InArg,OutArg);
disp(CFunCall)
disp(" ")
disp(" ")
adf
// Test Scalar Function
clear In1
clear Out1
In1.Name = "inscalar";
disp("Scalar Version:")
disp([SCI2CLib.cos.S.C.D.CINFO.NAME,"(",eval(SCI2CLib.cos.S.C.D.CINFO.ARGLIST),")"])
disp(" ")
disp(" ")
// Test Array Function
clear In1
clear Out1
In1.Name = "inarray";
In1.Size = [10,7];
Out1.Name = "outarray";
disp("Array Version:")
disp([SCI2CLib.sin.A.C.D.CINFO.NAME,"(",eval(SCI2CLib.sin.A.C.D.CINFO.ARGLIST),")"])
disp(" ")
disp(" ")
disp(" ")
disp(" ")
stoppami
// ------------------
// --- Example 1. ---
// ------------------
// In this example it is shown how to get the
// C function info from a given Scilab function.
// The user prepares a string (FunctionStructure) where
// all the Scilab function info are stored.
// Let's consider the following example:
// Scilab function: tan
// Input size: scalar (S)
// Input type: real (R)
// Input precision: double (D)
// In this case the user prepares the following string:
FunctionStructure = "tan.S.R.D.CINFO";
// And executes the following command:
getf("SearchFunctionName.sci");
[Cprototype, FlagFind] = SearchFunctionName(FunctionStructure, "SCI2CLib");
// ------------------
// --- Example 2. ---
// ------------------
// In this example it is shown what happens when
// the user searches for a function that is not
// stored neither in the SCI2CLib nor in the USER2CLib.
// Let's suppose that the user is searching for the
// foo function having two inputs:
FunctionStructure = "foo.S.R.D.S.R.D.CINFO";
// In this case if the user launches the following command:
// getf("SearchFunctionName.sci");
// [Cprototype, FlagFind] = SearchFunctionName(FunctionStructure, "SCI2CLib");
// he/she will get FlagFind = F!
// The problem is solved in the following way:
[Cprototype, FlagFind] = SearchFunctionName(FunctionStructure, "SCI2CLib");
if FlagFind==%F then
// We try to search the function in the USER2CLib.
[Cprototype, FlagFind] = SearchFunctionName(FunctionStructure, "USER2CLib");
if FlagFind==%F then
// If the function is not stored in the USER2CLib function,
// the tool will search for the file containing the function and will
// try to extract from that file the following info:
// CINFO.SIZE, CINFO.TYPE, CINFO.PREC
// Suppose the following CINFO have been retrieved:
// CINFO.SIZE = "S";
// CINFO.TYPE = "R";
// CINFO.PREC = "D";
// Once CINFO have been retrieved it is possible to insert the new function
// into the USER2CLib by using the following approach:
clear CINFO;
CINFO.CPROT = "y=SRDSRDfooSRD";
CINFO.SIZE = "S";
CINFO.TYPE = "R";
CINFO.PREC = "D";
USER2CLib = AddBranch( USER2CLib, FunctionStructure, CINFO );
[Cprototype, FlagFind] = SearchFunctionName(FunctionStructure, "USER2CLib");
end
end
// eval("USER2CLib." + FunctionStructure);
//
// now I change one output type
//
// Branch=FunctionStructure;
// TREE="USER2CLib";
//
getf("SearchFunctionName.sci");
getf("AddBranch.sci");
FunctionStructure = "fn.S.R.D.CINFO";
clear CINFO;
CINFO.CPROT = "y=SRDfnSRD";
CINFO.SIZE = "S";
CINFO.TYPE = "R";
CINFO.PREC = "D";
[Cprototype, FlagFind] = SearchFunctionName(FunctionStructure, "SCI2CLib");
if FlagFind==%F then
[Cprototype, FlagFind] = SearchFunctionName(FunctionStructure, "USER2CLib");
if FlagFind==%F then
USER2CLib = AddBranch( USER2CLib, FunctionStructure, CINFO );
end
end
eval("USER2CLib." + FunctionStructure)
//
// I add the same function with two outputs:
// 1. the first output is S.R.D
// 2. the second output is A.R.D.
//
// Branch=FunctionStructure;
// TREE="USER2CLib";
//
FunctionStructure = "fn.S.R.D.CINFO(2)";
clear CINFO;
S(1) = "S";
R(1) = "R";
D(1) = "D";
S(2) = "M";
R(2) = "R";
D(2) = "D";
CINFO.CPROT = "y=SRDfnSRDSRD";
CINFO.SIZE = S;
CINFO.TYPE = R;
CINFO.PREC = D;
[Cprototype, FlagFind] = SearchFunctionName(FunctionStructure, "SCI2CLib");
if FlagFind==%F then
[Cprototype, FlagFind] = SearchFunctionName(FunctionStructure, "USER2CLib");
if FlagFind==%F then
USER2CLib = AddBranch( USER2CLib, FunctionStructure, CINFO );
end
end
eval("USER2CLib." + FunctionStructure)
//
// fn.S.R.F.CINFO
//
// where
//
// CINFO.
// CPRO = "y=SRFfnSRF(x)"
// SIZE = "S"
// TYPE = "R"
// PREC = "F"
// Branch=FunctionStructure;
// TREE="USER2CLib";
getf("SearchFunctionName.sci");
getf("AddBranch.sci");
FunctionStructure = "fn.S.R.F.CINFO";
CINFO(1).CPROT = "y=SRFfnSRF";
CINFO(1).SIZE = "S";
CINFO(1).TYPE = "R";
CINFO(1).PREC = "F";
[Cprototype, FlagFind] = SearchFunctionName(FunctionStructure, "SCI2CLib");
if FlagFind==%F then
[Cprototype, FlagFind] = SearchFunctionName(FunctionStructure, "USER2CLib");
if FlagFind==%F then
USER2CLib = AddBranch(USER2CLib, FunctionStructure, CINFO );
end
end
eval("USER2CLib." + FunctionStructure)
|