-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQimppTranslator.java
More file actions
860 lines (672 loc) · 28.6 KB
/
QimppTranslator.java
File metadata and controls
860 lines (672 loc) · 28.6 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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/*
* xtc - The eXTensible Compiler
* Copyright (C) 2012 Robert Grimm
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
package qimpp;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import xtc.lang.JavaFiveParser;
import xtc.parser.ParseException;
import xtc.parser.Result;
import xtc.tree.GNode;
import xtc.tree.Node;
import xtc.tree.Visitor;
import xtc.tree.Location;
import xtc.tree.Printer;
import xtc.util.Tool;
import qimpp.SymbolTable;
/**
* A translator from (a subset of) Java to (a subset of) C++.
*
* Uses visitor and the return Object of dispatch()
*
*
*
* @author QIMPP
* @version 0.1
*/
public class QimppTranslator extends Tool {
GNode currentClass, currentMethod, currentConstructor, parentClassNode;
String currentClassName;
String currentPackageName;
String parentName;
CPPAST cppast;
InheritanceTreeManager treeManager;
GNode root;
//bool inReturnStatement;
HashMap<String, String> currentNameMap;
HashMap<String, Boolean> enqueued;
LinkedList<Node> readQueue;
boolean inBlock;
boolean processImmediately;
int blockDepth = 0;
/** Create a new translator. */
public QimppTranslator() {
cppast = new CPPAST();
}
public String getName() {
return "Java to C++ Translator";
}
public String getCopy() {
return "(C) 2012 qimpp";
}
public void init() {
super.init();
}
public void prepare() {
super.prepare();
// Perform consistency checks on command line arguments.
}
public File locate(String name) throws IOException {
File file = super.locate(name);
if (Integer.MAX_VALUE < file.length()) {
throw new IllegalArgumentException(file + ": file too large");
}
return file;
}
public Node parse(Reader in, File file) throws IOException, ParseException {
JavaFiveParser parser =
new JavaFiveParser(in, file.toString(), (int)file.length());
Result result = parser.pCompilationUnit(0);
return (Node)parser.value(result);
}
public void run(String[] args){
treeManager = new InheritanceTreeManager(cppast.generateObjectClassDeclaration());
readQueue = new LinkedList<Node>();
enqueued = new HashMap<String, Boolean>();
String[] stringQualified = {"java", "lang", "String"};
String[] classQualified = {"java", "lang", "Class"};
String[] arrayQualified = {"__rt", "Array"};
treeManager.insertClass(new ArrayList<String>(Arrays.asList(stringQualified)), null, cppast.generateStringClassDeclaration());
treeManager.insertClass(new ArrayList<String>(Arrays.asList(classQualified)), null, cppast.generateClassClassDeclaration());
treeManager.insertClass(new ArrayList<String>(Arrays.asList(arrayQualified)), null, cppast.generateArrayClassDeclaration());
// This gets the class name from the command line of the root class. Fix this later, as it only supports one argument
currentClassName = args[args.length - 1];
super.run(args);
//cppast.printAST();
}
public void processEnqueue(String name) throws IOException, ParseException {
// Locate the file.
File file = locate(name);
// Open the file.
Reader in = runtime.getReader(file);
// Parse the file.
Node root;
try {
root = parse(in, file);
} finally {
// Close the file.
try {
in.close();
} catch (IOException x) {
// Ignore.
}
}
if (!enqueued.containsKey(name)){
// Process the AST.
readQueue.add(root);
enqueued.put(name, new Boolean(true));
}
}
int processDepth = -1;
public void process(Node node) {
// Create a hashmap to hold maps of ambiguous names to unambiguous names
processDepth++;
if(currentNameMap == null){
currentNameMap = new HashMap<String, String>() {{
put("String", "java.lang.String");
put("Object", "java.lang.Object");
put("Class", "java.lang.Class");
put("Exception", "java.lang.Exception");
}};
}
currentPackageName = "";
//inReturnStatement = false;
// First, get contextual information with an initial visit of types and the package declaration
Visitor initialVisitor = new Visitor () {
public void visitClassDeclaration(GNode n) {
//Add the current class to the cppast, and set it as the current class global variable.
String qualifiedClassName = currentPackageName + "." + n.getString(1);
currentClass = cppast.addClass(qualifiedClassName);
currentClassName = qualifiedClassName;
parentClassNode = currentClass;
currentNameMap.put(n.getString(1), qualifiedClassName);
//add the current class to the inheritance tree, but parent it to Object for now
String[] qualifiedArray = qualifiedClassName.split("\\.");
treeManager.insertClass(new ArrayList<String>(Arrays.asList(qualifiedArray)), null, currentClass);
staticInitializerBlock = GNode.create("Block");
staticInitializerStatements = GNode.create("Block");
visit(n);
addStaticInitializerMethod();
setStaticInitializerMethodInstructions();
if (currentConstructor == null){
currentConstructor = cppast.addConstructor(currentClass);
cppast.setConstructorInstructions(GNode.create("Block"), currentConstructor);
}
}
/** Set the current package name context */
public void visitPackageDeclaration(GNode n){
GNode qualifiedIdentifier = n.getGeneric(1);
StringBuilder packageNameBuilder = new StringBuilder();
for ( int i = 0; i < qualifiedIdentifier.size(); i++ ){
packageNameBuilder.append(qualifiedIdentifier.getString(i));
if ( i < qualifiedIdentifier.size() - 1 ) {
packageNameBuilder.append(".");
}
}
currentPackageName = packageNameBuilder.toString();
}
//TODO: * imports
/** Handle explicit imports */
public void visitImportDeclaration(GNode n){
// Assuming it's not a * import
GNode qualifiedIdentifier = n.getGeneric(1);
StringBuilder qualifiedNameBuilder = new StringBuilder();
String unqualifiedName = "";
for ( int i = 0; i < qualifiedIdentifier.size(); i++ ) {
qualifiedNameBuilder.append(qualifiedIdentifier.getString(i));
if ( i < qualifiedIdentifier.size() - 1 ) {
qualifiedNameBuilder.append(".");
}
else {
unqualifiedName = qualifiedIdentifier.getString(i);
}
}
// Add the disambiguation to the hashmap
currentNameMap.put(unqualifiedName, qualifiedNameBuilder.toString());
}
/** Adds the field to the CPPAST */
public void visitFieldDeclaration(GNode n) {
//Get the string by dispatching the Type GNode
dispatch(n.getGeneric(0));
dispatch(n.getGeneric(2));
GNode type = (GNode)dispatch(n.getGeneric(1));
//Create a new GNode to hold all of the declarators;
GNode declarators = n.getGeneric(2);
//Loop through all declarators in this field declaration, dispatch them, and add each returned string plus type as its own field to the currentClass
//There may be multiple e.g. Java: double x,y,z; => C++: double x; double y; double z;
for(int i = 0; i < declarators.size(); i++){
String name = (String)dispatch(declarators.getGeneric(i));
GNode statement = declarators.getGeneric(i).getGeneric(2);
// Add the initialization to the static initializer if there is an initialization
if (blockDepth == 0) {
if (statement != null){
addStaticInitializerStatement(name, statement);
}
GNode currentField = cppast.addField(currentClassName.replace('.', '_') + "_" + name, name, type, currentClass, false);
GNode modifiers = n.getGeneric(0);
for (Object o : modifiers){
currentField.setProperty(((GNode)o).getString(0), new Boolean(true));
}
}
}
}
/** Visit block. */
public GNode visitBlock(GNode n) {
blockDepth++;
visit(n);
blockDepth--;
return n;
}
GNode staticInitializerBlock;
GNode staticInitializerStatements;
GNode staticInitializerMethod;
public void visitBlockDeclaration(GNode n){
visit(n);
String modifier = n.getString(0);
if (modifier != null && modifier.equals("static"))
addStaticInitializerBlock(n.getGeneric(1));
}
public void addStaticInitializerMethod(){
String methodName = "__static_init";
GNode returnType = GNode.create("ReturnType", GNode.create("PrimitiveType", "void"), null);
GNode parameters = GNode.create("FormalParameters");
staticInitializerMethod = cppast.addMethod(methodName, returnType, currentClass, parameters);
staticInitializerMethod.setProperty("static", new Boolean(true));
}
public void addStaticInitializerStatement(String identifier, GNode statement){
GNode assgnStatement = GNode.create("ExpressionStatement", GNode.create("Expression", GNode.create("PrimaryIdentifier", identifier),
"=", statement));
staticInitializerStatements.add(assgnStatement);
}
public void addStaticInitializerBlock(GNode block){
for (Object o : block){
staticInitializerBlock.add(o);
}
}
public void setStaticInitializerMethodInstructions(){
GNode block = GNode.create("Block");
for (Object o : staticInitializerStatements){
block.add(o);
}
for (Object o : staticInitializerBlock){
block.add(o);
}
cppast.setMethodInstructions(block, staticInitializerMethod);
}
public void visitMethodDeclaration(GNode n) {
//TODO: math names and remove
try{
String methodName = n.getString(3);
/*
if (methodName.equals("m8")){
System.exit();
}*/
if (methodName.equals("main")) {
n.set(2, GNode.create("Type",
GNode.create("PrimitiveType", "int"),
null));
}
GNode returnType = (GNode)dispatch(n.getGeneric(2));
GNode parameters = (GNode)dispatch(n.getGeneric(4));
currentMethod = cppast.addMethod(methodName, returnType, currentClass, parameters);
//Add the method block gotten by dispatching the block node
dispatch(n.getGeneric(7));
GNode block = n.getGeneric(7);
cppast.setMethodInstructions(block, currentMethod);
// Add any modifiers as a property of the method node
GNode modifiers = n.getGeneric(0);
for (Object o : modifiers){
currentMethod.setProperty(((GNode)o).getString(0), new Boolean(true));
}
} catch(Exception e) { e.printStackTrace(); }
}
public GNode visitFormalParameter(GNode n){
//Create a parameter by getting the name and dispatching the type
GNode param = GNode.create("FormalParameter");
param.add(n.getString(3));
param.addNode((GNode)dispatch(n.getGeneric(1)));
return param;
}
public GNode visitFormalParameters(GNode n){
visit(n);
//Loop through all the params dispatching them and adding the result to a parameters GNode
GNode parameters = GNode.create("FormalParameters");
for(Object o : n){
parameters.add((GNode)dispatch((GNode)o));
}
return parameters;
}
/**
* NewClassExpression hold a QualifiedIdentifier not wrapped by a type.
* We need to disambiguate it if possible.
*/
public void visitNewClassExpression(GNode n) {
// Make sure this type has been translated
GNode type = GNode.create("Type", n.getGeneric(2), null);
type = visitType(type);
n.set(2, type.getGeneric(0));
}
/**
* Visit all types. If it is unknown, process the file for that type.
* If it is known, fully qualify it
*/
public GNode visitType(GNode n) {
//Determine the type translated into C++ using
//Type.primitiveType(String) and Type.qualifiedIdentifier(String)
visit(n);
GNode identifier = n.getGeneric(0);
String typename = Disambiguator.getDotDelimitedName(identifier);
if(identifier.hasName("PrimitiveType")){
return n;
}
if ( currentNameMap.get(typename) != null ) {
// Create a QualifiedIdentifier node for the typename
// using the now obviously ill-named Disambiguator
//TODO: Give Disambiguator a name that reflects its purpose,
//which is just to construct a type node from a name.
// Maybe stick that function in CPPAST.
String qualifiedName = currentNameMap.get(typename);
GNode qualifiedIdentifierNode =
Disambiguator.disambiguate(qualifiedName);
n.set(0, qualifiedIdentifierNode);
typename = qualifiedName;
}
// Fix this later in treeManager
if ( typename.equals("java.lang.String")
|| typename.equals("java.lang.Class")
|| typename.equals("java.lang.Object")
|| typename.equals("java.lang.Exception")
) {
GNode type = GNode.create("Type");
type.add(Disambiguator.disambiguate(typename));
type.add(n.get(1));
return type;
}
// If the name is our name map,
// then replace it with the fully qualified name
else {
//
//
String[] qualified = typename.split("\\.");
if (typename.equals(currentClassName)){
return n;
}
// Reset currentClassName when we come back
String tempClassName = currentClassName;
currentClassName = typename;
String tempPackageName = currentPackageName;
GNode tempClass = currentClass;
GNode tempStatInitBlock = staticInitializerBlock;
GNode tempStatInitStat = staticInitializerStatements;
GNode tempStatInitMethod = staticInitializerMethod;
GNode tempCurrentConstructor = currentConstructor;
currentConstructor = null;
HashMap<String, String> tempNameMap = currentNameMap;
// disambiguate() - figure out the fully qualified name
// Later we'll keep track of already-imported types,
// and we'll automatically skip those or expand them
// as necessary
// For now we'll support only explicitly qualified name:
// "qimpp.Foo" ["qimpp", "Foo"]
GNode classTreeNode =
treeManager.dereference(new ArrayList(Arrays.asList(qualified)));
if (classTreeNode == null) {
try{
if (processImmediately){
process(typename.replace(".", "/")+".java");
enqueued.put(typename.replace(".", "/")+".java", new Boolean(true));
}
else
processEnqueue(typename.replace(".", "/")+".java");
} catch (Exception e) {
// If we can't find it in the source root, then it must be a reference to a file in the current package
try {
String currentPackageQualifiedTypename = currentPackageName + "." + typename;
currentNameMap.put(typename, currentPackageQualifiedTypename);
if (processImmediately){
process(currentPackageQualifiedTypename.replace(".", "/")+".java");
enqueued.put(currentPackageQualifiedTypename.replace(".", "/")+".java", new Boolean(true));
}
else
processEnqueue(currentPackageQualifiedTypename.replace(".", "/")+".java");
// Copy-paste yay...
String qualifiedName = currentNameMap.get(typename);
GNode qualifiedIdentifierNode = Disambiguator.disambiguate(qualifiedName);
n.set(0, qualifiedIdentifierNode);
}
catch (Exception f){
cppast.printAST();
e.printStackTrace();
System.exit(1);
}
}
// Fail and crash with error if the file cannot be located
}
currentClassName = tempClassName;
currentPackageName = tempPackageName;
//currentNameMap = tempNameMap;
currentClass = tempClass;
currentConstructor = tempCurrentConstructor;
staticInitializerBlock = tempStatInitBlock;
staticInitializerStatements = tempStatInitStat;
staticInitializerMethod = tempStatInitMethod;
return n;
}
}
public String visitDeclarator(GNode n) {
visit(n);
//A declarator just needs to return the name of it right now
return n.getString(0);
}
int selectionExpressionDepth = 0;
StringBuilder selectionExpressionBuilder;
/**
* Visit a selection expression, and if it's referring to a class
* type by name, make sure that type is translated.
*/
public void visitSelectionExpression(GNode n){
if (selectionExpressionDepth == 0){
selectionExpressionBuilder = new StringBuilder();
}
selectionExpressionDepth++;
dispatch(n.getGeneric(0));
selectionExpressionDepth--;
String name = n.getString(1);
selectionExpressionBuilder.append(".");
selectionExpressionBuilder.append(name);
// If this SelectionExpression is referring to a class,
// handle it as a type, making sure it's translated
if (currentNameMap.containsKey(selectionExpressionBuilder.toString())) {
GNode type = GNode.create("Type",
GNode.create("QualifiedIdentifier",
selectionExpressionBuilder.toString()),
null);
visitType(type);
}
}
/**
* Visit a PrimaryIdentifier node.
*/
public void visitPrimaryIdentifier(GNode n) {
// Check if this is a ambiguous name, and if it is, replace it with the fully qualified name
String name = n.getString(0);
//if(currentNameMap.containsKey(name)) name = currentNameMap.get(name);
// Make sure it's not an unvisited name in the local namespace
//TODO: This assumes that class names are capitalized
String s;
if (currentNameMap.containsKey(name) && !name.equals("System")){
GNode type = GNode.create("Type",
GNode.create("QualifiedIdentifier",
name));
visitType(type);
n.set(0, currentNameMap.get(name));
}
// If we're inside a SelectionExpression, build the expression
if (selectionExpressionDepth > 0){
selectionExpressionBuilder.append(name);
}
}
public GNode visitVoidType(GNode n){
GNode type = GNode.create("Type");
type.addNode(GNode.create("PrimitiveType")).getGeneric(0).add("void");
return type;
}
public GNode visitReturnStatement(GNode n){
visit(n);
return n;
}
public GNode visitExtension(GNode n){
// Assume the name of the parent is fully qualified
processImmediately = true;
visit(n);
processImmediately = false;
GNode parentNameQualifiedIdentifier = n.getGeneric(0).getGeneric(0);
parentName = Disambiguator.getDotDelimitedName(parentNameQualifiedIdentifier);
parentClassNode = treeManager.getClassDeclarationNode(parentName);
// Associate the current class with its parent's class Node
currentClass.setProperty("ParentClassNode", parentClassNode);
String parentNameQualified = parentClassNode.getString(0);
// Add the parent's type to the current class's Parent Node
currentClass.getGeneric(1).getGeneric(0).set(0, Disambiguator.disambiguate(parentNameQualified));
// Add inherited mehthods and fields using the parent's class
// TODO: Refactor here. Implemented and inherited methods should be interspersed, and there
// should only be one argument to addAllInheritedMethods
cppast.addAllInheritedMethods(parentClassNode.getGeneric(4), currentClass);
cppast.addAllInheritedFields(parentClassNode, currentClass);
parentClassNode = currentClass;
//add the current class to the inheritance tree, but parent it to Object for now
ArrayList parentQualified = new ArrayList<String>(Arrays.asList(parentName.split("\\.")));
ArrayList childQualified = new ArrayList<String>(Arrays.asList(currentClassName.split("\\.")));
treeManager.reparent(childQualified, parentQualified);
return null;
}
public void visitConstructorDeclaration(GNode n) {
//Add a constructor to currentClass and get the associated GNode
currentConstructor = cppast.addConstructor(currentClass);
//If there are formal parameters for the constructor, visit them and add them to the currentConstructor
if(n.getGeneric(4) != null){
cppast.setConstructorParameters((GNode)dispatch(n.getGeneric(4)), currentConstructor);
}
//If there are instructions in the block, visit them and add them to the constructor
if(n.getGeneric(5) != null){
cppast.setConstructorInstructions((GNode)dispatch(n.getGeneric(5)), currentConstructor);
}
}
public void visit(Node n) {
//
for (Object o : n) if (o instanceof Node) dispatch((Node)o);
}
};
initialVisitor.dispatch(node);
if (processDepth == 0){
// Reset currentClassName when we come back
String tempPackageName = currentPackageName;
GNode tempClass = currentClass;
String tempClassName = currentClassName;
GNode tempConstruct = currentConstructor;
currentConstructor = null;
Node target = readQueue.poll();
while (target != null){
process(target);
target = readQueue.poll();
}
currentConstructor = tempConstruct;
currentClassName = tempClassName;
currentPackageName = tempPackageName;
//currentNameMap = tempNameMap;
currentClass = tempClass;
}
/** SYMBOL TABLE */
SymbolTable table = new SymbolTable();
table.incorporate(currentClass);
// Now we can call .getProperty("qimpp.Constants.SCOPE") on certain
// scope-defining nodes and we'll get back a Scope object (look in
// SymbolTable).
final BlockMangler mangler = new BlockMangler(currentClass, treeManager, new MethodResolver());
new Visitor() {
/**
* Visit a block of code, and mangle it appropriately before we print it
* Do not visit deeper here
*/
public GNode visitBlock(GNode n) {
inBlock = true;
GNode block = GNode.create("Block");
if ( n.getProperty("Mangled") == null){
mangler.mangle(n);
n.setProperty("Mangled", new Boolean(true));
}
inBlock = false;
return n;
}
/*public GNode visitCallExpression(GNode n) {
// if this is a system call
if (n.getGeneric(0).getGeneric(0).getString(0).equals("System")) {
// if this is system.out
if (n.getGeneric(0).getString(1).equals("out")) {
// if this is system.out.print
if (n.getString(2).equals("print")) {
return cppast.addPrintExpression(null, n.getGeneric(3));
}
}
}
return n;
}*/
public void visitClassBody(GNode n){
visit(n);
}
public void visitClassDeclaration(GNode n){
// Add a default no-arg constructor. If there is an explicit constructor, we can add it
// later.
//Add a constructor to currentClass and get the associated GNode
visit(n);
}
public void visitCompilationUnit(GNode n) {
root = n;
//
visit(n);
//
//Print the AST after we're done for debugging
//cppast.printAST();
}
public GNode visitExpressionStatement(GNode n) {
//TODO: figure out what we need to do to the expressionstatements to make them parseable including fetching needed files
return n;
}
public void visitExpression(GNode n){
visit(n);
}
public void visitNewClassExpression(GNode n) {
visit(n);
}
public void visitPackageDeclaration(GNode n){
//
}
public void visitStringLiteral(GNode n){
visit(n);
}
public GNode visitType(GNode n) {
//Determine the type translated into C++ using Type.primitiveType(String) and Type.qualifiedIdentifier(String)
visit(n);
GNode identifier = n.getGeneric(0);
String typename = identifier.getString(0);
if (identifier.hasName("PrimitiveType")) {
return n;
}
// Fix this later in treeManager
else if ( typename.equals("String") || typename.equals("Class") || typename.equals("Object") ) {
GNode type = GNode.create("Type");
type.add(Disambiguator.disambiguate(typename));
type.add(n.get(1));
return type;
}
else {
GNode type = GNode.create("Type");
type.add(Disambiguator.disambiguate(typename));
type.add(n.get(1));
return type;
}
}
public GNode visitReturnStatement(GNode n) {
return n;
}
public void visit(Node n) {
//
//
for (Object o : n) if (o instanceof Node) dispatch((Node)o);
}
}.dispatch(currentClass);
if (processDepth == 0){
try{
PrintWriter h = new PrintWriter("out.h");
new HeaderWriter(new Printer(h)).dispatch(cppast.compilationUnit);
// cppast.printAST();
PrintWriter cc = new PrintWriter("out.cc");
new ImplementationPrinter(new Printer(cc), treeManager, cppast.compilationUnit).dispatch(cppast.compilationUnit);
} catch (Exception e) {
//
e.printStackTrace();
}
}
processDepth--;
}
/**
* Run the translator with the specified command line arguments.
*
* @param args The command line arguments.
*/
public static void main(String[] args) {
new QimppTranslator().run(args);
}
}