-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegenlib.js
403 lines (336 loc) · 13.4 KB
/
codegenlib.js
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
function eName(name,id) // make embed name
{
if (!id) return name;
return "E"+name+id;
}
function getIdentifierName(fwprop)
{
if (fwprop.type=='final') return editorSwitch("Final State","Final Node");
if (fwprop.type=='init') return editorSwitch("Initial State","Initial Node");
return fwprop.identifier;
}
// add note to notes
function FWaddNote(note,label,res)
{
note=$.trim(note);
if (note=='') return;
res.notes.push(" *");
res.notes.push(" * <b>"+label+"</b>");
res.notes.push(" * "+note.replace(/\n/g,"\n * "));
}
// create function or code for actions and guards
// also construct useful comment and add stub if own code is provided
function FWaddFunction(type,func,code,desc,label,isBool,res)
{
var autoname=false;
if (!isEmpty(desc))
{
if (type=='define function' && isEmpty(func)) return ""; // throws an error
if (type=='call function' && isEmpty(func)) return ""; // throws an error
if (type=='run code' && isEmpty(code)) return ""; // throws an error
} else desc='';
if (isEmpty(func))
{
if (type!='run code') return "NULL";
else if (isEmpty(code)) return "NULL";
func="code"+Math.floor(Math.random()*100000);
autoname=true;
}
var comment=[];
comment.push("/**");
comment.push(" * "+label);
if (desc)
{
if (desc.split("\n").length>1 || desc.length>90)
{
comment.push(" * <pre>");
comment.push(" * "+desc.replace(/\n/g,"\n * "));
comment.push(" * </pre>");
}
else
comment.push(" * "+desc.replace(/\n/g,"\n * "));
}
if (editorSwitch("state machine","procedure")=="state machine")
comment.push(" * @param smDesc the state machine descriptor");
else
comment.push(" * @param prDesc the procedure descriptor");
if (isBool) comment.push(" * @return 1 if the guard is fulfilled, otherwise 0.");
comment.push(" */");
if (type=='define function') code=''; // see mail from Alessandro on 1.11.2016
if (isEmpty(code)) code='';
code=code.replace(/\n/g,"\n\t").replace(/\s+$/,"");
if (type=='run code' || type=='define function')
{
var stub=(autoname?"static ":"")+(isBool?"Fw"+editorSwitch("Sm","Pr")+"Bool_t":"void")+" "+func+"(Fw"+editorSwitch("Sm","Pr")+"Desc_t "+editorSwitch("sm","pr")+"Desc)";
var target = code ? res.functions : res.dummycode;
if (!autoname) // named code
{
for (var i=0; i<comment.length; i++) res.stubs.push(comment[i]);
res.stubs.push(stub+";");
res.stubs.push("");
target.push("/* "+label+" */");
}
else // unnamed code with auto-generated name
{
for (var i=0; i<comment.length; i++) res.functions.push(comment[i]);
}
target.push(stub);
target.push("{\t(void)"+editorSwitch("sm","pr")+"Desc;");
if (type!='run code') target.push("\tprintf(\" "+label+"\\n\");");
if (isBool || code) target.push((isBool?"\t"+(code?code:"return rand()>RAND_MAX/2 ? 1 : 0")+";":(code?"\t"+code+";":"")).replace(/;+$/,";"));
target.push("}");
target.push("");
}
return "&"+func;
}
// find out nested level
//
function getParentLevel(state)
{
var i=0;
while (state.parentState) { state=state.parentState; i++; }
return i;
}
// test all connections if they are between objects on the same parent states
//
function checkConParentMatch()
{
var i;
for (i=0; i<g.connections.length; i++)
if (g.connections[i].stateFrom.parentState!=g.connections[i].stateTo.parentState && !stateIsNote(g.connections[i].stateFrom))
{
msg("Connection between elements of two different (embedded) state machines is not allowed","error");
return false;
}
return true;
}
// build camel case name, replacing all nonalphanum characters
function camelCase(text)
{
text=removeDiacritic(text);
return text.replace(/(^|\s)\w/g, function(match) {
return match.toUpperCase();
}).replace(/[^a-zA-Z0-9]/g,'');
}
function now()
{
var today = new Date()
var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
return months[today.getMonth()]+" "+today.getDate()+" "+today.getFullYear()+" "+today.getHours()+":"+today.getMinutes()+":"+today.getSeconds();
}
function isStatic()
{
if (g.fwprop.memalloc=='static') return true;
return false;
}
function fileTopCommentPush(filename)
{
var res=[];
res.push("/**");
res.push(" * @file "+filename);
res.push(" *");
res.push(" * @author FW Profile code generator"+(g.currentVersion?" version "+g.currentVersion:""));
res.push(" * @date Created on: "+now());
if (!isEmpty(g.fwprop.smNotes))
{
res.push(" *");
res.push(" * "+g.fwprop.smNotes.replace(/\n/g,"\n * "));
}
res.push(" */")
res.push("");
return res.join("\n")+"\n";
}
function codeText(res,t)
{
return res[t].join("\n").replace(/\n\n+/g,"\n\n");
}
function dlformAppendCode(i,code,name,folder,jsonString,pngData)
{
if ($('.dlflags[data-flag="'+folder+'"]').length>0) folder+=' [uniq@'+(i+1)+']';
dlformAppend(++i,folder);
$('#dlname'+i).val(name+".h");
$('#dldata'+i).val(codeText(code,"headers"));
$('#dlfolder'+i).val(folder);
dlformAppend(++i);
$('#dlname'+i).val(name+".c");
$('#dldata'+i).val(fileTopCommentPush(name+".c")+codeText(code,"core"));
$('#dlfolder'+i).val(folder);
dlformAppend(++i);
$('#dlname'+i).val(name+"Main.c");
$('#dldata'+i).val(fileTopCommentPush(name+"Main.c")+codeText(code,"main"));
$('#dlfolder'+i).val(folder);
dlformAppend(++i);
$('#dlname'+i).val(name+".json");
$('#dldata'+i).val(jsonString);
$('#dlfolder'+i).val(folder);
dlformAppend(++i);
$('#dlname'+i).val(name+".png");
$('#dldata'+i).val(pngData);
$('#dlfolder'+i).val(folder);
$('#dldecode'+i).val('true');
return i;
}
function downloadCode(memalloc)
{
if (!userIsSigned())
{
signinShow();
return;
}
var i=0;
var j=0;
var code,fwdata,id,file;
var all={};
for (var k=0; k<g.knownFiles.length; k++)
{
fwdata=g.knownFiles[k].fwdata;
id=g.knownFiles[k].id;
all[id]={'data':fwdata, 'embeds':[]};
for (var n=0; n<fwdata.states.length; n++)
if (fwdata.states[n].fwprop.embedSmId>0)
arrayAddNonempty(all[id].embeds,parseInt(fwdata.states[n].fwprop.embedSmId));
}
dlformReset();
if (memalloc) // only consider selected files if memalloc is set - when it is not then user wants only current diagram
for (var key in g.selectedFiles)
{
code=FWcode(all,all[key].data,key,memalloc);
if (!code) return; // error on code generator
file=findFileById(key);
j=dlformAppendCode(j,code,camelCase(file.name),file.name,file.fwprop,getImagePNGdata($('#thumbimg'+key).get(0)));
dlformZIPname(file.name); // will be overwritten later if i>1
i++;
}
if (i==1)
{
dlformSubmit();
saveIDmap();
}
else if (i>1)
{
dlformZIPname("selected_"+i+"_code_folders");
dlformSubmit();
saveIDmap();
}
else
{
var image = new Image;
image.onload = function()
{
code=FWcode();
if (!code) return; // error on code generator
dlformAppendCode(1,code,camelCase(g.fwprop.smName),g.fwprop.smName,getExportString(),getImagePNGdata(image))
dlformZIPname(g.fwprop.smName);
dlformSubmit();
saveIDmap();
}
image.src = getSVG("image/svg+xml");
}
}
// Include all child state machines (from other documents) and globvar data
// in global structures g.states, g.connections and g.fwprop
function extendSMembed(all,currentJSON,id,memalloc)
{
var i,j;
var shift=1;
var max=1;
var queue=[{'data':currentJSON,'parent':false}];
var queued=[id];
// reset g.*
g.states=[];
g.connections=[];
g.fwprop.globalvar=[];
g.fwprop.smIncludes="";
g.fwprop.memalloc=memalloc;
g.fwprop.editorType=currentJSON.globals.fwprop.editorType;
g.fwprop.smName=currentJSON.globals.fwprop.smName;
while (queue.length>0)
{
var q=queue.shift();
var current=q.data;
var parent=q.parent;
// first we need to modify IDs of states to ensure unique values across all embedded State Machines
// we will do this by shifting the values by 'shift' on all states, and increasing the shift for the next run
for (i=0; i<current.states.length; i++)
{
current.states[i].id+=shift;
current.states[i].attr=function(e){ return this.attrs[e]; }
max=Math.max(current.states[i].id,max);
// if our state embeds other diagram file, add it to queue
var eID=parseInt(current.states[i].fwprop.embedSmId);
if (eID>0 && eID in all && jQuery.inArray(eID,queued) === -1)
{
queue.push({'data':all[eID].data,'parent':current.states[i]});
queued.push(eID);
}
}
for (i=0; i<current.connections.length; i++)
{
current.connections[i].stateFromID+=shift;
current.connections[i].stateToID+=shift;
for (j=0; j<current.states.length; j++)
{
if (current.connections[i].stateFromID==current.states[j].id) current.connections[i].stateFrom=current.states[j];
if (current.connections[i].stateToID==current.states[j].id) current.connections[i].stateTo=current.states[j];
}
}
shift=max+1;
updateParents(current.states,parent);
for (i=0; i<current.states.length; i++) g.states.push(current.states[i]);
for (i=0; i<current.connections.length; i++) g.connections.push(current.connections[i]);
for (i=0; i<current.globals.fwprop.globalvar.length; i++) g.fwprop.globalvar.push(current.globals.fwprop.globalvar[i]);
if (!isEmpty(current.globals.fwprop.smIncludes)) g.fwprop.smIncludes+="\n"+current.globals.fwprop.smIncludes;
}
// remove duplicites from includes and global variables
g.fwprop.smIncludes=array_unique_unsorted(g.fwprop.smIncludes.split("\n")).join("\n");
g.fwprop.globalvar=array_unique_unsorted(g.fwprop.globalvar);
}
function FWcode(all,currentJSON,id,memalloc)
{
var res;
// Important: g.* is unmodified yet at this moment and contains correct data for current state machine
if (!all) all=allSmFilesData();
if (!currentJSON) currentJSON=getExportJSON();
if (!id) id=parseInt(g.internalID);
if (!memalloc) memalloc=g.fwprop.memalloc;
// This is a bit tricky. The entire FW Profile editor was initially written as
// single-document editor, holding all states and connections in global variables,
// which are just referrenced and never passed to functions as arguments.
// Later, when the editor has been enhanced to support multiple tabs
// and embedded state machines from other saved documents, it was necessary to
// rewrite code generator (with all supporting functions it uses). This could lead
// to lots of issues, so I decided to do it the lazy and safe way.
// So lets backup the global variables which hold real states and connections on the current paper,
// and then hack on the globals to pretend for a while that all embed states are drawn on the same paper.
// Thus code generator can remain almost unchanged, still operating on the global states & connections.
// When done, revert all changes back (restore states&connections from backup). Shhhh, nobody will notice.
// Backup first
var backup={};
backup.states=g.states;
backup.connections=g.connections;
backup.fwprop=clone(g.fwprop);
// Now lets build our superglobal state machine or procedure
// including all child state machines (from other documents)
// This touches only g.states, g.connections and g.fwprop
// and obviously adds no childs for procedures since there are none
extendSMembed(all,currentJSON,id,memalloc);
// generate the actual code
if (editorIsPR()) res=FWcodePR();
if (editorIsSM()) res=FWcodeSM();
// Revert back to original values
g.states=backup.states;
g.connections=backup.connections;
g.fwprop=backup.fwprop;
// if generated result is empty, do nothing
if (!res) return res;
// output the resulting code to the user
$("#codegenHname").text(res.name+".h:");
$("#codegenHcode").text(codeText(res,"headers"));
$("#codegenCname").text(res.name+".c:");
$("#codegenCcode").text(codeText(res,"core"));
$("#codegenMname").text(res.name+"Main.c:");
$("#codegenMcode").text(codeText(res,"main"));
// highlight C code for easier reading
sh_highlightDocument();
return res;
}