Charj : added support for the dot access operator (as opposed to the
[charm.git] / src / langs / charj / src / charj / translator / PupRoutineCreator.java
1 package charj.translator;
2
3 import org.antlr.runtime.tree.CommonTree;
4 import org.antlr.runtime.Token;
5 import org.antlr.runtime.CommonToken;
6
7 class PupRoutineCreator
8 {
9     private CharjAST pupNode;
10     private CharjAST block;
11
12     PupRoutineCreator()
13     {
14         createPupNode();
15         block = pupNode.getChild(4);
16     }
17
18     protected CharjAST getPupRoutineNode()
19     {
20         return pupNode;
21     }
22
23     private CharjAST createNode(int type, String text)
24     {
25         return new CharjAST(new CommonToken(type, text));
26     }    
27
28     private void createPupNode()
29     {
30         pupNode = createNode(CharjParser.FUNCTION_METHOD_DECL, "FUNCTION_METHOD_DECL");
31
32         pupNode.addChild(createNode(CharjParser.MODIFIER_LIST, "MODIFIER_LIST"));
33         pupNode.addChild(createNode(CharjParser.VOID, "void"));
34         pupNode.addChild(createNode(CharjParser.IDENT, "pup"));
35         pupNode.addChild(createNode(CharjParser.FORMAL_PARAM_LIST, "FORMAL_PARAM_LIST"));
36         pupNode.addChild(createNode(CharjParser.BLOCK, "BLOCK"));
37
38         pupNode.getChild(0).addChild(createNode(CharjParser.PUBLIC, "public"));
39
40         pupNode.getChild(3).addChild(createNode(CharjParser.FORMAL_PARAM_STD_DECL, "FORMAL_PARAM_STD_DECL"));
41         pupNode.getChild(3).getChild(0).addChild(createNode(CharjParser.REFERENCE_TYPE, "REFERENCE_TYPE"));
42         pupNode.getChild(3).getChild(0).getChild(0).addChild(createNode(CharjParser.QUALIFIED_TYPE_IDENT, "QUALIFIED_TYPE_IDENT"));
43         pupNode.getChild(3).getChild(0).getChild(0).getChild(0).addChild(createNode(CharjParser.IDENT, "PUP::er"));
44
45         pupNode.getChild(3).getChild(0).addChild(createNode(CharjParser.IDENT, "p"));
46     }
47
48     protected CharjAST getEnclosingType(CharjAST varDeclNode)
49     {
50         for(CharjAST p = varDeclNode.getParent(); p != null; p = p.getParent())
51             if(p.getType() == CharjParser.TYPE)
52                 return p;
53         return null;
54     }         
55
56     protected void varPup(CharjAST idNode)
57     {
58         System.out.println("in var pup: " + idNode.getText());
59         int type = -1;
60
61         for(CharjAST p = idNode.getParent(); p != null; p = p.getParent())
62         {
63             switch(p.getType())
64             {
65                 case CharjParser.PRIMITIVE_VAR_DECLARATION:
66                     System.out.println("got the type, it's primitive");
67                     type = p.getType();
68                     break;
69                 case CharjParser.OBJECT_VAR_DECLARATION:
70                     System.out.println("got the type, it's an object!");
71                     type = p.getChild(0).getType();
72                     break;
73                 case CharjParser.FUNCTION_METHOD_DECL:
74                 case CharjParser.BLOCK:
75                 case CharjParser.FORMAL_PARAM_LIST:
76                     System.out.println("local var, not puping...");
77                     return;
78                 case CharjParser.TYPE:
79                     System.out.println("class member var, puping... type " + type);
80                     switch(type)
81                     {
82                         case CharjParser.REFERENCE_TYPE: System.out.println("found ref");
83                         case CharjParser.PRIMITIVE_VAR_DECLARATION: System.out.println("found simple or ref");
84                             primitiveVarPup(idNode);
85                             break;
86                         case CharjParser.POINTER_TYPE: System.out.println("found pointer");
87                             pointerVarPup(idNode);
88                             break;
89                         default:
90                             System.out.println("unknown type -- THIS IS AN ERROR in method varPup, in PupRoutineCreator.java");
91                             break;
92                     }
93                     return;
94             }
95         }
96         System.out.println("THIS IS AN ERROR in method varPup, in PupRoutineCreator.java");
97     }
98
99     protected void primitiveVarPup(CharjAST idNode)
100     {
101         pupNode.getChild(4).addChild(createNode(CharjParser.EXPR, "EXPR"));
102         
103         int index = pupNode.getChild(4).getChildren().size() - 1;
104
105         pupNode.getChild(4).getChild(index).addChild(createNode(CharjParser.BITWISE_OR, "|"));
106         pupNode.getChild(4).getChild(index).getChild(0).addChild(createNode(CharjParser.IDENT, "p"));
107         pupNode.getChild(4).getChild(index).getChild(0).addChild(idNode.dupNode());
108     }
109     
110     protected void pointerVarPup(CharjAST idNode)
111     {
112         pupNode.getChild(4).addChild(createNode(CharjParser.EXPR, "EXPR"));
113         
114         int index = pupNode.getChild(4).getChildren().size() - 1;
115
116         pupNode.getChild(4).getChild(index).addChild(createNode(CharjParser.METHOD_CALL, "METHOD_CALL"));
117         pupNode.getChild(4).getChild(index).getChild(0).addChild(createNode(CharjParser.ARROW, "ARROW"));
118         pupNode.getChild(4).getChild(index).getChild(0).getChild(0).addChild(idNode.dupNode());
119         pupNode.getChild(4).getChild(index).getChild(0).getChild(0).addChild(createNode(CharjParser.IDENT, "pup"));
120         pupNode.getChild(4).getChild(index).getChild(0).addChild(createNode(CharjParser.ARGUMENT_LIST, "ARGUMENT_LIST"));
121         pupNode.getChild(4).getChild(index).getChild(0).getChild(1).addChild(createNode(CharjParser.EXPR, "EXPR"));
122         pupNode.getChild(4).getChild(index).getChild(0).getChild(1).getChild(0).addChild(createNode(CharjParser.IDENT, "p"));
123     }
124
125 }