summaryrefslogtreecommitdiff
path: root/modules/string/macros/expression2code.sci
blob: 977a02f40ff0972e37c57f20bb62341af127f8c8 (plain)
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
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) INRIA - Vincent COUVERT
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt


function C=expression2code(e)
    // Translate an expression tree to macro code (called by tree2code)
    // Input:
    // - e: expression 'tree'
    // Output:
    // - C: Scilab code corresponding to e
    // V.C.

    // Change format for constants
    fmtsav=format();
    format(16);

    // Tables of symbols arranged by priority in computations
    sumops=["+","-","&","|"] //1
    prodops=["*","/",".*","./","\",".\","^",".^"] //2
    othops=["==",">=","<=","~=",">","<","~",".''",".''",":"] //3

    C=[]
    select typeof(e)
        // ---------
        // OPERATION
        // ---------
    case "operation" then
        operator=e.operator
        operands=[]
        nb_op=size(e.operands)
        if and(operator<>["cc","cceol"]) then
            for i=1:nb_op
                if e.operands(i)==list("EOL") then
                    continue
                end
                expression = expression2code(e.operands(i));
                if expression == "eye()" then
                    expression = ":";
                end
                operands=[operands;expression]
            end
        end

        // Row concatenation
        if operator=="rc" then
            for i=1:nb_op
                if typeof(e.operands(i))=="operation" then
                    if e.operands(i).operator=="rc" then
                        operands(i)=part(operands(i),2:length(operands(i))-1)
                    elseif or(e.operands(i).operator==["cceol"]) then
                        operands(1)=part(operands(1),2:length(operands(1)))
                        operands($)=part(operands($),1:length(operands($))-1)
                    end
                end
            end
            C="["+strcat(operands,",")+"]"
            // Multi-line column concatenation
        elseif operator=="cceol" then
            for i=1:nb_op
                opi=expression2code(e.operands(i))
                if typeof(e.operands(i))=="operation" then
                    if e.operands(i).operator=="rc" then
                        opi=part(opi,2:length(opi)-1)
                    elseif or(e.operands(i).operator==["cc","cceol"]) then
                        opi(1)=part(opi(1),2:length(opi(1)))
                        opi($)=part(opi($),1:length(opi($))-1)
                    end
                end

                if i==1 then
                    C="["
                    if size(opi,"*")>1 then
                        C = [C+opi(1);opi(2:$)]
                    else
                        C = C+opi
                    end
                    C($)=C($)+";"
                    C=[C;""]
                else
                    if size(opi,"*")>1 then
                        C = [C(1:$-1);C($)+opi(1);opi(2:$)]
                    else
                        C = [C(1:$-1);C($)+opi]
                    end
                    C($)=C($)+"]"
                end
            end
            // Column concatenation
        elseif operator=="cc" then
            C="["
            for i=1:nb_op
                opi=expression2code(e.operands(i))
                // Delete [ and ] if there are...
                if typeof(e.operands(i))=="operation" then
                    if e.operands(i).operator=="rc" then
                        opi=part(opi,2:length(opi)-1)
                    elseif or(e.operands(i).operator==["cc","cceol"]) then
                        opi(1)=part(opi(1),2:length(opi(1)))
                        opi($)=part(opi($),1:length(opi($))-1)
                    end
                end
                if i==1 then
                    if size(opi,"*")>1 then
                        C = [C+opi(1);opi(2:$)]
                    elseif opi=="(EOL)" then
                        C = [C;""];
                    else
                        C = C+opi
                    end
                    C($)=C($)+";"
                else
                    if size(opi,"*")>1 then
                        C = [C(1:$-1);C($)+opi(1);opi(2:$)]
                    elseif opi=="(EOL)" then
                        C = [C;""]
                    else
                        C = [C(1:$-1);C($)+opi]
                    end
                end
            end
            C($)=C($)+"]"
            // Extraction
        elseif operator=="ext" then
            if size(e.operands)==1 then
                C=e.operands(1).name
                return
            end
            if type(e.operands(2))==15 then // Recursive extraction
                C=operands(1)+operands(2)
            else
                // Deal with :
                for k=2:size(operands,"*")
                    if operands(k)==""":""" then
                        operands(k)=":"
                    elseif operands(k)=="""$""" then
                        operands(k)="$"
                    elseif operands(k)=="""*""" then // Only used for M2SCI
                        operands(k)="*"
                    end
                end
                val = part(operands(2),1)=="""" & part(operands(2),length(operands(2)))==""""
                if val then // struct field
                    C=operands(1)+"."+evstr(operands(2))
                    if size(operands,"*")>=3 then
                        C=C+"("
                    end
                    for k=3:size(operands,"*")
                        C=C+","+operands(k)
                    end
                    if size(operands,"*")>=3 then
                        C=C+")"
                    end
                else
                    C=operands(1)+"("+operands(2)
                    for k=3:size(operands,"*")
                        C=C+","+operands(k)
                    end
                    C=C+")"
                end
            end
            // Insertion
        elseif operator=="ins" then
            if type(e.operands(2))==15 then // Recursive insertion
                C=operands(1)+operands(2)
            else
                // Deal with :
                for k=2:size(operands,"*")
                    if operands(k)==""":""" then
                        operands(k)=":"
                    elseif operands(k)=="""$""" then
                        operands(k)="$"
                    elseif operands(k)=="""*""" then // Only used in M2SCI
                        operands(k)="*"
                    end
                end
                val = part(operands(2),1)=="""" & part(operands(2),length(operands(2)))==""""
                if val then // struct field
                    C=operands(1)+"."+evstr(operands(2))
                    if size(operands,"*")>=3 then
                        C=C+"("
                    end
                    for k=3:size(operands,"*")
                        C=C+","+operands(k)
                    end
                    if size(operands,"*")>=3 then
                        C=C+")"
                    end
                else
                    C=operands(1)+"("+operands(2)
                    for k=3:size(operands,"*")
                        C=C+","+operands(k)
                    end
                    C=C+")"
                end
            end
            // Unary Operators
        elseif size(operands,"*")==1 then
            if or(operator==["''",".''"]) then
                if typeof(e.operands(1))=="operation" then
                    if and(e.operands(1).operator<>["rc","cc","-","+"]) then
                        operands="("+operands+")"
                    end
                end
                C=operands+operator
            elseif or(operator==["+","-"]) then
                if typeof(e.operands(1))=="operation" then
                    if or(e.operands(1).operator==["-","+"]) then
                        operands="("+operands+")"
                    end
                end
                C=operator+operands
            else
                C=operator+operands
            end
            // Other operators
        else
            // Parenthesize
            if or(operator==["+","-"]) then
                for i=1:nb_op
                    if typeof(e.operands(i))=="operation" then
                        if or(e.operands(i).operator==othops) then
                            operands=[operands(1:i-1) "("+operands(i)+")" operands(i+1:$)]
                        end
                    end
                end
                for i=2:nb_op
                    if typeof(e.operands(i))=="operation" then
                        if or(e.operands(i).operator==sumops) then
                            operands=[operands(1:i-1) "("+operands(i)+")" operands(i+1:$)]
                        end
                    end
                end
            end
            if or(operator==[prodops,othops]) & (operator<>":") then
                if typeof(e.operands(1))=="operation" then
                    if or(e.operands(1).operator==[sumops,prodops,othops]) then
                        operands(1)="("+operands(1)+")"
                    end
                end
                if typeof(e.operands(2))=="operation" then
                    if or(e.operands(2).operator==[sumops,prodops,othops]) then
                        operands(2)="("+operands(2)+")"
                    end
                end
            end
            if part(operator,1)=="." & part(operator,length(operator))=="." | or(operator==["&","|"]) then
                C=strcat(operands," "+operator+" ")
            elseif part(operator,1)=="." then
                C=strcat(operands," "+operator)
            elseif part(operator,length(operator))=="." then
                C=strcat(operands,operator+" ")
            else
                C=strcat(operands,operator)
            end
        end
        // --------
        // CONSTANT
        // --------
    case "cste" then
        C=sci2exp(e.value)
        if C==""":""" then
            //C=":"
        elseif C=="""$""" then
            C="$"
        elseif C=="""*""" then // Only used in M2SCI
            C="""*"""
            //  C="*"
        end
        // --------
        // VARIABLE
        // --------
    case "variable" then
        C=e.name
        // ----------------
        // CHARACTER STRING
        // ----------------
    case "string" then
        C=e
        // -------
        // FUNCALL
        // -------
    case "funcall" then
        if size(e.rhs)==0 then
            I=I; // I is defined in instruction2code
            C=e.name
            [l,mac]=where()
            if size(grep(mac,"expression2code"),"*")>1 then
                C=C+"()"
            elseif typeof(I)=="equal" & ( (typeof(I.lhs(1))=="variable" & I.lhs(1).name<>"ans") | typeof(I.lhs(1))=="operation") then
                C=C+"()"
            end
        else
            rhscode = rhs2code(e.rhs);
            if size(rhscode,"*")==1 then
                C=[e.name+"("+rhscode+")"]
            else
                C=[e.name+"("+rhscode(1);rhscode(2:($-1));rhscode($)+")"]
            end
        end
        // ----
        // LIST
        // ----
    case "list"
        // Recursive extraction
        C=[]
        for k=1:lstsize(e)
            ind=expression2code(e(k))
            if type(e(k))==15 then // Recursive extraction in recursive extraction
                ind=strsubst(ind,")(",",")
                if part(ind, 1)=="(" then
                    ind = part(ind, 2:length(ind))
                end
                if part(ind, length(ind))==")" then
                    ind = part(ind, 1:(length(ind)-1))
                end
            end
            if ind==""":""" then
                ind=":"
            elseif ind=="""$""" then
                ind="$"
            elseif ind=="""*""" then // Only used in M2SCI
                ind="*"
            end
            val = part(ind,1)=="""" & part(ind,length(ind))==""""
            if val then
                C=C+"."+evstr(ind)
            else
                C=C+"("+ind+")"
            end
        end
        C=strsubst(C,")(",",")
        // -----
        // EQUAL (can occur fir disp(a=1) for example)
        // -----
    case "equal"
        C=instruction2code(e)
        // -------
        // COMMENT (inside a matrix declaration for example)
        // -------
    case "comment"
        C=instruction2code(e)
    else
        error(msprintf(gettext("%s: This feature has not been implemented: %s.\n"),"expression2code",typeof(e)));
    end

    // Restore format
    if fmtsav(1)==1 then
        format("v",fmtsav(2));
    else
        format("e",fmtsav(2));
    end
endfunction