-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.h
More file actions
691 lines (636 loc) · 21.4 KB
/
javascript.h
File metadata and controls
691 lines (636 loc) · 21.4 KB
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
#ifndef ADHOC_JAVASCRIPT_H
#define ADHOC_JAVASCRIPT_H
#include <stdlib.h>
#include <stdbool.h>
#include "hashmap.h"
#include "adhoc_types.h"
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wswitch"
// Keep track of the current scope
ASTnode* scope;
// Maintain a list of all functions that need to be declared at the top level
ASTnode** functions;
int countFuncs, sizeFuncs;
bool isExec;
// Headers for master functions
void lang_javascript_initialize(ASTnode*, short, FILE*, hashMap*, char*);
void lang_javascript_generate(bool, ASTnode*, short, FILE*, hashMap*, char*);
// C names for data types
void lang_javascript_printTypeName(ASTnode* n, FILE* o){
switch(n->dataType){
case TYPE_NULL:
fprintf(o, "void");
break;
case TYPE_BOOL:
fprintf(o, "bool");
break;
case TYPE_INT:
fprintf(o, "int");
break;
case TYPE_FLOAT:
fprintf(o, "float");
break;
case TYPE_STRNG:
fprintf(o, "char*");
break;
case TYPE_ARRAY:
fprintf(o, "<<ARRAY>>");
break;
case TYPE_HASH:
fprintf(o, "<<HASH>>");
break;
case TYPE_STRCT:
fprintf(o, "<<STRUCT>>");
break;
case TYPE_ACTN:
fprintf(o, "<<ACTION>>");
break;
}
}
// Indentation function
void lang_javascript_indent(short i, FILE* o){
if(i>=0) fprintf(o, "%.*s", i, "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t");
}
// Generating Null nodes should just throw an error
void lang_javascript_generate_null(bool isInit, ASTnode* n, short indent, FILE* outFile, hashMap* nodes, char* errBuf){
adhoc_errorNode = n->parent;
sprintf(errBuf, "Null nodes should be removed before generating.");
}
// Generating actions differs most between init and gen, and decl and call
void lang_javascript_generate_action(bool isInit, ASTnode* n, short indent, FILE* outFile, hashMap* nodes, char* errBuf){
int i,j;
switch(n->which){
case ACTION_DEFIN:
// For the init run, nothing is printed
if(isInit){
// If this is a top-level action, add it to the list of functions
if(!n->scope){
if(++countFuncs > sizeFuncs){
sizeFuncs *= 2;
functions = realloc(functions, sizeFuncs * sizeof(ASTnode*));
}
functions[countFuncs-1] = n;
}
// Handle the children
for(i=0; i<n->countChildren; ++i){
// Set scope to this node
scope = n;
// Initialize children
lang_javascript_initialize(n->children[i], indent, outFile, nodes, errBuf);
}
break;
// Print the full function body
}else{
// Leave a comment above the function definition
fprintf(outFile, "\n");
if(n->value && strlen(n->value)){
lang_javascript_indent(indent, outFile);
fprintf(outFile, "// %s\n", n->value);
}
// If a statment, print "function" keyword and name
if(n->childType == STATEMENT || n->childType == CHILD_NULL){
lang_javascript_indent(indent, outFile);
if(isExec){
fprintf(outFile, "%s.%s = %s.%s || function(", n->package, n->name, n->package, n->name);
}else{
fprintf(outFile, "function %s(", n->name);
}
// If an expression, just print the keyword
}else if(n->childType == EXPRESSION){
fprintf(outFile, "function(");
}
// Print the action's parameters
for(i=0; i<n->countChildren; ++i){
if(n->children[i]->childType == PARAMETER){
if(i>0) fprintf(outFile, ", ");
if(isInit){
// Set scope to this node
scope = n;
// Initialize arguments
lang_javascript_initialize(n->children[i], -1, outFile, nodes, errBuf);
}else{
// Set scope to this node
scope = n;
// Generate arguments
lang_javascript_generate(false, n->children[i], -1, outFile, nodes, errBuf);
}
}else{
break;
}
}
// Close the function signature and open the body block
fprintf(outFile, "){\n");
// Print declarations for any scope vars
for(j=0; j<n->countScopeVars; ++j){
if(n->scopeVars[j]->childType == PARAMETER) continue;
if(n->scopeVars[j]->which != VARIABLE_ASIGN) continue;
if(n->scopeVars[j]->parent->which == ASSIGNMENT_EQUAL) continue;
lang_javascript_indent(indent+1, outFile);
fprintf(outFile, "var %s;\n", n->scopeVars[j]->name);
}
// Print the child statements
for(; i<n->countChildren; ++i){
// Set scope to this node
scope = n;
// Generate children
lang_javascript_generate(false, n->children[i], indent+1, outFile, nodes, errBuf);
}
// Close the function body
lang_javascript_indent(indent, outFile);
fprintf(outFile, "}");
if(n->countChildren
&& n->children[0]->childType==PARAMETER
&& n->children[0]->countChildren
){
fprintf(outFile, "(");
for(i=0; i<n->countChildren; ++i){
if(n->children[i]->childType!=PARAMETER) break;
if(i) fprintf(outFile, ", ");
lang_javascript_generate(false, n->children[i]->children[0], 0, outFile, nodes, errBuf);
}
}
fprintf(outFile, ");\n");
if(!n->parent) break;
}
case ACTION_CALL:
// Nothing needs to be done during initialization
if(isInit){
for(i=0; i<n->countChildren; ++i){
lang_javascript_initialize(n->children[i], -1, outFile, nodes, errBuf);
}
break;
}
// Indent this function call, and call it
lang_javascript_indent(indent, outFile);
if(isExec && strcmp(n->package, "System")){
fprintf(outFile, "%s.%s(", n->package, n->name);
}else{
fprintf(outFile, "%s(", n->name);
}
// Print the action's arguments
for(i=0; i<n->countChildren; ++i){
if(n->children[i]->childType == ARGUMENT || n->children[i]->childType == PARAMETER){
if(i>0) fprintf(outFile, ", ");
lang_javascript_generate(true, n->children[i], -1, outFile, nodes, errBuf);
}
}
// Close the function call
fprintf(outFile, ")");
// If this is the end of a statement, add a semicolon
if(n->childType == STATEMENT){
fprintf(outFile, ";\n");
}
break;
}
}
// Groups are just a sequential ordering of children, nothing need be done
void lang_javascript_generate_group(bool isInit, ASTnode* n, short indent, FILE* outFile, hashMap* nodes, char* errBuf){
int i;
if(isInit){
// Initialize all children in order
for(i=0; i<n->countChildren; ++i){
lang_javascript_initialize(n->children[i], indent, outFile, nodes, errBuf);
}
}else{
// Generate all children in order
for(i=0; i<n->countChildren; ++i){
lang_javascript_generate(false, n->children[i], indent, outFile, nodes, errBuf);
}
}
}
// Controls vary greatly
void lang_javascript_generate_control(bool isInit, ASTnode* n, short indent, FILE* outFile, hashMap* nodes, char* errBuf){
int i;
switch(n->which){
case CONTROL_IF:
// Nothing to do on initialization
if(isInit){
for(i=0; i<n->countChildren; ++i){
// Initialize children
lang_javascript_initialize(n->children[i], indent, outFile, nodes, errBuf);
}
break;
}
// Print the opening of the 'if' block
lang_javascript_indent(indent, outFile);
fprintf(outFile, "if(");
// Print the condition
for(i=0; i<n->countChildren; ++i){
if(n->children[i]->childType == CONDITION){
lang_javascript_generate(false, n->children[i], -1, outFile, nodes, errBuf);
break;
}
}
// Close the 'if' line
fprintf(outFile, "){\n");
// Print the 'if' statements
for(i=0; i<n->countChildren; ++i){
if(n->children[i]->childType != IF) continue;
lang_javascript_generate(false, n->children[i], indent+1, outFile, nodes, errBuf);
}
// Print the 'else' statements
bool needElse = true;
for(i=0; i<n->countChildren; ++i){
if(n->children[i]->childType != ELSE) continue;
if(needElse){
lang_javascript_indent(indent, outFile);
fprintf(outFile, "}else{\n");
needElse = false;
}
lang_javascript_generate(false, n->children[i], indent+1, outFile, nodes, errBuf);
}
// Close the whole 'if' block
lang_javascript_indent(indent, outFile);
fprintf(outFile, "}\n");
break;
case CONTROL_LOOP:
// Nothing to do during initialization
if(isInit){
for(i=0; i<n->countChildren; ++i){
// Initialize children
lang_javascript_initialize(n->children[i], indent, outFile, nodes, errBuf);
}
break;
}
// Declare scope vars
for(i=0; i<n->countScopeVars; ++i){
lang_javascript_indent(indent, outFile);
fprintf(outFile, "var %s;\n", n->scopeVars[i]->name);
}
// Open for statement
lang_javascript_indent(indent, outFile);
fprintf(outFile, "for(");
// Find the initialization and print it
for(i=0; i<n->countChildren; ++i){
if(n->children[i]->childType == INITIALIZATION){
// Generate initialization
lang_javascript_generate(false, n->children[i], -1, outFile, nodes, errBuf);
break;
}
}
fprintf(outFile, "; ");
// Find the condition and print it
for(i=0; i<n->countChildren; ++i){
if(n->children[i]->childType == CONDITION){
// Generate condition
lang_javascript_generate(false, n->children[i], -1, outFile, nodes, errBuf);
break;
}
}
fprintf(outFile, "; ){\n");
// Print all the body elements
for(i=0; i<n->countChildren; ++i){
if(n->children[i]->childType == INITIALIZATION) continue;
if(n->children[i]->childType == CONDITION) continue;
// Generate children
lang_javascript_generate(false, n->children[i], indent+1, outFile, nodes, errBuf);
}
// Close the for statement
lang_javascript_indent(indent, outFile);
fprintf(outFile, "}\n");
break;
case CONTROL_SWITCH:
// Nothing to do on initialization
if(isInit){
for(i=0; i<n->countChildren; ++i){
// Initialize children
lang_javascript_initialize(n->children[i], indent, outFile, nodes, errBuf);
}
break;
}
sprintf(errBuf, "SWITCH statements are not implemented yet :(");
break;
case CONTROL_CASE:
// Nothing to do on initialization
if(isInit){
for(i=0; i<n->countChildren; ++i){
lang_javascript_initialize(n->children[i], indent, outFile, nodes, errBuf);
}
break;
}
sprintf(errBuf, "CASE statements are not implemented yet :(");
break;
case CONTROL_FORK:
// Nothing to do on initialization
if(isInit){
for(i=0; i<n->countChildren; ++i){
// Initialize children
lang_javascript_initialize(n->children[i], indent, outFile, nodes, errBuf);
}
break;
}
sprintf(errBuf, "FORK statements are not implemented yet :(");
break;
case CONTROL_CNTNU:
case CONTROL_BREAK:
// Nothing to do on initialization
if(isInit){
for(i=0; i<n->countChildren; ++i){
// Initialize children
lang_javascript_initialize(n->children[i], indent, outFile, nodes, errBuf);
}
break;
}
// Just print the keyword and a semicolon
lang_javascript_indent(indent, outFile);
fprintf(outFile, "%s;\n", adhoc_nodeWhich_names[n->which]);
break;
case CONTROL_RETRN:
// During initialization, set the parent function's datatype
if(isInit){
for(i=0; i<n->countChildren; ++i){
// Initialize children
lang_javascript_initialize(n->children[i], indent, outFile, nodes, errBuf);
// Get datatype from child and pass it to the parent function
n->dataType = n->children[i]->dataType;
if(scope){
if(scope->dataType==TYPE_VOID && n->dataType!=TYPE_VOID){
scope->dataType = n->dataType;
}
}
}
break;
}
// Print the keyword
lang_javascript_indent(indent, outFile);
fprintf(outFile, "return ");
// Generate the children
for(i=0; i<n->countChildren; ++i){
lang_javascript_generate(false, n->children[i], 0, outFile, nodes, errBuf);
}
fprintf(outFile, ";\n");
break;
}
}
// Generation rules for operators
void lang_javascript_generate_operator(bool isInit, ASTnode* n, short indent, FILE* outFile, hashMap* nodes, char* errBuf){
int i;
if(isInit){
for(i=0; i<n->countChildren; ++i){
lang_javascript_initialize(n->children[i], indent, outFile, nodes, errBuf);
switch(n->which){
case OPERATOR_NOT:
n->dataType = TYPE_BOOL;
break;
case OPERATOR_TRNIF:
n->dataType = n->children[1]->dataType;
break;
default:
n->dataType = n->children[0]->dataType;
}
}
}else{
if(n->childType == STATEMENT) lang_javascript_indent(indent, outFile);
switch(n->which){
case OPERATOR_NOT:
fprintf(outFile, "%s", adhoc_nodeWhich_names[n->which]);
lang_javascript_generate(false, n->children[0], indent+1, outFile, nodes, errBuf);
break;
case OPERATOR_ARIND:
lang_javascript_generate(false, n->children[0], indent+1, outFile, nodes, errBuf);
fprintf(outFile, "[");
lang_javascript_generate(false, n->children[1], indent+1, outFile, nodes, errBuf);
fprintf(outFile, "]");
break;
case OPERATOR_PLUS:
case OPERATOR_MINUS:
case OPERATOR_TIMES:
case OPERATOR_DIVBY:
case OPERATOR_MOD:
case OPERATOR_EXP:
case OPERATOR_OR:
case OPERATOR_AND:
case OPERATOR_EQUIV:
case OPERATOR_GRTTN:
case OPERATOR_LESTN:
case OPERATOR_GRTEQ:
case OPERATOR_LESEQ:
case OPERATOR_NOTEQ:
lang_javascript_generate(false, n->children[0], indent+1, outFile, nodes, errBuf);
fprintf(outFile, " %s ", adhoc_nodeWhich_names[n->which]);
lang_javascript_generate(false, n->children[1], indent+1, outFile, nodes, errBuf);
break;
case OPERATOR_TRNIF:
lang_javascript_generate(false, n->children[0], indent+1, outFile, nodes, errBuf);
fprintf(outFile, " ? ");
lang_javascript_generate(false, n->children[1], indent+1, outFile, nodes, errBuf);
fprintf(outFile, " : ");
lang_javascript_generate(false, n->children[2], indent+1, outFile, nodes, errBuf);
break;
}
if(n->childType == STATEMENT) fprintf(outFile, ";\n");
}
}
// Generation rules for assignments
void lang_javascript_generate_assignment(bool isInit, ASTnode* n, short indent, FILE* outFile, hashMap* nodes, char* errBuf){
int i;
if(isInit){
// Initialize the children and pass their types to the assignment and storage
for(i=0; i<n->countChildren; ++i){
lang_javascript_initialize(n->children[i], indent, outFile, nodes, errBuf);
}
switch(n->which){
// Boolean types
case ASSIGNMENT_OR:
case ASSIGNMENT_AND:
case ASSIGNMENT_NEGPR:
case ASSIGNMENT_NEGPS:
n->dataType = TYPE_BOOL;
n->children[0]->dataType = TYPE_BOOL;
break;
// Unaries
case ASSIGNMENT_INCPR:
case ASSIGNMENT_INCPS:
case ASSIGNMENT_DECPR:
case ASSIGNMENT_DECPS:
n->dataType = n->children[0]->dataType;
break;
// Other assignments take their types from second argument
default:
n->dataType = n->children[1]->dataType;
n->children[0]->dataType = n->dataType;
}
}else{
if(n->childType == STATEMENT
|| n->childType == IF
|| n->childType == ELSE)
lang_javascript_indent(indent, outFile);
switch(n->which){
case ASSIGNMENT_INCPR:
case ASSIGNMENT_DECPR:
fprintf(outFile, "%s", adhoc_nodeWhich_names[n->which]);
lang_javascript_generate(false, n->children[0], indent+1, outFile, nodes, errBuf);
break;
case ASSIGNMENT_INCPS:
case ASSIGNMENT_DECPS:
lang_javascript_generate(false, n->children[0], indent+1, outFile, nodes, errBuf);
fprintf(outFile, "%s", adhoc_nodeWhich_names[n->which]);
break;
case ASSIGNMENT_EQUAL:
if(n->children[0]->which==VARIABLE_ASIGN && n->children[0]->defined){
fprintf(outFile, "var ");
}
case ASSIGNMENT_PLUS:
case ASSIGNMENT_MINUS:
case ASSIGNMENT_TIMES:
case ASSIGNMENT_DIVBY:
case ASSIGNMENT_MOD:
case ASSIGNMENT_EXP:
case ASSIGNMENT_OR:
case ASSIGNMENT_AND:
lang_javascript_generate(false, n->children[0], indent+1, outFile, nodes, errBuf);
fprintf(outFile, " %s ", adhoc_nodeWhich_names[n->which]);
lang_javascript_generate(false, n->children[1], indent+1, outFile, nodes, errBuf);
break;
case ASSIGNMENT_NEGPR:
case ASSIGNMENT_NEGPS:
sprintf(
errBuf
,"Node %d: %s"
,n->children[0]->id
,"Negation operator does not exist in C."
);
break;
}
if(n->childType == STATEMENT
|| n->childType == IF
|| n->childType == ELSE)
fprintf(outFile, ";\n");
}
}
// Generation rules for variables
void lang_javascript_generate_variable(bool isInit, bool call, ASTnode* n, short indent, FILE* outFile, hashMap* nodes, char* errBuf){
int i;
if(isInit){
for(i=0; i<n->countChildren; ++i){
lang_javascript_initialize(n->children[i], indent, outFile, nodes, errBuf);
if(n->which == VARIABLE_ASIGN && n->dataType == TYPE_VOID){
n->dataType = n->children[i]->dataType;
}
}
if(n->which == VARIABLE_EVAL){
ASTnode* def;
def = adhoc_findScope(n->name, scope);
if(def) n->dataType = def->dataType;
}
}else{
if(n->childType == INITIALIZATION){
fprintf(outFile, "%s = ", n->name);
}else if(!(n->childType==PARAMETER && call)){
fprintf(outFile, "%s", n->name);
}
if(n->childType!=PARAMETER || call){
for(i=0; i<n->countChildren; ++i){
lang_javascript_generate(false, n->children[i], indent+1, outFile, nodes, errBuf);
}
}
}
}
// Generation rules for literals
void lang_javascript_generate_literal(bool isInit, ASTnode* n, short indent, FILE* outFile, hashMap* nodes, char* errBuf){
int i;
if(isInit){
switch(n->which){
case LITERAL_BOOL: n->dataType = TYPE_BOOL; break;
case LITERAL_INT: n->dataType = TYPE_INT; break;
case LITERAL_FLOAT: n->dataType = TYPE_FLOAT; break;
case LITERAL_STRNG: n->dataType = TYPE_STRNG; break;
case LITERAL_ARRAY: n->dataType = TYPE_ARRAY; break;
case LITERAL_HASH: n->dataType = TYPE_HASH; break;
case LITERAL_STRCT: n->dataType = TYPE_STRCT; break;
}
}else{
switch(n->which){
case LITERAL_BOOL:
if(!strcmp(n->value, "true") || !strcmp(n->value, "false")){
fprintf(outFile, "%s", n->value);
}else{
fprintf(outFile, "%s", (atoi(n->value) ? "true" : "false"));
}
break;
case LITERAL_INT:
fprintf(outFile, "%d", atoi(n->value));
break;
case LITERAL_FLOAT:
fprintf(outFile, "%s", n->value);
break;
case LITERAL_STRNG:
fprintf(outFile, "\"%s\"", n->value);
break;
case LITERAL_ARRAY:
fprintf(outFile, "[");
for(i=0; i<n->countChildren; ++i){
if(i) fprintf(outFile, ", ");
lang_javascript_generate(false, n->children[i]->children[0], -1, outFile, nodes, errBuf);
}
fprintf(outFile, "]");
break;
case LITERAL_HASH:
sprintf(errBuf, "%s", "Printing hashes is not implemented :(");
break;
case LITERAL_STRCT:
sprintf(errBuf, "%s", "Printing structs is not implemented :(");
break;
}
}
}
// Function to initialize an AST node
void lang_javascript_initialize(ASTnode* n, short indent, FILE* outFile, hashMap* nodes, char* errBuf){
// Handle different node types
switch(n->nodeType){
case TYPE_NULL: lang_javascript_generate_null(true, n, indent, outFile, nodes, errBuf); break;
case ACTION: lang_javascript_generate_action(true, n, indent, outFile, nodes, errBuf); break;
case GROUP: lang_javascript_generate_group(true, n, indent, outFile, nodes, errBuf); break;
case CONTROL: lang_javascript_generate_control(true, n, indent, outFile, nodes, errBuf); break;
case OPERATOR: lang_javascript_generate_operator(true, n, indent, outFile, nodes, errBuf); break;
case ASSIGNMENT: lang_javascript_generate_assignment(true, n, indent, outFile, nodes, errBuf); break;
case VARIABLE: lang_javascript_generate_variable(true, false, n, indent, outFile, nodes, errBuf); break;
case LITERAL: lang_javascript_generate_literal(true, n, indent, outFile, nodes, errBuf); break;
}
}
// Function to generate code from an AST node
void lang_javascript_generate(bool call, ASTnode* n, short indent, FILE* outFile, hashMap* nodes, char* errBuf){
switch(n->nodeType){
case TYPE_NULL: lang_javascript_generate_null(false, n, indent, outFile, nodes, errBuf); break;
case ACTION: lang_javascript_generate_action(false, n, indent, outFile, nodes, errBuf); break;
case GROUP: lang_javascript_generate_group(false, n, indent, outFile, nodes, errBuf); break;
case CONTROL: lang_javascript_generate_control(false, n, indent, outFile, nodes, errBuf); break;
case OPERATOR: lang_javascript_generate_operator(false, n, indent, outFile, nodes, errBuf); break;
case ASSIGNMENT: lang_javascript_generate_assignment(false, n, indent, outFile, nodes, errBuf); break;
case VARIABLE: lang_javascript_generate_variable(false, call, n, indent, outFile, nodes, errBuf); break;
case LITERAL: lang_javascript_generate_literal(false, n, indent, outFile, nodes, errBuf); break;
}
}
// Hook function for generalized initialization
void lang_javascript_init(ASTnode* n, FILE* outFile, hashMap* nodes, bool exec, char* errBuf){
isExec = exec;
countFuncs = 0;
sizeFuncs = 2;
functions = realloc(functions, sizeFuncs * sizeof(ASTnode*));
if(exec){
fprintf(outFile, "//Pull in the ADHOC JS library with support functions\n");
fprintf(outFile, "(function(){try{\n");
fprintf(outFile, "var req = new XMLHttpRequest(); req.open('GET', './libadhoc.js', false); req.send();\n");
fprintf(outFile, "if(req.status == 200) return eval(req.responseText);\n");
fprintf(outFile, "}catch(e){};\n");
fprintf(outFile, "alert('Could not load ADHOC library file.\\n\\nYou may download it here: https://raw.githubusercontent.com/pieman72/adhoc/HEAD/libadhoc.js');\n");
fprintf(outFile, "})();\n");
}
lang_javascript_initialize(n, 0, outFile, nodes, errBuf);
}
// Hook function for generalized code generation
void lang_javascript_gen(ASTnode* n, FILE* outFile, hashMap* nodes, bool exec, char* errBuf){
if(exec && countFuncs){
fprintf(outFile, "\n// Namespacing wrapper\n");
fprintf(outFile, "var %s = %s || {}\n", functions[0]->package, functions[0]->package);
}
lang_javascript_generate(false, functions[0], 0, outFile, nodes, errBuf);
if(exec && countFuncs){
fprintf(outFile, "\n// Execute primary function from namespace\n");
fprintf(outFile, "%s.%s();\n", functions[0]->package, functions[0]->name);
}
free(functions);
}
#pragma clang diagnostic pop
#endif