summaryrefslogtreecommitdiff
path: root/2.3-1/macros/ASTManagement/AST2Ccode.sci
blob: d44a3393e3f5e4d3607a8f19b5b6f7f18d86f04a (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
function AST2Ccode(FileInfoDatFile)
// function AST2Ccode(FileInfoDatFile)
// -----------------------------------------------------------------
// Read the AST and call the corresponding handlers.
//
// Input data:
// //NUT: add description here
//
// Output data:
// //NUT: add description here
//
// Status:
// 11-May-2007 -- Raffaele Nutricato: Author.
// 15-June-2017 -- Ukasha Noor: Revised By
//
// Copyright 2007 Raffaele Nutricato.
// Contact: raffaele.nutricato@tiscali.it
// -----------------------------------------------------------------
// ------------------------------
// --- Check input arguments. ---
// ------------------------------
SCI2CNInArgCheck(argn(2),1,1);


//NUT: questa funzione e' da sistemare meglio

// ---------------------
// --- Load section. ---
// ---------------------
// --- Load File Info Structure. ---
load(FileInfoDatFile,'FileInfo');

// --- Load Shared Info Structure. ---
load(FileInfo.SharedInfoDatFile,'SharedInfo');
// -------------------------
// --- End load section. ---
// -------------------------

// ---------------------------------------------------
// --- Extraction of the function name and number. ---
// ---------------------------------------------------
nxtscifunname   = SharedInfo.NextSCIFunName;
nxtscifunnumber = SharedInfo.NextSCIFunNumber;
ReportFileName  = FileInfo.Funct(nxtscifunnumber).ReportFileName;
SharedInfo.Function_list = [];
SharedInfo.Function_list_index = 1;

// ---------------------------------
// --- Parameter Initialization. ---
// ---------------------------------

global cc_count
cc_count = 0;

global rc_count
rc_count = 0;

global SCI2CSTACK
SCI2CSTACK = ['EMPTYSTACK'];

global StackPosition;
StackPosition = 1;

global STACKDEDUG
STACKDEDUG = 0; // 1 -> Every Pop and Push operation on the stack, the stack content will be printed on screen.

global disp_isthere
disp_isthere = 0;

FName = null
// -------------------------------------
// --- End parameter Initialization. ---
// -------------------------------------

ASTFileName = FileInfo.Funct(nxtscifunnumber).ASTFileName;

// -----------------------
// --- Initialization. ---
// -----------------------
// --- Open AST file. ---
SharedInfo.ASTReader.fidAST = SCI2COpenFileRead(ASTFileName);
fidAST = SharedInfo.ASTReader.fidAST;

OrigWorkAreaUsedBytes = SharedInfo.WorkAreaUsedBytes;
OrigUsedTempScalarVars = SharedInfo.UsedTempScalarVars;

PrintStepInfo('Generate C code in '+FileInfo.Funct(nxtscifunnumber).FinalCFileName,...
   FileInfo.GeneralReport,'both');
// ---------------------------
// --- End initialization. ---
// ---------------------------

// ------------------------
// --- Parse AST header. ---
// ------------------------
ASTHeader  = AST_ReadASTHeader(fidAST,ReportFileName);
SharedInfo = AST_HandleHeader(ASTHeader,FileInfo,SharedInfo);
//NUT: le metto per ora perche' quando provo a cercare lo specifier di precisione al termine
//NUT: del programma non ho piu' nulla da poppare se lo specifier e' assente. Al limite posso mettere la program e i nomi
//NUT: al posto di dummy.
AST_PushASTStack('Dummy');
AST_PushASTStack('Dummy');
AST_PushASTStack('Dummy');
AST_PushASTStack('Dummy');
AST_PushASTStack('Dummy');
AST_PushASTStack('Dummy');
AST_PushASTStack('Dummy');
AST_PushASTStack('Dummy');
//NUT: Se ne tolgo qualcuno ottengo errori
// ----------------------------
// --- End Parse AST header. ---
// ----------------------------
         //NUT: better to have a function.

         // --- Reset TempVars Structure. ---
         TempVars = [];
         // Reset info related to temp variables used in the C code.
         SharedInfo.WorkAreaUsedBytes  = OrigWorkAreaUsedBytes;
         SharedInfo.UsedTempScalarVars = OrigUsedTempScalarVars;
         //NUT: put here a manageeol so that you can have all the save and load you want.
         SharedInfo.ASTReader.UsedTempVars = 0;
// ----------------------------------
// --- Main loop to read the AST. ---
// ----------------------------------
//NUT: file ottenuto con m2sci se hai tempo prova a vedere se ci sono inesattezze.
//NUT: inoltre per maggiore eleganza si puo' pensare di introdurre piu' funzioni

while ~meof(fidAST)
   // Read a line from the AST
   tline = mgetl(fidAST,1);
   AST_CheckLineLength(tline);
   treeline = stripblanks(tline);

   if STACKDEDUG == 1
      disp('Read AST Line: '+treeline);
   end
   // Analyze line.
   select treeline

      // ------------------
      // --- Functions. ---
      // ------------------
      //NUT: qui puoi anche aggiunger piu' case per specificare meglio la struttura della funcall
      //NUT: i case aggiunti ovviamente faranno solo il push della treeline.
      case 'EndOperation' then
         [disp_isthere,FileInfo,SharedInfo] = AST_HandleEndGenFun(disp_isthere,FileInfo,SharedInfo,'Operation');
      case 'EndFuncall' then
	 if rc_count > 0 & cc_count == 0
		[EqualInArgName,EqualInArgScope,EqualNInArg,FName] = AST_HandleFuncArray(FileInfo,SharedInfo);
	elseif cc_count > 0
		[EqualInArgName,EqualInArgScope,EqualNInArg,FName] = AST_HandleFuncArray2D(FileInfo,SharedInfo);
	else
         [disp_isthere,FileInfo,SharedInfo] = AST_HandleEndGenFun(disp_isthere,FileInfo,SharedInfo,'Funcall');
	end
      // --------------
      // --- Equal. ---
      // --------------
      case 'EndEqual' then
      //NUT: prima di lanciare l'analisi della equal puoi mettere degli argomenti dummy
      //NUT: per fare in modo di coprire le ins, anche se ci puo' essere qualche rischio quando
      //NUT: ho miste ins e variabili, per esempio [c(1,1), a] = twooutfun();
      //NUT: in questo caso solo una delle due equal va scartata.
 	 if rc_count > 0 & cc_count == 0
		[FileInfo,SharedInfo] = AST_HandleFunRC(FName,FileInfo,SharedInfo);
		rc_count = 0;
	 elseif cc_count > 0
		[FileInfo,SharedInfo] = AST_HandleFunCC(FName,cc_count,FileInfo,SharedInfo);
		rc_count = 0;
                cc_count = 0;
	 else
		if disp_isthere == 0
         	[disp_isthere,FileInfo,SharedInfo] = AST_HandleEndGenFun(disp_isthere,FileInfo,SharedInfo,'Equal');
         	SharedInfo = INIT_SharedInfoEqual(SharedInfo);
		end
	 end
	 disp_isthere = 0;
 	 FName = null
      case 'Equal' then
         SharedInfo.Equal.Enabled = 1; // 1 means enabled -> we are inside an equal AST block.
         AST_PushASTStack(treeline);
      case 'Lhs       :' then
		if rc_count > 0 & cc_count == 0
		SharedInfo.Equal.Lhs = 1;
		[EqualInArgName,EqualInArgScope,EqualNInArg] = AST_HandleRC(FileInfo,SharedInfo);
		SharedInfo.Equal.NInArg = EqualNInArg - rc_count -1;
		AST_PushASTStack(treeline);
		for tmpcnt = 1:SharedInfo.Equal.NInArg
	           SharedInfo.Equal.InArg(tmpcnt).Name = EqualInArgName(tmpcnt);
	           SharedInfo.Equal.InArg(tmpcnt).Scope = EqualInArgScope(tmpcnt);
         	end
		elseif cc_count > 0
		SharedInfo.Equal.Lhs = 1;
 		[EqualInArgName,EqualInArgScope,EqualNInArg] = AST_HandleCC(FileInfo,SharedInfo);
                AST_PushASTStack(treeline);
		else
	        SharedInfo.Equal.Lhs = 1; // 1 means that we are inside the Lhs block of the Equal
	 	//if SharedInfo.Equal.NOutArg > 0
                if disp_isthere == 0
	        [EqualInArgName,EqualInArgScope,EqualNInArg] = AST_ReadEqualRhsNames(FileInfo,SharedInfo);
		SharedInfo.Equal.NInArg = EqualNInArg;
		//end
     
	        // lengthNumber = length('Number_');
	        // if (part(EqualInArgScope,1:lengthNumber) == 'Number_')
	        //    SharedInfo.SkipNextEqual = 1
	        // end
	         
	        for tmpcnt = 1:SharedInfo.Equal.NInArg
	           SharedInfo.Equal.InArg(tmpcnt).Name = EqualInArgName(tmpcnt);
	           SharedInfo.Equal.InArg(tmpcnt).Scope = EqualInArgScope(tmpcnt);
         	end
	 	//end
                end
         	AST_PushASTStack(treeline);
		end
      // ----------------
      // --- If/Else. ---
      // ----------------
      //NUT: da verificare la gestione dello stack
      case 'If Statements' then
         [FileInfo,SharedInfo] = AST_HandleIfElse(FileInfo,SharedInfo,'if');
      case 'Else If Expression' then
         AST_PushASTStack(treeline);
         [FileInfo,SharedInfo] = AST_HandleIfElse(FileInfo,SharedInfo,'else');
      case 'Else If Statements' then
         [FileInfo,SharedInfo] = AST_HandleIfElse(FileInfo,SharedInfo,'elseif');
      case 'Else Statements' then
         [FileInfo,SharedInfo] = AST_HandleIfElse(FileInfo,SharedInfo,'else');
      case 'EndIf' then
         for counter=1:SharedInfo.CountNestedIf+1
            SharedInfo = C_IfElseBlocks(FileInfo,SharedInfo,'out');
         end
         SharedInfo.CountNestedIf = 0;
      // --------------
      // --- Dummy. ---
      // --------------
      case 'Comment :' then
         AST_HandleEOL(FileInfo,SharedInfo); //NUT: si potrebbe differenziare comment da EOL
      case '<EOL>' then
         AST_HandleEOL(FileInfo,SharedInfo);

      // -----------------
      // --- Epilogue. ---
      // -----------------
      case 'EndProgram'
         SharedInfo = AST_HandleEndProgram(FileInfo,SharedInfo);
	 disp_isthere = 0;
         //NUT: per essere precisi si puo' pensare di mettere un check
         //NUT: alla fine dell'albero per accertarsi che c'e' end program li' dove ce lo aspettiamo
      // ------------
      // --- For. ---
      // ------------
      case 'For' then
         SharedInfo.For.Level = SharedInfo.For.Level + 1;
         FileInfo = AST_HandleFor(FileInfo,SharedInfo);
      case 'ForExpression:'
         AST_PushASTStack(treeline);
         SharedInfo.ForExpr.OnExec = SharedInfo.ForExpr.OnExec + 1;
      case 'ForStatements:'
         [FileInfo,SharedInfo] = AST_HandleForStatem(FileInfo,SharedInfo);
      case 'EndFor' then
         SharedInfo = AST_HandleEndFor(FileInfo,SharedInfo);
         SharedInfo.For.Level = SharedInfo.For.Level - 1;

      // --------------
      // --- While. ---
      // --------------
      case 'While' then
         AST_PushASTStack(treeline);
         SharedInfo.While.Level = SharedInfo.While.Level + 1;
      case 'WhileExpression:'
         AST_PushASTStack(treeline);
         [FileInfo,SharedInfo] = AST_HandleWhileExpr(FileInfo,SharedInfo);
      case 'WhileStatements:'
         [FileInfo,SharedInfo] = AST_HandleWhileStatem(FileInfo,SharedInfo);
      case 'EndWhile' then
         SharedInfo = AST_HandleEndWhile(FileInfo,SharedInfo);
         SharedInfo.While.Level = SharedInfo.While.Level - 1;


      case 'Endrc' then
	 rc_count = rc_count + 1;
      
      case 'Endcc' then
	 cc_count = cc_count + 1;
	
         //[FileInfo,SharedInfo] = AST_HandleRC(FileInfo,SharedInfo);

      // ----------------
      // --- Default. ---
      // ----------------
      else
         AST_PushASTStack(treeline);
   end
end
// ------------------------------------
// -----List of functions Used--------
// -------------------------------------

SharedInfo.Function_list_index = SharedInfo.Function_list_index - 2;
SharedInfo.Function_list = SharedInfo.Function_list(1:SharedInfo.Function_list_index);
//To remove function repeatedly used----------
x = size(unique(SharedInfo.Function_list));                          
SharedInfo.Function_list_index = x(1);
SharedInfo.Function_list = unique(SharedInfo.Function_list);

SharedInfo.Function_list = SharedInfo.Function_list(1:SharedInfo.Function_list_index);



// --------------------------------------
// --- End main loop to read the AST. ---
// --------------------------------------

mclose(fidAST);
// ---------------------
// --- Save section. ---
// ---------------------
// --- Save Shared Info Structure. ---
save(SharedInfoDatFile, "SharedInfo");
// -------------------------
// --- End save section. ---
// -------------------------
endfunction